Files
logbuch/tests/api/projects.test.ts
T
Matthias Giesselmann b90ff252d1 Add Logbuch: project update blog with admin, media and API
Public archive with sidebar navigation, project pages, month archive,
search and entry pages with image blocks. Admin area for brands,
projects, post types, users, api clients, media and the entry editor
with drag and drop images, preview per audience, scheduling and
publish checks. Read and write API with bearer tokens, audience
scoping, idempotent creation, OpenAPI document and editorial guide.
Magic link login with configurable allowed domains, whole app behind
the session gate. 456 tests including design rule checks.
2026-07-31 21:33:42 +02:00

53 lines
2.0 KiB
TypeScript

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'])
})
})