feat: add reset password
This commit is contained in:
@@ -3,6 +3,7 @@ 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;
|
||||
@@ -48,7 +49,7 @@ describe('OTP verification', () => {
|
||||
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 },
|
||||
where: { phone: otp.phone, purpose: 'signup' },
|
||||
order: { createdAt: 'DESC', id: 'DESC' },
|
||||
lock: { mode: 'pessimistic_write' },
|
||||
});
|
||||
@@ -71,6 +72,19 @@ describe('OTP verification', () => {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user