forked from unimatrix/web
20 lines
3.3 KiB
JavaScript
20 lines
3.3 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
|
|
import {fixture} from './rebellion-fixture.mjs';import {createStaffHomeBlockKeys} from '../node/staff-homeblock-keys.mjs';import {STAFF_OFFICE_ACCOUNT_ID} from '../node/organisation-keys.mjs';
|
|
test('one authorised staff key removes the office Home Block and the existing rebellion rules stop the chain without another controller',async()=>{
|
|
const f=await fixture();try{
|
|
const declaration=await f.service.declare(f.winner,f.input),victory=await f.capture(declaration),before=await f.db.first("SELECT * FROM site_claim_registry WHERE organisation_kind='protected' AND organisation_id=?",[f.officeId]);
|
|
const staff=f.people.Other,service=createStaffHomeBlockKeys(f.db,f.keys,{authorizeStaff:async actor=>actor.id===staff.id,clock:f.now}),input={kind:'protected',id:f.officeId,expectedRevision:1,ticketId:'417',reason:'Stabilise the realm by removing the Imperial Home Block',requestId:randomUUID()};
|
|
await assert.rejects(service.issue(f.winner,input),/staff overwrite authority/);
|
|
const issued=await service.issue(staff,input);assert.equal((await service.issue(staff,input)).keyId,issued.keyId);
|
|
assert.ok(JSON.parse((await f.db.first("SELECT snapshot_json FROM site_claim_registry WHERE organisation_kind='protected' AND organisation_id=?",[f.officeId])).snapshot_json).homeBlock);
|
|
const command={keyId:issued.keyId,requestId:randomUUID()};await service.apply(staff,command);assert.equal((await service.apply(staff,command)).replayed,true);
|
|
const after=await f.db.first("SELECT * FROM site_claim_registry WHERE organisation_kind='protected' AND organisation_id=?",[f.officeId]),expected=JSON.parse(before.snapshot_json);expected.homeBlock=null;assert.deepEqual(JSON.parse(after.snapshot_json),expected);assert.equal(Number(after.revision),2);
|
|
await assert.rejects(f.service.applyCapture(f.producer,victory),/Home Block is not registered/);
|
|
assert.equal((await f.db.first('SELECT COUNT(*) n FROM jarvis_rebellion_rewards')).n,0);assert.equal((await f.db.first('SELECT user_id FROM political_office_terms WHERE id=?',[f.term])).user_id,f.incumbent.id);
|
|
assert.equal((await f.db.first("SELECT COUNT(*) n FROM account_audit WHERE action='staff-homeblock-removed'")).n,1);
|
|
assert.equal((await f.keys.list(STAFF_OFFICE_ACCOUNT_ID)).find(k=>k.id===issued.keyId).status,'used');
|
|
}finally{f.db.close();}
|
|
});
|
|
test('a changed claim or revoked staff authority leaves the Home Block and issued key intact',async()=>{
|
|
const f=await fixture();try{let allowed=true;const service=createStaffHomeBlockKeys(f.db,f.keys,{authorizeStaff:async()=>allowed}),key=await service.issue(f.incumbent,{kind:'protected',id:f.officeId,expectedRevision:1,ticketId:'417',reason:'Approved staff stabilisation',requestId:randomUUID()});allowed=false;await assert.rejects(service.apply(f.incumbent,{keyId:key.keyId,requestId:randomUUID()}),/authority/);allowed=true;await f.db.query("UPDATE site_claim_registry SET revision=revision+1 WHERE organisation_kind='protected' AND organisation_id=?",[f.officeId]);await assert.rejects(service.apply(f.incumbent,{keyId:key.keyId,requestId:randomUUID()}),/Home Block changed/);assert.equal((await f.keys.list(STAFF_OFFICE_ACCOUNT_ID)).find(k=>k.id===key.keyId).status,'available');}finally{f.db.close();}
|
|
});
|

Staff