Skip to content

Commit

Permalink
Missing unit tests (#177)
Browse files Browse the repository at this point in the history
* πŸ› Store invite fix (#169)

* πŸ› fix: update the invitation API id-server call payload

* 🚨 chore: lint

* πŸ› fix: the lookup API handler timing out

* πŸ› fix: the store-invite API handler

* πŸ› fix: the getKeys method

* ✨ feat: added buildUrl helper to matrix-identity-server

* 🏷️ chore: add matrix_server to matrix-id-server types

* πŸ› fix: tweetnacl-util import

fixes base64Encode call error

* 🎨 feat: add express.json and express.urlencoded middlewares

* πŸ§ͺ chore: add json and urlencoded support to test

* 🎨 chore: refactor decode json util

* πŸ₯… chore: handle empty body

* πŸ§ͺ chore: update test

* πŸ› chore: better log errors

* πŸ§ͺ chore: update tests

* 🚨 chore: lint

* πŸ§ͺ chore: update tests

* ✨ SMS service (#172)

* ✨ feat: added SMS service

* 🏷️ chore: added sms service related types

* πŸ”§ chore: fetch SMS service config from env vars

* 🎨 chore: use SMS service in sore-invite API

* πŸ“„ chore: add 3pid SMS invitation template

* πŸ”§ chore: added chat_url config

* πŸ§‘β€πŸ’» chore: fix buildUrl util

* 🎨 chore: update invitation process

add invitation link to store-invite API

* πŸ› chore: update SMS service

* πŸ§ͺ chore: add SMS Service API test

* πŸ§ͺ chore: fix missing config in terms tests

* πŸ§ͺ chore: add store-invite tests

* 🚨 chore: lint

* πŸ§ͺ chore: fix decode json content unit test

* 🎨 chore: revert jsondecode changes

* πŸ§ͺ chore: revert id server test

* πŸ§ͺ chore: fix missing done in tests

* πŸ§ͺ chore: fix invitation test

* πŸ› fix: use correct medium address for phone

* πŸ§ͺ chore: fix broken skipped tests

* πŸ§ͺ chore: fix additional features skipped tests

* πŸ§ͺ chore: update test default config

* πŸ› fix: sms service constructor preventing server init

* πŸ› fix: sms service tests

* πŸ§ͺ chore: update test config files

* πŸ› chore: fix skipped unit tests

* ⚑️ chore: init SMS service when in case of msisdn

* πŸ§ͺ chore: fix tom server skipped tests

---------

Co-authored-by: Khaled Ferjani <[email protected]>
  • Loading branch information
pm-McFly and rezk2ll authored Feb 11, 2025
1 parent 4ac8066 commit 5014abc
Show file tree
Hide file tree
Showing 31 changed files with 765 additions and 234 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ ENV BASE_URL= \
RATE_LIMITING_WINDOW= \
RATE_LIMITING_NB_REQUESTS= \
TRUSTED_PROXIES= \
QRCODE_URL=
QRCODE_URL= \
CHAT_URL=

COPY --from=1 /usr/src/app /usr/src/app/

Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _nacl, { type Nacl } from 'js-nacl'
import nacl from 'tweetnacl'
import * as naclUtil from 'tweetnacl-util'
import naclUtil from 'tweetnacl-util'

// export const supportedHashes = ['sha256', 'sha512']
export const supportedHashes = ['sha256']
Expand Down
6 changes: 5 additions & 1 deletion packages/federated-identity-service/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ const conf = {
? JSON.parse(process.env.USERDB_SSL)
: false,
trust_x_forwarded_for: process.env.TRUST_X_FORWARDED_FOR || false,
trusted_servers_addresses: process.env.TRUSTED_SERVERS_ADDRESSES
trusted_servers_addresses: process.env.TRUSTED_SERVERS_ADDRESSES,
sms_api_key: process.env.SMS_API_KEY,
sms_api_login: process.env.SMS_API_LOGIN,
sms_api_url: process.env.SMS_API_URL,
chat_url: process.env.CHAT_URL ?? 'https://chat.twake.app'
}

const app = express()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Hi,

__inviter__ has invited you to join Twake Chat, a secure and decentralized communication platform.

Get started here: __invitation_link__

We’re excited to have you join us!

Best regards,
Twake Chat
6 changes: 5 additions & 1 deletion packages/matrix-client-server/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ const conf = {
userdb_ssl: process.env.USERDB_SSL
? JSON.parse(process.env.USERDB_SSL)
: false,
userdb_user: process.env.USERDB_USER
userdb_user: process.env.USERDB_USER,
sms_api_key: process.env.SMS_API_KEY,
sms_api_login: process.env.SMS_API_LOGIN,
sms_api_url: process.env.SMS_API_URL,
chat_url: process.env.CHAT_URL ?? 'https://chat.twake.app'
}

const app = express()
Expand Down
10 changes: 10 additions & 0 deletions packages/matrix-client-server/templates/3pidSmsInvitation.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Hi,

__inviter__ has invited you to join Twake Chat, a secure and decentralized communication platform.

Get started here: __invitation_link__

We’re excited to have you join us!

Best regards,
Twake Chat
6 changes: 5 additions & 1 deletion packages/matrix-identity-server/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ const conf = {
userdb_ssl: process.env.USERDB_SSL
? JSON.parse(process.env.USERDB_SSL)
: false,
userdb_user: process.env.USERDB_USER
userdb_user: process.env.USERDB_USER,
sms_api_key: process.env.SMS_API_KEY,
sms_api_login: process.env.SMS_API_LOGIN,
sms_api_url: process.env.SMS_API_URL,
chat_url: process.env.CHAT_URL ?? 'https://chat.twake.app'
}

const app = express()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"smtp_server": "localhost",
"template_dir": "./templates",
"userdb_engine": "sqlite",
"userdb_host": "./src/__testData__/test.db"
"userdb_host": "./src/__testData__/userdb.db",
"sms_api_key": "key",
"sms_api_login": "login",
"sms_api_url": "https://example.com"
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
},
"template_dir": "./templates",
"userdb_engine": "sqlite",
"userdb_host": "./src/__testData__/terms.db"
"userdb_host": "./src/__testData__/terms.db",
"sms_api_key": "test_key",
"sms_api_login": "test_secret",
"sms_api_url": "http://localhost/sms/api"
}
2 changes: 2 additions & 0 deletions packages/matrix-identity-server/src/account/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ const Register = <T extends string = never>(
send(res, 200, { token })
})
.catch((e) => {
console.error('Unable to creation session', { e })
/* istanbul ignore next */
logger.error('Unable to create session', e)
/* istanbul ignore next */
send(res, 500, errMsg('unknown', 'Unable to create session'))
})
})
.catch((e) => {
console.error('unable to validate token', { e })
logger.warn(`Unable to validate token ${JSON.stringify(obj)}`, e)
send(res, 401, errMsg('sessionNotValidated', e))
})
Expand Down
60 changes: 49 additions & 11 deletions packages/matrix-identity-server/src/additionalFeatures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const conf: Config = {
matrix_database_engine: 'sqlite',
matrix_database_host: 'src/__testData__/matrix.db',
userdb_engine: 'sqlite',
userdb_host: 'src/__testData__/add.db'
userdb_host: 'src/__testData__/user.db',
sms_api_url: 'https://example.com',
sms_api_key: 'key',
sms_api_login: 'login'
}
beforeAll((done) => {
if (process.env.TEST_PG === 'yes') {
Expand All @@ -54,19 +57,27 @@ beforeAll((done) => {

idServer.ready
.then(() => {
Object.keys(idServer.api.get).forEach((k) => {
for (const k of Object.keys(idServer.api.get)) {
app.get(k, idServer.api.get[k])
})
Object.keys(idServer.api.post).forEach((k) => {
}

for (const k of Object.keys(idServer.api.post)) {
app.post(k, idServer.api.post[k])
})
}

done()
})
.catch(done)
})
.catch(done)
.catch((e) => {
console.log({ e })
done(e)
})
})
.catch((e) => {
console.log({ e })
done(e)
})
.catch(done)
})

