From b3eb6e4339dbdfec69b6463a7afc13add3855139 Mon Sep 17 00:00:00 2001 From: Avinash Date: Mon, 19 Jun 2023 01:31:15 +0530 Subject: [PATCH] feat: increased test coverage for authService Signed-off-by: Avinash --- backend/src/auth/auth.service.spec.ts | 85 ++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/backend/src/auth/auth.service.spec.ts b/backend/src/auth/auth.service.spec.ts index 001671fe9..26031271f 100644 --- a/backend/src/auth/auth.service.spec.ts +++ b/backend/src/auth/auth.service.spec.ts @@ -11,7 +11,11 @@ import crypto from 'crypto'; import { Repository } from 'typeorm'; import appConfigMock from '../config/mock/app.config.mock'; -import { NotInDBError, TokenNotValidError } from '../errors/errors'; +import { + NotInDBError, + TokenNotValidError, + TooManyTokensError, +} from '../errors/errors'; import { Identity } from '../identity/identity.entity'; import { LoggerModule } from '../logger/logger.module'; import { Session } from '../users/session.entity'; @@ -109,6 +113,14 @@ describe('AuthService', () => { expect(tokens).toHaveLength(1); expect(tokens).toEqual([authToken]); }); + it('should return empty array if token for user do not exists', async () => { + jest + .spyOn(authTokenRepo, 'find') + .mockImplementationOnce(async () => null); + const tokens = await service.getTokensByUser(user); + expect(tokens).toHaveLength(0); + expect(tokens).toEqual([]); + }); }); describe('getAuthToken', () => { @@ -303,18 +315,70 @@ describe('AuthService', () => { expect(token.lastUsedAt).toBeNull(); expect(token.secret.startsWith(token.keyId)).toBeTruthy(); }); + it('should throw TooManyTokensError when number of tokens >= 200', async () => { + jest + .spyOn(authTokenRepo, 'find') + .mockImplementationOnce(async (): Promise => { + const inValidToken = [authToken]; + inValidToken.length = 201; + return inValidToken; + }); + const validUntil = new Date().getTime() + 30000; + await expect( + service.addToken(user, identifier, validUntil), + ).rejects.toThrow(TooManyTokensError); + }); + }); + }); + + describe('removeInvalidTokens', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it('works', async () => { + const expiredDate = new Date().getTime() - 30000; + const expiredToken = { ...authToken, validUntil: new Date(expiredDate) }; + jest + .spyOn(authTokenRepo, 'find') + .mockResolvedValueOnce([expiredToken, authToken]); + jest + .spyOn(authTokenRepo, 'remove') + .mockImplementationOnce(async (token): Promise => { + expect(token).toEqual(expiredToken); + expect(token).not.toBe(authToken); + return authToken; + }); + + await service.removeInvalidTokens(); + }); + }); + + describe('auto remove invalid tokens', () => { + beforeEach(() => { + jest.spyOn(service, 'removeInvalidTokens'); + }); + + it('handleCron should call removeInvalidTokens', async () => { + await service.handleCron(); + expect(service.removeInvalidTokens).toHaveBeenCalledTimes(1); + }); + + it('handleTimeout should call removeInvalidTokens', async () => { + await service.handleTimeout(); + expect(service.removeInvalidTokens).toHaveBeenCalledTimes(1); }); }); describe('toAuthTokenDto', () => { + const authToken = new AuthToken(); + authToken.keyId = 'testKeyId'; + authToken.label = 'testLabel'; + const date = new Date(); + date.setHours(date.getHours() - 1); + authToken.createdAt = date; + authToken.validUntil = new Date(); it('works', () => { - const authToken = new AuthToken(); - authToken.keyId = 'testKeyId'; - authToken.label = 'testLabel'; - const date = new Date(); - date.setHours(date.getHours() - 1); - authToken.createdAt = date; - authToken.validUntil = new Date(); const tokenDto = service.toAuthTokenDto(authToken); expect(tokenDto.keyId).toEqual(authToken.keyId); expect(tokenDto.lastUsedAt).toBeNull(); @@ -326,5 +390,10 @@ describe('AuthService', () => { authToken.createdAt.getTime(), ); }); + it('should have lastUsedAt', () => { + authToken.lastUsedAt = new Date(); + const tokenDto = service.toAuthTokenDto(authToken); + expect(tokenDto.lastUsedAt).toEqual(authToken.lastUsedAt); + }); }); });