eyrun-api/tests/accounts.test.ts
2026-02-07 17:53:23 +01:00

87 lines
2.2 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll, beforeEach } from "vitest";
import type { FastifyInstance } from "fastify";
import { createTestApp, cleanDb, signupUser } from "./helpers.js";
let app: FastifyInstance;
beforeAll(async () => {
app = await createTestApp();
});
afterAll(async () => {
await app.close();
});
beforeEach(async () => {
await cleanDb();
});
describe("GET /accounts", () => {
it("returns user's accounts", async () => {
const signup = await signupUser(app, "user@example.com", "Org");
const { accessToken } = signup.json();
const res = await app.inject({
method: "GET",
url: "/accounts",
headers: { authorization: `Bearer ${accessToken}` },
});
expect(res.statusCode).toBe(200);
const body = res.json();
expect(body).toHaveLength(1);
expect(body[0].name).toBe("Org");
expect(body[0].role).toBe("owner");
});
it("returns 401 without auth", async () => {
const res = await app.inject({
method: "GET",
url: "/accounts",
});
expect(res.statusCode).toBe(401);
});
});
describe("POST /accounts", () => {
it("creates a new account and makes user owner", async () => {
const signup = await signupUser(app, "user@example.com", "Org 1");
const { accessToken } = signup.json();
const res = await app.inject({
method: "POST",
url: "/accounts",
headers: { authorization: `Bearer ${accessToken}` },
payload: { name: "Org 2" },
});
expect(res.statusCode).toBe(201);
expect(res.json().name).toBe("Org 2");
expect(res.json().role).toBe("owner");
// User should now have 2 accounts
const list = await app.inject({
method: "GET",
url: "/accounts",
headers: { authorization: `Bearer ${accessToken}` },
});
expect(list.json()).toHaveLength(2);
});
it("returns 400 if name is missing", async () => {
const signup = await signupUser(app, "user@example.com", "Org");
const { accessToken } = signup.json();
const res = await app.inject({
method: "POST",
url: "/accounts",
headers: { authorization: `Bearer ${accessToken}` },
payload: {},
});
expect(res.statusCode).toBe(400);
});
});