176 lines
6.4 KiB
TypeScript
176 lines
6.4 KiB
TypeScript
/**
|
|
* HTTP-level tests for the P060/P067 routes added in this pass, in the same
|
|
* shape as tests/api/0pt-cross-tenant-routes.test.ts: call the handler
|
|
* directly with a hand-built NextRequest carrying `x-0pt-verified-tenant`,
|
|
* which is exactly what middleware.ts forwards once a real Bearer token has
|
|
* been verified.
|
|
*
|
|
* Two things are proved here that the service-level tests cannot:
|
|
* - the new routes carry the SAME cross-tenant guard as the existing ones
|
|
* (a new route with no guard would be the P014 hole reopened), and
|
|
* - the activation gate is enforced at the HTTP boundary too — an
|
|
* unclassified source gets 403, not a 200 with a quiet no-op.
|
|
*/
|
|
import { describe, expect, it } from "vitest";
|
|
import { NextRequest } from "next/server";
|
|
import { POST as sourcesPOST } from "@/app/api/0pt/sources/route";
|
|
import { GET as classifyGET, POST as activatePOST } from "@/app/api/0pt/sources/[sourceId]/activate/route";
|
|
import { PUT as authorizationPUT } from "@/app/api/0pt/sources/[sourceId]/authorization/route";
|
|
import { PUT as schedulePUT } from "@/app/api/0pt/sources/[sourceId]/schedule/route";
|
|
import { GET as lineageGET } from "@/app/api/0pt/lineage/route";
|
|
|
|
const VERIFIED = "x-0pt-verified-tenant";
|
|
|
|
function getReq(url: string, verifiedTenant = "t_a"): NextRequest {
|
|
return new NextRequest(new URL(url, "http://localhost"), { headers: { [VERIFIED]: verifiedTenant } });
|
|
}
|
|
function bodyReq(url: string, method: "POST" | "PUT", body: unknown, verifiedTenant = "t_a"): NextRequest {
|
|
return new NextRequest(new URL(url, "http://localhost"), {
|
|
method,
|
|
headers: { "content-type": "application/json", [VERIFIED]: verifiedTenant },
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
const params = (sourceId: string) => ({ params: Promise.resolve({ sourceId }) });
|
|
|
|
describe("P060 routes — cross-tenant guard", () => {
|
|
it("POST /activate rejects a foreign tenant_id (403)", async () => {
|
|
const res = await activatePOST(
|
|
bodyReq("/api/0pt/sources/src_x/activate", "POST", { tenant_id: "t_victim" }),
|
|
params("src_x"),
|
|
);
|
|
expect(res.status).toBe(403);
|
|
expect((await res.json()).error).toBe("OPT_ACCESS_DENIED");
|
|
});
|
|
|
|
it("GET /activate (classification) rejects a foreign tenant_id (403)", async () => {
|
|
const res = await classifyGET(
|
|
getReq("/api/0pt/sources/src_x/activate?tenant_id=t_victim"),
|
|
params("src_x"),
|
|
);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("PUT /authorization rejects a foreign tenant_id (403)", async () => {
|
|
const res = await authorizationPUT(
|
|
bodyReq("/api/0pt/sources/src_x/authorization", "PUT", {
|
|
tenant_id: "t_victim",
|
|
basis: "contract",
|
|
granted_by: "usr_attacker",
|
|
granted_at: "2026-09-04T00:00:00.000Z",
|
|
}),
|
|
params("src_x"),
|
|
);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("PUT /schedule rejects a foreign tenant_id (403)", async () => {
|
|
const res = await schedulePUT(
|
|
bodyReq("/api/0pt/sources/src_x/schedule", "PUT", { tenant_id: "t_victim", mode: "batch", enabled: true }),
|
|
params("src_x"),
|
|
);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("GET /lineage rejects a foreign tenant_id (403)", async () => {
|
|
const res = await lineageGET(getReq("/api/0pt/lineage?tenant_id=t_victim&output_name=0pt:dataset:ds_x"));
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("GET /lineage refuses an unfiltered dump (400)", async () => {
|
|
const res = await lineageGET(getReq("/api/0pt/lineage?tenant_id=t_a"));
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|
|
|
|
describe("P060 routes — the activation gate over HTTP", () => {
|
|
it("an unclassified source cannot be activated through the API", async () => {
|
|
const created = await sourcesPOST(
|
|
bodyReq("/api/0pt/sources", "POST", {
|
|
source_id: "src_http_gate_1",
|
|
tenant_id: "t_a",
|
|
project_id: "p_a",
|
|
source_type: "api",
|
|
name: "Unclassified source",
|
|
}),
|
|
);
|
|
expect(created.status).toBeLessThan(300);
|
|
const body = await created.json();
|
|
expect(body.source.status).toBe("pending");
|
|
|
|
const classification = await classifyGET(
|
|
getReq("/api/0pt/sources/src_http_gate_1/activate?tenant_id=t_a"),
|
|
params("src_http_gate_1"),
|
|
);
|
|
expect(classification.status).toBe(200);
|
|
expect((await classification.json()).classified).toBe(false);
|
|
|
|
const activated = await activatePOST(
|
|
bodyReq("/api/0pt/sources/src_http_gate_1/activate", "POST", { tenant_id: "t_a" }),
|
|
params("src_http_gate_1"),
|
|
);
|
|
expect(activated.status).toBe(403);
|
|
});
|
|
|
|
it("a classified source activates through the API", async () => {
|
|
await sourcesPOST(
|
|
bodyReq("/api/0pt/sources", "POST", {
|
|
source_id: "src_http_gate_2",
|
|
tenant_id: "t_a",
|
|
project_id: "p_a",
|
|
source_type: "api",
|
|
name: "Classified source",
|
|
owner_id: "usr_simon",
|
|
rights: {
|
|
license_id: "CC-BY-4.0",
|
|
allowed_purposes: ["catalog"],
|
|
pii_allowed: false,
|
|
redistribution: "internal",
|
|
verified_at: "2026-09-04",
|
|
verified_by: "usr_simon",
|
|
},
|
|
provenance: { method: "official_api" },
|
|
}),
|
|
);
|
|
const auth = await authorizationPUT(
|
|
bodyReq("/api/0pt/sources/src_http_gate_2/authorization", "PUT", {
|
|
tenant_id: "t_a",
|
|
basis: "public_register",
|
|
granted_by: "usr_simon",
|
|
granted_at: "2026-09-01T00:00:00.000Z",
|
|
}),
|
|
params("src_http_gate_2"),
|
|
);
|
|
expect(auth.status).toBe(200);
|
|
|
|
const activated = await activatePOST(
|
|
bodyReq("/api/0pt/sources/src_http_gate_2/activate", "POST", { tenant_id: "t_a", actor_id: "usr_simon" }),
|
|
params("src_http_gate_2"),
|
|
);
|
|
expect(activated.status).toBe(200);
|
|
expect((await activated.json()).status).toBe("active");
|
|
});
|
|
|
|
it("a service identity cannot be recorded as the authorizing actor (400)", async () => {
|
|
await sourcesPOST(
|
|
bodyReq("/api/0pt/sources", "POST", {
|
|
source_id: "src_http_gate_3",
|
|
tenant_id: "t_a",
|
|
project_id: "p_a",
|
|
source_type: "api",
|
|
name: "Self-authorizing source",
|
|
}),
|
|
);
|
|
const res = await authorizationPUT(
|
|
bodyReq("/api/0pt/sources/src_http_gate_3/authorization", "PUT", {
|
|
tenant_id: "t_a",
|
|
basis: "internal",
|
|
granted_by: "svc_0pt",
|
|
granted_at: "2026-09-01T00:00:00.000Z",
|
|
}),
|
|
params("src_http_gate_3"),
|
|
);
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|