From b09d5ce6c43773909ef515e5dfed4ca11bd989fb Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 10 Jan 2024 20:43:37 +0530 Subject: [PATCH] feat: get activation link from maildev --- tests/auth.spec.ts | 4 ++-- tests/config.ts | 1 + tests/maildev.ts | 27 +++++++++++++++++++++++++++ tests/user.ts | 30 +++++++++++++++++++++++++++--- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/maildev.ts diff --git a/tests/auth.spec.ts b/tests/auth.spec.ts index 7f8c1db..dcda10c 100644 --- a/tests/auth.spec.ts +++ b/tests/auth.spec.ts @@ -40,7 +40,7 @@ test("Test registration error: password mismatch", async ({ page }) => { "Register - Forgejo: Beyond coding. We forge." ); expect(page.locator(".negative > p:nth-child(1)")).toContainText( - "passwords do not match" + "The passwords do not match" ); }); @@ -113,7 +113,7 @@ test("Test successful registration", async ({ page }) => { ); expect(page.locator(".positive > p:nth-child(1)")).toContainText( - "Account was successfully created. Welcome!" + "Account has been activated" ); }); diff --git a/tests/config.ts b/tests/config.ts index 5a16b07..d5eab7e 100644 --- a/tests/config.ts +++ b/tests/config.ts @@ -3,4 +3,5 @@ // SPDX-License-Identifier: AGPL-3.0-or-later export const INSTANCE_URL = new URL(process.env.INSTANCE_URL) +export const MAILDEV_URL = new URL(process.env.MAILDEV_URL) export const ADMIN_USERNAME = "admin"; diff --git a/tests/maildev.ts b/tests/maildev.ts new file mode 100644 index 0000000..97bd1b4 --- /dev/null +++ b/tests/maildev.ts @@ -0,0 +1,27 @@ +import * as config from "./config"; + +export const getEmails = async (toAddr: string) => { + const url = new URL(config.MAILDEV_URL); + url.pathname = "/email"; + let res = await fetch(url); + let data = await res.json(); + let mails = []; + Array.from(data).forEach((mail: any) => { + if (mail.to[0].address == toAddr) { + mails.push(mail); + } + }); + return mails; +}; + +export const getActivationLink = async ( + toAddr: string +): Promise => { + let emails = await getEmails(toAddr); + for (let i = 0; i < emails.length; i++) { + let mail = emails[i]; + if (mail.subject.includes("Please activate your account")) { + return mail.text.split('\n')[4] + } + } +}; diff --git a/tests/user.ts b/tests/user.ts index 21bf2cf..09487d3 100644 --- a/tests/user.ts +++ b/tests/user.ts @@ -1,6 +1,7 @@ import { Page, expect } from "@playwright/test"; import { getRandomString } from "./utils"; import * as config from "./config"; +import { getActivationLink } from "./maildev"; export default class User { username: string; @@ -78,14 +79,37 @@ export default class User { await page.getByLabel("Password", { exact: true }).fill(this.password); await page.getByLabel("Confirm Password").fill(this.password); await page.getByRole("button", { name: "Register Account" }).click(); - await page.waitForURL(config.INSTANCE_URL.toString()); + await page.waitForURL("**/user/sign_up"); + + // account activation through email + await expect(page.locator("h2.ui")).toContainText("Activate Your Account"); + await expect( + page.locator("div.ui:nth-child(3) > p:nth-child(1)") + ).toContainText(this.email); + + let activationEmail: String | null = null; + while (true) { + let x = await getActivationLink(this.email); + if (typeof x === "string") { + activationEmail = x; + break; + } + } + await page.goto(activationEmail.toString()); + // go to forgejo + await page.waitForURL("**/user/activate**"); + await page.getByLabel("Password", { exact: true }).fill(this.password); + // click verify account btn + await page.getByRole("button", { name: "Confirm Password" }).click(); + + // registration successful + await page.waitForURL(config.INSTANCE_URL.toString()); await expect(page).toHaveTitle( `${this.username} - Dashboard - Forgejo: Beyond coding. We forge.` ); - expect(page.locator(".positive > p:nth-child(1)")).toContainText( - "Account was successfully created. Welcome!" + "Account has been activated" ); } }