|
| 1 | +import { expect, test } from "@playwright/test" |
| 2 | +import { findLastEmail } from "./utils/mailcatcher" |
| 3 | +import { randomEmail } from "./utils/random" |
| 4 | +import { logInUser, signUpNewUser } from "./utils/user" |
| 5 | + |
| 6 | +test.use({ storageState: { cookies: [], origins: [] } }) |
| 7 | + |
| 8 | +test("Password Recovery title is visible", async ({ page }) => { |
| 9 | + await page.goto("/recover-password") |
| 10 | + |
| 11 | + await expect( |
| 12 | + page.getByRole("heading", { name: "Password Recovery" }), |
| 13 | + ).toBeVisible() |
| 14 | +}) |
| 15 | + |
| 16 | +test("Input is visible, empty and editable", async ({ page }) => { |
| 17 | + await page.goto("/recover-password") |
| 18 | + |
| 19 | + await expect(page.getByPlaceholder("Email")).toBeVisible() |
| 20 | + await expect(page.getByPlaceholder("Email")).toHaveText("") |
| 21 | + await expect(page.getByPlaceholder("Email")).toBeEditable() |
| 22 | +}) |
| 23 | + |
| 24 | +test("Continue button is visible", async ({ page }) => { |
| 25 | + await page.goto("/recover-password") |
| 26 | + |
| 27 | + await expect(page.getByRole("button", { name: "Continue" })).toBeVisible() |
| 28 | +}) |
| 29 | + |
| 30 | +test("User can reset password successfully using the link", async ({ |
| 31 | + page, |
| 32 | + request, |
| 33 | +}) => { |
| 34 | + const full_name = "Test User" |
| 35 | + const email = randomEmail() |
| 36 | + const password = "changethis" |
| 37 | + const new_password = "changethat" |
| 38 | + |
| 39 | + // Sign up a new user |
| 40 | + await signUpNewUser(page, full_name, email, password) |
| 41 | + |
| 42 | + await page.goto("/recover-password") |
| 43 | + await page.getByPlaceholder("Email").fill(email) |
| 44 | + |
| 45 | + await page.getByRole("button", { name: "Continue" }).click() |
| 46 | + |
| 47 | + const emailData = await findLastEmail({ |
| 48 | + request, |
| 49 | + filter: (e) => e.recipients.includes(`<${email}>`), |
| 50 | + timeout: 5000, |
| 51 | + }) |
| 52 | + |
| 53 | + await page.goto(`http://localhost:1080/messages/${emailData.id}.html`) |
| 54 | + |
| 55 | + const selector = 'a[href*="/reset-password?token="]' |
| 56 | + |
| 57 | + let url = await page.getAttribute(selector, "href") |
| 58 | + |
| 59 | + // TODO: update var instead of doing a replace |
| 60 | + url = url!.replace("http://localhost/", "http://localhost:5173/") |
| 61 | + |
| 62 | + // Set the new password and confirm it |
| 63 | + await page.goto(url) |
| 64 | + |
| 65 | + await page.getByLabel("Set Password").fill(new_password) |
| 66 | + await page.getByLabel("Confirm Password").fill(new_password) |
| 67 | + await page.getByRole("button", { name: "Reset Password" }).click() |
| 68 | + await expect(page.getByText("Password updated successfully")).toBeVisible() |
| 69 | + |
| 70 | + // Check if the user is able to login with the new password |
| 71 | + await logInUser(page, email, new_password) |
| 72 | +}) |
| 73 | + |
| 74 | +test("Expired or invalid reset link", async ({ page }) => { |
| 75 | + const invalidUrl = "/reset-password?token=invalidtoken" |
| 76 | + |
| 77 | + await page.goto(invalidUrl) |
| 78 | + |
| 79 | + await page.getByLabel("Set Password").fill("newpassword") |
| 80 | + await page.getByLabel("Confirm Password").fill("newpassword") |
| 81 | + await page.getByRole("button", { name: "Reset Password" }).click() |
| 82 | + |
| 83 | + await expect(page.getByText("Invalid token")).toBeVisible() |
| 84 | +}) |
| 85 | + |
| 86 | +test("Weak new password validation", async ({ page, request }) => { |
| 87 | + const full_name = "Test User" |
| 88 | + const email = randomEmail() |
| 89 | + const password = "password" |
| 90 | + |
| 91 | + // Sign up a new user |
| 92 | + await signUpNewUser(page, full_name, email, password) |
| 93 | + |
| 94 | + await page.goto("/recover-password") |
| 95 | + await page.getByPlaceholder("Email").fill(email) |
| 96 | + await page.getByRole("button", { name: "Continue" }).click() |
| 97 | + |
| 98 | + const emailData = await findLastEmail({ |
| 99 | + request, |
| 100 | + filter: (e) => e.recipients.includes(`<${email}>`), |
| 101 | + timeout: 5000, |
| 102 | + }) |
| 103 | + |
| 104 | + await page.goto(`http://localhost:1080/messages/${emailData.id}.html`) |
| 105 | + |
| 106 | + const selector = 'a[href*="/reset-password?token="]' |
| 107 | + let url = await page.getAttribute(selector, "href") |
| 108 | + url = url!.replace("http://localhost/", "http://localhost:5173/") |
| 109 | + |
| 110 | + // Set a weak new password |
| 111 | + await page.goto(url) |
| 112 | + await page.getByLabel("Set Password").fill("123") |
| 113 | + await page.getByLabel("Confirm Password").fill("123") |
| 114 | + await page.getByRole("button", { name: "Reset Password" }).click() |
| 115 | + |
| 116 | + await expect( |
| 117 | + page.getByText("Password must be at least 8 characters"), |
| 118 | + ).toBeVisible() |
| 119 | +}) |
0 commit comments