79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
hashAdminBookingManageNonce,
|
|
issueAdminBookingManageToken,
|
|
verifyAdminBookingManageToken,
|
|
} from "./admin-booking-manage-token.server.js";
|
|
|
|
const secret = "test-secret-with-at-least-thirty-two-characters";
|
|
const now = new Date("2026-07-25T12:00:00.000Z");
|
|
const nonce = "abcdefghijklmnopqrstuvwxyz_123456";
|
|
|
|
describe("admin booking manage tokens", () => {
|
|
it("round-trips bounded signed claims", () => {
|
|
const token = issueAdminBookingManageToken({
|
|
bookingId: "booking_1",
|
|
adminUserId: "admin_1",
|
|
version: 2,
|
|
expiresAt: new Date("2026-08-01T12:00:00.000Z"),
|
|
secret,
|
|
now,
|
|
nonce,
|
|
});
|
|
expect(
|
|
verifyAdminBookingManageToken({ token, secret, now })
|
|
).toEqual({
|
|
bookingId: "booking_1",
|
|
adminUserId: "admin_1",
|
|
version: 2,
|
|
expiresAt: new Date("2026-08-01T12:00:00.000Z"),
|
|
nonce,
|
|
});
|
|
});
|
|
|
|
it("rejects tampering, expiry and an unbounded lifetime", () => {
|
|
const token = issueAdminBookingManageToken({
|
|
bookingId: "booking_1",
|
|
adminUserId: "admin_1",
|
|
version: 1,
|
|
expiresAt: new Date("2026-08-01T12:00:00.000Z"),
|
|
secret,
|
|
now,
|
|
nonce,
|
|
});
|
|
expect(() =>
|
|
verifyAdminBookingManageToken({
|
|
token: `${token.slice(0, -1)}x`,
|
|
secret,
|
|
now,
|
|
})
|
|
).toThrow("invalid_token");
|
|
expect(() =>
|
|
verifyAdminBookingManageToken({
|
|
token,
|
|
secret,
|
|
now: new Date("2026-08-02T12:00:00.000Z"),
|
|
})
|
|
).toThrow("expired_token");
|
|
expect(() =>
|
|
issueAdminBookingManageToken({
|
|
bookingId: "booking_1",
|
|
adminUserId: "admin_1",
|
|
version: 1,
|
|
expiresAt: new Date("2027-08-01T12:00:00.000Z"),
|
|
secret,
|
|
now,
|
|
nonce,
|
|
})
|
|
).toThrow("invalid_expiry");
|
|
});
|
|
|
|
it("derives a stable storage-safe replay marker", () => {
|
|
expect(hashAdminBookingManageNonce(nonce)).toMatch(/^[a-f0-9]{64}$/u);
|
|
expect(hashAdminBookingManageNonce(nonce)).toBe(
|
|
hashAdminBookingManageNonce(nonce)
|
|
);
|
|
});
|
|
});
|