feat(phone): Add `removePhoneNumber` to RecoveryPhoneService

This commit is contained in:
Vijay Budhram 2024-11-18 11:25:42 -05:00
Родитель ea8f8b1e9d
Коммит edf4db1612
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 9778545895B2532B
3 изменённых файлов: 41 добавлений и 3 удалений

Просмотреть файл

@ -90,7 +90,13 @@ export class RecoveryPhoneManager {
*/
async removePhoneNumber(uid: string): Promise<boolean> {
const uidBuffer = Buffer.from(uid, 'hex');
return await removePhoneNumber(this.db, uidBuffer);
const removed = await removePhoneNumber(this.db, uidBuffer);
if (!removed) {
throw new RecoveryNumberNotExistsError(uid);
}
return true;
}
/**

Просмотреть файл

@ -8,7 +8,10 @@ import { OtpManager } from '@fxa/shared/otp';
import { SmsManager } from './sms.manager';
import { RecoveryPhoneServiceConfig } from './recovery-phone.service.config';
import { RecoveryPhoneManager } from './recovery-phone.manager';
import { RecoveryNumberNotSupportedError } from './recovery-phone.errors';
import {
RecoveryNumberNotExistsError,
RecoveryNumberNotSupportedError,
} from './recovery-phone.errors';
describe('RecoveryPhoneService', () => {
const phoneNumber = '+15005551234';
@ -16,7 +19,10 @@ describe('RecoveryPhoneService', () => {
const code = '000000';
const mockSmsManager = { sendSMS: jest.fn().mockReturnValue(true) };
const mockRecoveryPhoneManager = { storeUnconfirmed: jest.fn() };
const mockRecoveryPhoneManager = {
storeUnconfirmed: jest.fn(),
removePhoneNumber: jest.fn(),
};
const mockOtpManager = { generateCode: jest.fn() };
const mockRecoveryPhoneServiceConfig = {
allowedNumbers: ['+1500'],
@ -94,4 +100,20 @@ describe('RecoveryPhoneService', () => {
mockError
);
});
describe('removePhoneNumber', () => {
it('should remove a phone number', async () => {
mockRecoveryPhoneManager.removePhoneNumber.mockResolvedValueOnce(true);
const result = await service.removePhoneNumber(uid);
expect(result).toBeTruthy();
expect(mockRecoveryPhoneManager.removePhoneNumber).toBeCalledWith(uid);
});
it('should throw if phone number not found', () => {
mockRecoveryPhoneManager.removePhoneNumber.mockRejectedValueOnce(
new RecoveryNumberNotExistsError(uid)
);
expect(service.removePhoneNumber(uid)).rejects.toThrow();
});
});
});

Просмотреть файл

@ -49,4 +49,14 @@ export class RecoveryPhoneService {
);
return true;
}
/**
* Remove phone number from an account. Each user can only have one associated
* phone number.
*
* @param uid
*/
public async removePhoneNumber(uid: string) {
return await this.recoveryPhoneManager.removePhoneNumber(uid);
}
}