|
4 | 4 |
|
5 | 5 | import { describe, expect, it } from "vitest"; |
6 | 6 | import { getTestServer } from "./helpers/sharedTestServer"; |
| 7 | +import { seedTestSession, seedTestUser } from "./helpers/seedTestSession"; |
7 | 8 |
|
8 | 9 | describe("Dashboard", () => { |
9 | | - // Placeholder until TRI-8742+ adds the actual matrix. |
10 | 10 | it("shared webapp container redirects /admin/concurrency to /login when unauthenticated", async () => { |
11 | 11 | const server = getTestServer(); |
12 | 12 | const res = await server.webapp.fetch("/admin/concurrency", { redirect: "manual" }); |
13 | 13 | expect(res.status).toBe(302); |
14 | 14 | }); |
| 15 | + |
| 16 | + // Admin pages migrated to dashboardLoader({ authorization: { requireSuper: true } }) |
| 17 | + // in TRI-8717. The dashboardLoader resolves auth in three stages: |
| 18 | + // 1. No session → redirect to /login?redirectTo=<path>. |
| 19 | + // 2. Session, user.admin === false → redirect to / (no path leakage). |
| 20 | + // 3. Session, user.admin === true → run the loader handler. |
| 21 | + // |
| 22 | + // Coverage strategy: pick three representative routes (the index, a |
| 23 | + // tabbed sub-page, and the back-office tree) rather than all 14 — |
| 24 | + // they all share the same dashboardLoader config so testing every |
| 25 | + // file would just confirm the wrapper works, which the harness |
| 26 | + // already proves. If the wrapper config drifts per-route in the |
| 27 | + // future, add targeted tests for the divergent ones. |
| 28 | + describe("Admin pages — requireSuper gate", () => { |
| 29 | + const adminRoutes = [ |
| 30 | + "/admin", |
| 31 | + "/admin/concurrency", |
| 32 | + "/admin/back-office", |
| 33 | + ]; |
| 34 | + |
| 35 | + for (const path of adminRoutes) { |
| 36 | + describe(`GET ${path}`, () => { |
| 37 | + it("no session: redirects to /login?redirectTo=<path>", async () => { |
| 38 | + const server = getTestServer(); |
| 39 | + const res = await server.webapp.fetch(path, { redirect: "manual" }); |
| 40 | + expect(res.status).toBe(302); |
| 41 | + const location = res.headers.get("location") ?? ""; |
| 42 | + expect(location).toContain("/login"); |
| 43 | + // Path leaks deliberately so a successful login bounces the |
| 44 | + // user back to where they were headed. |
| 45 | + expect(location).toContain(`redirectTo=${encodeURIComponent(path)}`); |
| 46 | + }); |
| 47 | + |
| 48 | + it("session for non-admin user: redirects to / (no path leakage)", async () => { |
| 49 | + const server = getTestServer(); |
| 50 | + const user = await seedTestUser(server.prisma, { admin: false }); |
| 51 | + const cookie = await seedTestSession({ userId: user.id }); |
| 52 | + const res = await server.webapp.fetch(path, { |
| 53 | + redirect: "manual", |
| 54 | + headers: { Cookie: cookie }, |
| 55 | + }); |
| 56 | + expect(res.status).toBe(302); |
| 57 | + const location = res.headers.get("location") ?? ""; |
| 58 | + // unauthorizedRedirect default in dashboardBuilder is "/". |
| 59 | + // A non-admin landing on /admin shouldn't get redirectTo |
| 60 | + // back to /admin once they upgrade — they're not getting in |
| 61 | + // by re-auth. |
| 62 | + expect(new URL(location, "http://localhost").pathname).toBe("/"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("session for admin user: 2xx", async () => { |
| 66 | + const server = getTestServer(); |
| 67 | + const user = await seedTestUser(server.prisma, { admin: true }); |
| 68 | + const cookie = await seedTestSession({ userId: user.id }); |
| 69 | + const res = await server.webapp.fetch(path, { |
| 70 | + redirect: "manual", |
| 71 | + headers: { Cookie: cookie }, |
| 72 | + }); |
| 73 | + // Loader handler ran — could be 200 (HTML) or 204 (Remix |
| 74 | + // _data fetch). Either way, NOT a redirect. |
| 75 | + expect(res.status).toBeLessThan(300); |
| 76 | + }); |
| 77 | + }); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + // Action handlers behind requireSuper used to return 403 Unauthorized |
| 82 | + // pre-RBAC — now they redirect to / via dashboardAction's |
| 83 | + // unauthorizedRedirect. The ticket flagged this as a behaviour |
| 84 | + // change worth locking in (any XHR fetcher that branched on 403 |
| 85 | + // would have regressed silently). Use admin.feature-flags POST as |
| 86 | + // the canary — it's the simplest action of the bunch. |
| 87 | + describe("Admin action — requireSuper gate (admin.feature-flags POST)", () => { |
| 88 | + const path = "/admin/feature-flags"; |
| 89 | + |
| 90 | + it("no session: redirects to /login (POST)", async () => { |
| 91 | + const server = getTestServer(); |
| 92 | + const res = await server.webapp.fetch(path, { |
| 93 | + method: "POST", |
| 94 | + body: JSON.stringify({}), |
| 95 | + headers: { "Content-Type": "application/json" }, |
| 96 | + redirect: "manual", |
| 97 | + }); |
| 98 | + expect(res.status).toBe(302); |
| 99 | + const location = res.headers.get("location") ?? ""; |
| 100 | + expect(location).toContain("/login"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("session for non-admin user: redirects to / (was 403 pre-RBAC)", async () => { |
| 104 | + const server = getTestServer(); |
| 105 | + const user = await seedTestUser(server.prisma, { admin: false }); |
| 106 | + const cookie = await seedTestSession({ userId: user.id }); |
| 107 | + const res = await server.webapp.fetch(path, { |
| 108 | + method: "POST", |
| 109 | + body: JSON.stringify({}), |
| 110 | + headers: { "Content-Type": "application/json", Cookie: cookie }, |
| 111 | + redirect: "manual", |
| 112 | + }); |
| 113 | + // Behaviour change from the TRI-8717 migration: the legacy |
| 114 | + // path returned 403 Unauthorized; dashboardAction returns a |
| 115 | + // 302 to "/" instead. Any client code branching on 403 needs |
| 116 | + // updating — locking this in so a silent regression is loud. |
| 117 | + expect(res.status).toBe(302); |
| 118 | + const location = res.headers.get("location") ?? ""; |
| 119 | + expect(new URL(location, "http://localhost").pathname).toBe("/"); |
| 120 | + }); |
| 121 | + }); |
15 | 122 | }); |
0 commit comments