beforeEach(() => {
Expand All @@ -80,9 +91,12 @@ beforeEach(() => {
})

afterAll(() => {
fs.unlinkSync('src/__testData__/add.db')
fs.unlinkSync('src/__testData__/matrix.db')
idServer.cleanJobs()
try {
fs.unlinkSync('src/__testData__/add.db')
fs.unlinkSync('src/__testData__/user.db')
fs.unlinkSync('src/__testData__/matrix.db')
} catch (error) {}
idServer?.cleanJobs()
})

describe('/_matrix/identity/v2/account/register', () => {
Expand All @@ -99,7 +113,6 @@ describe('/_matrix/identity/v2/account/register', () => {
})
// @ts-expect-error mock is unknown
fetch.mockImplementation(async () => await mockResponse)
await mockResponse
const response = await request(app)
.post('/_matrix/identity/v2/account/register')
.send({
Expand All @@ -116,6 +129,31 @@ describe('/_matrix/identity/v2/account/register', () => {
})

describe('/_matrix/identity/v2/lookup', () => {
beforeEach(async () => {
const mockResponse = Promise.resolve({
ok: true,
status: 200,
json: () => {
return {
sub: '@dwho:example.com',
'm.server': 'matrix.example.com:8448'
}
}
})
// @ts-expect-error mock is unknown
fetch.mockImplementation(async () => await mockResponse)
const response = await request(app)
.post('/_matrix/identity/v2/account/register')
.send({
access_token: 'bar',
expires_in: 86400,
matrix_server_name: 'matrix.example.com',
token_type: 'Bearer'
})
.set('Accept', 'application/json')

validToken = response.body.token
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let pepper = ''
describe('/_matrix/identity/v2/hash_details', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/matrix-identity-server/src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@
"sms_api_login": "",
"sms_api_key": "",
"qr_code_url": "twake.chat://login",
"invitation_redirect_url": "https://sign-up.twake.app/download/chat"
"invitation_redirect_url": "https://sign-up.twake.app/download/chat",
"chat_url": "https://chat.twake.app"
}
4 changes: 1 addition & 3 deletions packages/matrix-identity-server/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,7 @@ class IdentityServerDb<T extends string = never>
return new Promise((resolve, reject) => {
const _type = type === 'current' ? 'currentKey' : 'previousKey'
this.db
.get('longTermKeypairs', ['keyID', 'public', 'private'], {
name: _type
})
.getAll('shortTermKeypairs', ['keyID', 'public', 'private'])
.then((rows) => {
if (rows.length === 0) {
reject(new Error(`No ${_type} found`))
Expand Down
Loading

0 comments on commit 5014abc

Please sign in to comment.