From 30b0cabbbc76aedee1f9ee4a18e315947dfa40c1 Mon Sep 17 00:00:00 2001 From: Antoine Clausse Date: Mon, 21 Jul 2025 14:52:43 +0200 Subject: [PATCH] [web] Update tests to add emails with 6-digits flow (#27076) * In tests, post to `/user/emails/secondary` (6-digits) instead of the deprecated `/user/emails` (link-token) * Update `addEmailAndConfirm` so it calls the right endpoint * Remove unnecessary `userId` from `confirmEmail` and `addEmailAndConfirm` args * Use `updateUser` to add unconfirmed email to user * Confirm, then unconfirm emails, in order to test on unconfirmed emails * Lowercase emails in `unconfirmSecondaryEmail`, so they get matched correctly * Update UserEmailsTests.mjs with 6-digits flow, fetch, no `npm:async` GitOrigin-RevId: 71b9ed65daebea5f22272240559caab375515f0c --- .../acceptance/src/PrimaryEmailCheckTests.mjs | 20 +++--------- .../acceptance/src/helpers/UserHelper.mjs | 31 +++++++++++++++---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/services/web/test/acceptance/src/PrimaryEmailCheckTests.mjs b/services/web/test/acceptance/src/PrimaryEmailCheckTests.mjs index 1598373f34..bc85f96a5d 100644 --- a/services/web/test/acceptance/src/PrimaryEmailCheckTests.mjs +++ b/services/web/test/acceptance/src/PrimaryEmailCheckTests.mjs @@ -69,10 +69,7 @@ describe('PrimaryEmailCheck', function () { $set: { lastPrimaryEmailCheck: new Date(time) }, }) - await userHelper.confirmEmail( - userHelper.user._id, - userHelper.user.email - ) + await userHelper.confirmEmail(userHelper.user.email) }) it("shouldn't be redirected from project list to the primary email check page", async function () { @@ -153,10 +150,7 @@ describe('PrimaryEmailCheck', function () { $set: { lastPrimaryEmailCheck: new Date(time) }, }) - await userHelper.confirmEmail( - userHelper.user._id, - userHelper.user.email - ) + await userHelper.confirmEmail(userHelper.user.email) }) it("shouldn't be redirected from project list to the primary email check page", async function () { @@ -219,14 +213,8 @@ describe('PrimaryEmailCheck', function () { }) beforeEach(async function () { - await userHelper.confirmEmail( - userHelper.user._id, - userHelper.user.email - ) - await userHelper.addEmailAndConfirm( - userHelper.user._id, - 'secondary@overleaf.com' - ) + await userHelper.confirmEmail(userHelper.user.email) + await userHelper.addEmailAndConfirm('secondary@overleaf.com') checkResponse = await userHelper.fetch( '/user/emails/primary-email-check', diff --git a/services/web/test/acceptance/src/helpers/UserHelper.mjs b/services/web/test/acceptance/src/helpers/UserHelper.mjs index 951dccaf2b..05fef85604 100644 --- a/services/web/test/acceptance/src/helpers/UserHelper.mjs +++ b/services/web/test/acceptance/src/helpers/UserHelper.mjs @@ -162,7 +162,7 @@ class UserHelper { /** * - * @param {'pendingExistingEmail'|'pendingUserRegistration'}sessionKey + * @param {'pendingExistingEmail'|'pendingUserRegistration'|'pendingSecondaryEmail'}sessionKey * @return {Promise<*>} */ async getEmailConfirmationCode(sessionKey) { @@ -431,16 +431,16 @@ class UserHelper { } async addEmail(email) { - const response = await this.fetch('/user/emails', { + const response = await this.fetch('/user/emails/secondary', { method: 'POST', body: new URLSearchParams([['email', email]]), }) await throwIfErrorResponse(response) } - async addEmailAndConfirm(userId, email) { + async addEmailAndConfirm(email) { await this.addEmail(email) - await this.confirmEmail(userId, email) + await this.confirmSecondaryEmail() } async changeConfirmationDate(userId, email, date) { @@ -499,9 +499,9 @@ class UserHelper { await this.changeConfirmationDate(userId, email, date) } - async confirmEmail(userId, email) { + async confirmEmail(email) { // clear ratelimiting on resend confirmation endpoint - await rateLimiters.sendConfirmation.delete(userId) + await rateLimiters.sendConfirmation.delete(this.user._id) const requestConfirmationCode = await this.fetch( '/user/emails/send-confirmation-code', { @@ -517,6 +517,25 @@ class UserHelper { }) await throwIfErrorResponse(requestConfirmCode) } + + async confirmSecondaryEmail() { + const code = await this.getEmailConfirmationCode('pendingSecondaryEmail') + const requestConfirmCode = await this.fetch( + '/user/emails/confirm-secondary', + { + method: 'POST', + body: new URLSearchParams({ code }), + } + ) + await throwIfErrorResponse(requestConfirmCode) + } + + async unconfirmEmail(email) { + await UserUpdater.promises.updateUser( + { _id: this.user._id, 'emails.email': email.toLowerCase() }, + { $unset: { 'emails.$.confirmedAt': 1, 'emails.$.reconfirmedAt': 1 } } + ) + } } export default UserHelper