Add test for HMAC generation
This commit is contained in:
parent
7560d76c4b
commit
f414326737
@ -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<typeof axios>
|
||||
@ -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 } })
|
||||
|
||||
@ -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${
|
||||
|
||||
Loading…
Reference in New Issue
Block a user