Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): mock crypto functions in password tests #5522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading