111 lines
4.0 KiB
TypeScript
111 lines
4.0 KiB
TypeScript
import { Repository } from 'typeorm';
|
|
import { OTP } from '../entities/otp.entity';
|
|
import { OtpService } from './otp.service';
|
|
import { HashingProvider } from '@/common/modules/hashing/providers/hashing.provider';
|
|
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
import { OtpPurpose } from '../enums/otp-purpose.enum';
|
|
|
|
describe('OTP verification', () => {
|
|
let otp: OTP;
|
|
let service: OtpService;
|
|
let compare: jest.Mock<HashingProvider['compare']>;
|
|
let save: jest.Mock<(record: OTP) => Promise<OTP>>;
|
|
let findOne: jest.Mock<() => Promise<OTP | null>>;
|
|
let transactionCommitted: boolean;
|
|
|
|
beforeEach(() => {
|
|
otp = Object.assign(new OTP(), {
|
|
id: 1,
|
|
phone: '09121111111',
|
|
code: 'hashed-code',
|
|
used: false,
|
|
failedAttempts: 0,
|
|
expiresAt: new Date(Date.now() + 120_000),
|
|
});
|
|
compare = jest.fn<HashingProvider['compare']>().mockResolvedValue(true);
|
|
save = jest
|
|
.fn<(record: OTP) => Promise<OTP>>()
|
|
.mockImplementation((record: OTP) => Promise.resolve(record));
|
|
findOne = jest.fn<() => Promise<OTP | null>>().mockResolvedValue(otp);
|
|
transactionCommitted = false;
|
|
const repository = {
|
|
manager: {
|
|
transaction: async (
|
|
callback: (manager: unknown) => Promise<unknown>,
|
|
) => {
|
|
const result = await callback({
|
|
getRepository: () => ({ findOne, save }),
|
|
});
|
|
transactionCommitted = true;
|
|
return result;
|
|
},
|
|
},
|
|
} as unknown as Repository<OTP>;
|
|
service = new OtpService(repository, {
|
|
compare,
|
|
} as unknown as HashingProvider);
|
|
});
|
|
|
|
it('consumes the latest OTP under a database write lock and rejects reuse', async () => {
|
|
await expect(service.verifyOTP(otp.phone, '12345')).resolves.toBe(otp);
|
|
expect(findOne).toHaveBeenCalledWith({
|
|
where: { phone: otp.phone, purpose: 'signup' },
|
|
order: { createdAt: 'DESC', id: 'DESC' },
|
|
lock: { mode: 'pessimistic_write' },
|
|
});
|
|
expect(otp.used).toBe(true);
|
|
await expect(service.verifyOTP(otp.phone, '12345')).rejects.toThrow();
|
|
expect(compare).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('commits ten wrong guesses and then rejects even the correct code', async () => {
|
|
compare.mockResolvedValue(false);
|
|
for (let attempt = 1; attempt <= 10; attempt++) {
|
|
transactionCommitted = false;
|
|
await expect(service.verifyOTP(otp.phone, 'wrong')).rejects.toThrow();
|
|
expect(transactionCommitted).toBe(true);
|
|
expect(otp.failedAttempts).toBe(attempt);
|
|
}
|
|
compare.mockResolvedValue(true);
|
|
await expect(service.verifyOTP(otp.phone, '12345')).rejects.toThrow();
|
|
expect(compare).toHaveBeenCalledTimes(10);
|
|
expect(save).toHaveBeenCalledTimes(10);
|
|
});
|
|
|
|
it('looks up password reset codes by both phone and purpose', async () => {
|
|
findOne.mockResolvedValue(null);
|
|
await expect(
|
|
service.verifyOTP(otp.phone, '12345', OtpPurpose.PASSWORD_RESET),
|
|
).rejects.toThrow();
|
|
expect(findOne).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: { phone: otp.phone, purpose: 'password-reset' },
|
|
}),
|
|
);
|
|
expect(compare).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('allows a correct code after nine wrong guesses', async () => {
|
|
otp.failedAttempts = 9;
|
|
await expect(service.verifyOTP(otp.phone, '12345')).resolves.toBe(otp);
|
|
expect(otp.used).toBe(true);
|
|
});
|
|
|
|
it('rejects expired or missing codes without comparing hashes', async () => {
|
|
otp.expiresAt = new Date(Date.now() - 1);
|
|
await expect(service.verifyOTP(otp.phone, '12345')).rejects.toThrow();
|
|
findOne.mockResolvedValue(null);
|
|
await expect(service.verifyOTP(otp.phone, '12345')).rejects.toThrow();
|
|
expect(compare).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects a code that expires while its hash is being compared', async () => {
|
|
compare.mockImplementation(() => {
|
|
otp.expiresAt = new Date(Date.now() - 1);
|
|
return Promise.resolve(true);
|
|
});
|
|
await expect(service.verifyOTP(otp.phone, '12345')).rejects.toThrow();
|
|
expect(save).not.toHaveBeenCalled();
|
|
});
|
|
});
|