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

73 lines
1.9 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 /me", () => {
it("returns user with accounts", async () => {
const signup = await signupUser(app, "user@example.com", "My Org");
const { accessToken } = signup.json();
const res = await app.inject({
method: "GET",
url: "/me",
headers: { authorization: `Bearer ${accessToken}` },
});
expect(res.statusCode).toBe(200);
const body = res.json();
expect(body.email).toBe("user@example.com");
expect(body.accounts).toHaveLength(1);
expect(body.accounts[0].name).toBe("My Org");
});
it("returns 401 without auth", async () => {
const res = await app.inject({ method: "GET", url: "/me" });
expect(res.statusCode).toBe(401);
});
});
describe("PATCH /me", () => {
it("updates user name", async () => {
const signup = await signupUser(app, "user@example.com", "Org");
const { accessToken } = signup.json();
const res = await app.inject({
method: "PATCH",
url: "/me",
headers: { authorization: `Bearer ${accessToken}` },
payload: { name: "New Name" },
});
expect(res.statusCode).toBe(200);
expect(res.json().name).toBe("New Name");
});
it("returns 400 if nothing to update", async () => {
const signup = await signupUser(app, "user@example.com", "Org");
const { accessToken } = signup.json();
const res = await app.inject({
method: "PATCH",
url: "/me",
headers: { authorization: `Bearer ${accessToken}` },
payload: {},
});
expect(res.statusCode).toBe(400);
});
});