|
| 1 | +import { assertEquals } from '@std/assert'; |
| 2 | +import { |
| 3 | + validateServiceRoleAuth, |
| 4 | + createUnauthorizedResponse, |
| 5 | + createServerErrorResponse, |
| 6 | +} from '../../../src/shared/authValidation.ts'; |
| 7 | +import { |
| 8 | + KNOWN_LOCAL_ANON_KEY, |
| 9 | + KNOWN_LOCAL_SERVICE_ROLE_KEY, |
| 10 | +} from '../../../src/shared/localDetection.ts'; |
| 11 | + |
| 12 | +// ============================================================ |
| 13 | +// Helper functions |
| 14 | +// ============================================================ |
| 15 | + |
| 16 | +function createRequest(authHeader?: string): Request { |
| 17 | + const headers = new Headers(); |
| 18 | + if (authHeader !== undefined) { |
| 19 | + headers.set('Authorization', authHeader); |
| 20 | + } |
| 21 | + return new Request('http://localhost/test', { headers }); |
| 22 | +} |
| 23 | + |
| 24 | +function localEnv(): Record<string, string | undefined> { |
| 25 | + return { |
| 26 | + SUPABASE_ANON_KEY: KNOWN_LOCAL_ANON_KEY, |
| 27 | + SUPABASE_SERVICE_ROLE_KEY: KNOWN_LOCAL_SERVICE_ROLE_KEY, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +function productionEnv(serviceRoleKey?: string): Record<string, string | undefined> { |
| 32 | + return { |
| 33 | + SUPABASE_ANON_KEY: 'production-anon-key-abc', |
| 34 | + SUPABASE_SERVICE_ROLE_KEY: serviceRoleKey, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +const PRODUCTION_SERVICE_ROLE_KEY = 'production-service-role-key-xyz'; |
| 39 | + |
| 40 | +// ============================================================ |
| 41 | +// validateServiceRoleAuth() - Local mode tests |
| 42 | +// ============================================================ |
| 43 | + |
| 44 | +Deno.test('validateServiceRoleAuth - local mode: allows request without auth header', () => { |
| 45 | + const request = createRequest(); |
| 46 | + const result = validateServiceRoleAuth(request, localEnv()); |
| 47 | + assertEquals(result, { valid: true }); |
| 48 | +}); |
| 49 | + |
| 50 | +Deno.test('validateServiceRoleAuth - local mode: allows request with wrong auth header', () => { |
| 51 | + const request = createRequest('Bearer wrong-key'); |
| 52 | + const result = validateServiceRoleAuth(request, localEnv()); |
| 53 | + assertEquals(result, { valid: true }); |
| 54 | +}); |
| 55 | + |
| 56 | +Deno.test('validateServiceRoleAuth - local mode: allows request with correct auth header', () => { |
| 57 | + const request = createRequest(`Bearer ${KNOWN_LOCAL_SERVICE_ROLE_KEY}`); |
| 58 | + const result = validateServiceRoleAuth(request, localEnv()); |
| 59 | + assertEquals(result, { valid: true }); |
| 60 | +}); |
| 61 | + |
| 62 | +// ============================================================ |
| 63 | +// validateServiceRoleAuth() - Production mode tests |
| 64 | +// ============================================================ |
| 65 | + |
| 66 | +Deno.test('validateServiceRoleAuth - production: rejects request without auth header', () => { |
| 67 | + const request = createRequest(); |
| 68 | + const result = validateServiceRoleAuth(request, productionEnv(PRODUCTION_SERVICE_ROLE_KEY)); |
| 69 | + assertEquals(result, { valid: false, error: 'Missing Authorization header' }); |
| 70 | +}); |
| 71 | + |
| 72 | +Deno.test('validateServiceRoleAuth - production: rejects request with wrong auth header', () => { |
| 73 | + const request = createRequest('Bearer wrong-key'); |
| 74 | + const result = validateServiceRoleAuth(request, productionEnv(PRODUCTION_SERVICE_ROLE_KEY)); |
| 75 | + assertEquals(result, { valid: false, error: 'Invalid Authorization header' }); |
| 76 | +}); |
| 77 | + |
| 78 | +Deno.test('validateServiceRoleAuth - production: accepts request with correct auth header', () => { |
| 79 | + const request = createRequest(`Bearer ${PRODUCTION_SERVICE_ROLE_KEY}`); |
| 80 | + const result = validateServiceRoleAuth(request, productionEnv(PRODUCTION_SERVICE_ROLE_KEY)); |
| 81 | + assertEquals(result, { valid: true }); |
| 82 | +}); |
| 83 | + |
| 84 | +Deno.test('validateServiceRoleAuth - production: rejects when service role key not configured', () => { |
| 85 | + const request = createRequest('Bearer any-key'); |
| 86 | + const result = validateServiceRoleAuth(request, productionEnv(undefined)); |
| 87 | + assertEquals(result, { valid: false, error: 'Server misconfigured: missing service role key' }); |
| 88 | +}); |
| 89 | + |
| 90 | +Deno.test('validateServiceRoleAuth - production: rejects Basic auth scheme', () => { |
| 91 | + const request = createRequest(`Basic ${PRODUCTION_SERVICE_ROLE_KEY}`); |
| 92 | + const result = validateServiceRoleAuth(request, productionEnv(PRODUCTION_SERVICE_ROLE_KEY)); |
| 93 | + assertEquals(result, { valid: false, error: 'Invalid Authorization header' }); |
| 94 | +}); |
| 95 | + |
| 96 | +Deno.test('validateServiceRoleAuth - production: rejects malformed Bearer token', () => { |
| 97 | + const request = createRequest('Bearer'); |
| 98 | + const result = validateServiceRoleAuth(request, productionEnv(PRODUCTION_SERVICE_ROLE_KEY)); |
| 99 | + assertEquals(result, { valid: false, error: 'Invalid Authorization header' }); |
| 100 | +}); |
| 101 | + |
| 102 | +Deno.test('validateServiceRoleAuth - production: rejects auth header without scheme', () => { |
| 103 | + const request = createRequest(PRODUCTION_SERVICE_ROLE_KEY); |
| 104 | + const result = validateServiceRoleAuth(request, productionEnv(PRODUCTION_SERVICE_ROLE_KEY)); |
| 105 | + assertEquals(result, { valid: false, error: 'Invalid Authorization header' }); |
| 106 | +}); |
| 107 | + |
| 108 | +// ============================================================ |
| 109 | +// createUnauthorizedResponse() tests |
| 110 | +// ============================================================ |
| 111 | + |
| 112 | +Deno.test('createUnauthorizedResponse - returns 401 status', () => { |
| 113 | + const response = createUnauthorizedResponse(); |
| 114 | + assertEquals(response.status, 401); |
| 115 | +}); |
| 116 | + |
| 117 | +Deno.test('createUnauthorizedResponse - returns JSON content type', () => { |
| 118 | + const response = createUnauthorizedResponse(); |
| 119 | + assertEquals(response.headers.get('Content-Type'), 'application/json'); |
| 120 | +}); |
| 121 | + |
| 122 | +Deno.test('createUnauthorizedResponse - returns error body', async () => { |
| 123 | + const response = createUnauthorizedResponse(); |
| 124 | + const body = await response.json(); |
| 125 | + assertEquals(body, { error: 'Unauthorized', message: 'Unauthorized' }); |
| 126 | +}); |
| 127 | + |
| 128 | +// ============================================================ |
| 129 | +// createServerErrorResponse() tests |
| 130 | +// ============================================================ |
| 131 | + |
| 132 | +Deno.test('createServerErrorResponse - returns 500 status', () => { |
| 133 | + const response = createServerErrorResponse(); |
| 134 | + assertEquals(response.status, 500); |
| 135 | +}); |
| 136 | + |
| 137 | +Deno.test('createServerErrorResponse - returns JSON content type', () => { |
| 138 | + const response = createServerErrorResponse(); |
| 139 | + assertEquals(response.headers.get('Content-Type'), 'application/json'); |
| 140 | +}); |
| 141 | + |
| 142 | +Deno.test('createServerErrorResponse - returns error body', async () => { |
| 143 | + const response = createServerErrorResponse(); |
| 144 | + const body = await response.json(); |
| 145 | + assertEquals(body, { error: 'Internal Server Error', message: 'Internal Server Error' }); |
| 146 | +}); |
0 commit comments