iterate.com / cloud

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

cloud / internal-packages / dummy-petshop / src / test-controls.test.tsmain at aacbc74

/**
 * The shop's expire-tokens control (test-controls.ts): it names one account of one client, and
 * moves on that account's revocation epoch alone. What an expiry does to the tokens themselves is
 * authorization-server.test.ts. And its fail-token-endpoint control, at the token routes of the
 * fake GitHub and the fake Google.
 */
import { expect, test } from "vitest";
import { handleGithubRequest } from "./github.ts";
import { handleGoogleRequest } from "./google.ts";
import { memoryPetshop } from "./memory-state.ts";
import { handleTestControls } from "./test-controls.ts";

test.for([
  {
    name: "without an account it answers 400 and expires nothing",
    body: { clientId: "petshop-default" },
    answer: { status: 400, body: { error: "invalid_request" } },
    epochs: [],
  },
  {
    name: "with an account it moves on that account's epoch at the client, and no other",
    body: { clientId: "petshop-default", account: "ada@example.com" },
    answer: {
      status: 200,
      body: { clientId: "petshop-default", account: "ada@example.com", accessTokenEpoch: 1 },
    },
    epochs: [["petshop-default:ada@example.com", 1]],
  },
])("expire-tokens $name", async ({ body, answer, epochs }) => {
  const petshop = memoryPetshop();

  const response = await handleTestControls(
    new Request("https://petshop.test/__test-controls/expire-tokens", {
      method: "POST",
      body: JSON.stringify(body),
    }),
    petshop,
  );

  expect({ status: response.status, body: await response.json() }).toMatchObject(answer);
  // exactly these epochs: an expiry touches no other account's
  expect(Object.entries((await petshop.state.getState()).accessTokenEpochs)).toEqual(epochs);
});

test.for([
  ["GitHub's", "https://petshop.test/login/oauth/access_token", handleGithubRequest],
  ["Google's", "https://petshop.test/token", handleGoogleRequest],
] as const)(
  "fail-token-endpoint: %s token route answers 500 for the client's next call, issues nothing, and answers the call after it",
  async ([, url, handle]) => {
    const petshop = memoryPetshop();
    await handleTestControls(
      new Request("https://petshop.test/__test-controls/fail-token-endpoint", {
        method: "POST",
        body: JSON.stringify({ clientId: "petshop-default", times: 1 }),
      }),
      petshop,
    );
    // a refresh with a token that the shop never gave: only the call after the failure reads it
    const refresh = () =>
      handle(
        new Request(url, {
          method: "POST",
          headers: { "content-type": "application/x-www-form-urlencoded" },
          body: new URLSearchParams({
            grant_type: "refresh_token",
            refresh_token: "no token of the shop's",
            client_id: "petshop-default",
            client_secret: "petshop-default-secret",
          }),
        }),
        petshop,
      );

    const [failed, answered] = [await refresh(), await refresh()];

    expect({
      failed: { status: failed?.status, body: await failed?.json() },
      answered: answered?.status,
    }).toEqual({
      failed: { status: 500, body: expect.objectContaining({ error: "temporarily_unavailable" }) },
      // the provider's own refusal of that token, in its own way: GitHub's is a 200
      answered: expect.toSatisfy((status: number) => status < 500),
    });
  },
);