Files
korting/test/app.test.js
T
Sjoerd de Vries 7f378115b5 fix: guard the async /check-now handler against crashing (finding 8)
Express 4 geeft een afgewezen promise uit een async handler niet door aan
de error-handling; met Node 22 crashte daardoor het hele proces. De
handler vangt de fout nu zelf af, logt hem en antwoordt met een 500.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013gwc3aCiyQxmCWQke8RvEz
2026-09-05 15:53:20 +02:00

88 lines
3.2 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const request = require('supertest');
const { createApp } = require('../src/app');
function makeDeps(overrides = {}) {
let products = [];
let nextId = 1;
return {
listProducts: () => products,
addProduct: ({ name, url }) => {
const product = { id: nextId++, name, url, last_price: null, last_checked_at: null, last_check_status: null };
products.push(product);
return product;
},
deleteProduct: (id) => { products = products.filter((p) => p.id !== id); },
checkAllProducts: async () => [],
authUser: 'admin',
authPass: 'secret',
...overrides,
};
}
test('rejects requests without basic auth credentials', async () => {
const app = createApp(makeDeps());
const res = await request(app).get('/');
assert.equal(res.status, 401);
});
test('lists products for authenticated requests', async () => {
const deps = makeDeps();
deps.addProduct({ name: 'Voorbeeld', url: 'https://example.com' });
const app = createApp(deps);
const res = await request(app).get('/').auth('admin', 'secret');
assert.equal(res.status, 200);
assert.match(res.text, /Voorbeeld/);
});
test('adding a product redirects to the list', async () => {
const deps = makeDeps();
const app = createApp(deps);
const res = await request(app)
.post('/products')
.auth('admin', 'secret')
.type('form')
.send({ name: 'Nieuw product', url: 'https://shop.example/p' });
assert.equal(res.status, 302);
assert.equal(res.headers.location, '/');
const list = await request(app).get('/').auth('admin', 'secret');
assert.match(list.text, /Nieuw product/);
});
test('deleting a product removes it from the list', async () => {
const deps = makeDeps();
const product = deps.addProduct({ name: 'Weg ermee', url: 'https://shop.example/p2' });
const app = createApp(deps);
const res = await request(app).post(`/products/${product.id}/delete`).auth('admin', 'secret');
assert.equal(res.status, 302);
const list = await request(app).get('/').auth('admin', 'secret');
assert.doesNotMatch(list.text, /Weg ermee/);
});
test('check-now triggers checkAllProducts and redirects with confirmation', async () => {
let called = false;
const deps = makeDeps({ checkAllProducts: async () => { called = true; return []; } });
const app = createApp(deps);
const res = await request(app).post('/check-now').auth('admin', 'secret');
assert.equal(res.status, 302);
assert.equal(res.headers.location, '/?checked=1');
assert.equal(called, true);
});
test('check-now answers 500 when the check fails, without crashing the process', async (t) => {
t.mock.method(console, 'error', () => {});
const deps = makeDeps({
checkAllProducts: async () => { throw new Error('db weg'); },
});
deps.addProduct({ name: 'Nog steeds hier', url: 'https://shop.example/p3' });
const app = createApp(deps);
const res = await request(app).post('/check-now').auth('admin', 'secret');
assert.equal(res.status, 500);
assert.match(res.text, /Er ging iets mis/);
// Same process, same app instance: proves nothing crashed.
const list = await request(app).get('/').auth('admin', 'secret');
assert.equal(list.status, 200);
assert.match(list.text, /Nog steeds hier/);
});