Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
} from "./hooks/use-reauth.tsx";
import { TUserSettings } from "./components/ui/user-settings/types";
import { RequireNonGuest } from "./components/layout/auth/require-non-guest.tsx";
import { RequireFeature } from "./components/layout/auth/require-feature.tsx";
import { onRoleAuthCleared } from "./api/role-auth-store.ts";
import { PresetProvider } from "./contexts/preset-context.tsx";

const DisplayBoxPositioningContainer = styled(FlexContainer)`
Expand Down Expand Up @@ -171,7 +173,9 @@ const AppContent = ({
path="/create"
element={
<RequireNonGuest>
<CreateProductionPage />
<RequireFeature feature="canCreateProduction">
<CreateProductionPage />
</RequireFeature>
</RequireNonGuest>
}
errorElement={<ErrorPage />}
Expand All @@ -180,9 +184,11 @@ const AppContent = ({
path="/manage"
element={
<RequireNonGuest>
<ManageProductionsPage
setApiError={() => setApiError(true)}
/>
<RequireFeature feature="canEditProduction">
<ManageProductionsPage
setApiError={() => setApiError(true)}
/>
</RequireFeature>
</RequireNonGuest>
}
errorElement={<ErrorPage />}
Expand Down Expand Up @@ -231,6 +237,15 @@ const App = () => {

useLocalUserSettings({ devices, dispatch });

// Clear global-state role when the store drops a stale role password (401).
useEffect(
() =>
onRoleAuthCleared(() =>
dispatch({ type: "UPDATE_ROLE_AUTH", payload: null })
),
[dispatch]
);

return (
<GlobalStateContext.Provider value={initializedGlobalState}>
<AppContent
Expand Down
88 changes: 88 additions & 0 deletions src/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,91 @@ describe("API.forceDisconnectParticipant", () => {
).rejects.toThrow("Session with id session-1 not found on line 2");
});
});

// ---------------------------------------------------------------------------
// #632 — role permission request endpoint.
// ---------------------------------------------------------------------------

describe("API.requestPermissions", () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.unstubAllEnvs();
vi.resetModules();
});

const features = {
canCreateProduction: true,
canEditProduction: true,
canDeleteProduction: true,
canManageLines: true,
canRemoveParticipants: true,
canManageIngests: true,
canJoinLine: true,
};

it("POSTs the password and returns the granted role + features on 200", async () => {
vi.stubEnv("VITE_BACKEND_URL", "https://example.com/");
vi.resetModules();

const fetchMock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({ role: "admin", features }),
} as unknown as Response);
vi.stubGlobal("fetch", fetchMock);

const { API } = await import("./api.ts");

const result = await API.requestPermissions({ password: "hunter2" });

expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, options] = fetchMock.mock.calls[0];
expect(url).toContain("permissions");
expect(options.method).toBe("POST");
expect(JSON.parse(options.body)).toEqual({ password: "hunter2" });
expect(result).toEqual({ role: "admin", features });
});

it("throws with the manager message and status 401 on a bad password", async () => {
vi.stubEnv("VITE_BACKEND_URL", "https://example.com/");
vi.resetModules();

const fetchMock = vi.fn().mockResolvedValue({
ok: false,
status: 401,
statusText: "Unauthorized",
json: async () => ({ message: "Invalid or unknown role password" }),
} as unknown as Response);
vi.stubGlobal("fetch", fetchMock);

const { API } = await import("./api.ts");

await expect(
API.requestPermissions({ password: "wrong" })
).rejects.toMatchObject({
message: "Invalid or unknown role password",
status: 401,
});
});

it("throws with status 429 when rate-limited", async () => {
vi.stubEnv("VITE_BACKEND_URL", "https://example.com/");
vi.resetModules();

const fetchMock = vi.fn().mockResolvedValue({
ok: false,
status: 429,
statusText: "Too Many Requests",
json: async () => {
throw new Error("no body");
},
} as unknown as Response);
vi.stubGlobal("fetch", fetchMock);

const { API } = await import("./api.ts");

await expect(
API.requestPermissions({ password: "whatever" })
).rejects.toMatchObject({ status: 429 });
});
});
Loading
Loading