|
| 1 | +/** |
| 2 | + * Tests for src/routes/quotas/health.ts — GET /api/quotas/health |
| 3 | + * |
| 4 | + * Coverage targets (≥90% on changed lines): |
| 5 | + * |
| 6 | + * ✓ 200 + status "ok" when the database check succeeds |
| 7 | + * ✓ 503 + status "down" when the database check fails |
| 8 | + * ✓ response shape: { status, timestamp, dependencies: { database }, correlationId } |
| 9 | + * ✓ error messages are sanitized (no raw connection string / stack leakage) |
| 10 | + * ✓ correlation ID is echoed back when x-correlation-id is provided |
| 11 | + * ✓ a correlation ID is generated when none is provided |
| 12 | + * ✓ X-Correlation-Id response header is set |
| 13 | + * ✓ falls back to the shared app pool when no pool is injected |
| 14 | + * ✓ drain tracker middleware is applied (does not break normal responses) |
| 15 | + */ |
| 16 | + |
| 17 | +jest.mock('better-sqlite3', () => { |
| 18 | + return class MockDatabase { |
| 19 | + prepare() { return { get: () => null }; } |
| 20 | + exec() {} |
| 21 | + close() {} |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +import express from 'express'; |
| 26 | +import request from 'supertest'; |
| 27 | +import type { Pool, QueryResult } from 'pg'; |
| 28 | +import { createQuotaHealthRouter } from './health.js'; |
| 29 | +import { errorHandler } from '../../middleware/errorHandler.js'; |
| 30 | + |
| 31 | +function buildApp(pool?: Pool) { |
| 32 | + const app = express(); |
| 33 | + app.use(express.json()); |
| 34 | + app.use('/api/quotas/health', createQuotaHealthRouter(pool ? { pool } : {})); |
| 35 | + app.use(errorHandler); |
| 36 | + return app; |
| 37 | +} |
| 38 | + |
| 39 | +function createMockPool(queryResult: QueryResult | Error): Pool { |
| 40 | + return { |
| 41 | + query: async () => { |
| 42 | + if (queryResult instanceof Error) { |
| 43 | + throw queryResult; |
| 44 | + } |
| 45 | + return queryResult; |
| 46 | + }, |
| 47 | + } as unknown as Pool; |
| 48 | +} |
| 49 | + |
| 50 | +describe('GET /api/quotas/health', () => { |
| 51 | + it('returns 200 with status "ok" when the database check succeeds', async () => { |
| 52 | + const pool = createMockPool({ rows: [{ result: 1 }] } as QueryResult); |
| 53 | + const app = buildApp(pool); |
| 54 | + |
| 55 | + const res = await request(app).get('/api/quotas/health'); |
| 56 | + |
| 57 | + expect(res.status).toBe(200); |
| 58 | + expect(res.body.status).toBe('ok'); |
| 59 | + expect(res.body.timestamp).toEqual(expect.any(String)); |
| 60 | + expect(res.body.dependencies.database.status).toBe('ok'); |
| 61 | + expect(typeof res.body.dependencies.database.responseTime).toBe('number'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns 503 with status "down" when the database is unreachable', async () => { |
| 65 | + const pool = createMockPool(new Error('Connection refused')); |
| 66 | + const app = buildApp(pool); |
| 67 | + |
| 68 | + const res = await request(app).get('/api/quotas/health'); |
| 69 | + |
| 70 | + expect(res.status).toBe(503); |
| 71 | + expect(res.body.status).toBe('down'); |
| 72 | + expect(res.body.dependencies.database.status).toBe('down'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('sanitizes error messages to prevent leaking connection details', async () => { |
| 76 | + const pool = createMockPool( |
| 77 | + new Error('FATAL: connection to postgres://admin:s3cret@db.internal:5432/prod failed'), |
| 78 | + ); |
| 79 | + const app = buildApp(pool); |
| 80 | + |
| 81 | + const res = await request(app).get('/api/quotas/health'); |
| 82 | + |
| 83 | + expect(res.status).toBe(503); |
| 84 | + expect(res.body.dependencies.database.error).toBe('unavailable'); |
| 85 | + const body = JSON.stringify(res.body); |
| 86 | + expect(body).not.toContain('s3cret'); |
| 87 | + expect(body).not.toContain('db.internal'); |
| 88 | + expect(body).not.toContain('postgres://'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('only reports the database dependency (quotas has no other external dependency today)', async () => { |
| 92 | + const pool = createMockPool({ rows: [{ result: 1 }] } as QueryResult); |
| 93 | + const app = buildApp(pool); |
| 94 | + |
| 95 | + const res = await request(app).get('/api/quotas/health'); |
| 96 | + |
| 97 | + expect(Object.keys(res.body.dependencies)).toEqual(['database']); |
| 98 | + }); |
| 99 | + |
| 100 | + it('echoes the correlation ID when x-correlation-id is provided', async () => { |
| 101 | + const pool = createMockPool({ rows: [{ result: 1 }] } as QueryResult); |
| 102 | + const app = buildApp(pool); |
| 103 | + |
| 104 | + const res = await request(app) |
| 105 | + .get('/api/quotas/health') |
| 106 | + .set('x-correlation-id', 'corr-quota-health-1'); |
| 107 | + |
| 108 | + expect(res.status).toBe(200); |
| 109 | + expect(res.body.correlationId).toBe('corr-quota-health-1'); |
| 110 | + expect(res.headers['x-correlation-id']).toBe('corr-quota-health-1'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('generates a correlation ID when none is provided', async () => { |
| 114 | + const pool = createMockPool({ rows: [{ result: 1 }] } as QueryResult); |
| 115 | + const app = buildApp(pool); |
| 116 | + |
| 117 | + const res = await request(app).get('/api/quotas/health'); |
| 118 | + |
| 119 | + expect(res.status).toBe(200); |
| 120 | + expect(typeof res.body.correlationId).toBe('string'); |
| 121 | + expect(res.body.correlationId.length).toBeGreaterThan(0); |
| 122 | + expect(res.headers['x-correlation-id']).toBe(res.body.correlationId); |
| 123 | + }); |
| 124 | + |
| 125 | + it('falls back to the shared app pool when no pool is injected', async () => { |
| 126 | + // No pool override: the router falls back to src/db.ts's shared pool, |
| 127 | + // which is not reachable in this unit test environment, so the probe |
| 128 | + // should report the database as down rather than throwing. |
| 129 | + const app = express(); |
| 130 | + app.use(express.json()); |
| 131 | + app.use('/api/quotas/health', createQuotaHealthRouter()); |
| 132 | + app.use(errorHandler); |
| 133 | + |
| 134 | + const res = await request(app).get('/api/quotas/health'); |
| 135 | + |
| 136 | + expect([200, 503]).toContain(res.status); |
| 137 | + expect(res.body.dependencies.database).toBeDefined(); |
| 138 | + }, 10_000); |
| 139 | + |
| 140 | + it('applies the drain tracker middleware without breaking normal responses', async () => { |
| 141 | + const pool = createMockPool({ rows: [{ result: 1 }] } as QueryResult); |
| 142 | + const app = buildApp(pool); |
| 143 | + |
| 144 | + const res = await request(app).get('/api/quotas/health'); |
| 145 | + |
| 146 | + expect(res.status).toBe(200); |
| 147 | + expect(res.body.dependencies).toBeDefined(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments