cast/test/team.test.ts

122 lines
4.4 KiB
TypeScript
Raw Normal View History

feat: assert the token's team before touching Coolify (fail-closed) Coolify API tokens are team-scoped, and a wrong-team token does not error: the API resolves what it cannot see to `null` (getResourceByUuid walks resource → environment → project → team_id and returns null on a mismatch). To cast, `null` is indistinguishable from "this resource does not exist yet" — an invitation to create it. So an apply with a token minted under the wrong team would not fail loudly; it would provision a duplicate set of resources into the wrong team, against whatever server that team owns. Silent, mutating, discovered late. That makes this a correctness bug, not hardening. - environments.yaml carries a required `team:` per environment (id, name, or both). Required is the point: an environment with no declared team is one cast cannot verify it is pointed at. - Every command that reaches a live Coolify (apply, diff, server add, smoke) resolves GET /teams/current — the only endpoint that answers "what team does this token act as?" — and aborts on mismatch before its first READ, not merely its first write: a wrong-team diff reports "everything is absent", which is the very lie an apply would then act on. - server add and smoke take --env for this reason. A server belongs to exactly one team forever (no pivot, no is_system_wide escape hatch), and smoke writes env vars onto a live app. - New read-only `cast team` prints the token's team, so the binding can be filled in without a chicken-and-egg. With --env it also checks the binding: the dry run for "would apply refuse?". Team id 0 is a first-class value, not a falsy absent — it is the Root Team that a single-admin instance keeps everything in (app/Models/User.php). Also records the #4 investigation in docs/semantics.md: GithubApp `is_system_wide` IS the supported way to serve every team — list_github_apps scopes to `team_id = token's team OR is_system_wide`, and POST /github-apps accepts the flag — so per-team App duplication is unnecessary. Corollary: resolving a GitHub App by name is NOT a proxy for being in the right team, which is the second reason the assert has to be explicit. Closes #9 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-12 20:55:04 +00:00
import { describe, expect, it, vi } from "vitest";
import { CoolifyClient } from "../src/coolify.js";
import { assertTeam } from "../src/team.js";
function clientReturning(team: unknown, status = 200): CoolifyClient {
const fetchImpl = vi.fn(
async () => new Response(JSON.stringify(team), { status }),
) as unknown as typeof fetch;
return new CoolifyClient("https://coolify.test", "tok", fetchImpl);
}
const HEAVY_DUTY = { id: 1, name: "heavy-duty", personal_team: false };
describe("assertTeam", () => {
it("passes when both id and name match, returning the live team", async () => {
const team = await assertTeam(
clientReturning(HEAVY_DUTY),
{ id: 1, name: "heavy-duty" },
"prod",
);
expect(team).toEqual({ id: 1, name: "heavy-duty" });
});
it("passes on id alone, and on name alone", async () => {
await expect(
assertTeam(clientReturning(HEAVY_DUTY), { id: 1 }, "prod"),
).resolves.toBeTruthy();
await expect(
assertTeam(clientReturning(HEAVY_DUTY), { name: "heavy-duty" }, "prod"),
).resolves.toBeTruthy();
});
// The whole point of the issue: a token minted under another team must turn
// a silent mis-target into a refusal, because Coolify itself would not
// error — it would resolve every resource to null and invite a duplicate
// create in the wrong team.
it("refuses on an id mismatch, naming both teams and the environment", async () => {
const err = await assertTeam(
clientReturning({ id: 3, name: "personal" }),
{ id: 1, name: "heavy-duty" },
"prod",
).catch((e: Error) => e);
expect(err).toBeInstanceOf(Error);
expect((err as Error).message).toMatch(/wrong team/);
expect((err as Error).message).toMatch(/environment:\s+prod/);
expect((err as Error).message).toMatch(/expected team:\s+id=1/);
expect((err as Error).message).toMatch(/token's team:\s+id=3/);
expect((err as Error).message).toMatch(/mismatched:\s+id, name/);
});
// A renamed-but-same-id team, or an id typo against a right-named team:
// either half mismatching is a refusal. Both are compared when both given.
it("refuses when only the name mismatches", async () => {
await expect(
assertTeam(
clientReturning({ id: 1, name: "some-other-team" }),
{ id: 1, name: "heavy-duty" },
"prod",
),
).rejects.toThrow(/mismatched:\s+name/);
});
it("refuses when only the id mismatches", async () => {
await expect(
assertTeam(
clientReturning({ id: 9, name: "heavy-duty" }),
{ id: 1, name: "heavy-duty" },
"prod",
),
).rejects.toThrow(/mismatched:\s+id/);
});
// An unreadable answer is not "no team" — it is an unknown answer to the one
// question we must not guess at, so it fails rather than degrading to a pass.
it("refuses when /teams/current has no usable identity", async () => {
await expect(
assertTeam(
clientReturning({ message: "Unauthenticated." }),
{ id: 1 },
"prod",
),
).rejects.toThrow(/no usable team identity/);
});
it("surfaces a token rejection rather than swallowing it", async () => {
await expect(
assertTeam(
clientReturning({ message: "bad token" }, 401),
{ id: 1 },
"prod",
),
).rejects.toThrow(/GET \/teams\/current → 401/);
});
// Team 0 is the Root Team — the team the first user of an instance gets
// (app/Models/User.php @ v4.1.2). On a single-admin Coolify it is the team
// everything lives in, so `0` must be a first-class expectation, not a
// falsy value that quietly compares as absent.
it("compares team id 0 (the Root Team) as a real expectation", async () => {
const root = { id: 0, name: "Root Team" };
await expect(
assertTeam(clientReturning(root), { id: 0 }, "prod"),
).resolves.toEqual(root);
await expect(
assertTeam(
clientReturning({ id: 3, name: "personal" }),
{ id: 0 },
"prod",
),
).rejects.toThrow(/wrong team/);
});
// The gate must fail closed on its own, without leaning on the bindings
// schema to have rejected an empty team first: an expectation that names
// nothing would compare nothing and pass against ANY team.
it("refuses an expectation that names neither id nor name", async () => {
await expect(
assertTeam(clientReturning({ id: 99, name: "anything" }), {}, "prod"),
).rejects.toThrow(/names neither an id nor a name/);
});
});