forked from unimatrix/web
12 lines
10 KiB
JavaScript
12 lines
10 KiB
JavaScript
import {PostbankStatementArchive} from '../node/postbank-statement-archive.mjs';
|
|
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';import {mkdtemp,rm} from 'node:fs/promises';import {tmpdir} from 'node:os';import {join} from 'node:path';
|
|
import {BankFixture} from './postbank-fixture.mjs';import {PostbankService} from '../node/postbank-service.mjs';import {PostbankPayments} from '../node/postbank-payments.mjs';import {PostbankBeneficiaries} from '../node/postbank-beneficiaries.mjs';import {PostbankStatements,statementMonth} from '../node/postbank-statements.mjs';import {createPostbankApi} from '../node/postbank-api.mjs';import {BANK_VERSION} from '../node/postbank-catalogue.mjs';
|
|
const now=Date.parse('2026-11-13T20:00:00Z');
|
|
async function setup(){const db=new BankFixture(),users=[0,1].map(i=>({id:randomUUID(),username:'reader_'+i})),entities=users.map(u=>({id:randomUUID(),key:u.id,kind:'person',name:u.username}));const authority=async u=>({entities:entities.map(e=>({...e,canView:e.key===u.id,canActivate:e.key===u.id,canPay:e.key===u.id}))});const service=new PostbankService(db,authority,{clock:()=>Date.parse('2026-10-01T12:00:00Z')});for(let i=0;i<2;i++)await service.activate(users[i],{entityId:entities[i].id,packageKey:'pb_flow',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});const accounts=await db.query('SELECT * FROM pb_accounts ORDER BY id'),payments=new PostbankPayments(db,authority,{clock:()=>now,resolveUser:async id=>users.find(u=>u.id===id)}),contacts=new PostbankBeneficiaries(db,{clock:()=>now});return {db,users,authority,accounts,payments,contacts};}
|
|
test('saved beneficiaries are private, idempotent, validated references and cannot expose foreign ownership',async()=>{const f=await setup();try{const [own,,other]=f.accounts,body={requestId:randomUUID(),number:other.account_number,label:'Partner'};await f.contacts.write(f.users[0],body);assert.equal((await f.contacts.write(f.users[0],body)).replayed,true);assert.equal((await f.contacts.list(f.users[1])).beneficiaries.length,0);const list=await f.contacts.list(f.users[0]);assert.equal(list.beneficiaries.length,1);assert.equal('balance' in list.beneficiaries[0],false);await f.contacts.write(f.users[1],{requestId:randomUUID(),beneficiaryId:list.beneficiaries[0].id},true);assert.equal((await f.contacts.list(f.users[0])).beneficiaries.length,1);await assert.rejects(f.contacts.write(f.users[0],{...body,label:'Changed'}),e=>e.status===409);await f.contacts.write(f.users[0],{requestId:randomUUID(),beneficiaryId:list.beneficiaries[0].id},true);assert.equal((await f.contacts.list(f.users[0])).beneficiaries.length,0);await assert.rejects(f.contacts.write(f.users[0],{...body,requestId:randomUUID(),number:'DR00000000000000'}));}finally{f.db.close();}});
|
|
test('revoked contact session rolls back the edit and dormant accounts are not selectable',async()=>{const f=await setup();try{let calls=0;const body={requestId:randomUUID(),number:f.accounts[2].account_number,label:'Recipient'};await assert.rejects(f.contacts.write(f.users[0],body,false,{revalidate:async()=>++calls===1?f.users[0]:null}),e=>e.status===409);assert.equal((await f.contacts.list(f.users[0])).beneficiaries.length,0);await f.db.query("UPDATE pb_accounts SET state='dormant' WHERE id=?",[f.accounts[2].id]);await assert.rejects(f.contacts.write(f.users[0],body),e=>e.status===409);}finally{f.db.close();}});
|
|
async function seed(f){const [current,savings]=f.accounts,at=Date.parse('2026-10-15T12:00:00Z'),id=randomUUID();await f.db.query('INSERT INTO pb_orders(id,actor_id,source_id,target_id,fingerprint,amount_minor,memo,mode,state,created_at,posted_at) VALUES(?,?,?,?,?,?,?,?,?,?,?)',[id,f.users[0].id,savings.id,current.id,'0'.repeat(64),10000,'Fixture opening','system','posted',at,at]);for(const [account,amount,balance]of [[current.id,10000,10000],[savings.id,-10000,-10000]]){await f.db.query('INSERT INTO pb_entries(order_id,account_id,amount_minor,balance_after_minor,created_at) VALUES(?,?,?,?,?)',[id,account,amount,balance,at]);await f.db.query('UPDATE pb_accounts SET balance_minor=? WHERE id=?',[balance,account]);}return {current,savings};}
|
|
test('interactive history is bounded to 30 days and filters are literal without deleting old entries',async()=>{const f=await setup();try{const {current,savings}=await seed(f);await f.payments.transfer(f.users[0],{requestId:randomUUID(),sourceAccount:String(current.id),targetNumber:savings.account_number,amount:'10.00',reference:'November transfer','mode':'instant',confirmed:false});assert.equal((await f.payments.activity(f.users[0],{q:'November'})).records.length,1);assert.equal((await f.payments.activity(f.users[0],{q:'%'})).records.length,0);assert.equal((await f.payments.activity(f.users[0],{state:'queued'})).records.length,0);await f.db.query('UPDATE pb_orders SET created_at=? WHERE memo=?',[now-31*86400000,'Fixture opening']);assert.equal((await f.payments.activity(f.users[0])).records.length,1);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_entries')).n,4);await assert.rejects(f.payments.activity(f.users[0],{from:'2026-02-30'}));await assert.rejects(f.payments.activity(f.users[0],{to:'2026-01-01'}));}finally{f.db.close();}});
|
|
test('monthly statements use posting dates, preserve opening/closing balances, cache closed months and enforce access',async()=>{const f=await setup(),dir=await mkdtemp(join(tmpdir(),'pb-statement-test-'));try{const {current,savings}=await seed(f);await f.payments.transfer(f.users[0],{requestId:randomUUID(),sourceAccount:String(current.id),targetNumber:savings.account_number,amount:'10.00',reference:'November transfer',mode:'instant',confirmed:false});const payloads=[],statements=new PostbankStatements(f.db,f.payments,{clock:()=>now,cacheDir:dir,renderPDF:async data=>{payloads.push(data);return Buffer.from('%PDF-fixture');}});await statements.download(f.users[0],{accountId:String(current.id),month:'2026-10'});assert.equal(payloads[0].opening,'0.00');assert.equal(payloads[0].closing,'100.00');assert.equal(payloads[0].entries.length,1);await statements.download(f.users[0],{accountId:String(current.id),month:'2026-10'});assert.equal(payloads.length,1);await statements.download(f.users[0],{accountId:String(current.id),month:'2026-11'});assert.equal(payloads[1].provisional,true);assert.equal(payloads[1].opening,'100.00');assert.equal(payloads[1].closing,'90.00');await assert.rejects(statements.download(f.users[1],{accountId:String(current.id),month:'2026-10'}),e=>e.status===403);await assert.rejects(statements.download(f.users[0],{accountId:String(current.id),month:'2026-09'}),e=>e.status===404);assert.equal(statementMonth('2026-10',now).start,Date.parse('2026-09-30T22:00:00Z'));assert.equal(statementMonth('2026-11',now).start,Date.parse('2026-10-31T23:00:00Z'));}finally{f.db.close();await rm(dir,{recursive:true,force:true});}});
|
|
test('bank ticker builds closed-month archives; reads and downloads cannot generate statements',async()=>{const f=await setup(),dir=await mkdtemp(join(tmpdir(),'pb-auto-archive-'));try{const {current}=await seed(f);let rendered=0;const renderPDF=async()=>{rendered++;return Buffer.from('%PDF-fixture');},archive=new PostbankStatementArchive(f.db,f.payments,{clock:()=>now,cacheDir:dir,renderPDF});await assert.rejects(archive.download(f.users[0],{accountId:String(current.id),month:'2026-10'}),e=>e.status===404);assert.equal(rendered,0);assert.equal((await archive.list(f.users[0],{accountId:String(current.id)})).records.length,0);await archive.seed();await archive.seed();assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_statement_archives')).n,4);const results=await Promise.all([archive.process(),archive.process()]);assert.equal(results.reduce((s,r)=>s+r.completed,0),4);assert.equal(rendered,4);const list=await archive.list(f.users[0],{accountId:String(current.id)});assert.equal(list.records[0].month,'2026-10');assert.equal(list.records[0].state,'ready');await archive.download(f.users[0],{accountId:String(current.id),month:'2026-10'});assert.equal(rendered,4);await assert.rejects(archive.download(f.users[0],{accountId:String(current.id),month:'2026-11'}),e=>e.status===409);await assert.rejects(archive.list(f.users[1],{accountId:String(current.id)}),e=>e.status===403);assert.equal((await archive.process()).completed,0);}finally{f.db.close();await rm(dir,{recursive:true,force:true});}});
|
|
test('archive failures retry on a bank tick, interrupted claims recover, API downloads stay private',async()=>{const f=await setup(),dir=await mkdtemp(join(tmpdir(),'pb-archive-retry-'));try{const {current}=await seed(f);let clock=now,broken=true,renders=0;const renderer=new PostbankStatements(f.db,null,{clock:()=>clock,cacheDir:dir,renderPDF:async()=>{renders++;if(broken)throw Error('fixture failure');return Buffer.from('%PDF-fixture');}}),archive=new PostbankStatementArchive(f.db,f.payments,{clock:()=>clock,cacheDir:dir,renderer});await archive.seed();assert.equal((await archive.process({limit:1})).failed,1);broken=false;await f.db.query("UPDATE pb_statement_archives SET state='processing',lease_until=?,claim_token=? WHERE account_id=?",[clock-1,randomUUID(),f.accounts[1].id]);clock+=4*3600000+1;assert.equal((await archive.process()).completed,4);const settings={POSTBANK_STATEMENT_CACHE:dir};let actor=f.users[0];const deps={clock:()=>clock,authority:f.authority,authenticate:async()=>actor,renderPDF:async()=>{throw Error('HTTP MUST NOT RENDER');}},api=createPostbankApi(null,f.db,settings,deps),url='https://cp.dunerealms.com/api/bank/statements?accountId='+current.id+'&month=2026-10';const response=await api(new Request(url));assert.equal(response.status,200);assert.equal(response.headers.get('content-type'),'application/pdf');assert.equal(response.headers.get('cache-control'),'private, no-store');const archiveURL='https://cp.dunerealms.com/api/bank/statements/archive?accountId='+current.id;assert.equal((await api(new Request(archiveURL))).status,200);actor=f.users[1];assert.equal((await api(new Request(url))).status,403);assert.equal((await api(new Request(archiveURL))).status,403);assert.equal((await api(new Request(url+'&actor=other'))).status,400);assert.equal(renders,5);}finally{f.db.close();await rm(dir,{recursive:true,force:true});}});
|

Staff