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

chore: added update email tests #2166

Merged
merged 15 commits into from
Apr 23, 2024
9 changes: 9 additions & 0 deletions apps/laboratory/tests/email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { expect } from '@playwright/test'
import { testMEmail } from './shared/fixtures/w3m-email-fixture'
import { SECURE_WEBSITE_URL } from './shared/constants'

const mailsacApiKey = process.env['MAILSAC_API_KEY']
ignaciosantise marked this conversation as resolved.
Show resolved Hide resolved
if (!mailsacApiKey) {
throw new Error('MAILSAC_API_KEY is not set')
}

testMEmail.beforeEach(async ({ modalValidator }) => {
await modalValidator.expectConnected()
})
Expand Down Expand Up @@ -48,3 +53,7 @@ testMEmail('it should disconnect correctly', async ({ modalPage, modalValidator
await modalPage.disconnect()
await modalValidator.expectDisconnected()
})

testMEmail('it should update email', async ({ modalPage }) => {
await modalPage.updateEmail(mailsacApiKey)
})
60 changes: 54 additions & 6 deletions apps/laboratory/tests/shared/pages/ModalPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class ModalPage {

private readonly connectButton: Locator
private readonly url: string
private emailAddress = ''

constructor(
public readonly page: Page,
Expand Down Expand Up @@ -61,6 +62,8 @@ export class ModalPage {
): Promise<void> {
await this.load()

this.emailAddress = emailAddress

const email = new Email(mailsacApiKey)

await email.deleteAllMessages(emailAddress)
Expand Down Expand Up @@ -90,9 +93,11 @@ export class ModalPage {
otp = email.getOtpCodeFromBody(emailBody)
}
}
if (otp.replace(' ', '').length !== 6) {

if (otp === '') {
otp = email.getOtpCodeFromBody(emailBody)
}

await this.enterOTP(otp)
}

Expand All @@ -114,16 +119,15 @@ export class ModalPage {
})
}

async enterOTP(otp: string) {
await expect(this.page.getByText('Confirm Email')).toBeVisible({
async enterOTP(otp: string, headerTitle = 'Confirm Email') {
await expect(this.page.getByText(headerTitle)).toBeVisible({
timeout: 10_000
})
await expect(this.page.getByText('Enter the code we sent')).toBeVisible({
timeout: 10_000
})

const splitted = otp.split('')
// Remove empy space in OTP code 111 111
splitted.splice(3, 1)

// eslint-disable-next-line no-plusplus
for (let i = 0; i < splitted.length; i++) {
Expand All @@ -143,7 +147,7 @@ export class ModalPage {
await input.fill(digit)
}

await expect(this.page.getByText('Confirm Email')).not.toBeVisible()
await expect(this.page.getByText(headerTitle)).not.toBeVisible()
}

async disconnect() {
Expand Down Expand Up @@ -226,4 +230,48 @@ export class ModalPage {
async closeModal() {
await this.page.getByTestId('w3m-header-close')?.click?.()
}

async updateEmail(mailsacApiKey: string) {
const email = new Email(mailsacApiKey)
const newEmailAddress = email.getEmailAddressToUse(1)

await this.page.getByTestId('account-button').click()
await this.page.getByTestId('w3m-account-email-update').click()
await this.page.getByTestId('wui-email-input').locator('input').focus()
await this.page.getByTestId('wui-email-input').locator('input').fill(newEmailAddress)

// Clear messages before putting new email
await email.deleteAllMessages(this.emailAddress)
await this.page.getByTestId('wui-email-input').locator('input').press('Enter')

// Wait until the next screen appears
await expect(this.page.getByText('Enter the code we sent')).toBeVisible({
timeout: 10_000
})
const confirmCurrentEmail = await this.page.getByText('Confirm Current Email').isVisible()
if (confirmCurrentEmail) {
await this.updateOtpFlow(this.emailAddress, mailsacApiKey, 'Confirm Current Email')
}

await this.updateOtpFlow(newEmailAddress, mailsacApiKey, 'Confirm New Email')

expect(
this.page.getByTestId('w3m-account-email-update'),
`Expected to go to the account screen after the update`
).toBeVisible()
}

async updateOtpFlow(emailAddress: string, mailsacApiKey: string, headerTitle: string) {
const email = new Email(mailsacApiKey)

const messageId = await email.getLatestMessageId(emailAddress)

if (!messageId) {
throw new Error('No messageId found')
}
const emailBody = await email.getEmailBody(emailAddress, messageId)
const otp = email.getOtpCodeFromBody(emailBody)

await this.enterOTP(otp, headerTitle)
}
}
3 changes: 2 additions & 1 deletion apps/laboratory/tests/shared/utils/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export class Email {
getOtpCodeFromBody(body: string): string {
const match = body.match(OTP_CODE_REGEX)
if (match) {
return match[0]
// Remove empty space in OTP code 111 111
return match[0].replace(' ', '')
}

throw new Error(`No code found in email: ${body}`)
Expand Down
Loading