import { describe, expect, test } from 'bun:test' import { settingsRoute } from './settings' import { JWT_SECRET } from '../middleware/auth' import { jwt } from '@elysiajs/jwt' import { Elysia } from 'elysia' const signer = new Elysia().use(jwt({ name: 'jwt', secret: JWT_SECRET })) function jsonRequest(path: string, method: string, body: unknown, token?: string) { return new Request(`http://localhost${path}`, { method, headers: { 'content-type': 'application/json', ...(token ? { authorization: `Bearer ${token}` } : {}), }, body: JSON.stringify(body), }) } describe('footer layout route boundaries', () => { for (const method of ['PUT', 'POST']) { test(`${method} /settings requires admin authentication`, async () => { const response = await settingsRoute.handle(jsonRequest('/settings', method, { footerLayout: { columns: ['brand'] }, })) expect(response.status).toBe(401) expect(await response.json()).toEqual({ error: 'Unauthorized: Missing or invalid token format', }) }) test(`${method} /settings with admin token rejects footerLayout writes`, async () => { const adminToken = await (signer as any).decorator.jwt.sign({ role: 'ADMIN' }) const response = await settingsRoute.handle(jsonRequest('/settings', method, { footerLayout: { columns: ['brand'] }, }, adminToken)) expect(response.status).toBe(400) expect(await response.json()).toEqual({ error: 'Use PATCH /settings/footer-layout to update footer layout', }) }) } test('PATCH /settings/footer-layout requires admin authentication', async () => { const response = await settingsRoute.handle(jsonRequest('/settings/footer-layout', 'PATCH', { dryRun: true, })) expect(response.status).toBe(401) expect(await response.json()).toEqual({ error: 'Unauthorized: Missing or invalid token format', }) }) })