Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task(recovery-phone): Add confirmCode method #18022

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ describe('RecoveryPhoneService', () => {
const code = '000000';

const mockSmsManager = { sendSMS: jest.fn().mockReturnValue(true) };
const mockRecoveryPhoneManager = { storeUnconfirmed: jest.fn() };
const mockRecoveryPhoneManager = {
storeUnconfirmed: jest.fn(),
getUnconfirmed: jest.fn(),
registerPhoneNumber: jest.fn(),
};
const mockOtpManager = { generateCode: jest.fn() };
const mockRecoveryPhoneServiceConfig = {
allowedNumbers: ['+1500'],
Expand Down Expand Up @@ -94,4 +98,66 @@ describe('RecoveryPhoneService', () => {
mockError
);
});

describe('confirm code', () => {
it('can confirm valid sms code', async () => {
mockRecoveryPhoneManager.getUnconfirmed.mockReturnValue({});

const result = await service.confirmCode(uid, code);

expect(result).toBeTruthy();
expect(mockRecoveryPhoneManager.getUnconfirmed).toBeCalledWith(uid, code);
});

it('can confirm valid sms code used for setup', async () => {
mockRecoveryPhoneManager.getUnconfirmed.mockReturnValue({
isSetup: true,
});
mockRecoveryPhoneManager.registerPhoneNumber.mockReturnValue(true);

const result = await service.confirmCode(uid, code);

expect(result).toBeTruthy();
expect(mockRecoveryPhoneManager.getUnconfirmed).toBeCalledWith(uid, code);
});

it('can confirm valid sms code used for setup', async () => {
mockRecoveryPhoneManager.getUnconfirmed.mockResolvedValue({
isSetup: true,
phoneNumber,
});
mockRecoveryPhoneManager.registerPhoneNumber.mockResolvedValue({});

const result = await service.confirmCode(uid, code);

expect(result).toEqual(true);
expect(mockRecoveryPhoneManager.getUnconfirmed).toBeCalledWith(uid, code);
expect(mockRecoveryPhoneManager.registerPhoneNumber).toBeCalledWith(
uid,
phoneNumber
);
});

it('can indicate invalid sms code', async () => {
mockRecoveryPhoneManager.getUnconfirmed.mockReturnValue(null);

const result = await service.confirmCode(uid, code);

expect(result).toEqual(false);
expect(mockRecoveryPhoneManager.getUnconfirmed).toBeCalledWith(uid, code);
});

it('throws library error while confirming sms code', () => {
mockRecoveryPhoneManager.getUnconfirmed.mockRejectedValueOnce(mockError);
expect(service.confirmCode(uid, code)).rejects.toEqual(mockError);
});

it('throws library error while registering phone number for sms code', () => {
mockRecoveryPhoneManager.getUnconfirmed.mockResolvedValue({
isSetup: true,
});
mockRecoveryPhoneManager.registerPhoneNumber.mockRejectedValue(mockError);
expect(service.confirmCode(uid, code)).rejects.toEqual(mockError);
});
});
});
27 changes: 27 additions & 0 deletions libs/accounts/recovery-phone/src/lib/recovery-phone.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,31 @@ export class RecoveryPhoneService {
);
return true;
}

/**
* Confirms a UID code. This will also and finalizes the phone number setup if the code provided was
* intended for phone number setup.
* @param uid An account id
* @param code A otp code
* @returns True if successful
*/
public async confirmCode(uid: string, code: string) {
const data = await this.recoveryPhoneManager.getUnconfirmed(uid, code);

// If there is no data, it means there's no record of this code being sent to the uid provided
if (data == null) {
return false;
}

// If this was for a setup operation. Register the phone number to the uid.
if (data.isSetup === true) {
await this.recoveryPhoneManager.registerPhoneNumber(
uid,
data.phoneNumber
);
}

// There was a record matching, the uid / code. The confirmation was successful.
return true;
}
}