|
| 1 | +// A flashed toast survives exactly one hop: the root loader reads it with `session.get` |
| 2 | +// (which deletes the flash) and commits the emptied session, so any hop that runs the root |
| 3 | +// loader spends the message — including a hop whose leaf loader only redirects again and |
| 4 | +// never renders the toast. The general settings action must therefore redirect to a page |
| 5 | +// that renders. |
| 6 | + |
| 7 | +import { okAsync } from "neverthrow"; |
| 8 | +import { describe, expect, it, vi } from "vitest"; |
| 9 | +import { commitSession, getSession, redirectWithErrorMessage } from "~/models/message.server"; |
| 10 | + |
| 11 | +vi.mock("~/services/routeBuilders/dashboardBuilder", () => ({ |
| 12 | + dashboardAction: (_options: unknown, handler: unknown) => handler, |
| 13 | + dashboardLoader: (_options: unknown, handler: unknown) => handler, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("~/models/organization.server", () => ({ |
| 17 | + resolveOrgIdFromSlug: vi.fn().mockResolvedValue("org_1"), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("~/services/projectSettings.server", () => ({ |
| 21 | + ProjectSettingsService: class { |
| 22 | + verifyProjectMembership() { |
| 23 | + return okAsync({ projectId: "proj_1" }); |
| 24 | + } |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +const SETTINGS_PATH = "/orgs/o/projects/p/env/prod/settings/general"; |
| 29 | + |
| 30 | +// Mirrors the read in app/root.tsx's loader. |
| 31 | +async function rootLoaderHop(cookie: string | null) { |
| 32 | + const session = await getSession(cookie); |
| 33 | + const toastMessage = session.get("toastMessage"); |
| 34 | + return { toastMessage, setCookie: await commitSession(session) }; |
| 35 | +} |
| 36 | + |
| 37 | +function asRequestCookie(setCookie: string) { |
| 38 | + return setCookie.split(";")[0]; |
| 39 | +} |
| 40 | + |
| 41 | +async function denialRedirect(action: "rename" | "delete") { |
| 42 | + const module = |
| 43 | + await import("~/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.settings.general/route"); |
| 44 | + |
| 45 | + const body = new URLSearchParams( |
| 46 | + action === "rename" ? { action, projectName: "New name" } : { action, projectSlug: "p" } |
| 47 | + ); |
| 48 | + |
| 49 | + try { |
| 50 | + await (module.action as any)({ |
| 51 | + user: { id: "user_1" }, |
| 52 | + ability: { can: () => false }, |
| 53 | + request: new Request(`https://app.example.com${SETTINGS_PATH}`, { method: "POST", body }), |
| 54 | + params: { organizationSlug: "o", projectParam: "p", envParam: "prod" }, |
| 55 | + context: {}, |
| 56 | + searchParams: undefined, |
| 57 | + }); |
| 58 | + } catch (thrown) { |
| 59 | + return thrown as Response; |
| 60 | + } |
| 61 | + |
| 62 | + throw new Error("expected the action to throw a redirect"); |
| 63 | +} |
| 64 | + |
| 65 | +describe("toast flash through a redirect chain", () => { |
| 66 | + it("is lost when the redirect target redirects again", async () => { |
| 67 | + const request = new Request(`https://app.example.com${SETTINGS_PATH}`, { method: "POST" }); |
| 68 | + const response = await redirectWithErrorMessage("/orgs/o/projects/p", request, "Denied"); |
| 69 | + |
| 70 | + const projectRootHop = await rootLoaderHop( |
| 71 | + asRequestCookie(response.headers.get("Set-Cookie")!) |
| 72 | + ); |
| 73 | + expect(projectRootHop.toastMessage?.message).toBe("Denied"); |
| 74 | + |
| 75 | + const tasksPageHop = await rootLoaderHop(asRequestCookie(projectRootHop.setCookie)); |
| 76 | + expect(tasksPageHop.toastMessage).toBeUndefined(); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("general settings permission denial", () => { |
| 81 | + it("redirects a denied rename back to the settings page with the message", async () => { |
| 82 | + const response = await denialRedirect("rename"); |
| 83 | + |
| 84 | + expect(response.headers.get("Location")).toBe(SETTINGS_PATH); |
| 85 | + |
| 86 | + const hop = await rootLoaderHop(asRequestCookie(response.headers.get("Set-Cookie")!)); |
| 87 | + expect(hop.toastMessage?.message).toBe("You don't have permission to rename this project"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("redirects a denied delete back to the settings page with the message", async () => { |
| 91 | + const response = await denialRedirect("delete"); |
| 92 | + |
| 93 | + expect(response.headers.get("Location")).toBe(SETTINGS_PATH); |
| 94 | + |
| 95 | + const hop = await rootLoaderHop(asRequestCookie(response.headers.get("Set-Cookie")!)); |
| 96 | + expect(hop.toastMessage?.message).toBe("You don't have permission to delete this project"); |
| 97 | + }); |
| 98 | +}); |
0 commit comments