Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/services/admin/admin-email-oauth-state.server.test.ts
T

95 lines
2.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const { config } = vi.hoisted(() => ({
config: {
nodeEnv: "test",
sessionSecret: "admin-email-oauth-state-secret-aaaaaaaa",
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 () => {
const actual =
await vi.importActual<typeof import("@hivegrid/config")>(
"@hivegrid/config"
);
return {
...actual,
loadRequiredWebConfig: () => config,
};
});
import {
consumeAdminEmailOAuthState,
createAdminEmailOAuthState,
} from "./admin-email-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 email OAuth state", () => {
it("round-trips one owner/provider state and clears it", () => {
const created = createAdminEmailOAuthState({
adminUserId: "admin-1",
provider: "google_mail",
now: () => 1_000,
});
expect(
consumeAdminEmailOAuthState(requestWith(created.cookie), {
expectedState: created.state,
expectedProvider: "google_mail",
expectedAdminUserId: "admin-1",
now: () => 2_000,
})
).toMatchObject({
ok: true,
clearCookie: expect.stringContaining("Max-Age=0"),
});
});
it("rejects provider, owner and replay-age mismatches", () => {
const created = createAdminEmailOAuthState({
adminUserId: "admin-1",
provider: "google_mail",
now: () => 1_000,
});
expect(
consumeAdminEmailOAuthState(requestWith(created.cookie), {
expectedState: created.state,
expectedProvider: "microsoft_mail",
expectedAdminUserId: "admin-1",
now: () => 2_000,
})
).toMatchObject({ ok: false, reason: "mismatch" });
expect(
consumeAdminEmailOAuthState(requestWith(created.cookie), {
expectedState: created.state,
expectedProvider: "google_mail",
expectedAdminUserId: "admin-2",
now: () => 2_000,
})
).toMatchObject({ ok: false, reason: "mismatch" });
expect(
consumeAdminEmailOAuthState(requestWith(created.cookie), {
expectedState: created.state,
expectedProvider: "google_mail",
expectedAdminUserId: "admin-1",
now: () => 11 * 60 * 1_000,
})
).toMatchObject({ ok: false, reason: "expired" });
});
});