Repository navigation
Expand file tree
/
Copy pathapi.test.js
More file actions
99 lines (86 loc) · 3.62 KB
/
Copy pathapi.test.js
File metadata and controls
99 lines (86 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const { createApp } = require('../src/app');
let server;
let base;
test.before(async () => {
server = createApp().listen(0);
await new Promise((r) => server.once('listening', r));
base = `http://127.0.0.1:${server.address().port}`;
});
test.after(() => server.close());
test('GET /health is ok', async () => {
const res = await fetch(`${base}/health`);
assert.equal(res.status, 200);
assert.equal((await res.json()).status, 'ok');
});
test('POST /api/split returns shares', async () => {
const res = await fetch(`${base}/api/split`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ total: 90, guests: [{ name: 'A' }, { name: 'B' }, { name: 'C' }] })
});
assert.equal(res.status, 200);
assert.deepEqual((await res.json()).shares.map((s) => s.amount), [30, 30, 30]);
});
test('POST /api/split validates input', async () => {
const res = await fetch(`${base}/api/split`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ total: 0, guests: [] })
});
assert.equal(res.status, 400);
});
test('bad JSON gives 400', async () => {
const res = await fetch(`${base}/api/split`, {
method: 'POST', headers: { 'content-type': 'application/json' }, body: '{oops'
});
assert.equal(res.status, 400);
});
test('landing page is served', async () => {
const res = await fetch(`${base}/`);
assert.equal(res.status, 200);
assert.match(await res.text(), /Evenly/);
});
test('unknown route is 404', async () => {
assert.equal((await fetch(`${base}/nope`)).status, 404);
});
test('POST /api/receipt/parse returns items', async () => {
const res = await fetch(`${base}/api/receipt/parse`, {
method: 'POST', headers: { 'content-type': 'application/json' },
body: JSON.stringify({ text: 'Burger 62.00\nTOTAL 62.00' })
});
assert.equal(res.status, 200);
assert.equal((await res.json()).items[0].price, 62);
});
test('group room flow over HTTP', async () => {
const post = (u, b) => fetch(`${base}${u}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(b) });
const room = await (await post('/api/rooms', { items: [{ name: 'Pizza', price: 50 }] })).json();
const after = await (await post(`/api/rooms/${room.id}/claim`, { name: 'Noya', itemId: 1 })).json();
assert.equal(after.totals[0].amount, 50);
assert.equal((await fetch(`${base}/api/rooms/zzzzzzz`)).status, 400);
});
test('POST /api/settle', async () => {
const res = await fetch(`${base}/api/settle`, {
method: 'POST', headers: { 'content-type': 'application/json' },
body: JSON.stringify({ people: ['A', 'B'], expenses: [{ amount: 100, payer: 'A' }] })
});
assert.deepEqual((await res.json()).transfers, [{ from: 'B', to: 'A', amount: 50 }]);
});
test('responses carry security headers', async () => {
const res = await fetch(`${base}/health`);
assert.equal(res.headers.get('x-content-type-options'), 'nosniff');
assert.equal(res.headers.get('x-frame-options'), 'SAMEORIGIN');
});
test('POST /api is rate limited per IP', async () => {
const s = createApp({ limit: { windowMs: 60_000, max: 3 } }).listen(0);
await new Promise((r) => s.once('listening', r));
const url = `http://127.0.0.1:${s.address().port}/api/settle`;
const post = () => fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' });
for (let i = 0; i < 3; i++) assert.notEqual((await post()).status, 429);
const res = await post();
assert.equal(res.status, 429);
assert.ok(res.headers.get('retry-after'));
s.close();
});