|
1 | | -import { expect, test } from "@playwright/test" |
| 1 | +import { expect, test } from "@playwright/test"; |
2 | 2 |
|
3 | 3 | test("getSession()", async ({ page }) => { |
4 | | - await page.goto("/auth/login?returnTo=/app-router/server") |
| 4 | + await page.goto("/auth/login?returnTo=/app-router/server"); |
5 | 5 |
|
6 | 6 | // fill out Auth0 form |
7 | | - await page.fill('input[id="username"]', "test@example.com") |
8 | | - await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!) |
9 | | - await page.getByText("Continue", { exact: true }).click() |
| 7 | + await page.fill('input[id="username"]', "test@example.com"); |
| 8 | + await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); |
| 9 | + await page.getByText("Continue", { exact: true }).click(); |
10 | 10 |
|
11 | 11 | // check that the page says "Welcome, test@example.com!" |
12 | 12 | expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe( |
13 | 13 | "Welcome, test@example.com!" |
14 | | - ) |
| 14 | + ); |
15 | 15 |
|
16 | 16 | // ensure we're redirected back to the home page on logout |
17 | | - await page.goto("/auth/logout") |
| 17 | + await page.goto("/auth/logout"); |
18 | 18 | expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe( |
19 | 19 | "Home" |
20 | | - ) |
| 20 | + ); |
21 | 21 |
|
22 | 22 | // check that `getSession()` returns null after logging out |
23 | | - await page.goto("/app-router/server") |
24 | | - expect(page.getByRole("link", { name: "Log in" })).toBeVisible() |
25 | | -}) |
| 23 | + await page.goto("/app-router/server"); |
| 24 | + expect(page.getByRole("link", { name: "Log in" })).toBeVisible(); |
| 25 | +}); |
26 | 26 |
|
27 | 27 | test("useUser()", async ({ page }) => { |
28 | | - await page.goto("/auth/login?returnTo=/app-router/client") |
| 28 | + await page.goto("/auth/login?returnTo=/app-router/client"); |
29 | 29 |
|
30 | 30 | // fill out Auth0 form |
31 | | - await page.fill('input[id="username"]', "test@example.com") |
32 | | - await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!) |
33 | | - await page.getByText("Continue", { exact: true }).click() |
| 31 | + await page.fill('input[id="username"]', "test@example.com"); |
| 32 | + await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); |
| 33 | + await page.getByText("Continue", { exact: true }).click(); |
34 | 34 |
|
35 | 35 | // check that the page says "Welcome, test@example.com!" |
36 | 36 | expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe( |
37 | 37 | "Welcome, test@example.com!" |
38 | | - ) |
| 38 | + ); |
39 | 39 |
|
40 | 40 | // ensure we're redirected back to the home page on logout |
41 | | - await page.goto("/auth/logout") |
| 41 | + await page.goto("/auth/logout"); |
42 | 42 | expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe( |
43 | 43 | "Home" |
44 | | - ) |
| 44 | + ); |
45 | 45 |
|
46 | 46 | // check that `getSession()` returns null after logging out |
47 | | - await page.goto("/app-router/client") |
48 | | - expect(page.getByRole("link", { name: "Log in" })).toBeVisible() |
49 | | -}) |
| 47 | + await page.goto("/app-router/client"); |
| 48 | + expect(page.getByRole("link", { name: "Log in" })).toBeVisible(); |
| 49 | +}); |
50 | 50 |
|
51 | 51 | test("getAccessToken()", async ({ page }) => { |
52 | | - await page.goto("/auth/login?returnTo=/app-router/client") |
| 52 | + await page.goto("/auth/login?returnTo=/app-router/client"); |
53 | 53 |
|
54 | 54 | // fill out Auth0 form |
55 | | - await page.fill('input[id="username"]', "test@example.com") |
56 | | - await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!) |
57 | | - await page.getByText("Continue", { exact: true }).click() |
| 55 | + await page.fill('input[id="username"]', "test@example.com"); |
| 56 | + await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); |
| 57 | + await page.getByText("Continue", { exact: true }).click(); |
58 | 58 |
|
59 | 59 | // fetch a token |
60 | | - const requestPromise = page.waitForRequest("/auth/access-token") |
61 | | - await page.getByText("Get token").click() |
62 | | - const request = await requestPromise |
63 | | - const tokenRequest = await (await request.response())?.json() |
64 | | - expect(tokenRequest).toHaveProperty("token") |
65 | | - expect(tokenRequest).toHaveProperty("expires_at") |
66 | | -}) |
| 60 | + const requestPromise = page.waitForRequest("/auth/access-token"); |
| 61 | + await page.getByText("Get token").click(); |
| 62 | + const request = await requestPromise; |
| 63 | + const tokenRequest = await (await request.response())?.json(); |
| 64 | + expect(tokenRequest).toHaveProperty("token"); |
| 65 | + expect(tokenRequest).toHaveProperty("expires_at"); |
| 66 | +}); |
67 | 67 |
|
68 | 68 | test("protected server route", async ({ page, context }) => { |
69 | 69 | // before establishing a session, we should receive a 401 |
70 | | - const unauthedRes = await context.request.fetch("/app-router/api/test") |
71 | | - expect(unauthedRes.status()).toBe(401) |
| 70 | + const unauthedRes = await context.request.fetch("/app-router/api/test"); |
| 71 | + expect(unauthedRes.status()).toBe(401); |
72 | 72 |
|
73 | | - await page.goto("/auth/login?returnTo=/app-router/server") |
| 73 | + await page.goto("/auth/login?returnTo=/app-router/server"); |
74 | 74 |
|
75 | 75 | // fill out Auth0 form |
76 | | - await page.fill('input[id="username"]', "test@example.com") |
77 | | - await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!) |
78 | | - await page.getByText("Continue", { exact: true }).click() |
| 76 | + await page.fill('input[id="username"]', "test@example.com"); |
| 77 | + await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); |
| 78 | + await page.getByText("Continue", { exact: true }).click(); |
79 | 79 |
|
80 | 80 | // after establishing a session, we should receive a 200 |
81 | | - const authedRes = await context.request.fetch("/app-router/api/test") |
82 | | - expect(authedRes.status()).toBe(200) |
83 | | - expect(await authedRes.json()).toEqual({ email: "test@example.com" }) |
84 | | -}) |
| 81 | + const authedRes = await context.request.fetch("/app-router/api/test"); |
| 82 | + expect(authedRes.status()).toBe(200); |
| 83 | + expect(await authedRes.json()).toEqual({ email: "test@example.com" }); |
| 84 | +}); |
85 | 85 |
|
86 | 86 | test("protected server action", async ({ page }) => { |
87 | | - await page.goto("/app-router/action") |
| 87 | + await page.goto("/app-router/action"); |
88 | 88 |
|
89 | 89 | // call protected server action |
90 | | - await page.getByText("Call server action").click() |
91 | | - await expect(page.locator("#status")).toHaveValue("unauthenticated") |
| 90 | + await page.getByText("Call server action").click(); |
| 91 | + await expect(page.locator("#status")).toHaveValue("unauthenticated"); |
92 | 92 |
|
93 | | - await page.goto("/auth/login?returnTo=/app-router/action") |
| 93 | + await page.goto("/auth/login?returnTo=/app-router/action"); |
94 | 94 |
|
95 | 95 | // fill out Auth0 form |
96 | | - await page.fill('input[id="username"]', "test@example.com") |
97 | | - await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!) |
98 | | - await page.getByText("Continue", { exact: true }).click() |
| 96 | + await page.fill('input[id="username"]', "test@example.com"); |
| 97 | + await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); |
| 98 | + await page.getByText("Continue", { exact: true }).click(); |
99 | 99 |
|
100 | 100 | // call protected server action, now authenticated |
101 | | - await page.getByText("Call server action").click() |
102 | | - await expect(page.locator("#status")).toHaveValue("authenticated") |
103 | | -}) |
| 101 | + await page.getByText("Call server action").click(); |
| 102 | + await expect(page.locator("#status")).toHaveValue("authenticated"); |
| 103 | +}); |
104 | 104 |
|
105 | 105 | test("updateSession()", async ({ page, context }) => { |
106 | | - const now = Date.now() |
| 106 | + const now = Date.now(); |
107 | 107 |
|
108 | | - await page.goto("/auth/login?returnTo=/app-router/server") |
| 108 | + await page.goto("/auth/login?returnTo=/app-router/server"); |
109 | 109 |
|
110 | 110 | // fill out Auth0 form |
111 | | - await page.fill('input[id="username"]', "test@example.com") |
112 | | - await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!) |
113 | | - await page.getByText("Continue", { exact: true }).click() |
| 111 | + await page.fill('input[id="username"]', "test@example.com"); |
| 112 | + await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); |
| 113 | + await page.getByText("Continue", { exact: true }).click(); |
114 | 114 |
|
115 | 115 | // check that the page says "Welcome, test@example.com!" |
116 | 116 | expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe( |
117 | 117 | "Welcome, test@example.com!" |
118 | | - ) |
| 118 | + ); |
119 | 119 |
|
120 | 120 | // the session should not have an `updatedAt` field initially |
121 | | - let getSessionRes = await context.request.fetch("/app-router/api/get-session") |
122 | | - let getSessionJson = await getSessionRes.json() |
123 | | - expect(getSessionJson.updatedAt).toBeUndefined() |
| 121 | + let getSessionRes = await context.request.fetch( |
| 122 | + "/app-router/api/get-session" |
| 123 | + ); |
| 124 | + let getSessionJson = await getSessionRes.json(); |
| 125 | + expect(getSessionJson.updatedAt).toBeUndefined(); |
124 | 126 |
|
125 | 127 | // now update the session and check that the `updatedAt` field is updated |
126 | 128 | const updateSessionRes = await context.request.fetch( |
127 | 129 | "/app-router/api/update-session" |
128 | | - ) |
129 | | - expect(updateSessionRes.status()).toBe(200) |
130 | | - getSessionRes = await context.request.fetch("/app-router/api/get-session") |
131 | | - getSessionJson = await getSessionRes.json() |
132 | | - expect(getSessionJson.updatedAt).toBeGreaterThan(now) |
133 | | -}) |
| 130 | + ); |
| 131 | + expect(updateSessionRes.status()).toBe(200); |
| 132 | + getSessionRes = await context.request.fetch("/app-router/api/get-session"); |
| 133 | + getSessionJson = await getSessionRes.json(); |
| 134 | + expect(getSessionJson.updatedAt).toBeGreaterThan(now); |
| 135 | +}); |
0 commit comments