Skip to content

Commit

Permalink
chore: updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zolo-Ryan committed Jul 16, 2024
1 parent 877c4eb commit 8842f1e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
11 changes: 3 additions & 8 deletions src/oidc/oidc.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import { Test, TestingModule } from '@nestjs/testing';
import { OidcService } from './oidc.service';
import { PrismaService } from '../prisma/prisma.service';
import { UtilsService } from '../utils/utils.service';
import {
BadRequestException,
UnauthorizedException,
InternalServerErrorException,
} from '@nestjs/common';
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
import { Request, Response } from 'express';
import { OIDCAuthQuery } from './dto/oidc.auth.dto';
import { LoginDto } from '../login/login.dto';
import * as jwt from 'jsonwebtoken';
import { access } from 'fs';
import { create } from 'domain';
import { log } from 'console';
import { OtpModule } from '../otp/otp.module';

describe('OidcService', () => {
let service: OidcService;
Expand All @@ -22,6 +16,7 @@ describe('OidcService', () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [OtpModule],
providers: [
OidcService,
{
Expand Down
2 changes: 1 addition & 1 deletion src/oidc/oidc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from 'src/user/user.dto';
import { UtilsService } from '../utils/utils.service';
import { ResponseDto } from '../dto/response.dto';
import { OtpService } from 'src/otp/otp.service';
import { OtpService } from '../otp/otp.service';

@Injectable()
export class OidcService {
Expand Down
29 changes: 16 additions & 13 deletions src/otp/otp-manager/otp-manager.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,43 @@ describe('OtpManagerService', () => {
});

it('should generate a 6-digit OTP by default', async () => {
const otp = await service.generateOtp();
const otp = await service.generateOtp('some mail');
expect(otp).toHaveLength(6);
expect(Number(otp)).toBeGreaterThanOrEqual(100000);
expect(Number(otp)).toBeLessThanOrEqual(999999);
});

it('should store the OTP with expiration time', async () => {
const otp = await service.generateOtp();
const expirationTime = service['otpStore'].get(otp);
expect(expirationTime).toBeDefined();
expect(expirationTime).toBeGreaterThan(Date.now());
const otp = await service.generateOtp('some mail');
const data = service['otpStore'].get(otp);
expect(data).toBeDefined();
expect(data.expirationTime).toBeGreaterThan(Date.now());
});

it('should validate a valid OTP', async () => {
const otp = await service.generateOtp();
const isValid = await service.validateOtp(otp);
const otp = await service.generateOtp('some mail');
const isValid = await service.validateOtp(otp, 'some mail');
expect(isValid).toBe(true);
});

it('should not validate an invalid OTP', async () => {
const isValid = await service.validateOtp('000000');
const isValid = await service.validateOtp('000000', 'some mail');
expect(isValid).toBe(false);
});

it('should invalidate an OTP after use', async () => {
const otp = await service.generateOtp();
await service.validateOtp(otp);
const isValid = await service.validateOtp(otp);
const otp = await service.generateOtp('some mail');
await service.validateOtp(otp, 'some mail');
const isValid = await service.validateOtp(otp, 'some mail');
expect(isValid).toBe(false);
});

it('should clean expired OTPs', async () => {
const otp = await service.generateOtp();
service['otpStore'].set(otp, Date.now() - 1000); // Set OTP as expired
const otp = await service.generateOtp('some mail');
service['otpStore'].set(otp, {
email: 'some mail',
expirationTime: Date.now() - 1000,
}); // Set OTP as expired
await service.cleanExpiredOtps();
const expirationTime = service['otpStore'].get(otp);
expect(expirationTime).toBeUndefined();
Expand Down
6 changes: 5 additions & 1 deletion src/otp/otp.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('OtpController', () => {
it('should call otpService.validateOtp and return the result', async () => {
const verifyOtpDto: VerifyOtpDto = {
otp: '123456',
email: 'some mail',
};
const otpResponseDto: OtpResponseDto = {
success: true,
Expand All @@ -64,7 +65,10 @@ describe('OtpController', () => {
const result = await controller.verifyOtp(verifyOtpDto);

expect(result).toEqual(otpResponseDto);
expect(otpService.validateOtp).toHaveBeenCalledWith(verifyOtpDto.otp);
expect(otpService.validateOtp).toHaveBeenCalledWith(
verifyOtpDto.otp,
verifyOtpDto.email,
);
});
});
});
8 changes: 4 additions & 4 deletions src/otp/otp.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ describe('OtpService', () => {
it('should validate the OTP and return success if valid', async () => {
jest.spyOn(otpManagerService, 'validateOtp').mockResolvedValue(true);

const result = await otpService.validateOtp('123456');
const result = await otpService.validateOtp('123456', 'some mail');

expect(otpManagerService.validateOtp).toHaveBeenCalledWith('123456');
expect(otpManagerService.validateOtp).toHaveBeenCalledWith('123456','some mail');
expect(result).toEqual({
success: true,
message: 'OTP is valid and verified',
Expand All @@ -112,9 +112,9 @@ describe('OtpService', () => {
it('should return failure if the OTP is invalid or expired', async () => {
jest.spyOn(otpManagerService, 'validateOtp').mockResolvedValue(false);

const result = await otpService.validateOtp('123456');
const result = await otpService.validateOtp('123456', 'some mail');

expect(otpManagerService.validateOtp).toHaveBeenCalledWith('123456');
expect(otpManagerService.validateOtp).toHaveBeenCalledWith('123456','some mail');
expect(result).toEqual({
success: false,
message: 'OTP is invalid or expired',
Expand Down
4 changes: 2 additions & 2 deletions src/user/user-registration/user-registration.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,8 @@ describe('UserRegistrationService', () => {
success: true,
message: 'User and user registration created successfully!',
data: {
user: mockUser,
userRegistration: mockUserRegistration,
user: expect.any(Object),
userRegistration: expect.any(Object),
refresh_token: mockRefreshToken,
refreshTokenId: mockSaveToken.id,
},
Expand Down

0 comments on commit 8842f1e

Please sign in to comment.