iterate.com / kernel

git clone https://git.iterate.com/repos/kernel

kernel / core / lib / src / cli / zone-protocols.test.tsmain at 949894f

import { expect, test } from "vitest";
import type { Answer, Cloudflare } from "./cloudflare-access.ts";
import { protocolChangeLine, setZoneProtocols, zoneProtocolChanges } from "./zone-protocols.ts";

const answer = (status: number, result: unknown, error = ""): Answer => ({
  status,
  result,
  total: 0,
  error,
});

/** A Cloudflare with two zones of account `a`, whose settings are `settings`, and every call. */
function fake(settings: Record<string, Record<string, string>>) {
  const calls: string[] = [];
  const cf: Cloudflare = async (method, path, body) => {
    calls.push(`${method} ${path}${body === undefined ? "" : ` ${JSON.stringify(body)}`}`);
    const listed = /^\/zones\?name=([^&]+)&account\.id=a$/.exec(path);
    if (listed) return answer(200, settings[listed[1]!] ? [{ id: `id-${listed[1]}` }] : []);
    const [, zone, setting] = /^\/zones\/id-([^/]+)\/settings\/(\w+)$/.exec(path) || [];
    const zoneSettings = zone ? settings[zone] : undefined;
    if (!zoneSettings || !setting || !(setting in zoneSettings))
      return answer(403, null, "Unauthorized to access requested resource");
    if (method === "PATCH") zoneSettings[setting] = (body as { value: string }).value;
    return answer(200, { id: setting, value: zoneSettings[setting], editable: true });
  };
  return { cf, calls };
}

test("a deploy leaves each zone with HTTP/3 on and 0-RTT off, and writes only what differs", async () => {
  const settings = {
    "one.example": { http3: "off", "0rtt": "on" },
    "two.example": { http3: "on", "0rtt": "off" },
  };
  const { cf, calls } = fake(settings);
  const changes = await zoneProtocolChanges(cf, "a", ["one.example", "two.example"]);
  expect(changes.map((change) => protocolChangeLine(change, false))).toEqual([
    "A deploy turns HTTP/3 on for one.example (it is off).",
    "A deploy turns 0-RTT Connection Resumption off for one.example (it is on).",
  ]);
  // the read wrote nothing
  expect(calls.every((call) => call.startsWith("GET "))).toBe(true);

  await setZoneProtocols(cf, changes);
  expect(calls.filter((call) => !call.startsWith("GET "))).toEqual([
    'PATCH /zones/id-one.example/settings/http3 {"value":"on"}',
    'PATCH /zones/id-one.example/settings/0rtt {"value":"off"}',
  ]);
  expect(settings["one.example"]).toEqual({ http3: "on", "0rtt": "off" });
  expect(protocolChangeLine(changes[1]!, true)).toBe(
    "Turned 0-RTT Connection Resumption off for one.example (it was on).",
  );
  // a second run finds nothing to change
  expect(await zoneProtocolChanges(cf, "a", ["one.example", "two.example"])).toEqual([]);
});

test("an answer that names no zone or no value stops the run before any write", async () => {
  const { cf, calls } = fake({ "one.example": { http3: "on" } });
  await expect(zoneProtocolChanges(cf, "a", ["gone.example"])).rejects.toThrow(
    "Cloudflare did not answer with the zone gone.example of the account a (200). Nothing was changed.",
  );
  await expect(zoneProtocolChanges(cf, "a", ["one.example"])).rejects.toThrow(
    "Cloudflare did not say whether 0-RTT Connection Resumption is on for one.example (403: Unauthorized to access requested resource). Nothing was changed.",
  );
  expect(calls.every((call) => call.startsWith("GET "))).toBe(true);
});

test("a write that Cloudflare refuses, or answers with the old value, stops the run", async () => {
  const change = {
    zone: "one.example",
    path: "/zones/id/settings/0rtt",
    name: "0-RTT Connection Resumption",
    from: "on",
    to: "off",
  };
  await expect(
    setZoneProtocols(async () => answer(403, null, "Authentication error"), [change]),
  ).rejects.toThrow(
    "Cloudflare did not turn 0-RTT Connection Resumption off for one.example (403: Authentication error). The deploy did not start.",
  );
  await expect(
    setZoneProtocols(async () => answer(200, { value: "on" }), [change]),
  ).rejects.toThrow(
    "Cloudflare did not turn 0-RTT Connection Resumption off for one.example (200).",
  );
});