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

fix: email update #2158

Merged
merged 10 commits into from
Apr 19, 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']
if (!mailsacApiKey) {
throw new Error('MAILSAC_API_KEY is not set')
}

ignaciosantise marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -69,7 +69,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
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export class W3mAccountDefaultWidget extends LitElement {
iconVariant="overlay"
icon="mail"
iconSize="sm"
data-testid="w3m-account-email-update"
?chevron=${true}
@click=${() => this.onGoToUpdateEmail(email)}
>
Expand Down
18 changes: 13 additions & 5 deletions packages/scaffold/src/views/w3m-update-email-wallet-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,20 @@ export class W3mUpdateEmailWalletView extends LitElement {
throw new Error('w3m-update-email-wallet: Email connector not found')
}

await emailConnector.provider.updateEmail({ email: this.email })
const response = await emailConnector.provider.updateEmail({ email: this.email })
EventsController.sendEvent({ type: 'track', event: 'EMAIL_EDIT' })
RouterController.replace('UpdateEmailPrimaryOtp', {
email: this.initialEmail,
newEmail: this.email
})

if (response.action === 'VERIFY_SECONDARY_OTP') {
RouterController.push('UpdateEmailSecondaryOtp', {
email: this.initialEmail,
newEmail: this.email
})
} else {
RouterController.push('UpdateEmailPrimaryOtp', {
email: this.initialEmail,
newEmail: this.email
})
}
} catch (error) {
SnackController.showError(error)
this.loading = false
Expand Down
12 changes: 7 additions & 5 deletions packages/wallet/src/W3mFrameProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type IsConnectedResolver = Resolver<W3mFrameTypes.Responses['FrameIsConnectedRes
type GetChainIdResolver = Resolver<W3mFrameTypes.Responses['FrameGetChainIdResponse']>
type SwitchChainResolver = Resolver<W3mFrameTypes.Responses['FrameSwitchNetworkResponse']>
type RpcRequestResolver = Resolver<W3mFrameTypes.RPCResponse>
type UpdateEmailResolver = Resolver<undefined>
type UpdateEmailResolver = Resolver<W3mFrameTypes.Responses['FrameUpdateEmailResponse']>
type UpdateEmailPrimaryOtpResolver = Resolver<undefined>
type UpdateEmailSecondaryOtpResolver = Resolver<
W3mFrameTypes.Responses['FrameUpdateEmailSecondaryOtpResolver']
Expand Down Expand Up @@ -109,7 +109,7 @@ export class W3mFrameProvider {
case W3mFrameConstants.FRAME_SESSION_UPDATE:
return this.onSessionUpdate(event)
case W3mFrameConstants.FRAME_UPDATE_EMAIL_SUCCESS:
return this.onUpdateEmailSuccess()
return this.onUpdateEmailSuccess(event)
case W3mFrameConstants.FRAME_UPDATE_EMAIL_ERROR:
return this.onUpdateEmailError(event)
case W3mFrameConstants.FRAME_UPDATE_EMAIL_PRIMARY_OTP_SUCCESS:
Expand Down Expand Up @@ -210,7 +210,7 @@ export class W3mFrameProvider {
W3mFrameHelpers.checkIfAllowedToTriggerEmail()
this.w3mFrame.events.postAppEvent({ type: W3mFrameConstants.APP_UPDATE_EMAIL, payload })

return new Promise((resolve, reject) => {
return new Promise<W3mFrameTypes.Responses['FrameUpdateEmailResponse']>((resolve, reject) => {
this.updateEmailResolver = { resolve, reject }
})
}
Expand Down Expand Up @@ -523,8 +523,10 @@ export class W3mFrameProvider {
}
}

private onUpdateEmailSuccess() {
this.updateEmailResolver?.resolve(undefined)
private onUpdateEmailSuccess(
event: Extract<W3mFrameTypes.FrameEvent, { type: '@w3m-frame/UPDATE_EMAIL_SUCCESS' }>
) {
this.updateEmailResolver?.resolve(event.payload)
this.setNewLastEmailLoginTime()
}

Expand Down
5 changes: 4 additions & 1 deletion packages/wallet/src/W3mFrameSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export const AppSetPreferredAccountRequest = z.object({ type: z.string() })
export const FrameConnectEmailResponse = z.object({
action: z.enum(['VERIFY_DEVICE', 'VERIFY_OTP'])
})
export const FrameUpdateEmailResponse = z.object({
action: z.enum(['VERIFY_PRIMARY_OTP', 'VERIFY_SECONDARY_OTP'])
})
export const FrameGetUserResponse = z.object({
email: z.string().email(),
address: z.string(),
Expand Down Expand Up @@ -406,7 +409,7 @@ export const W3mFrameSchema = {

.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_ERROR'), payload: zError }))

.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_SUCCESS') }))
.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_SUCCESS'), payload: FrameUpdateEmailResponse }))

.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_PRIMARY_OTP_ERROR'), payload: zError }))

Expand Down
4 changes: 3 additions & 1 deletion packages/wallet/src/W3mFrameTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ import {
RpcEthChainId,
FrameSwitchNetworkResponse,
AppSyncDappDataRequest,
FrameGetSmartAccountEnabledNetworksResponse
FrameGetSmartAccountEnabledNetworksResponse,
FrameUpdateEmailResponse
} from './W3mFrameSchema.js'
import type { W3mFrameRpcConstants } from './W3mFrameConstants.js'

Expand Down Expand Up @@ -86,6 +87,7 @@ export namespace W3mFrameTypes {
FrameGetSmartAccountEnabledNetworksResponse: z.infer<
typeof FrameGetSmartAccountEnabledNetworksResponse
>
FrameUpdateEmailResponse: z.infer<typeof FrameUpdateEmailResponse>
}

export interface Network {
Expand Down