MC/website/tests/postbank-client-management.test.mjs
2026-11-27 23:43:05 +00:00

38 lines
4.5 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
import {BankFixture} from './postbank-fixture.mjs';import {PostbankService} from '../node/postbank-service.mjs';import {PostbankClientManagement} from '../node/postbank-client-management.mjs';import {BANK_VERSION} from '../node/postbank-catalogue.mjs';
async function fixture(){
const db=new BankFixture(),owner={id:randomUUID(),username:'Client'},officer={id:randomUUID(),username:'Officer'},viewer={id:randomUUID()},manager={id:randomUUID()},e={id:randomUUID(),kind:'person',key:owner.id,name:'Client'};let revoked=false;
const authority=async u=>({entities:[{...e,canView:u.id===owner.id,canActivate:u.id===owner.id,canPay:u.id===owner.id}],readAdmin:[officer.id,viewer.id,manager.id].includes(u.id),manageAdmin:u.id===manager.id,manageClients:u.id===officer.id&&!revoked});
const service=new PostbankService(db,authority);await service.activate(owner,{entityId:e.id,packageKey:'pb_flow',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});
const pkg=await db.first('SELECT * FROM pb_packages'),account=await db.first("SELECT * FROM pb_accounts WHERE type_code='102'"),clients=new PostbankClientManagement(db,authority);
const input=()=>({entityId:e.id,packageId:pkg.id,expectedPackageKey:'pb_flow',packageKey:'pb_start',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID(),reason:'Client requested lower package'});
return {db,owner,officer,viewer,manager,e,authority,service,clients,pkg,account,input,revoke:()=>{revoked=true;}};
}
test('staff product edits retain actual authoriser, reason, account numbers and balanced funds',async()=>{const f=await fixture();try{
await assert.rejects(f.clients.detail(f.owner,f.e.id),e=>e.status===403);
assert.equal((await f.clients.detail(f.viewer,f.e.id)).canManage,false);
const view=await f.clients.detail(f.officer,f.e.id);assert.equal(view.entities.length,1);assert.equal(view.canManage,true);
assert.equal((await f.service.overview(f.officer)).entities.length,0);
const input=f.input();await assert.rejects(f.clients.change(f.viewer,input,'package'),e=>e.status===403);
await f.clients.change(f.officer,input,'package');assert.equal((await f.clients.change(f.officer,input,'package')).replayed,true);
const current=await f.db.first('SELECT * FROM pb_packages');assert.equal(current.accepted_by,f.officer.id);assert.equal(current.package_key,'pb_start');
assert.equal((await f.db.first('SELECT * FROM pb_accounts WHERE id=?',[f.account.id])).account_number,f.account.account_number);
const audit=await f.db.first("SELECT * FROM pb_audit WHERE action='package_changed'");assert.equal(audit.actor_id,f.officer.id);assert.equal(JSON.parse(audit.details).reason,input.reason);
await assert.rejects(f.clients.change(f.officer,{...input,reason:'Another request reason'},'package'),e=>e.status===409);
assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,0);
}finally{f.db.close();}});
test('revocation during confirmation rolls back edits and official clients require management',async()=>{const f=await fixture();try{
await assert.rejects(f.clients.change(f.officer,f.input(),'package',{revalidate:async()=>{f.revoke();return f.officer;}}),e=>e.status===403);
assert.equal((await f.db.first('SELECT package_key FROM pb_packages')).package_key,'pb_flow');
await f.db.query("UPDATE pb_entities SET kind='staff' WHERE id=?",[f.e.id]);
const another=new PostbankClientManagement(f.db,async u=>({...await f.authority(u),manageClients:true}));
assert.equal((await another.detail(f.officer,f.e.id)).canManage,false);assert.equal((await another.detail(f.manager,f.e.id)).canManage,true);
}finally{f.db.close();}});
test('staff option changes cannot target another client or bypass version/confirmation',async()=>{const f=await fixture();try{
const body={entityId:f.e.id,accountId:String(f.account.id),balanceGuard:true,expectedBalanceGuard:false,termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID(),reason:'Client requested Balance Guard'};
await assert.rejects(f.clients.change(f.officer,{...body,entityId:randomUUID()},'options'));
await f.clients.change(f.officer,body,'options');assert.equal((await f.clients.change(f.officer,body,'options')).replayed,true);
assert.equal((await f.db.first('SELECT * FROM pb_account_options')).accepted_by,f.officer.id);
await assert.rejects(f.clients.change(f.officer,{...body,requestId:randomUUID(),balanceGuard:false},'options'),e=>e.status===409);
await assert.rejects(f.clients.change(f.officer,{...body,requestId:randomUUID(),targetNumber:'FORGED'},'options'));
}finally{f.db.close();}});