Skip to content

Commit

Permalink
test(core): mock crypto functions in password tests (#5522)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Mar 19, 2024
1 parent b40a347 commit 9aa07bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
34 changes: 29 additions & 5 deletions packages/core/src/libraries/user.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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 () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/routes/interaction/additional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
() => ({
Expand Down Expand Up @@ -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');
});
});
Expand Down

0 comments on commit 9aa07bf

Please sign in to comment.