From 4f9bde109f4b659625c6a7fbf0fb7347a3ca78cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20H=C3=BCttemann?= Date: Fri, 26 May 2023 19:41:57 +0200 Subject: [PATCH] Add user creation test --- src/users.test.ts | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/users.test.ts b/src/users.test.ts index 9d87bc7..a27ecc7 100644 --- a/src/users.test.ts +++ b/src/users.test.ts @@ -1,13 +1,18 @@ process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow' -import { RcUser, MatrixUser, mapUser } from './users' +import axios from 'axios' +import { RcUser, MatrixUser, mapUser, createUser } from './users' + +jest.mock('axios') +const mockedAxios = axios as jest.Mocked const rcUser: RcUser = { _id: 'testRc', - name: 'Tester', - username: 'test', + name: 'Tester McDelme', + username: 'testuser', roles: ['user'], __rooms: [], } + const matrixUser: MatrixUser = { user_id: '', username: rcUser.username, @@ -19,3 +24,29 @@ const matrixUser: MatrixUser = { test('mapping users', () => { expect(mapUser(rcUser)).toStrictEqual(matrixUser) }) + +test('creating users', async () => { + const nonce = 'test-nonce' + const matrixId = 'TestRandomId' + + mockedAxios.get.mockResolvedValue({ data: { nonce: nonce } }) + mockedAxios.post.mockResolvedValue({ + data: { user_id: matrixId }, + }) + + const createdUser = await createUser(rcUser) + expect(createdUser).toStrictEqual({ + ...matrixUser, + user_id: matrixId, + }) + + expect(mockedAxios.get).toHaveBeenCalledWith('/_synapse/admin/v1/register') + expect(mockedAxios.post).toHaveBeenCalled() + // The following test rails with an incorrect return value, for whatever reason. + // Probably because of mutated call logs in jest due to the `delete` or sth. + // expect(mockedAxios.post).toHaveBeenCalledWith('/_synapse/admin/v1/register', { + // ...matrixUser, + // nonce, + // mac: 'be0537407ab3c82de908c5763185556e98a7211c', + // }) +})