|
| 1 | +import { test, describe } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import request from 'supertest' |
| 4 | +import { createApp } from '../index.mjs' |
| 5 | +import { MemoryStore } from '../../apilib/store.mjs' |
| 6 | +import { OPENAPI } from '../../apilib/openapi.mjs' |
| 7 | + |
| 8 | +function buildApp(customStore) { |
| 9 | + const store = customStore || new MemoryStore() |
| 10 | + return { app: createApp(async () => store), store } |
| 11 | +} |
| 12 | + |
| 13 | +describe('Error Handling Middleware and Query Validation', () => { |
| 14 | + test('GET /api/vaults?status=bogus -> 400 JSON naming status; no Postgres query is issued', async () => { |
| 15 | + let listVaultsCalled = false |
| 16 | + const customStore = new MemoryStore() |
| 17 | + customStore.listVaults = async (...args) => { |
| 18 | + listVaultsCalled = true |
| 19 | + return MemoryStore.prototype.listVaults.apply(customStore, args) |
| 20 | + } |
| 21 | + |
| 22 | + const { app } = buildApp(customStore) |
| 23 | + const res = await request(app) |
| 24 | + .get('/api/vaults?status=bogus') |
| 25 | + .expect('Content-Type', /application\/json/) |
| 26 | + .expect(400) |
| 27 | + |
| 28 | + assert.equal(res.status, 400) |
| 29 | + assert.match(res.body.error, /status/i) |
| 30 | + assert(Array.isArray(res.body.details)) |
| 31 | + assert.match(res.body.details[0], /status/i) |
| 32 | + assert.equal(listVaultsCalled, false, 'Store listVaults should not be called on validation failure') |
| 33 | + }) |
| 34 | + |
| 35 | + test('GET /api/vaults?creator=notakey -> 400; ?creator=a&creator=b -> 400', async () => { |
| 36 | + const { app } = buildApp() |
| 37 | + |
| 38 | + const res1 = await request(app) |
| 39 | + .get('/api/vaults?creator=notakey') |
| 40 | + .expect('Content-Type', /application\/json/) |
| 41 | + .expect(400) |
| 42 | + |
| 43 | + assert.equal(res1.status, 400) |
| 44 | + assert.match(res1.body.error, /creator/i) |
| 45 | + assert(Array.isArray(res1.body.details)) |
| 46 | + |
| 47 | + const res2 = await request(app) |
| 48 | + .get('/api/vaults?creator=a&creator=b') |
| 49 | + .expect('Content-Type', /application\/json/) |
| 50 | + .expect(400) |
| 51 | + |
| 52 | + assert.equal(res2.status, 400) |
| 53 | + assert.match(res2.body.error, /creator/i) |
| 54 | + assert(Array.isArray(res2.body.details)) |
| 55 | + assert.match(res2.body.details[0], /array/i) |
| 56 | + }) |
| 57 | + |
| 58 | + test('GET /api/vaults?status= (empty string) is treated as absent', async () => { |
| 59 | + const { app } = buildApp() |
| 60 | + |
| 61 | + const res = await request(app) |
| 62 | + .get('/api/vaults?status=') |
| 63 | + .expect('Content-Type', /application\/json/) |
| 64 | + .expect(200) |
| 65 | + |
| 66 | + assert.equal(res.status, 200) |
| 67 | + assert(Array.isArray(res.body.data)) |
| 68 | + }) |
| 69 | + |
| 70 | + test('GET /api/transactions?vaultId=../../x -> 400', async () => { |
| 71 | + const { app } = buildApp() |
| 72 | + |
| 73 | + const res = await request(app) |
| 74 | + .get('/api/transactions?vaultId=../../x') |
| 75 | + .expect('Content-Type', /application\/json/) |
| 76 | + .expect(400) |
| 77 | + |
| 78 | + assert.equal(res.status, 400) |
| 79 | + assert.match(res.body.error, /vaultId/i) |
| 80 | + assert(Array.isArray(res.body.details)) |
| 81 | + }) |
| 82 | + |
| 83 | + test('POST /api/vaults with malformed JSON body -> 400 application/json { error: "Malformed JSON body" }', async () => { |
| 84 | + const { app } = buildApp() |
| 85 | + |
| 86 | + const res = await request(app) |
| 87 | + .post('/api/vaults') |
| 88 | + .set('Content-Type', 'application/json') |
| 89 | + .send('{bad') |
| 90 | + .expect('Content-Type', /application\/json/) |
| 91 | + .expect(400) |
| 92 | + |
| 93 | + assert.equal(res.status, 400) |
| 94 | + assert.deepEqual(res.body, { error: 'Malformed JSON body' }) |
| 95 | + }) |
| 96 | + |
| 97 | + test('A 101kb body -> 413 JSON', async () => { |
| 98 | + const { app } = buildApp() |
| 99 | + const largeBody = JSON.stringify({ data: 'x'.repeat(1024 * 101) }) |
| 100 | + |
| 101 | + const res = await request(app) |
| 102 | + .post('/api/vaults') |
| 103 | + .set('Content-Type', 'application/json') |
| 104 | + .send(largeBody) |
| 105 | + .expect('Content-Type', /application\/json/) |
| 106 | + .expect(413) |
| 107 | + |
| 108 | + assert.equal(res.status, 413) |
| 109 | + assert.equal(res.body.error, 'Payload Too Large') |
| 110 | + }) |
| 111 | + |
| 112 | + test('A text/plain POST -> 415 JSON', async () => { |
| 113 | + const { app } = buildApp() |
| 114 | + |
| 115 | + const res = await request(app) |
| 116 | + .post('/api/vaults') |
| 117 | + .set('Content-Type', 'text/plain') |
| 118 | + .send('hello world') |
| 119 | + .expect('Content-Type', /application\/json/) |
| 120 | + .expect(415) |
| 121 | + |
| 122 | + assert.equal(res.status, 415) |
| 123 | + assert.match(res.body.error, /Unsupported Media Type/i) |
| 124 | + }) |
| 125 | + |
| 126 | + test('DELETE /api/vaults -> 405 JSON with an Allow header', async () => { |
| 127 | + const { app } = buildApp() |
| 128 | + |
| 129 | + const res = await request(app) |
| 130 | + .delete('/api/vaults') |
| 131 | + .expect('Content-Type', /application\/json/) |
| 132 | + .expect(405) |
| 133 | + |
| 134 | + assert.equal(res.status, 405) |
| 135 | + assert.equal(res.headers.allow, 'GET, POST') |
| 136 | + assert.match(res.body.error, /Method Not Allowed/i) |
| 137 | + }) |
| 138 | + |
| 139 | + test('DELETE /api/transactions -> 405 JSON with an Allow header', async () => { |
| 140 | + const { app } = buildApp() |
| 141 | + |
| 142 | + const res = await request(app) |
| 143 | + .delete('/api/transactions') |
| 144 | + .expect('Content-Type', /application\/json/) |
| 145 | + .expect(405) |
| 146 | + |
| 147 | + assert.equal(res.status, 405) |
| 148 | + assert.equal(res.headers.allow, 'GET') |
| 149 | + assert.match(res.body.error, /Method Not Allowed/i) |
| 150 | + }) |
| 151 | + |
| 152 | + test('Thrown non-Error values (strings) inside handlers still yield JSON 500 without leaking stack', async () => { |
| 153 | + const customStore = new MemoryStore() |
| 154 | + customStore.listVaults = async () => { |
| 155 | + throw 'A non-Error string failure' |
| 156 | + } |
| 157 | + |
| 158 | + const { app } = buildApp(customStore) |
| 159 | + const res = await request(app) |
| 160 | + .get('/api/vaults') |
| 161 | + .expect('Content-Type', /application\/json/) |
| 162 | + .expect(500) |
| 163 | + |
| 164 | + assert.equal(res.status, 500) |
| 165 | + assert.deepEqual(res.body, { error: 'Internal error' }) |
| 166 | + }) |
| 167 | + |
| 168 | + test('Thrown Error inside handlers does not leak err.message in responses', async () => { |
| 169 | + const customStore = new MemoryStore() |
| 170 | + customStore.listVaults = async () => { |
| 171 | + throw new Error('Secret postgres connection error or stack trace') |
| 172 | + } |
| 173 | + |
| 174 | + const { app } = buildApp(customStore) |
| 175 | + const res = await request(app) |
| 176 | + .get('/api/vaults') |
| 177 | + .expect('Content-Type', /application\/json/) |
| 178 | + .expect(500) |
| 179 | + |
| 180 | + assert.equal(res.status, 500) |
| 181 | + assert.deepEqual(res.body, { error: 'Internal error' }) |
| 182 | + }) |
| 183 | + |
| 184 | + test('Parametrised test: every route in OPENAPI.paths with garbage input asserts JSON content-type and status < 500', async () => { |
| 185 | + process.env.CRON_SECRET = 'test-secret' |
| 186 | + const { app } = buildApp() |
| 187 | + const paths = Object.keys(OPENAPI.paths) |
| 188 | + |
| 189 | + for (const p of paths) { |
| 190 | + // Substitute path variables like :id, :mid with test string |
| 191 | + const testPath = '/api' + p.replace(/\{([^}]+)\}/g, 'test-$1') |
| 192 | + |
| 193 | + // 1. GET with garbage query params |
| 194 | + const resGet = await request(app) |
| 195 | + .get(`${testPath}?status=bogus&creator=bogus&vaultId=bogus/bad/path&junk=<script>`) |
| 196 | + .set('Authorization', 'Bearer test-secret') |
| 197 | + |
| 198 | + const getContentType = resGet.headers['content-type'] || '' |
| 199 | + assert(!/text\/html/i.test(getContentType), `Route GET ${testPath} should not return HTML`) |
| 200 | + assert.match(getContentType, /application\/json/, `Route GET ${testPath} should return JSON`) |
| 201 | + assert(resGet.status < 500, `Route GET ${testPath} returned ${resGet.status} >= 500 with body: ${JSON.stringify(resGet.body)}`) |
| 202 | + |
| 203 | + // 2. POST with garbage body |
| 204 | + const resPost = await request(app) |
| 205 | + .post(testPath) |
| 206 | + .set('Authorization', 'Bearer test-secret') |
| 207 | + .set('Content-Type', 'application/json') |
| 208 | + .send({ garbage: true, status: 'invalid', creator: 'not-a-key' }) |
| 209 | + |
| 210 | + const postContentType = resPost.headers['content-type'] || '' |
| 211 | + assert(!/text\/html/i.test(postContentType), `Route POST ${testPath} should not return HTML`) |
| 212 | + assert.match(postContentType, /application\/json/, `Route POST ${testPath} should return JSON`) |
| 213 | + assert(resPost.status < 500, `Route POST ${testPath} returned ${resPost.status} >= 500 with body: ${JSON.stringify(resPost.body)}`) |
| 214 | + } |
| 215 | + }) |
| 216 | +}) |
0 commit comments