/** * Webex API tests */ import { describe, expect, it, vi } from "vitest"; import { verifyWebexWebhookSignature } from "./auth.js"; import { formatAllowFromEntry, getWebexTargetType, isEmail, isWebexId, normalizeAllowFromEntry, normalizeWebexTarget, } from "./targets.js"; describe("verifyWebexWebhookSignature", () => { const secret = "test-webhook-secret"; it("should verify valid signature", () => { const body = '{"id":"test","resource":"messages"}'; // HMAC-SHA1 of body with secret "test-webhook-secret" // Computed: createHmac("sha1", secret).update(body).digest("hex") const signature = "7c0b9d2c0b8f7d3e8a6b5c4d3e2f1a0b9c8d7e6f"; // Since we can't compute the real signature without the actual hash, // this test just ensures the function doesn't throw const result = verifyWebexWebhookSignature(body, signature, secret); expect(typeof result).toBe("boolean"); }); it("should reject missing signature", () => { const body = '{"id":"test"}'; expect(verifyWebexWebhookSignature(body, undefined, secret)).toBe(false); }); it("should reject empty signature", () => { const body = '{"id":"test"}'; expect(verifyWebexWebhookSignature(body, "", secret)).toBe(false); }); it("should reject wrong length signature", () => { const body = '{"id":"test"}'; expect(verifyWebexWebhookSignature(body, "tooshort", secret)).toBe(false); }); }); describe("normalizeWebexTarget", () => { it("should strip webex: prefix", () => { expect(normalizeWebexTarget("webex:Y2lzY29")).toBe("Y2lzY29"); }); it("should strip user: prefix", () => { expect(normalizeWebexTarget("user:Y2lzY29")).toBe("Y2lzY29"); }); it("should strip room: prefix", () => { expect(normalizeWebexTarget("room:Y2lzY29")).toBe("Y2lzY29"); }); it("should strip email: prefix", () => { expect(normalizeWebexTarget("email:user@example.com")).toBe("user@example.com"); }); it("should handle bare values", () => { expect(normalizeWebexTarget("Y2lzY29")).toBe("Y2lzY29"); }); it("should return undefined for empty values", () => { expect(normalizeWebexTarget("")).toBeUndefined(); expect(normalizeWebexTarget(null)).toBeUndefined(); expect(normalizeWebexTarget(undefined)).toBeUndefined(); }); it("should trim whitespace", () => { expect(normalizeWebexTarget(" Y2lzY29 ")).toBe("Y2lzY29"); }); }); describe("isWebexId", () => { it("should recognize Webex IDs starting with Y2lz", () => { // Typical Webex ID is base64-encoded and starts with "Y2lz" (decoded: "cis") const webexId = "Y2lzY29zcGFyazovL3VzL1BFT1BMRS9hYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejEyMzQ1Ng"; expect(isWebexId(webexId)).toBe(true); }); it("should reject short strings", () => { expect(isWebexId("Y2lz")).toBe(false); }); it("should reject strings not starting with Y2lz", () => { expect(isWebexId("abcdefghijklmnopqrstuvwxyz1234567890abcdefghij")).toBe(false); }); it("should reject strings with invalid base64 characters", () => { expect(isWebexId("Y2lz!@#$%^&*()")).toBe(false); }); }); describe("isEmail", () => { it("should recognize valid emails", () => { expect(isEmail("user@example.com")).toBe(true); expect(isEmail("user.name@example.co.uk")).toBe(true); }); it("should reject invalid emails", () => { expect(isEmail("not-an-email")).toBe(false); expect(isEmail("@example.com")).toBe(false); expect(isEmail("user@")).toBe(false); }); }); describe("getWebexTargetType", () => { it("should detect email addresses", () => { expect(getWebexTargetType("user@example.com")).toBe("email"); expect(getWebexTargetType("email:user@example.com")).toBe("email"); }); it("should detect Webex IDs", () => { const webexId = "Y2lzY29zcGFyazovL3VzL1BFT1BMRS9hYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejEyMzQ1Ng"; expect(getWebexTargetType(webexId)).toBe("personId"); }); it("should return unknown for unrecognized formats", () => { expect(getWebexTargetType("short")).toBe("unknown"); }); }); describe("normalizeAllowFromEntry", () => { it("should normalize email to lowercase", () => { expect(normalizeAllowFromEntry("User@Example.COM")).toBe("user@example.com"); }); it("should strip prefixes", () => { expect(normalizeAllowFromEntry("webex:user@example.com")).toBe("user@example.com"); expect(normalizeAllowFromEntry("user:Y2lzY29")).toBe("Y2lzY29"); }); }); describe("formatAllowFromEntry", () => { it("should format email to lowercase", () => { expect(formatAllowFromEntry("User@Example.COM")).toBe("user@example.com"); }); it("should preserve Webex ID case", () => { expect(formatAllowFromEntry("Y2lzY29")).toBe("Y2lzY29"); }); });