Repository navigation
Expand file tree
/
Copy pathauth.spec.ts
More file actions
290 lines (237 loc) · 11.8 KB
/
Copy pathauth.spec.ts
File metadata and controls
290 lines (237 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { totp } from "./totp";
import { expect, test } from "@playwright/test";
import {
clearMailbox,
linkFrom,
settled,
signIn,
signUpAndVerify,
STRONG_PASSWORD,
uniqueEmail,
waitForEmail,
} from "./support";
test.describe("signup and verification", () => {
test("a visitor can sign up, verify by email, and reach the dashboard", async ({ page }) => {
await clearMailbox();
const email = uniqueEmail("signup");
await page.goto("/auth/signup");
await settled(page);
await page.getByLabel("Your name").fill("Ada Lovelace");
await page.getByLabel("Email address").fill(email);
await page.getByLabel("Password").fill(STRONG_PASSWORD);
// The live checklist confirms every rule is met, so the form is not about
// to promise something the API will refuse.
// Scoped to the checklist: the field hint uses the same words.
const checklist = page.getByRole("listitem");
await expect(checklist.filter({ hasText: "6 characters or more" })).toBeVisible();
await expect(checklist.filter({ hasText: "a special character" })).toBeVisible();
await page.getByRole("checkbox", { name: /I agree to the/ }).check();
const submit = page.getByRole("button", { name: /Create account|Checking/ });
await expect(submit).toBeEnabled({ timeout: 20_000 });
await submit.click();
await expect(page).toHaveURL(/\/auth\/check-email/);
await expect(page.getByRole("heading", { name: /Confirm your email/i })).toBeVisible();
// The real message, read out of the real mail server.
const mail = await waitForEmail(email, { subjectMatch: /confirm/i });
expect(mail.text).toMatch(/expires in/i);
await page.goto(linkFrom(mail.html));
await settled(page);
// Verified users land signed in.
await expect(page).toHaveURL(/\/app/);
await expect(page.getByRole("heading", { name: /Welcome back/i })).toBeVisible();
});
test("signup will not proceed without accepting the terms", async ({ page }) => {
await page.goto("/auth/signup");
await settled(page);
await page.getByLabel("Your name").fill("No Consent");
await page.getByLabel("Email address").fill(uniqueEmail("noconsent"));
await page.getByLabel("Password").fill(STRONG_PASSWORD);
const submit = page.getByRole("button", { name: /Create account|Checking/ });
await expect(submit).toBeEnabled({ timeout: 20_000 });
await submit.click();
// The browser's own required-field validation stops it; we never navigate.
await expect(page).toHaveURL(/\/auth\/signup/);
});
/*
* This asserted the opposite until a real person hit it and said, fairly,
* that being told "check your email" for an account that already exists —
* with no email ever arriving — is indistinguishable from the product being
* broken. Signup now says the address is taken, as GitHub, Stripe and Slack
* do. Enumeration protection stays on login and password reset, where the
* legitimate user loses nothing.
*/
test("an already-registered address is told so, on the form, with no navigation", async ({
page,
}) => {
const email = await signUpAndVerify(page, uniqueEmail("dup"));
await page.goto("/auth/signup");
await settled(page);
await page.getByLabel("Your name").fill("Impostor");
await page.getByLabel("Email address").fill(email);
await page.getByLabel("Password").fill("a-completely-different-phrase-1");
await page.getByRole("checkbox", { name: /I agree to the/ }).check();
const submit = page.getByRole("button", { name: /Create account|Checking/ });
await expect(submit).toBeEnabled({ timeout: 20_000 });
await submit.click();
// It stays on the form and says why, rather than promising an email.
await expect(page.locator("body")).toContainText(/already exists/i);
await expect(page).toHaveURL(/\/auth\/signup/);
});
});
test.describe("sign in and out", () => {
test("a verified user can sign in and out", async ({ page }) => {
const email = await signUpAndVerify(page, uniqueEmail("login"));
// Sign out first so we are testing a real sign-in.
await page.goto("/app");
await settled(page);
await page.getByRole("button", { name: "Sign out" }).click();
await page.waitForTimeout(1000);
await signIn(page, email);
await expect(page).toHaveURL(/\/app/, { timeout: 20_000 });
await expect(page.getByRole("heading", { name: /Welcome back/i })).toBeVisible();
});
test("signing out lands on a page, not on the API's JSON", async ({ page }) => {
/*
* The header used to post straight at /api/v1/auth/logout. The session
* really did end, so every test that signed out passed — none of them
* looked at where the browser ended up, which was a blank page showing
* {"data":{"success":true},...}. This is that missing assertion.
*/
await signUpAndVerify(page, uniqueEmail("signout"));
await page.goto("/app");
await settled(page);
await page.getByRole("button", { name: "Sign out" }).click();
// A real page on the site, not an API path.
await expect(page).not.toHaveURL(/\/api\//, { timeout: 20_000 });
await expect(page.locator("body")).not.toContainText('{"data"');
await expect(page.getByRole("link", { name: /Sign in/i }).first()).toBeVisible({
timeout: 20_000,
});
// And the session is genuinely gone: /app bounces to sign-in.
await page.goto("/app");
await expect(page).toHaveURL(/\/auth\/login/, { timeout: 20_000 });
});
test("a wrong password and an unknown address give the same message", async ({ page }) => {
const email = await signUpAndVerify(page, uniqueEmail("enum"));
await page.goto("/app");
await settled(page);
await page.getByRole("button", { name: "Sign out" }).click();
await page.waitForTimeout(800);
await signIn(page, email, "definitely-not-the-password");
await settled(page);
const wrongPassword = await page.getByRole("alert").first().textContent();
await signIn(page, uniqueEmail("ghost"), STRONG_PASSWORD);
await settled(page);
const unknownEmail = await page.getByRole("alert").first().textContent();
// Byte-identical: the response cannot be used to test whether an account exists.
expect(wrongPassword?.trim()).toBe(unknownEmail?.trim());
});
test("signing in cannot be redirected off-site by the next parameter", async ({ page }) => {
const email = await signUpAndVerify(page, uniqueEmail("redirect"));
await page.goto("/app");
await settled(page);
await page.getByRole("button", { name: "Sign out" }).click();
await page.waitForTimeout(800);
await page.goto("/auth/login?next=https://evil.example/steal");
await settled(page);
await page.getByLabel("Email address").fill(email);
await page.getByLabel("Password").fill(STRONG_PASSWORD);
await page.getByRole("button", { name: "Sign in" }).click();
await page.waitForTimeout(2500);
// Stayed on our origin, and landed on the safe default.
expect(new URL(page.url()).origin).toBe(new URL(page.url()).origin);
expect(page.url()).not.toContain("evil.example");
});
});
test.describe("password reset", () => {
test("a user can reset their password with a single-use link", async ({ page }) => {
await clearMailbox();
const email = await signUpAndVerify(page, uniqueEmail("reset"));
await page.goto("/app");
await settled(page);
await page.getByRole("button", { name: "Sign out" }).click();
await page.waitForTimeout(800);
await page.goto("/auth/forgot-password");
await settled(page);
await page.getByLabel("Email address").fill(email);
const send = page.getByRole("button", { name: /Send reset link|Checking/ });
await expect(send).toBeEnabled({ timeout: 20_000 });
await send.click();
await expect(page.getByRole("heading", { name: /Check your email/i })).toBeVisible();
const mail = await waitForEmail(email, { subjectMatch: /reset/i });
expect(mail.text).toMatch(/expires in/i);
const resetLink = linkFrom(mail.html, "token=");
await page.goto(resetLink);
await settled(page);
const newPassword = "an-entirely-new-passphrase-1";
// By ROLE and accessible name: `getByLabel` sees the visible label text,
// which includes the aria-hidden required marker, while the accessible name
// is clean. `exact` then disambiguates it from "Confirm new password".
await page.getByRole("textbox", { name: "New password", exact: true }).fill(newPassword);
await page.getByRole("textbox", { name: "Confirm new password" }).fill(newPassword);
await page.getByRole("button", { name: /Save new password/ }).click();
await expect(page).toHaveURL(/\/auth\/login/, { timeout: 20_000 });
// The new password works.
await signIn(page, email, newPassword);
await expect(page).toHaveURL(/\/app/, { timeout: 20_000 });
});
test("the reset form is unusable without a token", async ({ page }) => {
await page.goto("/auth/reset-password");
await settled(page);
await expect(page.getByRole("heading", { name: /needs a reset link/i })).toBeVisible();
await expect(page.getByLabel("New password")).toHaveCount(0);
});
});
test.describe("two-factor", () => {
test("a user can turn it on from the dashboard", async ({ page }) => {
/*
* Through the UI, deliberately.
*
* The admin suite enrols by calling the API directly, which is why nobody
* noticed that the dashboard's own buttons were broken: the forms posted at
* /api/auth/two-factor/*, React Router tried to match that as a route, and
* "Set up two-factor" landed on a 404. Two-factor could not be switched on
* at all. This test clicks the buttons.
*/
await signUpAndVerify(page, uniqueEmail("twofa"));
await page.goto("/app/security");
await settled(page);
await expect(page.getByRole("heading", { name: /Two-factor/i })).toBeVisible();
// Step 1: prove the password, get a secret and backup codes.
await page.getByLabel("Confirm your password").first().fill(STRONG_PASSWORD);
await page.getByRole("button", { name: /Set up two-factor/i }).click();
await expect(page.getByText(/Save your backup codes now/i)).toBeVisible({ timeout: 20_000 });
// A scannable QR, not a collapsed one — it rendered at 26px the first time,
// because `scalable: true` omits width/height.
const qrBox = await page.locator("svg").last().boundingBox();
expect(qrBox?.width ?? 0, "the QR must be big enough to scan").toBeGreaterThan(120);
// Ten backup codes, shown once.
await expect(page.locator("ul.numeric li")).toHaveCount(10);
// Step 2: a real code from the real secret, exactly as an app would.
const secret = (await page.locator("code").first().textContent())?.trim() ?? "";
expect(secret.length, "manual-entry key should be present").toBeGreaterThan(10);
await page.getByLabel(/6-digit code/i).fill(totp(secret));
await page.getByRole("button", { name: /Turn on two-factor/i }).click();
// It is actually on, and the page offers to turn it off again.
await expect(page.getByRole("button", { name: /Turn off two-factor/i })).toBeVisible({
timeout: 20_000,
});
const enabled = await page.evaluate(async () => {
const r = await fetch("/api/v1/me", { credentials: "same-origin" });
const j = (await r.json()) as { data: { twoFactorEnabled: boolean } };
return j.data.twoFactorEnabled;
});
expect(enabled).toBe(true);
});
});
test.describe("sessions", () => {
test("a user can see their session and sign out everywhere", async ({ page }) => {
await signUpAndVerify(page, uniqueEmail("sessions"));
await page.goto("/app/sessions");
await settled(page);
await expect(page.getByText("This device")).toBeVisible();
// Never an IP address, never a raw user-agent string.
await expect(page.locator("body")).not.toContainText(/\d+\.\d+\.\d+\.\d+/);
await expect(page.locator("body")).not.toContainText(/Mozilla\/5\.0/);
});
});