refactor: consolidate auth logic into helper, add letter E2E tests,

This commit is contained in:
ramvignesh-b
2026-04-15 01:43:03 +05:30
parent 9c6400b18a
commit 5594ac50d0
5 changed files with 156 additions and 57 deletions
+50
View File
@@ -0,0 +1,50 @@
import { expect, type Page } from "@playwright/test";
import { MailpitHelper } from "./mailpit";
/**
* Completes the full registration -> activation -> login cycle.
*/
export async function registerAndLogin(
page: Page,
email: string,
fullName: string,
password: string,
) {
// 1. Registration
console.log(`[Auth] Registering user: ${email}`);
await page.goto("/onboard");
await page.getByLabel(/full name/i).fill(fullName);
await page.getByLabel("Email", { exact: true }).fill(email);
await page.getByLabel("Password", { exact: true }).fill(password);
await page.getByLabel(/confirm password/i).fill(password);
await page.getByRole("button", { name: /^register$/i }).click();
await expect(page).toHaveURL(/\/verify-email/);
// 2. Activation via Mailpit
console.log(`[Auth] Polling Mailpit for activation email...`);
const activationLink = await MailpitHelper.getActivationLink(email);
await page.goto(activationLink);
await expect(page.getByText(/account activated/i)).toBeVisible();
await page.getByRole("button", { name: /start writing/i }).click();
// 3. Login
console.log(`[Auth] Logging in...`);
await expect(page).toHaveURL(/\/login/);
const welcomeButton = page.getByRole("button", { name: /i understand/i });
await welcomeButton.waitFor({ state: "visible", timeout: 10000 });
await welcomeButton.click();
await expect(welcomeButton).toBeHidden();
await page.getByLabel("Email", { exact: true }).fill(email);
await page.getByLabel("Password", { exact: true }).fill(password);
await page.getByRole("button", { name: /sign in/i }).click();
await expect(page).toHaveURL(/\/drawer/);
console.log(`[Auth] Successfully authenticated ${email}`);
}
// Maintain backward compatibility if needed, or update callers
export const AuthHelper = { registerAndLogin };