|
1 | 1 | import { createRequester } from '#src/utils/test-utils.js'; |
2 | 2 |
|
| 3 | +import { EnvSet } from '../env-set/index.js'; |
| 4 | + |
3 | 5 | import statusRoutes from './status.js'; |
4 | 6 |
|
5 | 7 | describe('status router', () => { |
6 | 8 | const requester = createRequester({ anonymousRoutes: statusRoutes }); |
7 | | - it('GET /status', async () => { |
8 | | - await expect(requester.get('/status')).resolves.toHaveProperty('status', 204); |
| 9 | + |
| 10 | + it('should respond with status 204', async () => { |
| 11 | + const response = await requester.get('/status'); |
| 12 | + |
| 13 | + expect(response.status).toBe(204); |
| 14 | + expect(response.headers).not.toHaveProperty('logto-tenant-id'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should not respond with tenant ID when no API key is set', async () => { |
| 18 | + const response = await requester.get('/status').set('logto-status-api-key', 'any-key'); |
| 19 | + |
| 20 | + expect(response.status).toBe(204); |
| 21 | + expect(response.headers).not.toHaveProperty('logto-tenant-id'); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('status router with API key set', () => { |
| 26 | + const testApiKey = 'test-status-api-key'; |
| 27 | + const originalApiKey = EnvSet.values.statusApiKey; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + // eslint-disable-next-line @silverhand/fp/no-mutating-methods |
| 31 | + Object.defineProperty(EnvSet.values, 'statusApiKey', { |
| 32 | + value: testApiKey, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + // Restore original API key |
| 38 | + // eslint-disable-next-line @silverhand/fp/no-mutating-methods |
| 39 | + Object.defineProperty(EnvSet.values, 'statusApiKey', { |
| 40 | + value: originalApiKey, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should respond with tenant ID when valid API key is provided', async () => { |
| 45 | + const requester = createRequester({ anonymousRoutes: statusRoutes }); |
| 46 | + |
| 47 | + const response = await requester.get('/status').set('logto-status-api-key', testApiKey); |
| 48 | + expect(response.headers).toHaveProperty('logto-tenant-id', 'mock_id'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should not respond with tenant ID when invalid API key is provided', async () => { |
| 52 | + const requester = createRequester({ anonymousRoutes: statusRoutes }); |
| 53 | + |
| 54 | + const response = await requester.get('/status').set('logto-status-api-key', 'invalid-api-key'); |
| 55 | + |
| 56 | + expect(response.status).toBe(204); |
| 57 | + expect(response.headers).not.toHaveProperty('logto-tenant-id'); |
9 | 58 | }); |
10 | 59 | }); |
0 commit comments