/** * stacksjs/stacks#3576: the generated OpenAPI document described every * endpoint as unauthenticated. Security now comes from route middleware. */ import { describe, expect, test } from 'bun:test' import { defaultSecuritySchemes, operationSecurity, securityResponses } from '../src/openapi-security' describe('operationSecurity()', () => { test('a public route is marked as needing no security', () => { expect(operationSecurity([])).toMatchObject({ security: [], authenticated: false }) expect(operationSecurity(undefined).security).toEqual([]) expect(operationSecurity(['throttle:60,1', 'guest']).security).toEqual([]) }) test('auth documents the bearer token, with the login cookie as the alternative', () => { expect(operationSecurity(['auth']).security).toEqual([{ bearerAuth: [] }, { cookieAuth: [] }]) }) test('abilities become required scopes, and a 403', () => { const security = operationSecurity(['auth', 'abilities:places:read,places:write']) expect(security.scopes).toEqual(['places:read', 'places:write']) expect(securityResponses(security)).toEqual({ 401: { description: expect.any(String) }, 403: { description: expect.any(String) }, }) }) test('throttled routes document a 429', () => { expect(securityResponses(operationSecurity(['auth', 'throttle:60,1']))).toHaveProperty('429') }) test('negated middleware adds no requirement', () => { expect(operationSecurity(['!auth']).security).toEqual([]) }) test('an app maps its own middleware, and two requirements must both be met', () => { const config = { middleware: { 'api-key': 'apiKeyAuth', 'internal': null } } expect(operationSecurity(['api-key'], config).security).toEqual([{ apiKeyAuth: [] }]) expect(operationSecurity(['internal'], config).security).toEqual([]) expect(operationSecurity(['auth', 'api-key'], config).security).toEqual([ { bearerAuth: [], apiKeyAuth: [] }, { cookieAuth: [], apiKeyAuth: [] }, ]) }) test('the framework schemes name the login cookie', () => { expect(defaultSecuritySchemes('session')).toMatchObject({ bearerAuth: { type: 'http', scheme: 'bearer' }, cookieAuth: { type: 'apiKey', in: 'cookie', name: 'session' }, }) }) }) describe('the committed document', () => { // The generated artifact CI regenerates and compares byte for byte, read as // a snapshot of mixed public and protected routes. const spec = JSON.parse(require('node:fs').readFileSync(new URL('../../../api/openapi.json', import.meta.url), 'utf8')) as { paths: Record }>> components: { securitySchemes?: Record } } test('declares the schemes it uses', () => { expect(spec.components.securitySchemes).toMatchObject({ bearerAuth: { type: 'http', scheme: 'bearer' }, cookieAuth: { type: 'apiKey', in: 'cookie' }, }) }) test('documents a route behind auth with the bearer scheme and a 401', () => { const switchTeam = spec.paths['/api/teams/{id}/switch']!.post! expect(switchTeam.security).toEqual([{ bearerAuth: [] }, { cookieAuth: [] }]) expect(switchTeam.responses).toHaveProperty('401') }) test('marks a public route as needing no security', () => { expect(spec.paths['/api/contact']!.post!.security).toEqual([]) }) test('gives every operation an explicit security list', () => { const missing = Object.entries(spec.paths).flatMap(([path, item]) => Object.entries(item).filter(([, op]) => !Array.isArray(op.security)).map(([method]) => `${method} ${path}`)) expect(missing).toEqual([]) }) }) describe('API keys (stacksjs/stacks#3555)', () => { test('the api middleware documents the key header, and its scopes', () => { const security = operationSecurity(['api:geocode.read']) expect(security.security).toEqual([{ apiKeyAuth: [] }]) expect(security.scopes).toEqual(['geocode.read']) expect(defaultSecuritySchemes('auth-token', 'X-Maps-Key').apiKeyAuth).toMatchObject({ type: 'apiKey', in: 'header', name: 'X-Maps-Key' }) }) })