|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | + |
| 3 | +import { appConfigs } from '../presets'; |
| 4 | +import { createTestUtils, testAgainstRunningApps } from '../testUtils'; |
| 5 | +import { stringPhoneNumber } from '../testUtils/phoneUtils'; |
| 6 | +import { fakerPhoneNumber } from '../testUtils/usersService'; |
| 7 | + |
| 8 | +testAgainstRunningApps({ withEnv: [appConfigs.envs.withSessionTasksSetupMfa] })( |
| 9 | + 'session tasks setup-mfa flow @nextjs', |
| 10 | + ({ app }) => { |
| 11 | + test.describe.configure({ mode: 'serial' }); |
| 12 | + |
| 13 | + test.afterAll(async () => { |
| 14 | + await app.teardown(); |
| 15 | + }); |
| 16 | + |
| 17 | + test.afterEach(async ({ page, context }) => { |
| 18 | + const u = createTestUtils({ app, page, context }); |
| 19 | + await u.page.signOut(); |
| 20 | + await u.page.context().clearCookies(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('setup MFA with new phone number - happy path', async ({ page, context }) => { |
| 24 | + const u = createTestUtils({ app, page, context }); |
| 25 | + const user = u.services.users.createFakeUser({ |
| 26 | + fictionalEmail: true, |
| 27 | + withPassword: true, |
| 28 | + }); |
| 29 | + await u.services.users.createBapiUser(user); |
| 30 | + |
| 31 | + await u.po.signIn.goTo(); |
| 32 | + await u.po.signIn.waitForMounted(); |
| 33 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: user.email, password: user.password }); |
| 34 | + await u.po.expect.toBeSignedIn(); |
| 35 | + |
| 36 | + await u.page.goToRelative('/page-protected'); |
| 37 | + |
| 38 | + await u.page.getByText(/set up two-step verification/i).waitFor({ state: 'visible' }); |
| 39 | + |
| 40 | + await u.page.getByRole('button', { name: /sms code/i }).click(); |
| 41 | + |
| 42 | + const testPhoneNumber = fakerPhoneNumber(); |
| 43 | + await u.po.signIn.getPhoneNumberInput().fill(testPhoneNumber); |
| 44 | + await u.page.getByRole('button', { name: /continue/i }).click(); |
| 45 | + |
| 46 | + await u.po.signIn.enterTestOtpCode(); |
| 47 | + |
| 48 | + await u.page.getByText(/save these backup codes/i).waitFor({ state: 'visible', timeout: 10000 }); |
| 49 | + |
| 50 | + await u.po.signIn.continue(); |
| 51 | + |
| 52 | + await u.page.waitForAppUrl('/page-protected'); |
| 53 | + await u.po.expect.toBeSignedIn(); |
| 54 | + |
| 55 | + await user.deleteIfExists(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('setup MFA with existing phone number - happy path', async ({ page, context }) => { |
| 59 | + const u = createTestUtils({ app, page, context }); |
| 60 | + const user = u.services.users.createFakeUser({ |
| 61 | + fictionalEmail: true, |
| 62 | + withPhoneNumber: true, |
| 63 | + withPassword: true, |
| 64 | + }); |
| 65 | + await u.services.users.createBapiUser(user); |
| 66 | + |
| 67 | + await u.po.signIn.goTo(); |
| 68 | + await u.po.signIn.waitForMounted(); |
| 69 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: user.email, password: user.password }); |
| 70 | + await u.po.expect.toBeSignedIn(); |
| 71 | + |
| 72 | + await u.page.goToRelative('/page-protected'); |
| 73 | + |
| 74 | + await u.page.getByText(/set up two-step verification/i).waitFor({ state: 'visible' }); |
| 75 | + |
| 76 | + await u.page.getByRole('button', { name: /sms code/i }).click(); |
| 77 | + |
| 78 | + const formattedPhoneNumber = stringPhoneNumber(user.phoneNumber); |
| 79 | + await u.page |
| 80 | + .getByRole('button', { |
| 81 | + name: formattedPhoneNumber, |
| 82 | + }) |
| 83 | + .click(); |
| 84 | + |
| 85 | + await u.page.getByText(/save these backup codes/i).waitFor({ state: 'visible', timeout: 10000 }); |
| 86 | + |
| 87 | + await u.po.signIn.continue(); |
| 88 | + |
| 89 | + await u.page.waitForAppUrl('/page-protected'); |
| 90 | + await u.po.expect.toBeSignedIn(); |
| 91 | + |
| 92 | + await user.deleteIfExists(); |
| 93 | + }); |
| 94 | + |
| 95 | + test('setup MFA with invalid phone number - error handling', async ({ page, context }) => { |
| 96 | + const u = createTestUtils({ app, page, context }); |
| 97 | + const user = u.services.users.createFakeUser({ |
| 98 | + fictionalEmail: true, |
| 99 | + withPassword: true, |
| 100 | + }); |
| 101 | + await u.services.users.createBapiUser(user); |
| 102 | + |
| 103 | + await u.po.signIn.goTo(); |
| 104 | + await u.po.signIn.waitForMounted(); |
| 105 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: user.email, password: user.password }); |
| 106 | + await u.po.expect.toBeSignedIn(); |
| 107 | + |
| 108 | + await u.page.goToRelative('/page-protected'); |
| 109 | + |
| 110 | + await u.page.getByText(/set up two-step verification/i).waitFor({ state: 'visible' }); |
| 111 | + |
| 112 | + await u.page.getByRole('button', { name: /sms code/i }).click(); |
| 113 | + |
| 114 | + const invalidPhoneNumber = '123091293193091311'; |
| 115 | + await u.po.signIn.getPhoneNumberInput().fill(invalidPhoneNumber); |
| 116 | + await u.po.signIn.continue(); |
| 117 | + // we need to improve this error message |
| 118 | + await expect(u.page.getByTestId('form-feedback-error')).toBeVisible(); |
| 119 | + |
| 120 | + const validPhoneNumber = fakerPhoneNumber(); |
| 121 | + await u.po.signIn.getPhoneNumberInput().fill(validPhoneNumber); |
| 122 | + await u.po.signIn.continue(); |
| 123 | + |
| 124 | + await u.po.signIn.enterTestOtpCode(); |
| 125 | + |
| 126 | + await u.page.getByText(/save these backup codes/i).waitFor({ state: 'visible', timeout: 10000 }); |
| 127 | + |
| 128 | + await u.po.signIn.continue(); |
| 129 | + |
| 130 | + await u.page.waitForAppUrl('/page-protected'); |
| 131 | + await u.po.expect.toBeSignedIn(); |
| 132 | + |
| 133 | + await user.deleteIfExists(); |
| 134 | + }); |
| 135 | + |
| 136 | + test('setup MFA with invalid verification code - error handling', async ({ page, context }) => { |
| 137 | + const u = createTestUtils({ app, page, context }); |
| 138 | + const user = u.services.users.createFakeUser({ |
| 139 | + fictionalEmail: true, |
| 140 | + withPassword: true, |
| 141 | + }); |
| 142 | + await u.services.users.createBapiUser(user); |
| 143 | + |
| 144 | + await u.po.signIn.goTo(); |
| 145 | + await u.po.signIn.waitForMounted(); |
| 146 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: user.email, password: user.password }); |
| 147 | + await u.po.expect.toBeSignedIn(); |
| 148 | + |
| 149 | + await u.page.goToRelative('/page-protected'); |
| 150 | + |
| 151 | + await u.page.getByText(/set up two-step verification/i).waitFor({ state: 'visible' }); |
| 152 | + |
| 153 | + await u.page.getByRole('button', { name: /sms code/i }).click(); |
| 154 | + |
| 155 | + const testPhoneNumber = fakerPhoneNumber(); |
| 156 | + await u.po.signIn.getPhoneNumberInput().fill(testPhoneNumber); |
| 157 | + await u.po.signIn.continue(); |
| 158 | + |
| 159 | + await u.po.signIn.enterOtpCode('111111', { |
| 160 | + awaitRequests: false, |
| 161 | + }); |
| 162 | + |
| 163 | + await expect(u.page.getByTestId('form-feedback-error')).toBeVisible(); |
| 164 | + |
| 165 | + await user.deleteIfExists(); |
| 166 | + }); |
| 167 | + |
| 168 | + test('can navigate back during MFA setup', async ({ page, context }) => { |
| 169 | + const u = createTestUtils({ app, page, context }); |
| 170 | + const user = u.services.users.createFakeUser({ |
| 171 | + fictionalEmail: true, |
| 172 | + withPhoneNumber: true, |
| 173 | + withPassword: true, |
| 174 | + }); |
| 175 | + await u.services.users.createBapiUser(user); |
| 176 | + |
| 177 | + await u.po.signIn.goTo(); |
| 178 | + await u.po.signIn.waitForMounted(); |
| 179 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: user.email, password: user.password }); |
| 180 | + await u.po.expect.toBeSignedIn(); |
| 181 | + |
| 182 | + await u.page.goToRelative('/page-protected'); |
| 183 | + |
| 184 | + await u.page.getByText(/set up two-step verification/i).waitFor({ state: 'visible' }); |
| 185 | + |
| 186 | + await u.page.getByRole('button', { name: /sms code/i }).click(); |
| 187 | + |
| 188 | + const formattedPhoneNumber = stringPhoneNumber(user.phoneNumber); |
| 189 | + await u.page |
| 190 | + .getByRole('button', { |
| 191 | + name: formattedPhoneNumber, |
| 192 | + }) |
| 193 | + .waitFor({ state: 'visible' }); |
| 194 | + |
| 195 | + await u.page |
| 196 | + .getByRole('button', { name: /cancel/i }) |
| 197 | + .first() |
| 198 | + .click(); |
| 199 | + |
| 200 | + await u.page.getByText(/set up two-step verification/i).waitFor({ state: 'visible' }); |
| 201 | + await u.page.getByRole('button', { name: /sms code/i }).waitFor({ state: 'visible' }); |
| 202 | + |
| 203 | + await user.deleteIfExists(); |
| 204 | + }); |
| 205 | + }, |
| 206 | +); |
0 commit comments