79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const config = vi.hoisted(() => ({
|
|
nodeEnv: "test",
|
|
routing: {
|
|
adminHost: "admin.localhost",
|
|
appHost: "app.localhost",
|
|
},
|
|
sessionSecret: "admin-phone-oauth-state-secret-aaaaaaaa",
|
|
}));
|
|
|
|
vi.mock("@hivegrid/config", async (importOriginal) => {
|
|
const original = await importOriginal<typeof import("@hivegrid/config")>();
|
|
return {
|
|
...original,
|
|
loadRequiredWebConfig: () => config,
|
|
};
|
|
});
|
|
|
|
import {
|
|
consumeAdminTelephonyOAuthState,
|
|
createAdminTelephonyOAuthState,
|
|
} from "./admin-telephony-oauth-state.server.js";
|
|
|
|
describe("admin telephony OAuth state", () => {
|
|
beforeEach(() => {
|
|
config.nodeEnv = "test";
|
|
});
|
|
|
|
it("binds the signed challenge to the admin and expires it after ten minutes", () => {
|
|
const challenge = createAdminTelephonyOAuthState({
|
|
adminUserId: "admin-1",
|
|
now: () => 1_000,
|
|
});
|
|
const cookie = challenge.cookie.split(";")[0]!;
|
|
const accepted = consumeAdminTelephonyOAuthState(
|
|
new Request("http://admin.localhost/callback", {
|
|
headers: { cookie },
|
|
}),
|
|
{
|
|
expectedState: challenge.state,
|
|
expectedAdminUserId: "admin-1",
|
|
now: () => 1_000 + 9 * 60_000,
|
|
}
|
|
);
|
|
expect(accepted.ok).toBe(true);
|
|
|
|
const expired = consumeAdminTelephonyOAuthState(
|
|
new Request("http://admin.localhost/callback", {
|
|
headers: { cookie },
|
|
}),
|
|
{
|
|
expectedState: challenge.state,
|
|
expectedAdminUserId: "admin-1",
|
|
now: () => 1_000 + 11 * 60_000,
|
|
}
|
|
);
|
|
expect(expired).toMatchObject({ ok: false, reason: "expired" });
|
|
});
|
|
|
|
it("rejects a challenge copied to another admin", () => {
|
|
const challenge = createAdminTelephonyOAuthState({
|
|
adminUserId: "admin-1",
|
|
now: () => 1_000,
|
|
});
|
|
const result = consumeAdminTelephonyOAuthState(
|
|
new Request("http://admin.localhost/callback", {
|
|
headers: { cookie: challenge.cookie.split(";")[0]! },
|
|
}),
|
|
{
|
|
expectedState: challenge.state,
|
|
expectedAdminUserId: "admin-2",
|
|
now: () => 2_000,
|
|
}
|
|
);
|
|
expect(result).toMatchObject({ ok: false, reason: "mismatch" });
|
|
});
|
|
});
|