|
| 1 | +import {expect, test, type Page} from "playwright/test"; |
| 2 | +import { |
| 3 | + DEFAULT_TEST_PASSWORD, |
| 4 | + createUserViaUserManagement, |
| 5 | + gotoSettings, |
| 6 | + gotoUserManagement, |
| 7 | + login, |
| 8 | + loginAsAdmin, |
| 9 | + logout, |
| 10 | + uniqueUsername, |
| 11 | +} from "./helpers"; |
| 12 | + |
| 13 | +async function createRegularUserAndLogin(page: Page): Promise<{ |
| 14 | + username: string; |
| 15 | + password: string; |
| 16 | +}> { |
| 17 | + const username = uniqueUsername("e2e_account"); |
| 18 | + const password = DEFAULT_TEST_PASSWORD; |
| 19 | + |
| 20 | + await loginAsAdmin(page); |
| 21 | + await gotoUserManagement(page); |
| 22 | + await createUserViaUserManagement(page, username, password); |
| 23 | + await logout(page); |
| 24 | + await login(page, username, password); |
| 25 | + |
| 26 | + return {username, password}; |
| 27 | +} |
| 28 | + |
| 29 | +async function submitLoginForm( |
| 30 | + page: Page, |
| 31 | + username: string, |
| 32 | + password: string, |
| 33 | +): Promise<void> { |
| 34 | + await page.goto("/login"); |
| 35 | + |
| 36 | + const loginForm = page.locator("form").first(); |
| 37 | + await loginForm.locator('input[autocomplete="username"]').fill(username); |
| 38 | + await loginForm |
| 39 | + .locator('input[autocomplete="current-password"]') |
| 40 | + .fill(password); |
| 41 | + await loginForm.getByRole("button", {name: "Login"}).click(); |
| 42 | +} |
| 43 | + |
| 44 | +async function expectLoginFailure( |
| 45 | + page: Page, |
| 46 | + username: string, |
| 47 | + password: string, |
| 48 | +): Promise<void> { |
| 49 | + await submitLoginForm(page, username, password); |
| 50 | + await expect(page.getByText("Invalid username or password")).toBeVisible(); |
| 51 | +} |
| 52 | + |
| 53 | +test.describe("account settings", () => { |
| 54 | + test("renames username and allows login with the new name", async ({page}) => { |
| 55 | + const {password} = await createRegularUserAndLogin(page); |
| 56 | + const renamedUsername = uniqueUsername("e2e_account_renamed"); |
| 57 | + |
| 58 | + await gotoSettings(page); |
| 59 | + |
| 60 | + const usernameInput = page.getByPlaceholder("Your username..."); |
| 61 | + await usernameInput.fill(renamedUsername); |
| 62 | + await page.getByRole("button", {name: "Save Changes"}).click(); |
| 63 | + await expect( |
| 64 | + page.getByText("Username successfully updated!"), |
| 65 | + ).toBeVisible({timeout: 15_000}); |
| 66 | + |
| 67 | + await expect(usernameInput).toHaveValue(renamedUsername); |
| 68 | + |
| 69 | + await logout(page); |
| 70 | + await login(page, renamedUsername, password); |
| 71 | + }); |
| 72 | + |
| 73 | + test("changes password end-to-end", async ({page}) => { |
| 74 | + const {username, password} = await createRegularUserAndLogin(page); |
| 75 | + const newPassword = "Password123!New"; |
| 76 | + |
| 77 | + await gotoSettings(page); |
| 78 | + |
| 79 | + await page.getByPlaceholder("Your current password...").fill(password); |
| 80 | + await page.getByPlaceholder("Your new password...").fill(newPassword); |
| 81 | + await page |
| 82 | + .getByPlaceholder("Your new password again...") |
| 83 | + .fill(newPassword); |
| 84 | + |
| 85 | + await page.getByRole("button", {name: "Update Password"}).click(); |
| 86 | + await expect( |
| 87 | + page.getByText("Password successfully updated!"), |
| 88 | + ).toBeVisible({timeout: 15_000}); |
| 89 | + |
| 90 | + await logout(page); |
| 91 | + |
| 92 | + await expectLoginFailure(page, username, password); |
| 93 | + await login(page, username, newPassword); |
| 94 | + }); |
| 95 | + |
| 96 | + test("deactivates account end-to-end", async ({page}) => { |
| 97 | + const {username, password} = await createRegularUserAndLogin(page); |
| 98 | + |
| 99 | + await gotoSettings(page); |
| 100 | + |
| 101 | + await page.getByRole("button", {name: "Deactivate this account"}).click(); |
| 102 | + |
| 103 | + const dialog = page.getByRole("alertdialog").first(); |
| 104 | + await dialog |
| 105 | + .getByRole("button", {name: "Yes, deactivate my account"}) |
| 106 | + .click(); |
| 107 | + |
| 108 | + await expect(page).toHaveURL(/\/login$/, {timeout: 15_000}); |
| 109 | + await expect(page.getByText("Welcome to Triage!")).toBeVisible({ |
| 110 | + timeout: 15_000, |
| 111 | + }); |
| 112 | + |
| 113 | + await expectLoginFailure(page, username, password); |
| 114 | + }); |
| 115 | + |
| 116 | + test("shows password mismatch validation", async ({page}) => { |
| 117 | + const {password} = await createRegularUserAndLogin(page); |
| 118 | + |
| 119 | + await gotoSettings(page); |
| 120 | + |
| 121 | + await page.getByPlaceholder("Your current password...").fill(password); |
| 122 | + await page.getByPlaceholder("Your new password...").fill("Password123!A"); |
| 123 | + await page |
| 124 | + .getByPlaceholder("Your new password again...") |
| 125 | + .fill("DifferentPassword123!"); |
| 126 | + |
| 127 | + await page.getByRole("button", {name: "Update Password"}).click(); |
| 128 | + |
| 129 | + await expect( |
| 130 | + page.getByText( |
| 131 | + "Passwords do not match. Please ensure both password fields are identical.", |
| 132 | + ), |
| 133 | + ).toBeVisible(); |
| 134 | + }); |
| 135 | + |
| 136 | + test("rejects password change when current password is wrong", async ({page}) => { |
| 137 | + const {username, password} = await createRegularUserAndLogin(page); |
| 138 | + const attemptedNewPassword = "Password123!Changed"; |
| 139 | + |
| 140 | + await gotoSettings(page); |
| 141 | + |
| 142 | + await page |
| 143 | + .getByPlaceholder("Your current password...") |
| 144 | + .fill("wrong-current-password"); |
| 145 | + await page |
| 146 | + .getByPlaceholder("Your new password...") |
| 147 | + .fill(attemptedNewPassword); |
| 148 | + await page |
| 149 | + .getByPlaceholder("Your new password again...") |
| 150 | + .fill(attemptedNewPassword); |
| 151 | + |
| 152 | + await page.getByRole("button", {name: "Update Password"}).click(); |
| 153 | + |
| 154 | + await logout(page); |
| 155 | + |
| 156 | + await expectLoginFailure(page, username, attemptedNewPassword); |
| 157 | + await login(page, username, password); |
| 158 | + }); |
| 159 | + |
| 160 | + test("prevents changing username to an existing one", async ({page}) => { |
| 161 | + await createRegularUserAndLogin(page); |
| 162 | + |
| 163 | + await gotoSettings(page); |
| 164 | + |
| 165 | + await page.getByPlaceholder("Your username...").fill("admin"); |
| 166 | + await page.getByRole("button", {name: "Save Changes"}).click(); |
| 167 | + |
| 168 | + await expect( |
| 169 | + page.getByText( |
| 170 | + "A user with this name already exists. Please choose a different username.", |
| 171 | + ), |
| 172 | + ).toBeVisible(); |
| 173 | + }); |
| 174 | +}); |
0 commit comments