Skip to content

Commit e13b540

Browse files
authored
Merge pull request #19382 from mozilla/FXA-12293
chore(functional-tests): Update sms references to smsClient
2 parents fe1e42b + b5a68a2 commit e13b540

17 files changed

Lines changed: 105 additions & 225 deletions

File tree

packages/functional-tests/lib/sms.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,36 @@ export class SmsClient {
8585
return this.getPhoneNumber() !== TEST_NUMBER;
8686
}
8787

88-
89-
async getCode(recipientNumber: string, uid: string, timeout = 10000) {
88+
/**
89+
* Gets the sent SMS code for an account uid. If no number is provided
90+
* `this.getPhoneNumber()` is used to get a default.
91+
* @returns
92+
*/
93+
async getCode({
94+
uid,
95+
phoneNumber = this.getPhoneNumber(),
96+
timeout = 10000,
97+
}: {
98+
uid: string;
99+
phoneNumber?: string;
100+
timeout?: number;
101+
}) {
90102
if (this.isTwilioEnabled()) {
91-
return this._getCodeTwilio(recipientNumber);
103+
return this._getCodeTwilio(phoneNumber);
92104
} else {
93105
return this._getCodeLocal(uid, timeout);
94106
}
95107
}
96108

97109
/**
98-
* Guard function against misconfiguration of test phone numbers.
110+
* Important! Twilio does not allow you to fetch messages when using test
111+
* credentials. Twilio also does not allow you to send messages to magic
112+
* test numbers with real credentials.
113+
*
114+
* Therefore, if a 'magic' test number is configured, then we need to
115+
* use redis to peek at codes sent out, and if a 'real' testing phone
116+
* number is being being used, then we need to check the Twilio API for
117+
* the message sent out and look at the code within.
99118
*
100119
* Two conditions are checked:
101120
* - If a `real` number is configured and Twilio is NOT enabled

packages/functional-tests/lib/targets/index.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,6 @@ export function create(name: TargetName): BaseTarget {
2727
export { BaseTarget as ServerTarget };
2828
export { Credentials } from './base';
2929

30-
// Default test number, see Twilio test credentials phone numbers:
31-
// https://www.twilio.com/docs/iam/test-credentials
32-
export const TEST_NUMBER = '4159929960';
33-
34-
/**
35-
* Checks the process env for a configured twilio test phone number. Defaults
36-
* to generic magic test number if one is not provided.
37-
* @param targetName The test target name. eg local, stage, prod.
38-
* @returns
39-
*/
40-
export function getPhoneNumber(targetName: TargetName) {
41-
if (targetName === 'local') {
42-
return TEST_NUMBER;
43-
}
44-
return getFromEnvWithFallback(
45-
'FUNCTIONAL_TESTS__TWILIO__TEST_NUMBER',
46-
targetName,
47-
TEST_NUMBER
48-
);
49-
}
50-
51-
export function usingRealTestPhoneNumber(targetName: TargetName) {
52-
return getPhoneNumber(targetName) !== TEST_NUMBER;
53-
}
54-
5530
/**
5631
* Helper function to get a value from the environment. If the key + __ + targetName exists, this
5732
* will be returned. Otherwise, if $key exists it will be returned. Otherwise undefined will be

packages/functional-tests/lib/totp.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import { authenticator as otplibAuthenticator } from 'otplib';
66

7-
// Generate a TOTP code matching server settings (hex-encoded secret)
8-
export async function getCode(secretHex: string): Promise<string> {
7+
/**
8+
* Generate a TOTP code matching server settings (hex-encoded secret)
9+
*/
10+
export async function getTotpCode(secretHex: string): Promise<string> {
911
const auth = new otplibAuthenticator.Authenticator();
1012
auth.options = Object.assign({}, otplibAuthenticator.options, {
1113
encoding: 'hex',

packages/functional-tests/pages/settings/totp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import jsQR from 'jsqr';
66
import UPNG from 'upng-js';
77
import { expect } from '../../lib/fixtures/standard';
88
import { SettingsLayout } from './layout';
9-
import { getCode } from '../../lib/totp';
9+
import { getTotpCode } from '../../lib/totp';
1010
import { DataTrioComponent } from './components/dataTrio';
1111

1212
export type TotpCredentials = {
@@ -122,7 +122,7 @@ export class TotpPage extends SettingsLayout {
122122
throw new Error('No secret found in QR code');
123123
}
124124

125-
const code = await getCode(secret);
125+
const code = await getTotpCode(secret);
126126
await this.step1AuthenticationCodeTextbox.fill(code);
127127
await this.step1SubmitButton.click();
128128
return secret;
@@ -134,7 +134,7 @@ export class TotpPage extends SettingsLayout {
134134

135135
await this.step1CantScanCodeLink.click();
136136
const secret = (await this.step1ManualCode.innerText())?.replace(/\s/g, '');
137-
const code = await getCode(secret);
137+
const code = await getTotpCode(secret);
138138
await this.step1AuthenticationCodeTextbox.fill(code);
139139
await this.step1SubmitButton.click();
140140
return secret;

packages/functional-tests/tests/cms/cms-2fa.spec.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44

55
import { expect, test } from '../../lib/fixtures/standard';
66
import { syncDesktopOAuthQueryParams } from '../../lib/query-params';
7-
import { getCode } from '../../lib/totp';
8-
import {
9-
TargetName,
10-
getFromEnvWithFallback,
11-
getPhoneNumber,
12-
usingRealTestPhoneNumber,
13-
} from '../../lib/targets';
7+
import { getTotpCode } from '../../lib/totp';
148

159
const ENTRYPOINT_123Done = 'purple';
1610
const CLIENTID_123Done = 'dcdb5ae7add825d2';
@@ -220,7 +214,7 @@ test.describe('severity-1 #smoke', () => {
220214
});
221215

222216
// Enter TOTP code
223-
const totpCode = await getCode(secret);
217+
const totpCode = await getTotpCode(secret);
224218
await signinTotpCode.fillOutCodeForm(totpCode);
225219

226220
// Verify successful login
@@ -294,15 +288,12 @@ test.describe('severity-1 #smoke', () => {
294288

295289
await expect(recoveryPhone.addHeader()).toBeVisible();
296290

297-
await recoveryPhone.enterPhoneNumber(getPhoneNumber(target.name));
291+
await recoveryPhone.enterPhoneNumber(target.smsClient.getPhoneNumber());
298292
await recoveryPhone.clickSendCode();
299293

300294
await expect(recoveryPhone.confirmHeader).toBeVisible();
301295

302-
const code = await target.smsClient.getCode(
303-
getPhoneNumber(target.name),
304-
credentials.uid
305-
);
296+
const code = await target.smsClient.getCode({ ...credentials });
306297

307298
await recoveryPhone.enterCode(code);
308299
await recoveryPhone.clickConfirm();
@@ -382,10 +373,7 @@ test.describe('severity-1 #smoke', () => {
382373
});
383374

384375
// Get SMS code and enter it
385-
const smsCode = await target.smsClient.getCode(
386-
getPhoneNumber(target.name),
387-
credentials.uid
388-
);
376+
const smsCode = await target.smsClient.getCode({ ...credentials });
389377

390378
await signinRecoveryPhone.enterCode(smsCode);
391379
await signinRecoveryPhone.clickConfirm();
@@ -656,7 +644,7 @@ test.describe('severity-1 #smoke', () => {
656644
});
657645

658646
// Enter TOTP code
659-
const totpCode = await getCode(secret);
647+
const totpCode = await getTotpCode(secret);
660648
await signinTotpCode.fillOutCodeForm(totpCode);
661649

662650
await page.getByRole('link', { name: 'Not now' }).click();

packages/functional-tests/tests/key-stretching-v2/totp.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { getCode } from '../../lib/totp';
5+
import { getTotpCode } from '../../lib/totp';
66
import { expect, test } from '../../lib/fixtures/standard';
77

88
/**
@@ -64,7 +64,7 @@ test.describe('severity-2 #smoke', () => {
6464

6565
await expect(page).toHaveURL(/signin_totp_code/);
6666

67-
const totpCode = await getCode(totpCredentials.secret);
67+
const totpCode = await getTotpCode(totpCredentials.secret);
6868
await signinTotpCode.fillOutCodeForm(totpCode);
6969

7070
await expect(page).toHaveURL(/settings/);

packages/functional-tests/tests/misc/recoveryKeyPromoInline.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import { expect, test } from '../../lib/fixtures/standard';
6-
import { getCode } from '../../lib/totp';
6+
import { getTotpCode } from '../../lib/totp';
77

88
test.describe('recovery key promo', () => {
99
test.describe('inline', () => {
@@ -187,7 +187,7 @@ test.describe('recovery key promo', () => {
187187

188188
await page.waitForURL(/signin_totp_code/);
189189

190-
const totpCode = await getCode(secret);
190+
const totpCode = await getTotpCode(secret);
191191
await signinTotpCode.fillOutCodeForm(totpCode);
192192

193193
await inlineRecoveryKey.getInlineRecoveryHeader();

packages/functional-tests/tests/misc/relayIntegration.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { FirefoxCommand } from '../../lib/channels';
66
import { expect, test } from '../../lib/fixtures/standard';
77
import { relayDesktopOAuthQueryParams } from '../../lib/query-params';
8-
import { getCode } from '../../lib/totp';
8+
import { getTotpCode } from '../../lib/totp';
99

1010
test.describe('relay integration', () => {
1111
test('signup with Relay desktop', async ({
@@ -119,7 +119,7 @@ test.describe('relay integration', () => {
119119

120120
await page.waitForURL(/signin_totp_code/);
121121

122-
const totpCode = await getCode(secret);
122+
const totpCode = await getTotpCode(secret);
123123
await signinTotpCode.fillOutCodeForm(totpCode);
124124

125125
await page.waitForURL(/settings/);

packages/functional-tests/tests/oauth/totp.spec.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { getCode } from '../../lib/totp';
5+
import { getTotpCode } from '../../lib/totp';
66
import { Page, expect, test } from '../../lib/fixtures/standard';
77
import { BaseTarget, Credentials } from '../../lib/targets/base';
88
import { SettingsPage } from '../../pages/settings';
99
import { SigninPage } from '../../pages/signin';
10-
import { getPhoneNumber } from '../../lib/targets';
1110

1211
test.describe('severity-1 #smoke', () => {
1312
test.describe('OAuth totp', () => {
@@ -34,7 +33,7 @@ test.describe('severity-1 #smoke', () => {
3433
await signin.fillOutEmailFirstForm(credentials.email);
3534
await signin.fillOutPasswordForm(credentials.password);
3635
await expect(page).toHaveURL(/signin_totp_code/);
37-
const code = await getCode(secret);
36+
const code = await getTotpCode(secret);
3837
await signinTotpCode.fillOutCodeForm(code);
3938

4039
expect(await relier.isLoggedIn()).toBe(true);
@@ -111,7 +110,7 @@ test.describe('severity-1 #smoke', () => {
111110
/\s/g,
112111
''
113112
);
114-
const code = await getCode(secret);
113+
const code = await getTotpCode(secret);
115114
await totp.step1AuthenticationCodeTextbox.fill(code);
116115
await totp.step1SubmitButton.click();
117116

@@ -177,7 +176,7 @@ test.describe('severity-1 #smoke', () => {
177176
const secret = (
178177
await signin.page.getByTestId('manual-datablock').innerText()
179178
)?.replace(/\s/g, '');
180-
const code = await getCode(secret);
179+
const code = await getTotpCode(secret);
181180

182181
await signin.page
183182
.getByRole('textbox', { name: 'Authentication code' })
@@ -247,7 +246,7 @@ test.describe('severity-1 #smoke', () => {
247246
const secret = (
248247
await signin.page.getByTestId('manual-datablock').innerText()
249248
)?.replace(/\s/g, '');
250-
const code = await getCode(secret);
249+
const code = await getTotpCode(secret);
251250

252251
await signin.page
253252
.getByRole('textbox', { name: 'Authentication code' })
@@ -315,7 +314,7 @@ test.describe('severity-1 #smoke', () => {
315314
const secret = (
316315
await signin.page.getByTestId('manual-datablock').innerText()
317316
)?.replace(/\s/g, '');
318-
const code = await getCode(secret);
317+
const code = await getTotpCode(secret);
319318

320319
await signin.page
321320
.getByRole('textbox', { name: 'Authentication code' })
@@ -326,15 +325,12 @@ test.describe('severity-1 #smoke', () => {
326325
await page.waitForURL(/inline_recovery_setup/);
327326

328327
await totp.chooseRecoveryPhoneOption();
329-
await recoveryPhone.enterPhoneNumber(getPhoneNumber(target.name));
328+
await recoveryPhone.enterPhoneNumber(target.smsClient.getPhoneNumber());
330329
await recoveryPhone.clickSendCode();
331330

332331
await expect(recoveryPhone.confirmHeader).toBeVisible();
333332

334-
const smsCode = await target.smsClient.getCode(
335-
getPhoneNumber(target.name),
336-
credentials.uid
337-
);
333+
const smsCode = await target.smsClient.getCode({ ...credentials });
338334

339335
await recoveryPhone.enterCode(smsCode);
340336
await recoveryPhone.clickConfirm();
@@ -383,7 +379,7 @@ test.describe('severity-1 #smoke', () => {
383379
/\s/g,
384380
''
385381
);
386-
const code = await getCode(secret);
382+
const code = await getTotpCode(secret);
387383
await totp.step1AuthenticationCodeTextbox.fill(code);
388384
await totp.step1SubmitButton.click();
389385

@@ -437,22 +433,19 @@ test.describe('severity-1 #smoke', () => {
437433
/\s/g,
438434
''
439435
);
440-
const code = await getCode(secret);
436+
const code = await getTotpCode(secret);
441437
await totp.step1AuthenticationCodeTextbox.fill(code);
442438
await totp.step1SubmitButton.click();
443439

444440
await page.waitForURL(/inline_recovery_setup/);
445441

446442
await totp.chooseRecoveryPhoneOption();
447-
await recoveryPhone.enterPhoneNumber(getPhoneNumber(target.name));
443+
await recoveryPhone.enterPhoneNumber(target.smsClient.getPhoneNumber());
448444
await recoveryPhone.clickSendCode();
449445

450446
await expect(recoveryPhone.confirmHeader).toBeVisible();
451447

452-
const smsCode = await target.smsClient.getCode(
453-
getPhoneNumber(target.name),
454-
credentials.uid
455-
);
448+
const smsCode = await target.smsClient.getCode({ ...credentials });
456449

457450
await recoveryPhone.enterCode(smsCode);
458451
await recoveryPhone.clickConfirm();

packages/functional-tests/tests/react-conversion/signinTotp.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { getCode } from '../../lib/totp';
5+
import { getTotpCode } from '../../lib/totp';
66
import { expect, test } from '../../lib/fixtures/standard';
77

88
test.describe('severity-1 #smoke', () => {
@@ -35,7 +35,7 @@ test.describe('severity-1 #smoke', () => {
3535
await signup.fillOutEmailForm(credentials.email);
3636
await signin.fillOutPasswordForm(credentials.password);
3737
await page.waitForURL(/signin_totp_code/);
38-
const totpCode = await getCode(secret);
38+
const totpCode = await getTotpCode(secret);
3939
await signinTotpCode.fillOutCodeForm(totpCode);
4040

4141
await expect(page).toHaveURL(/settings/);
@@ -87,7 +87,7 @@ test.describe('severity-1 #smoke', () => {
8787
await signup.fillOutEmailForm(credentials.email);
8888
await signin.fillOutPasswordForm(credentials.password);
8989
await page.waitForURL(/signin_totp_code/);
90-
const totpCode = await getCode(secret);
90+
const totpCode = await getTotpCode(secret);
9191
await signinTotpCode.fillOutCodeForm(totpCode);
9292

9393
await expect(page).toHaveURL(/pair/);
@@ -134,7 +134,7 @@ test.describe('severity-1 #smoke', () => {
134134
);
135135

136136
// Required before teardown
137-
const code = await getCode(secret);
137+
const code = await getTotpCode(secret);
138138
await signinTotpCode.fillOutCodeForm(code);
139139
await settings.disconnectTotp();
140140
});

0 commit comments

Comments
 (0)