mirror of
https://github.com/logto-io/logto.git
synced 2025-03-03 22:15:32 -05:00
test(core): mock crypto functions in password tests (#5522)
This commit is contained in:
parent
b40a3474aa
commit
9aa07bf8ed
2 changed files with 33 additions and 5 deletions
|
@ -1,12 +1,38 @@
|
|||
import { MfaFactor, UsersPasswordEncryptionMethod } from '@logto/schemas';
|
||||
import { createMockUtils } from '@logto/shared/esm';
|
||||
|
||||
import { mockResource, mockAdminUserRole, mockScope } from '#src/__mocks__/index.js';
|
||||
import { mockUser } from '#src/__mocks__/user.js';
|
||||
import RequestError from '#src/errors/RequestError/index.js';
|
||||
import { MockQueries } from '#src/test-utils/tenant.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
const { mockEsm } = createMockUtils(jest);
|
||||
|
||||
mockEsm('hash-wasm', () => ({
|
||||
argon2Verify: jest.fn(async ({ password }: { password: string }) => {
|
||||
return password === 'password';
|
||||
}),
|
||||
bcryptVerify: jest.fn(async ({ password }: { password: string }) => {
|
||||
return password === 'password';
|
||||
}),
|
||||
md5: jest.fn(async (password) => {
|
||||
return password === 'password' ? '5f4dcc3b5aa765d61d8327deb882cf99' : 'wrong';
|
||||
}),
|
||||
sha1: jest.fn(async (password) => {
|
||||
return password === 'password' ? '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8' : 'wrong';
|
||||
}),
|
||||
sha256: jest.fn(async (password) => {
|
||||
return password === 'password'
|
||||
? '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8'
|
||||
: 'wrong';
|
||||
}),
|
||||
}));
|
||||
|
||||
mockEsm('#src/utils/password.js', () => ({
|
||||
encryptPassword: jest.fn().mockResolvedValue('argon2:xxx'),
|
||||
}));
|
||||
|
||||
const { MockQueries } = await import('#src/test-utils/tenant.js');
|
||||
const { encryptUserPassword, createUserLibrary } = await import('./user.js');
|
||||
|
||||
const hasUserWithId = jest.fn();
|
||||
|
@ -74,9 +100,7 @@ describe('verifyUserPassword()', () => {
|
|||
|
||||
describe('Argon2', () => {
|
||||
it('resolves when password is correct', async () => {
|
||||
await expect(
|
||||
verifyUserPassword(mockUser, 'HOH2hTmW0xtYAJUfRSQjJdW5')
|
||||
).resolves.not.toThrowError();
|
||||
await expect(verifyUserPassword(mockUser, 'password')).resolves.not.toThrowError();
|
||||
});
|
||||
|
||||
it('rejects when password is incorrect', async () => {
|
||||
|
|
|
@ -44,6 +44,10 @@ const { getInteractionStorage, storeInteractionResult } = await mockEsmWithActua
|
|||
})
|
||||
);
|
||||
|
||||
await mockEsmWithActual('./utils/totp-validation.js', () => ({
|
||||
generateTotpSecret: jest.fn().mockReturnValue('secret'),
|
||||
}));
|
||||
|
||||
const { sendVerificationCodeToIdentifier } = await mockEsmWithActual(
|
||||
'./utils/verification-code-validation.js',
|
||||
() => ({
|
||||
|
@ -229,7 +233,7 @@ describe('interaction routes', () => {
|
|||
expect(getInteractionStorage).toBeCalled();
|
||||
expect(storeInteractionResult).toBeCalled();
|
||||
expect(response.statusCode).toEqual(200);
|
||||
expect(response.body).toHaveProperty('secret');
|
||||
expect(response.body).toHaveProperty('secret', 'secret');
|
||||
expect(response.body).toHaveProperty('secretQrCode');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue