From f41432673786a7a183584218c62a234c71d0e562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20H=C3=BCttemann?= Date: Mon, 5 Jun 2023 13:27:40 +0200 Subject: [PATCH] Add test for HMAC generation --- src/users.test.ts | 10 ++++++++-- src/users.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/users.test.ts b/src/users.test.ts index 6fdf809..82549d1 100644 --- a/src/users.test.ts +++ b/src/users.test.ts @@ -1,6 +1,6 @@ process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow' import axios from 'axios' -import { RcUser, MatrixUser, mapUser, createUser } from './users' +import { MatrixUser, RcUser, createUser, generateHmac, mapUser } from './users' jest.mock('axios') const mockedAxios = axios as jest.Mocked @@ -21,12 +21,18 @@ const matrixUser: MatrixUser = { admin: false, } +const nonce = 'test-nonce' + test('mapping users', () => { expect(mapUser(rcUser)).toStrictEqual(matrixUser) }) +test('generating correct hmac', () => { + expect(generateHmac({ ...matrixUser, nonce })).toStrictEqual( + 'be0537407ab3c82de908c5763185556e98a7211c' + ) +}) test('creating users', async () => { - const nonce = 'test-nonce' const matrixId = 'TestRandomId' mockedAxios.get.mockResolvedValue({ data: { nonce: nonce } }) diff --git a/src/users.ts b/src/users.ts index 027bfe9..7ca2ae6 100644 --- a/src/users.ts +++ b/src/users.ts @@ -37,7 +37,7 @@ if (!registration_shared_secret) { throw new Error(message) } -function generateHmac(user: MatrixUser): string { +export function generateHmac(user: MatrixUser): string { const hmac = createHmac('sha1', registration_shared_secret) hmac.write( `${user.nonce}\0${user.username}\0${user.password}\0${