Skip to content

Commit c7716d4

Browse files
committed
test: enhance AuthController and AuthService unit tests with additional mocks
- Added mocks for getLocalBruteforceBlock and hasWebAuthnKeyForUser in AuthController tests to improve test coverage and behavior clarity. - Introduced redisTtl and redisIncr mocks in AuthService tests to simulate Redis interactions, enhancing the reliability of unit tests. - Updated beforeEach hooks to set default return values for new mocks, ensuring consistent test environments.
1 parent adbb838 commit c7716d4

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

apps/api/tests/unit/core/auth/auth.controller.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { AuthController } from '~/core/auth/auth.controller';
33
describe('AuthController', () => {
44
const createTokens = jest.fn();
55
const authenticateWithLocal = jest.fn();
6+
const getLocalBruteforceBlock = jest.fn();
67
const isTotpEnabledForUser = jest.fn();
8+
const hasWebAuthnKeyForUser = jest.fn();
79
const createMfaChallenge = jest.fn();
810
const verifyTotpChallenge = jest.fn();
911
const getSessionData = jest.fn();
@@ -14,7 +16,9 @@ describe('AuthController', () => {
1416
const authService = {
1517
createTokens,
1618
authenticateWithLocal,
19+
getLocalBruteforceBlock,
1720
isTotpEnabledForUser,
21+
hasWebAuthnKeyForUser,
1822
createMfaChallenge,
1923
verifyTotpChallenge,
2024
getSessionData,
@@ -37,6 +41,9 @@ describe('AuthController', () => {
3741

3842
beforeEach(() => {
3943
jest.clearAllMocks();
44+
getLocalBruteforceBlock.mockResolvedValue({ blocked: false, retryAfterSeconds: 0 });
45+
hasWebAuthnKeyForUser.mockReturnValue(false);
46+
isTotpEnabledForUser.mockReturnValue(false);
4047
controller = new AuthController({} as any, authService as any, rolesService as any);
4148
});
4249

apps/api/tests/unit/core/auth/auth.service.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ describe('AuthService', () => {
1212
const redisSet = jest.fn();
1313
const redisExpire = jest.fn();
1414
const redisDel = jest.fn();
15+
const redisTtl = jest.fn();
16+
const redisIncr = jest.fn();
1517
const redis = {
1618
get: redisGet,
1719
set: redisSet,
1820
expire: redisExpire,
1921
del: redisDel,
22+
ttl: redisTtl,
23+
incr: redisIncr,
2024
};
2125

2226
const agentsFindOne = jest.fn();
@@ -48,6 +52,8 @@ describe('AuthService', () => {
4852

4953
beforeEach(() => {
5054
jest.clearAllMocks();
55+
redisTtl.mockResolvedValue(-2);
56+
redisIncr.mockResolvedValue(1);
5157
service = new AuthService(
5258
{} as any,
5359
agentsService as any,

0 commit comments

Comments
 (0)