feat: add reset password

This commit is contained in:
2026-11-22 09:25:21 +03:30
parent 88b4a99908
commit 302d06b44f
15 changed files with 197 additions and 26 deletions
+15 -1
View File
@@ -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);