80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const { config } = vi.hoisted(() => ({
|
|
config: {
|
|
nodeEnv: "test",
|
|
sessionSecret: "admin-calendar-oauth-state-secret-aaaaa",
|
|
routing: {
|
|
appBaseDomain: "example.test",
|
|
webAppHost: "app.example.test",
|
|
adminAppHost: "admin.example.test",
|
|
apiHost: "api.example.test",
|
|
protocol: "https" as const,
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("@hivegrid/config", async () => ({
|
|
...(await vi.importActual<typeof import("@hivegrid/config")>(
|
|
"@hivegrid/config"
|
|
)),
|
|
loadRequiredWebConfig: () => config,
|
|
}));
|
|
|
|
import {
|
|
consumeAdminCalendarOAuthState,
|
|
createAdminCalendarOAuthState,
|
|
} from "./admin-calendar-oauth-state.server.js";
|
|
|
|
function requestWith(cookie: string) {
|
|
return new Request("https://admin.example.test/callback", {
|
|
headers: { cookie: cookie.split(";")[0] ?? "" },
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
config.nodeEnv = "test";
|
|
});
|
|
|
|
describe("admin calendar OAuth state", () => {
|
|
it("round-trips only for the bound admin and provider", () => {
|
|
const created = createAdminCalendarOAuthState({
|
|
adminUserId: "admin-1",
|
|
provider: "google_calendar",
|
|
now: () => 1_000,
|
|
});
|
|
expect(
|
|
consumeAdminCalendarOAuthState(requestWith(created.cookie), {
|
|
expectedState: created.state,
|
|
expectedProvider: "google_calendar",
|
|
expectedAdminUserId: "admin-1",
|
|
now: () => 2_000,
|
|
})
|
|
).toMatchObject({ ok: true });
|
|
expect(
|
|
consumeAdminCalendarOAuthState(requestWith(created.cookie), {
|
|
expectedState: created.state,
|
|
expectedProvider: "microsoft_calendar",
|
|
expectedAdminUserId: "admin-1",
|
|
now: () => 2_000,
|
|
})
|
|
).toMatchObject({ ok: false, reason: "mismatch" });
|
|
});
|
|
|
|
it("rejects expired state", () => {
|
|
const created = createAdminCalendarOAuthState({
|
|
adminUserId: "admin-1",
|
|
provider: "google_calendar",
|
|
now: () => 1_000,
|
|
});
|
|
expect(
|
|
consumeAdminCalendarOAuthState(requestWith(created.cookie), {
|
|
expectedState: created.state,
|
|
expectedProvider: "google_calendar",
|
|
expectedAdminUserId: "admin-1",
|
|
now: () => 11 * 60 * 1_000,
|
|
})
|
|
).toMatchObject({ ok: false, reason: "expired" });
|
|
});
|
|
});
|