|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { getTotpCode } from '../../lib/totp'; |
| 6 | +import { expect, test } from '../../lib/fixtures/standard'; |
| 7 | +import { FirefoxCommand } from '../../lib/channels'; |
| 8 | +import { |
| 9 | + syncDesktopOAuthQueryParams, |
| 10 | + syncMobileOAuthQueryParams, |
| 11 | +} from '../../lib/query-params'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Auth state machine — OAuth native (Sync desktop/mobile via oauth_webchannel_v1) sign-in E2E. |
| 15 | + * |
| 16 | + * Flag delivery: authStateMachine=true is appended to the syncDesktopOAuthQueryParams / |
| 17 | + * syncMobileOAuthQueryParams set and passed to signin.goto('/authorization', params), |
| 18 | + * matching the pattern used in tests/oauth/syncSignIn.spec.ts for the same fixture. |
| 19 | + * |
| 20 | + * These tests mirror the coverage in tests/oauth/syncSignIn.spec.ts but with the |
| 21 | + * authStateMachine flag on, and additionally assert the fxaOAuthLogin and fxaLogin |
| 22 | + * web-channel messages fired by the native path. |
| 23 | + */ |
| 24 | + |
| 25 | +// Base params with the machine flag set — derived from syncDesktopOAuthQueryParams. |
| 26 | +const desktopParams = (() => { |
| 27 | + const p = new URLSearchParams(syncDesktopOAuthQueryParams); |
| 28 | + p.set('authStateMachine', 'true'); |
| 29 | + return p; |
| 30 | +})(); |
| 31 | + |
| 32 | +const mobileParams = (() => { |
| 33 | + const p = new URLSearchParams(syncMobileOAuthQueryParams); |
| 34 | + p.set('authStateMachine', 'true'); |
| 35 | + return p; |
| 36 | +})(); |
| 37 | + |
| 38 | +test.describe('auth-machine: OAuth native (oauth_webchannel_v1) sign-in', () => { |
| 39 | + test('verified Sync-Desktop account reaches connect-another-device and fires fxaOAuthLogin + fxaLogin web-channel messages', async ({ |
| 40 | + target, |
| 41 | + syncOAuthBrowserPages: { |
| 42 | + page, |
| 43 | + signin, |
| 44 | + signinTokenCode, |
| 45 | + connectAnotherDevice, |
| 46 | + }, |
| 47 | + testAccountTracker, |
| 48 | + }) => { |
| 49 | + const credentials = await testAccountTracker.signUpSync(); |
| 50 | + |
| 51 | + // Confirm the flag is present in the URL that reaches FxA. |
| 52 | + await signin.listenToWebChannelMessages(); |
| 53 | + await signin.goto('/authorization', desktopParams); |
| 54 | + await expect(page).toHaveURL(/authStateMachine=true/); |
| 55 | + |
| 56 | + await signin.fillOutEmailFirstForm(credentials.email); |
| 57 | + await signin.fillOutPasswordForm(credentials.password); |
| 58 | + |
| 59 | + // signUpSync uses a restmail address so a session token code is always required. |
| 60 | + await page.waitForURL(/signin_token_code/); |
| 61 | + const code = await target.emailClient.getVerifyLoginCode(credentials.email); |
| 62 | + await signinTokenCode.fillOutCodeForm(code); |
| 63 | + |
| 64 | + await expect(connectAnotherDevice.fxaConnected).toBeVisible(); |
| 65 | + |
| 66 | + // Key native-path assertions: both web-channel messages must fire. |
| 67 | + await signin.checkWebChannelMessage(FirefoxCommand.OAuthLogin); |
| 68 | + await signin.checkWebChannelMessage(FirefoxCommand.Login); |
| 69 | + }); |
| 70 | + |
| 71 | + test('unverified-session Sync-Desktop account routes to /signin_token_code, then reaches Sync destination + fires web-channel messages', async ({ |
| 72 | + target, |
| 73 | + syncOAuthBrowserPages: { |
| 74 | + page, |
| 75 | + signin, |
| 76 | + signinTokenCode, |
| 77 | + connectAnotherDevice, |
| 78 | + }, |
| 79 | + testAccountTracker, |
| 80 | + }) => { |
| 81 | + // preVerified: 'true' — email verified but every session requires OTP confirmation. |
| 82 | + const credentials = await testAccountTracker.signUpSync({ |
| 83 | + lang: 'en', |
| 84 | + service: 'sync', |
| 85 | + preVerified: 'true', |
| 86 | + }); |
| 87 | + |
| 88 | + await signin.listenToWebChannelMessages(); |
| 89 | + await signin.goto('/authorization', desktopParams); |
| 90 | + |
| 91 | + await signin.fillOutEmailFirstForm(credentials.email); |
| 92 | + await signin.fillOutPasswordForm(credentials.password); |
| 93 | + |
| 94 | + await expect(page).toHaveURL(/signin_token_code/); |
| 95 | + const code = await target.emailClient.getVerifyLoginCode(credentials.email); |
| 96 | + await signinTokenCode.fillOutCodeForm(code); |
| 97 | + |
| 98 | + await expect(connectAnotherDevice.fxaConnected).toBeVisible(); |
| 99 | + await signin.checkWebChannelMessage(FirefoxCommand.OAuthLogin); |
| 100 | + await signin.checkWebChannelMessage(FirefoxCommand.Login); |
| 101 | + }); |
| 102 | + |
| 103 | + test('unverified-email account routes to /confirm_signup_code, then reaches signup_confirmed_sync', async ({ |
| 104 | + target, |
| 105 | + syncOAuthBrowserPages: { |
| 106 | + page, |
| 107 | + signin, |
| 108 | + confirmSignupCode, |
| 109 | + signupConfirmedSync, |
| 110 | + }, |
| 111 | + testAccountTracker, |
| 112 | + }) => { |
| 113 | + // preVerified: 'false' — email not confirmed; sign-in routes to confirm_signup_code. |
| 114 | + // After code entry the destination is signup_confirmed_sync (not connectAnotherDevice), |
| 115 | + // matching the syncSignin.spec.ts pattern for new unverified accounts. |
| 116 | + const credentials = await testAccountTracker.signUpSync({ |
| 117 | + lang: 'en', |
| 118 | + service: 'sync', |
| 119 | + preVerified: 'false', |
| 120 | + }); |
| 121 | + |
| 122 | + await signin.listenToWebChannelMessages(); |
| 123 | + await signin.goto('/authorization', desktopParams); |
| 124 | + |
| 125 | + await signin.fillOutEmailFirstForm(credentials.email); |
| 126 | + await signin.fillOutPasswordForm(credentials.password); |
| 127 | + |
| 128 | + await expect(page).toHaveURL(/confirm_signup_code/); |
| 129 | + const code = await target.emailClient.getVerifyLoginCode(credentials.email); |
| 130 | + await confirmSignupCode.fillOutCodeForm(code); |
| 131 | + |
| 132 | + await expect(signupConfirmedSync.bannerConfirmed).toBeVisible(); |
| 133 | + await signin.checkWebChannelMessage(FirefoxCommand.OAuthLogin); |
| 134 | + await signin.checkWebChannelMessage(FirefoxCommand.Login); |
| 135 | + }); |
| 136 | + |
| 137 | + test('TOTP-enabled Sync-Desktop account routes to /signin_totp_code then reaches Sync destination', async ({ |
| 138 | + target, |
| 139 | + syncOAuthBrowserPages: { |
| 140 | + page, |
| 141 | + signin, |
| 142 | + signinTokenCode, |
| 143 | + signinTotpCode, |
| 144 | + connectAnotherDevice, |
| 145 | + settings, |
| 146 | + totp, |
| 147 | + }, |
| 148 | + testAccountTracker, |
| 149 | + }) => { |
| 150 | + const credentials = await testAccountTracker.signUpSync(); |
| 151 | + |
| 152 | + // Enable TOTP via a non-Sync settings session first. |
| 153 | + await page.goto(target.contentServerUrl); |
| 154 | + await signin.fillOutEmailFirstForm(credentials.email); |
| 155 | + await signin.fillOutPasswordForm(credentials.password); |
| 156 | + await page.waitForURL(/signin_token_code/); |
| 157 | + const setupCode = await target.emailClient.getVerifyLoginCode( |
| 158 | + credentials.email |
| 159 | + ); |
| 160 | + await signinTokenCode.fillOutCodeForm(setupCode); |
| 161 | + await page.waitForURL(/settings/); |
| 162 | + await expect(settings.settingsHeading).toBeVisible(); |
| 163 | + |
| 164 | + await settings.totp.addButton.click(); |
| 165 | + await settings.confirmMfaGuard(credentials.email); |
| 166 | + // Read recovery-phone availability so TOTP setup skips the chooser when it's unavailable. |
| 167 | + const { available: recoveryPhoneAvailable } = |
| 168 | + await target.authClient.recoveryPhoneAvailable(credentials.sessionToken); |
| 169 | + const { secret } = await totp.setUpTwoStepAuthWithQrAndBackupCodesChoice( |
| 170 | + credentials, |
| 171 | + recoveryPhoneAvailable |
| 172 | + ); |
| 173 | + await expect(settings.totp.status).toHaveText('Enabled'); |
| 174 | + await settings.signOut(); |
| 175 | + |
| 176 | + // Now sign in via native OAuth with the machine flag. |
| 177 | + await signin.listenToWebChannelMessages(); |
| 178 | + await signin.goto('/authorization', desktopParams); |
| 179 | + await signin.fillOutEmailFirstForm(credentials.email); |
| 180 | + await signin.fillOutPasswordForm(credentials.password); |
| 181 | + |
| 182 | + await expect(page).toHaveURL(/signin_totp_code/); |
| 183 | + const totpCode = await getTotpCode(secret); |
| 184 | + await signinTotpCode.fillOutCodeForm(totpCode); |
| 185 | + |
| 186 | + await expect(connectAnotherDevice.fxaConnected).toBeVisible(); |
| 187 | + await signin.checkWebChannelMessage(FirefoxCommand.OAuthLogin); |
| 188 | + await signin.checkWebChannelMessage(FirefoxCommand.Login); |
| 189 | + }); |
| 190 | + |
| 191 | + test('verified Sync-Mobile (iOS) account signs in and fires fxaOAuthLogin web-channel message', async ({ |
| 192 | + target, |
| 193 | + syncOAuthBrowserPages: { page, signin, signinTokenCode }, |
| 194 | + testAccountTracker, |
| 195 | + }) => { |
| 196 | + // syncMobileOAuthQueryParams (iOS client 1b1a3e44c54fbb58) omits service=sync, |
| 197 | + // so the post-auth destination is not connectAnotherDevice — the flow sends |
| 198 | + // OAuthLogin and Login web-channel events via the native webchannel path. |
| 199 | + const credentials = await testAccountTracker.signUpSync(); |
| 200 | + |
| 201 | + await signin.listenToWebChannelMessages(); |
| 202 | + await signin.goto('/authorization', mobileParams); |
| 203 | + await expect(page).toHaveURL(/authStateMachine=true/); |
| 204 | + |
| 205 | + await signin.fillOutEmailFirstForm(credentials.email); |
| 206 | + await signin.fillOutPasswordForm(credentials.password); |
| 207 | + |
| 208 | + await page.waitForURL(/signin_token_code/); |
| 209 | + const code = await target.emailClient.getVerifyLoginCode(credentials.email); |
| 210 | + await signinTokenCode.fillOutCodeForm(code); |
| 211 | + |
| 212 | + // The mobile client fires OAuthLogin (and Login) via web-channel on success. |
| 213 | + await signin.checkWebChannelMessage(FirefoxCommand.OAuthLogin); |
| 214 | + await signin.checkWebChannelMessage(FirefoxCommand.Login); |
| 215 | + }); |
| 216 | +}); |
0 commit comments