Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
136 lines
5.6 KiB
TypeScript
136 lines
5.6 KiB
TypeScript
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import { bearer, createTestApp, login, PASSWORD, resetDb, seedUser, type TestContext } from "./helpers.js";
|
|
|
|
let ctx: TestContext;
|
|
beforeAll(async () => {
|
|
ctx = await createTestApp();
|
|
});
|
|
afterAll(async () => {
|
|
await ctx.app.close();
|
|
await ctx.db.$disconnect();
|
|
});
|
|
beforeEach(() => resetDb(ctx.db));
|
|
|
|
describe("me", () => {
|
|
it("returns memberships and default editor modules", async () => {
|
|
await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "EDITOR" });
|
|
const { body } = await login(ctx.app, "[email protected]");
|
|
const me = (await ctx.app.inject({ method: "GET", url: "/v1/me", headers: bearer(body.accessToken) })).json();
|
|
expect(me.memberships).toEqual([expect.objectContaining({ role: "EDITOR", organization: expect.objectContaining({ slug: "fpa" }) })]);
|
|
expect(me.preferences.editorModules).toEqual(["crop", "basic"]);
|
|
});
|
|
|
|
it("stores the chosen editor modules and rejects unknown ones", async () => {
|
|
await seedUser(ctx.db, "[email protected]");
|
|
const { body } = await login(ctx.app, "[email protected]");
|
|
const ok = await ctx.app.inject({
|
|
method: "PUT",
|
|
url: "/v1/me/preferences",
|
|
headers: bearer(body.accessToken),
|
|
payload: { editorModules: ["crop", "basic", "toneCurve", "basic"] },
|
|
});
|
|
expect(ok.json().editorModules).toEqual(["crop", "basic", "toneCurve"]);
|
|
const bad = await ctx.app.inject({
|
|
method: "PUT",
|
|
url: "/v1/me/preferences",
|
|
headers: bearer(body.accessToken),
|
|
payload: { editorModules: ["photoshop"] },
|
|
});
|
|
expect(bad.statusCode).toBe(400);
|
|
});
|
|
|
|
it("requires a token", async () => {
|
|
expect((await ctx.app.inject({ method: "GET", url: "/v1/me" })).statusCode).toBe(401);
|
|
});
|
|
});
|
|
|
|
describe("organization members", () => {
|
|
it("lets an admin create an account and blocks lower roles", async () => {
|
|
const { org } = await seedUser(ctx.db, "[email protected]", { org: "photoreport", role: "ADMIN" });
|
|
await seedUser(ctx.db, "[email protected]", { org: "photoreport", role: "EDITOR" });
|
|
const admin = await login(ctx.app, "[email protected]");
|
|
const editor = await login(ctx.app, "[email protected]");
|
|
const url = `/v1/orgs/${org?.id}/members`;
|
|
|
|
const created = await ctx.app.inject({
|
|
method: "POST",
|
|
url,
|
|
headers: bearer(admin.body.accessToken),
|
|
payload: { email: "[email protected]", name: "Fotógrafo", password: PASSWORD, role: "PHOTOGRAPHER" },
|
|
});
|
|
expect(created.statusCode).toBe(201);
|
|
await login(ctx.app, "[email protected]"); // the new account can sign in
|
|
|
|
const listed = await ctx.app.inject({ method: "GET", url, headers: bearer(admin.body.accessToken) });
|
|
expect(listed.json().map((m: { email: string }) => m.email)).toContain("[email protected]");
|
|
|
|
const asEditor = await ctx.app.inject({ method: "GET", url, headers: bearer(editor.body.accessToken) });
|
|
expect(asEditor.statusCode).toBe(403);
|
|
|
|
const escalate = await ctx.app.inject({
|
|
method: "POST",
|
|
url,
|
|
headers: bearer(admin.body.accessToken),
|
|
payload: { email: "[email protected]", name: "Boss", password: PASSWORD, role: "OWNER" },
|
|
});
|
|
expect(escalate.json().error).toBe("cannot_grant_higher_role");
|
|
});
|
|
|
|
it("never leaves an organization without an owner", async () => {
|
|
const { user, org } = await seedUser(ctx.db, "[email protected]", { org: "photoreport", role: "OWNER" });
|
|
const owner = await login(ctx.app, "[email protected]");
|
|
const demote = await ctx.app.inject({
|
|
method: "PATCH",
|
|
url: `/v1/orgs/${org?.id}/members/${user.id}`,
|
|
headers: bearer(owner.body.accessToken),
|
|
payload: { role: "ADMIN" },
|
|
});
|
|
expect(demote.json().error).toBe("last_owner");
|
|
const leave = await ctx.app.inject({
|
|
method: "DELETE",
|
|
url: `/v1/orgs/${org?.id}/members/${user.id}`,
|
|
headers: bearer(owner.body.accessToken),
|
|
});
|
|
expect(leave.json().error).toBe("last_owner");
|
|
});
|
|
|
|
it("keeps organizations isolated", async () => {
|
|
const { org: other } = await seedUser(ctx.db, "[email protected]", { org: "liga", role: "OWNER" });
|
|
await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "OWNER" });
|
|
const admin = await login(ctx.app, "[email protected]");
|
|
const res = await ctx.app.inject({ method: "GET", url: `/v1/orgs/${other?.id}/members`, headers: bearer(admin.body.accessToken) });
|
|
expect(res.statusCode).toBe(403);
|
|
});
|
|
});
|
|
|
|
describe("cors", () => {
|
|
it("allows the web app to use every method the API exposes", async () => {
|
|
for (const method of ["PUT", "PATCH", "DELETE"]) {
|
|
const res = await ctx.app.inject({
|
|
method: "OPTIONS",
|
|
url: "/v1/me/preferences",
|
|
headers: { origin: "http://localhost:5173", "access-control-request-method": method },
|
|
});
|
|
expect(res.statusCode).toBe(204);
|
|
expect(res.headers["access-control-allow-methods"]).toContain(method);
|
|
expect(res.headers["access-control-allow-credentials"]).toBe("true");
|
|
}
|
|
});
|
|
|
|
it("refuses unknown origins", async () => {
|
|
const res = await ctx.app.inject({
|
|
method: "OPTIONS",
|
|
url: "/v1/me",
|
|
headers: { origin: "https://evil.example", "access-control-request-method": "GET" },
|
|
});
|
|
expect(res.headers["access-control-allow-origin"]).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("edits contract", () => {
|
|
it("validates envelopes", async () => {
|
|
const res = await ctx.app.inject({ method: "POST", url: "/v1/edits/validate", payload: { edit: {} } });
|
|
expect(res.statusCode).toBe(400);
|
|
});
|
|
});
|