studio.test.ts39 lines · main
| 1 | const ORIGINAL_SECRET = process.env.BRIVEN_BETTER_AUTH_SECRET; |
| 2 | process.env.BRIVEN_BETTER_AUTH_SECRET = ORIGINAL_SECRET ?? 'a'.repeat(32); |
| 3 | |
| 4 | import type { AppEnv } from '../types/app-env.js'; |
| 5 | import { describe, expect, it, afterAll } from 'bun:test'; |
| 6 | import { Hono } from 'hono'; |
| 7 | |
| 8 | describe('GET /v1/projects/:id/studio/schema-export', () => { |
| 9 | it('handler is mounted and responds (auth/db permitting)', async () => { |
| 10 | const { studioRouter } = await import('./studio.js'); |
| 11 | const { signCliToken } = await import('../lib/cli-jwt.js'); |
| 12 | |
| 13 | const app = new Hono<AppEnv>(); |
| 14 | app.route('/', studioRouter); |
| 15 | |
| 16 | const token = await signCliToken('u_schema_export_test'); |
| 17 | const res = await app.request('/v1/projects/p_test/studio/schema-export', { |
| 18 | headers: { authorization: `Bearer ${token}` }, |
| 19 | }); |
| 20 | // Post-A8: CLI JWT now traverses requireProjectAuth. A 401 here is |
| 21 | // only acceptable if it comes from the new cli-jwt branch (e.g. |
| 22 | // "invalid cli token" / "cli token user not found") — never from |
| 23 | // the old `brk_` rejection. The message guard proves which path ran. |
| 24 | if (res.status === 401) { |
| 25 | const body = (await res.json()) as { message?: string }; |
| 26 | expect(body.message ?? '').not.toMatch(/brk_/i); |
| 27 | } |
| 28 | expect([200, 401, 403, 500]).toContain(res.status); |
| 29 | if (res.status === 200) { |
| 30 | const body = (await res.json()) as { schemaTs: string }; |
| 31 | expect(body.schemaTs).toContain('export default schema('); |
| 32 | } |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | afterAll(() => { |
| 37 | if (ORIGINAL_SECRET === undefined) delete process.env.BRIVEN_BETTER_AUTH_SECRET; |
| 38 | else process.env.BRIVEN_BETTER_AUTH_SECRET = ORIGINAL_SECRET; |
| 39 | }); |