openvibers/openvibe.finance testapp.test.js
app.test.js
| 1 | ; |
| 2 | /** |
| 3 | * Finance end to end on a migrated PostgreSQL (PGlite, or the containers with FINANCE_TEST_STORE=pg): two people, |
| 4 | * accounts, typed and imported transactions, the same file twice, categories, the month, the API, export, deletion, |
| 5 | * and nobody reading another's rows. A stand-in signs people in with an x-test-user header. |
| 6 | */ |
| 7 | const assert = require('assert'); |
| 8 | const path = require('node:path'); |
| 9 | const crypto = require('node:crypto'); |
| 10 | const { createTestDb } = require('openvibe-sdk/testing'); |
| 11 | const { createVault } = require('../server/crypto'); |
| 12 | const { createApp } = require('../server/app'); |
| 13 | |
| 14 | const ADA = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAW'; |
| 15 | const BO = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAX'; |
| 16 | const SAME = { 'sec-fetch-site': 'same-origin' }; |
| 17 | |
| 18 | (async () => { |
| 19 | const { db, close } = await createTestDb({ migrations: path.join(__dirname, '..', 'migrations'), store: process.env.FINANCE_TEST_STORE || 'pglite', service: 'finance' }); |
| 20 | const auth = { |
| 21 | router: (express) => express.Router(), |
| 22 | optionalAuth: () => (req, _res, next) => { const u = req.get('x-test-user'); if (u) req.user = { subject_id: u }; next(); }, |
| 23 | }; |
| 24 | const app = createApp({ db, vault: createVault(crypto.randomBytes(32).toString('base64')), auth, now: () => new Date('2026-11-18T12:00:00Z') }); |
| 25 | const server = app.listen(0, '127.0.0.1'); |
| 26 | await new Promise((r) => server.once('listening', r)); |
| 27 | const base = `http://127.0.0.1:${server.address().port}`; |
| 28 | const go = async (p, { as = null, method = 'GET', form = null, json = null, text = null, headers = {} } = {}) => { |
| 29 | const h = { ...headers }; |
| 30 | if (as) h['x-test-user'] = as; |
| 31 | let body; |
| 32 | if (form) { h['content-type'] = 'application/x-www-form-urlencoded'; body = new URLSearchParams(form).toString(); } |
| 33 | if (json) { h['content-type'] = 'application/json'; body = JSON.stringify(json); } |
| 34 | if (text != null) { h['content-type'] = 'text/csv'; body = text; } |
| 35 | const r = await fetch(base + p, { method, headers: h, body, redirect: 'manual' }); |
| 36 | const t = await r.text(); |
| 37 | return { status: r.status, location: r.headers.get('location'), text: t, json: () => JSON.parse(t), headers: r.headers }; |
| 38 | }; |
| 39 | let failed = 0; |
| 40 | const check = async (name, fn) => { try { await fn(); console.log(` ✓ ${name}`); } catch (e) { failed++; console.log(` ✗ ${name}\n ${e.stack}`); } }; |
| 41 | |
| 42 | try { |
| 43 | let acc; |
| 44 | await check('signed out: what it is and its promises, never anyone\'s data; a page that needs you signs you in', async () => { |
| 45 | const home = await go('/'); |
| 46 | assert.strictEqual(home.status, 200); |
| 47 | assert.match(home.text, /Your money, your rules, your data/); |
| 48 | assert.match(home.text, /No money moves/); |
| 49 | assert.match(home.text, /href="\/finance\.css\?v=[0-9a-f]{10}"><script src="\/finance\.js\?v=[0-9a-f]{10}" defer>/, 'our files by their content'); |
| 50 | assert.strictEqual(home.headers.get('cache-control'), 'private, no-store'); |
| 51 | // The welcome is for search engines (the 11-23 crawl: a noindex header on it outweighed its "index, follow"). |
| 52 | assert.strictEqual(home.headers.get('x-robots-tag'), null, 'the signed-out welcome may be indexed'); |
| 53 | assert.match(home.text, /<meta name="robots" content="index, follow">/); |
| 54 | assert.strictEqual((await go('/', { as: ADA })).headers.get('x-robots-tag'), 'noindex, nofollow', 'a signed-in home (someone\'s money) never'); |
| 55 | const month = await go('/month'); |
| 56 | assert.strictEqual(month.headers.get('x-robots-tag'), 'noindex, nofollow', 'nor any other page'); |
| 57 | assert.strictEqual(month.status, 303); |
| 58 | assert.match(month.location, /^\/auth\/login\?next=/); |
| 59 | assert.strictEqual((await go('/api/v1/accounts')).status, 401); |
| 60 | }); |
| 61 | |
| 62 | await check('an account from the home page; its page shows the balance', async () => { |
| 63 | const r = await go('/accounts', { as: ADA, method: 'POST', headers: SAME, form: { name: 'Everyday checking', kind: 'checking', currency: 'usd', opening: '1,000.00' } }); |
| 64 | assert.strictEqual(r.status, 303, r.text); |
| 65 | acc = /^\/accounts\/(acc_[A-Za-z0-9_-]+)\?added=1$/.exec(r.location)[1]; |
| 66 | const page = await go(`/accounts/${acc}`, { as: ADA }); |
| 67 | assert.strictEqual(page.status, 200); |
| 68 | assert.match(page.text, /Everyday checking/); |
| 69 | assert.match(page.text, /\$1,000\.00/); |
| 70 | // With an account, the home page offers the money Experts (Actor #623), which read here only once allowed. |
| 71 | const home = await go('/', { as: ADA }); |
| 72 | assert.match(home.text, /Ask an Expert/); |
| 73 | assert.match(home.text, /href="https:\/\/openvibe\.actor\/experts\/bp_can_i_afford_it"/); |
| 74 | assert.match(home.text, /allow Actor in OpenVibe Finance on your <a href="https:\/\/openvibe\.network\/apps">Apps page<\/a>/); |
| 75 | assert.match(home.text, /the model you chose in Actor reads the numbers the Expert fetched/, 'said plainly which model reads them'); |
| 76 | }); |
| 77 | |
| 78 | await check('a typed transaction: money out by default, its payee sealed in the database', async () => { |
| 79 | const r = await go(`/accounts/${acc}/transactions`, { as: ADA, method: 'POST', headers: SAME, form: { posted_on: '2026-11-03', amount: '42.50', direction: 'out', payee: 'Corner Grocer', category: 'Groceries' } }); |
| 80 | assert.strictEqual(r.status, 303, r.text); |
| 81 | const row = await db.maybe('SELECT payee_enc, amount_cents, category, category_from FROM finance_transactions WHERE account_id = $1', [acc]); |
| 82 | assert.strictEqual(Number(row.amount_cents), -4250); |
| 83 | assert.ok(row.payee_enc.startsWith('v1.') && !row.payee_enc.includes('Corner'), 'stored sealed'); |
| 84 | assert.deepStrictEqual([row.category, row.category_from], ['Groceries', 'you']); |
| 85 | const page = await go(`/accounts/${acc}?m=2026-11`, { as: ADA }); |
| 86 | assert.match(page.text, /Corner Grocer/); |
| 87 | assert.match(page.text, /\$957\.50/, 'the balance moved'); |
| 88 | }); |
| 89 | |
| 90 | await check('a CSV import adds the new rows; the same file again adds nothing; two equal rows in one file stay two', async () => { |
| 91 | const csv = 'Date,Description,Amount\n2026-11-04,Coffee Bar,-4.50\n2026-11-04,Coffee Bar,-4.50\n2026-11-01,Paycheck,2500.00\n2026-10-28,Electric Co,-80.00\n'; |
| 92 | let r = await go(`/accounts/${acc}/import`, { as: ADA, method: 'POST', headers: SAME, form: { csv } }); |
| 93 | assert.strictEqual(r.status, 303, r.text); |
| 94 | assert.match(r.location, /imported=4,0$/); |
| 95 | r = await go(`/accounts/${acc}/import`, { as: ADA, method: 'POST', headers: SAME, form: { csv } }); |
| 96 | assert.match(r.location, /imported=0,4$/); |
| 97 | const page = await go(r.location, { as: ADA }); |
| 98 | assert.match(page.text, /Imported 0 new transactions; 4 already here/); |
| 99 | assert.strictEqual(Number(await db.value('SELECT count(*) FROM finance_transactions WHERE account_id = $1', [acc])), 5); |
| 100 | const bad = await go(`/accounts/${acc}/import`, { as: ADA, method: 'POST', headers: SAME, form: { csv: 'Description,Amount\nx,1\n' } }); |
| 101 | assert.strictEqual(bad.status, 422); |
| 102 | assert.match(bad.text, /No date column/); |
| 103 | }); |
| 104 | |
| 105 | await check('a category is corrected as a new revision; the month adds up by category, transfers left out', async () => { |
| 106 | const txns = (await go(`/api/v1/accounts/${acc}/transactions?m=2026-11`, { as: ADA })).json().transactions; |
| 107 | const coffee = txns.find((t) => t.payee === 'Coffee Bar'); |
| 108 | const r = await go(`/transactions/${coffee.id}/category`, { as: ADA, method: 'POST', headers: SAME, form: { category: 'Eating out' } }); |
| 109 | assert.strictEqual(r.status, 303); |
| 110 | const row = await db.maybe('SELECT category, category_from, revision FROM finance_transactions WHERE id = $1', [coffee.id]); |
| 111 | assert.deepStrictEqual([row.category, row.category_from, row.revision], ['Eating out', 'you', 2]); |
| 112 | const m = (await go('/api/v1/months/2026-11', { as: ADA })).json(); |
| 113 | assert.strictEqual(m.income_cents, 250000); |
| 114 | assert.strictEqual(m.spent_cents, 4250 + 450 + 450); |
| 115 | const page = await go('/month?m=2026-11', { as: ADA }); |
| 116 | assert.match(page.text, /November 2026/); |
| 117 | assert.match(page.text, /Groceries/); |
| 118 | }); |
| 119 | |
| 120 | await check('nobody reads or writes another person\'s rows', async () => { |
| 121 | assert.strictEqual((await go(`/accounts/${acc}`, { as: BO })).status, 404); |
| 122 | assert.strictEqual((await go(`/api/v1/accounts/${acc}/transactions`, { as: BO })).status, 404); |
| 123 | const sneak = await go(`/accounts/${acc}/transactions`, { as: BO, method: 'POST', headers: SAME, form: { posted_on: '2026-11-05', amount: '1', payee: 'x' } }); |
| 124 | assert.strictEqual(sneak.status, 404); |
| 125 | assert.deepStrictEqual((await go('/api/v1/accounts', { as: BO })).json().accounts, []); |
| 126 | assert.strictEqual((await go('/api/v1/months/2026-11', { as: BO })).json().spent_cents, 0); |
| 127 | }); |
| 128 | |
| 129 | await check('a cross-site form post is refused; the API takes a CSV body and JSON', async () => { |
| 130 | const cross = await go('/accounts', { as: ADA, method: 'POST', headers: { 'sec-fetch-site': 'cross-site' }, form: { name: 'x' } }); |
| 131 | assert.strictEqual(cross.status, 403); |
| 132 | const apiCross = await go('/api/v1/accounts', { as: ADA, method: 'POST', headers: { 'sec-fetch-site': 'cross-site' }, json: { name: 'x' } }); |
| 133 | assert.strictEqual(apiCross.status, 403); |
| 134 | const made = await go('/api/v1/accounts', { as: ADA, method: 'POST', headers: SAME, json: { name: 'Card', kind: 'credit' } }); |
| 135 | assert.strictEqual(made.status, 201, made.text); |
| 136 | const imp = await go(`/api/v1/accounts/${made.json().id}/import`, { as: ADA, method: 'POST', headers: SAME, text: 'Date,Payee,Amount\n2026-11-10,Bookshop,-15.00\nsoon,x,1\n' }); |
| 137 | assert.strictEqual(imp.status, 201, imp.text); |
| 138 | assert.strictEqual(imp.json().added, 1); |
| 139 | assert.strictEqual(imp.json().unreadable.length, 1); |
| 140 | }); |
| 141 | |
| 142 | await check('export is everything, readable; delete is everything, after a yes', async () => { |
| 143 | const ex = await go('/data/export', { as: ADA }); |
| 144 | assert.match(ex.headers.get('content-disposition'), /attachment; filename="openvibe-finance-2026-11\.json"/); |
| 145 | const data = ex.json(); |
| 146 | assert.strictEqual(data.accounts.length, 2); |
| 147 | assert.ok(data.transactions.some((t) => t.payee === 'Corner Grocer'), 'opened for its owner'); |
| 148 | const no = await go('/data/delete', { as: ADA, method: 'POST', headers: SAME, form: {} }); |
| 149 | assert.strictEqual(no.status, 422); |
| 150 | const yes = await go('/data/delete', { as: ADA, method: 'POST', headers: SAME, form: { sure: 'yes' } }); |
| 151 | assert.strictEqual(yes.status, 303); |
| 152 | assert.strictEqual(Number(await db.value("SELECT count(*) FROM finance_transactions WHERE owner = $1", [ADA])), 0); |
| 153 | assert.strictEqual(Number(await db.value("SELECT count(*) FROM finance_accounts WHERE owner = $1", [ADA])), 0); |
| 154 | }); |
| 155 | } finally { |
| 156 | server.close(); |
| 157 | await close(); |
| 158 | } |
| 159 | if (failed) { console.log(`${failed} failed`); process.exit(1); } |
| 160 | console.log('finance: accounts, imports, months, privacy, export and deletion'); |
| 161 | })().catch((err) => { console.error(err); process.exit(1); }); |
| 162 |