caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

123 lines
4.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
/**
* Auth + rate-limit contract for POST /api/v1/growth/rank-hooks.
*
* The credential check is delegated to the Menivor API's `/me` (stubbed here via
* a fake global fetch). TYPESAFE_API_KEY is unset so an allowed request takes
* the heuristic path and never calls Jev.
*/
const VALID_KEYS: Record<string, string> = { "alice-key": "user-alice", "bob-key": "user-bob" };
let meCalls = 0;
let apiDown = false;
function fakeFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const url = String(input);
if (!url.endsWith("/me")) throw new Error(`unexpected fetch ${url}`);
meCalls += 1;
if (apiDown) return Promise.reject(new Error("connect ECONNREFUSED"));
const headers = new Headers(init?.headers);
const bearer = headers.get("authorization")?.replace(/^Bearer\s+/i, "");
const key = headers.get("x-api-key") ?? bearer ?? "";
const userId = VALID_KEYS[key];
if (!userId) {
return Promise.resolve(new Response(JSON.stringify({ status: 401, message: "invalid api key" }), { status: 401 }));
}
return Promise.resolve(
new Response(JSON.stringify({ status: 200, data: { user_id: userId } }), { status: 200 }),
);
}
function post(headers: Record<string, string> = {}, body: unknown = { hooks: ["first hook", "second hook"] }) {
return new Request("http://localhost/api/v1/growth/rank-hooks", {
method: "POST",
headers: { "Content-Type": "application/json", ...headers },
body: JSON.stringify(body),
});
}
async function loadRoute() {
vi.resetModules();
return import("./route");
}
describe("POST /api/v1/growth/rank-hooks auth", () => {
beforeEach(() => {
meCalls = 0;
apiDown = false;
vi.stubEnv("TYPESAFE_API_KEY", "");
vi.stubGlobal("fetch", vi.fn(fakeFetch));
vi.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => {
vi.unstubAllGlobals();
vi.unstubAllEnvs();
vi.restoreAllMocks();
});
it("returns 401 with no credential and never verifies or ranks", async () => {
const { POST } = await loadRoute();
const res = await POST(post());
expect(res.status).toBe(401);
expect(meCalls).toBe(0);
});
it("returns 401 for an unknown key", async () => {
const { POST } = await loadRoute();
const res = await POST(post({ "x-api-key": "stolen-or-made-up" }));
expect(res.status).toBe(401);
const body = (await res.json()) as { error: string };
expect(body.error).not.toMatch(/TYPESAFE|x-api-key/);
});
it("returns 401 before parsing a bad body when unauthenticated", async () => {
const { POST } = await loadRoute();
const res = await POST(post({}, "not json at all"));
expect(res.status).toBe(401);
});
it("ranks for a verified device key (x-api-key) and a Bearer token", async () => {
const { POST } = await loadRoute();
const res = await POST(post({ "x-api-key": "alice-key" }));
expect(res.status).toBe(200);
const body = (await res.json()) as { engine: string; variants: unknown[] };
expect(body.engine).toBe("heuristic");
expect(body.variants).toHaveLength(2);
const bearer = await POST(post({ Authorization: "Bearer bob-key" }));
expect(bearer.status).toBe(200);
});
it("caches a verified credential so a burst costs one /me call", async () => {
const { POST } = await loadRoute();
for (let i = 0; i < 3; i += 1) {
expect((await POST(post({ "x-api-key": "alice-key" }))).status).toBe(200);
}
expect(meCalls).toBe(1);
});
it("returns 503 (not 401) when the API can't verify the session", async () => {
apiDown = true;
const { POST } = await loadRoute();
const res = await POST(post({ "x-api-key": "alice-key" }));
expect(res.status).toBe(503);
});
it("rate-limits per user with 429 + Retry-After, independent across users", async () => {
const { POST } = await loadRoute();
const alice = { "x-api-key": "alice-key" };
for (let i = 0; i < 60; i += 1) {
expect((await POST(post(alice))).status).toBe(200);
}
const limited = await POST(post(alice));
expect(limited.status).toBe(429);
expect(Number(limited.headers.get("Retry-After"))).toBeGreaterThan(0);
const bob = await POST(post({ "x-api-key": "bob-key" }));
expect(bob.status).toBe(200);
});
});