iterate.com / cloud

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

cloud / internal-packages / installer / test / give-up.mjsmain at aacbc74

// give-up.mjs — how a run gives up its Cloudflare token and proves it (worker/cloudflare-access.js
// `giveUp`, `forgetLinesOf`), against a stubbed fetch. Run: node test/give-up.mjs
import assert from "node:assert/strict";
import { test } from "node:test";
import { exchange, forgetLinesOf, giveUp } from "../worker/cloudflare-access.js";

const ENDS = "This access ends by itself at 2026-01-01 00:00 UTC.";
/** Cloudflare: the revoke answers `revoke`, and the token then gets `probes`, one per question
 *  (the last one again and again). A status of 0 is a request that fails. */
function cloudflare(revoke, probes) {
  const asked = [];
  let n = 0;
  const fetch = async (url, init = {}) => {
    asked.push([
      init.method || "GET",
      String(url),
      init.headers?.authorization || String(init.body),
    ]);
    const status = String(url).endsWith("/revoke")
      ? revoke
      : probes[Math.min(n++, probes.length - 1)];
    if (status === 0) throw new Error("no route");
    return new Response(null, { status });
  };
  return { fetch, asked };
}
const given = (fetch) => ({
  token: "TOKEN",
  accountId: "a".repeat(32),
  clientId: "client",
  clientSecret: "secret",
  revokeUrl: "https://cf.test/oauth2/revoke",
  api: "https://cf.test/client/v4",
  fetch,
  pause: 0,
});

test("a revoked token: both statuses are in the run's line", async () => {
  const { fetch, asked } = cloudflare(200, [401]);
  const gave = await giveUp(given(fetch));
  assert.deepEqual(gave, { revoke: 200, revoked: true, probe: 401, works: false });
  assert.deepEqual(forgetLinesOf(gave, ENDS), [
    "Cloudflare revoked the access (200). The token now gets 401.",
  ]);
  assert.deepEqual(
    asked.map(([method, url]) => `${method} ${url}`),
    [
      "POST https://cf.test/oauth2/revoke",
      `GET https://cf.test/client/v4/accounts/${"a".repeat(32)}`,
    ],
  );
  assert.equal(asked[1][2], "Bearer TOKEN");
});

test("a token that is refused a moment later counts as given up", async () => {
  const { fetch, asked } = cloudflare(200, [200, 200, 403]);
  const gave = await giveUp(given(fetch));
  assert.equal(gave.works, false);
  assert.equal(gave.probe, 403);
  assert.equal(asked.length, 4);
});

test("a token that still works after every question stops the run, with what the person does", async () => {
  const { fetch, asked } = cloudflare(200, [200]);
  const gave = await giveUp({ ...given(fetch), tries: 3 });
  assert.deepEqual(gave, { revoke: 200, revoked: true, probe: 200, works: true });
  assert.equal(asked.length, 4);
  const lines = forgetLinesOf(gave, ENDS);
  assert.equal(
    lines[0],
    "Cloudflare revoked the access (200). The token still works: it gets 200.",
  );
  assert.match(lines[1], /^Stopped: iterate could not give up its access/);
  assert.match(
    lines[1],
    /Remove “Iterate Cloud” yourself.*Access Management, then Connected Applications/,
  );
  assert.ok(lines[1].endsWith(ENDS));
});

test("a revoke that Cloudflare refuses, and a question with no answer, are said as they are", async () => {
  const refused = await giveUp({ ...given(cloudflare(401, [401]).fetch) });
  assert.deepEqual(forgetLinesOf(refused, ENDS), [
    "Cloudflare did not revoke the access (401). The token now gets 401.",
  ]);
  const silent = await giveUp({ ...given(cloudflare(200, [0]).fetch), tries: 2 });
  assert.equal(silent.works, null);
  assert.deepEqual(forgetLinesOf(silent, ENDS), [
    `Cloudflare revoked the access (200). Whether the token still works could not be checked (no answer). ${ENDS}`,
  ]);
  const busy = await giveUp({ ...given(cloudflare(200, [503]).fetch), tries: 2 });
  assert.equal(busy.works, null);
  assert.equal(busy.probe, 503);
});

test("the code exchange says whether Cloudflare sent a refresh token", async () => {
  const answer = (tokens) => async (url) =>
    String(url).endsWith("/token") ? Response.json(tokens) : new Response(null, { status: 200 });
  const args = {
    code: "c",
    verifier: "v",
    redirectUri: "r",
    clientId: "i",
    clientSecret: "s",
    tokenUrl: "https://cf.test/token",
    revokeUrl: "https://cf.test/revoke",
  };
  const none = await exchange({ ...args, fetch: answer({ access_token: "A", expires_in: 60 }) });
  assert.equal(none.refresh, null);
  const sent = await exchange({
    ...args,
    fetch: answer({ access_token: "A", refresh_token: "R" }),
  });
  assert.deepEqual(sent.refresh, { revoke: 200 });
  assert.equal(sent.token, "A");
});