import { describe, expect, it } from 'vitest' import { db } from '~/data/db' import { apiClients, brands, projects } from '~/data/schema' import { hashToken } from '~/lib/api-auth' import { GET as getProjects } from '~/app/api/v1/projects/route' async function seed() { const [brand] = await db.insert(brands).values({ slug: 'pocket-rocket', name: 'Pocket Rocket' }).returning() const [trakk] = await db.insert(projects).values({ brandId: brand!.id, slug: 'trakk', name: 'Trakk', sort: 1 }).returning() await db.insert(projects).values([ { brandId: brand!.id, slug: 'mta360', name: 'MTA360', sort: 2 }, { brandId: brand!.id, slug: 'altprojekt', name: 'Altprojekt', isActive: false, sort: 3 }, ]) await db.insert(apiClients).values([ { name: 'Trakk', tokenHash: hashToken('lb_trakk'), mode: 'read', scope: 'customer', projectId: trakk!.id }, { name: 'Intern', tokenHash: hashToken('lb_intern'), mode: 'read', scope: 'internal' }, ]) } function request(url: string, token?: string) { return new Request(url, { headers: token ? { authorization: `Bearer ${token}` } : {} }) } describe('GET /api/v1/projects', () => { it('antwortet 401 ohne Token', async () => { await seed() const response = await getProjects(request('http://localhost/api/v1/projects')) expect(response.status).toBe(401) }) it('gibt einem projektgebundenen Client nur sein eigenes Projekt', async () => { await seed() const response = await getProjects(request('http://localhost/api/v1/projects', 'lb_trakk')) const body = await response.json() expect(response.status).toBe(200) expect(body.items.map((item: { slug: string }) => item.slug)).toEqual(['trakk']) }) it('gibt einem Client ohne Projektbindung alle aktiven Projekte', async () => { await seed() const response = await getProjects(request('http://localhost/api/v1/projects', 'lb_intern')) const body = await response.json() expect(body.items.map((item: { slug: string }) => item.slug)).toEqual(['trakk', 'mta360']) }) })