openvibers/openvibe.finance testofx.test.js
ofx.test.js
| 1 | ; |
| 2 | /** |
| 3 | * OFX and QFX downloads (server/ofx.js): OFX 1.x (SGML, open leaf tags) and 2.x (XML), bank and credit-card statements, |
| 4 | * dates with times and zones, escaped names; FITIDs make the same download twice, and two downloads that overlap, add |
| 5 | * each transaction once; the page's import takes OFX as it takes CSV. |
| 6 | */ |
| 7 | const assert = require('assert'); |
| 8 | const path = require('node:path'); |
| 9 | const crypto = require('node:crypto'); |
| 10 | const { isOfx, readOfx, ofxDate } = require('../server/ofx'); |
| 11 | |
| 12 | const OFX1 = `OFXHEADER:100 |
| 13 | DATA:OFXSGML |
| 14 | VERSION:102 |
| 15 | SECURITY:NONE |
| 16 | ENCODING:USASCII |
| 17 | |
| 18 | <OFX> |
| 19 | <SIGNONMSGSRSV1><SONRS><STATUS><CODE>0<SEVERITY>INFO</STATUS><DTSERVER>20261118120000</SONRS></SIGNONMSGSRSV1> |
| 20 | <BANKMSGSRSV1><STMTTRNRS><STMTRS><CURDEF>USD |
| 21 | <BANKTRANLIST> |
| 22 | <STMTTRN> |
| 23 | <TRNTYPE>DEBIT |
| 24 | <DTPOSTED>20261102120000.000[-5:EST] |
| 25 | <TRNAMT>-23.18 |
| 26 | <FITID>2026110200001 |
| 27 | <NAME>CORNER GROCER & DELI |
| 28 | <MEMO>POS PURCHASE |
| 29 | </STMTTRN> |
| 30 | <STMTTRN> |
| 31 | <TRNTYPE>CREDIT |
| 32 | <DTPOSTED>20261101 |
| 33 | <TRNAMT>3120.00 |
| 34 | <FITID>2026110100007 |
| 35 | <NAME>ACME CORP PAYROLL |
| 36 | </STMTTRN> |
| 37 | <STMTTRN> |
| 38 | <TRNTYPE>DEBIT |
| 39 | <DTPOSTED>20261302 |
| 40 | <TRNAMT>-1.00 |
| 41 | <FITID>bad-date |
| 42 | <NAME>NOWHERE |
| 43 | </STMTTRN> |
| 44 | </BANKTRANLIST> |
| 45 | </STMTRS></STMTTRNRS></BANKMSGSRSV1> |
| 46 | </OFX>`; |
| 47 | |
| 48 | const OFX2 = `<?xml version="1.0" encoding="UTF-8"?> |
| 49 | <?OFX OFXHEADER="200" VERSION="211" SECURITY="NONE"?> |
| 50 | <OFX><CREDITCARDMSGSRSV1><CCSTMTTRNRS><CCSTMTRS><CURDEF>USD</CURDEF><BANKTRANLIST> |
| 51 | <STMTTRN><TRNTYPE>DEBIT</TRNTYPE><DTPOSTED>20261107</DTPOSTED><TRNAMT>-412.20</TRNAMT><FITID>CC-77</FITID><NAME>AIRLINE TICKETS</NAME></STMTTRN> |
| 52 | <STMTTRN><TRNTYPE>DEBIT</TRNTYPE><DTPOSTED>20261107</DTPOSTED><TRNAMT>-412.20</TRNAMT><FITID>CC-78</FITID><NAME>AIRLINE TICKETS</NAME></STMTTRN> |
| 53 | </BANKTRANLIST></CCSTMTRS></CCSTMTTRNRS></CREDITCARDMSGSRSV1></OFX>`; |
| 54 | |
| 55 | assert.ok(isOfx(OFX1) && isOfx(OFX2), 'both versions are OFX'); |
| 56 | assert.ok(!isOfx('Date,Description,Amount\n2026-11-01,x,1\n'), 'a CSV is not'); |
| 57 | assert.strictEqual(ofxDate('20261118120000.000[-5:EST]'), '2026-11-18'); |
| 58 | assert.strictEqual(ofxDate('20261302'), null); |
| 59 | |
| 60 | const one = readOfx(OFX1); |
| 61 | assert.strictEqual(one.currency, 'USD'); |
| 62 | assert.deepStrictEqual(one.rows, [ |
| 63 | { posted_on: '2026-11-02', amount_cents: -2318, payee: 'CORNER GROCER & DELI', memo: 'POS PURCHASE', category: '', fitid: '2026110200001' }, |
| 64 | { posted_on: '2026-11-01', amount_cents: 312000, payee: 'ACME CORP PAYROLL', memo: '', category: '', fitid: '2026110100007' }, |
| 65 | ]); |
| 66 | assert.deepStrictEqual(one.skipped, [{ line: 3, why: 'no posted date' }]); |
| 67 | const two = readOfx(OFX2); |
| 68 | assert.deepStrictEqual(two.rows.map((r) => [r.posted_on, r.amount_cents, r.fitid]), [['2026-11-07', -41220, 'CC-77'], ['2026-11-07', -41220, 'CC-78']]); |
| 69 | assert.match(readOfx('<OFX></OFX>').problem, /No transactions/); |
| 70 | |
| 71 | (async () => { |
| 72 | const { createTestDb } = require('openvibe-sdk/testing'); |
| 73 | const { createVault } = require('../server/crypto'); |
| 74 | const { createStore } = require('../server/store'); |
| 75 | const { createApp } = require('../server/app'); |
| 76 | const { db, close } = await createTestDb({ migrations: path.join(__dirname, '..', 'migrations'), store: process.env.FINANCE_TEST_STORE || 'pglite', service: 'finance' }); |
| 77 | const ADA = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAW'; |
| 78 | const vault = createVault(crypto.randomBytes(32).toString('base64')); |
| 79 | const store = createStore({ db, vault }); |
| 80 | const auth = { router: (express) => express.Router(), optionalAuth: () => (req, _res, next) => { const u = req.get('x-test-user'); if (u) req.user = { subject_id: u }; next(); } }; |
| 81 | const server = createApp({ db, vault, auth }).listen(0, '127.0.0.1'); |
| 82 | await new Promise((r) => server.once('listening', r)); |
| 83 | const base = `http://127.0.0.1:${server.address().port}`; |
| 84 | const importForm = (accId, text) => fetch(`${base}/accounts/${accId}/import`, { method: 'POST', redirect: 'manual', headers: { 'sec-fetch-site': 'same-origin', 'x-test-user': ADA, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ csv: text }).toString() }) |
| 85 | .then(async (r) => ({ status: r.status, location: r.headers.get('location'), text: await r.text() })); |
| 86 | try { |
| 87 | const checking = await store.addAccount(ADA, { name: 'Checking' }); |
| 88 | let r = await importForm(checking.id, OFX1); |
| 89 | assert.strictEqual(r.status, 303, r.text); |
| 90 | assert.match(r.location, /imported=2,0$/); |
| 91 | r = await importForm(checking.id, OFX1); |
| 92 | assert.match(r.location, /imported=0,2$/, 'the same download twice adds nothing'); |
| 93 | // A later download that overlaps: one transaction already here, one new. |
| 94 | const later = OFX1.replace(/<STMTTRN>\s*<TRNTYPE>CREDIT[\s\S]*?<\/STMTTRN>/, '<STMTTRN>\n<TRNTYPE>DEBIT\n<DTPOSTED>20261105\n<TRNAMT>-9.99\n<FITID>2026110500002\n<NAME>STREAMFLIX\n</STMTTRN>'); |
| 95 | r = await importForm(checking.id, later); |
| 96 | assert.match(r.location, /imported=1,1$/); |
| 97 | const card = await store.addAccount(ADA, { name: 'Card', kind: 'credit' }); |
| 98 | r = await importForm(card.id, OFX2); |
| 99 | assert.match(r.location, /imported=2,0$/, 'two equal charges with their own FITIDs are two'); |
| 100 | const all = await store.transactions(ADA, checking.id); |
| 101 | assert.deepStrictEqual(all.map((t) => t.payee).sort(), ['ACME CORP PAYROLL', 'CORNER GROCER & DELI', 'STREAMFLIX']); |
| 102 | const api = await fetch(`${base}/api/v1/accounts/${card.id}/import`, { method: 'POST', headers: { 'sec-fetch-site': 'same-origin', 'x-test-user': ADA, 'content-type': 'application/x-ofx' }, body: OFX2 }).then((x) => x.json()); |
| 103 | assert.deepStrictEqual([api.added, api.skipped], [0, 2], 'the API takes OFX too'); |
| 104 | const page = await fetch(`${base}/accounts/${checking.id}?m=2026-11`, { headers: { 'x-test-user': ADA } }).then((x) => x.text()); |
| 105 | assert.match(page, /CSV, OFX or QFX/); |
| 106 | assert.match(page, /accept="\.csv,\.ofx,\.qfx/); |
| 107 | } finally { |
| 108 | server.close(); |
| 109 | await close(); |
| 110 | } |
| 111 | console.log('ofx: 1.x and 2.x, bank and card, FITIDs exact across downloads, the page and the API'); |
| 112 | })().catch((err) => { console.error(err); process.exit(1); }); |
| 113 |