marketingAssetRegistry.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { createHash } from "node:crypto";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { createMarketingAssetRegistry } from "#src/marketing-touch/marketingAssetRegistry.js";
  4. import { parseMarketingTouchResponse } from "@zcode/shared";
  5. describe("published marketing assets", () => {
  6. it("authorizes banner ZIP and fallback only from a validated delivery", () => {
  7. const registry = createMarketingAssetRegistry();
  8. const bundle = { src: "https://cdn.example.com/banner.zip", sha256: "a".repeat(64) };
  9. const fallback = { src: "https://cdn.example.com/banner.png", sha256: "b".repeat(64) };
  10. expect(registry.allows(bundle)).toBe(false);
  11. registry.accept(
  12. parseMarketingTouchResponse({
  13. code: 0,
  14. data: {
  15. server_time: 1,
  16. language: "en-US",
  17. deliveries: [
  18. {
  19. campaign_id: "bundle",
  20. priority: 1,
  21. resource_position: "banner",
  22. banner: {
  23. background: { type: "bundle", bundle: { bundle, fallback, entry: "index.html" } },
  24. buttons: [],
  25. },
  26. },
  27. ],
  28. },
  29. }),
  30. );
  31. expect(registry.allows(bundle)).toBe(true);
  32. expect(registry.allows(fallback)).toBe(true);
  33. expect(registry.allows({ ...bundle, sha256: "0".repeat(64) })).toBe(false);
  34. });
  35. it("requires a published URL/digest, validates bytes and reuses verified media", async () => {
  36. const bytes = Buffer.from("89504e470d0a1a0a00000000", "hex");
  37. const asset = {
  38. src: "https://cdn.example.com/a.png",
  39. sha256: createHash("sha256").update(bytes).digest("hex"),
  40. };
  41. const fetcher = vi.fn(async () => new Response(bytes));
  42. const registry = createMarketingAssetRegistry({ fetch: fetcher });
  43. await expect(registry.readMedia(asset, "image")).rejects.toThrow(/source/);
  44. registry.allow(asset);
  45. expect(await registry.readMedia(asset, "image")).toContain("data:image/png;base64,");
  46. await registry.readMedia(asset, "image");
  47. expect(fetcher).toHaveBeenCalledTimes(1);
  48. await expect(registry.readMedia({ ...asset, sha256: "0".repeat(64) }, "image")).rejects.toThrow(
  49. /source/,
  50. );
  51. });
  52. it("rejects changed bytes and redirects instead of following unapproved destinations", async () => {
  53. const asset = { src: "https://cdn.example.com/a.png", sha256: "0".repeat(64) };
  54. const fetcher = vi.fn(async () => new Response("bad"));
  55. const registry = createMarketingAssetRegistry({ fetch: fetcher });
  56. registry.allow(asset);
  57. await expect(registry.readMedia(asset, "image")).rejects.toThrow(/integrity/);
  58. fetcher.mockImplementation(
  59. async () => new Response(null, { status: 302, headers: { Location: "http://localhost" } }),
  60. );
  61. await expect(registry.readMedia(asset, "image")).rejects.toThrow(/download/);
  62. });
  63. });