forked from unimatrix/web
67 lines
6.9 KiB
JavaScript
67 lines
6.9 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
|
|
import {companyFixture} from './company-deed-fixture.mjs';
|
|
import {initializeResearchSpecialists,createResearchSpecialists,isResearchSpecialist} from '../node/research-specialists.mjs';
|
|
import {createResearchSpecialistAPI} from '../node/research-specialist-api.mjs';
|
|
import {researchAuthority} from '../node/organisation-research.mjs';
|
|
import {officeLedgerSchema} from '../control-panel/election-office-ledger.mjs';
|
|
import {createPoliticalAppointments,POLITICAL_APPOINTMENT_SCHEMA} from '../node/political-appointments.mjs';
|
|
import {ensurePoliticalOfficeCharters} from '../node/political-office-charters.mjs';
|
|
import {initializeBoardStates} from '../node/board-state-keys.mjs';
|
|
async function fixture(){
|
|
const f=await companyFixture(),id=await f.create(),owner={kind:'company',id};await initializeResearchSpecialists(f.db);
|
|
await f.db.query('INSERT INTO site_company_members(company_id,user_id,joined_at) VALUES(?,?,?)',[id,f.people.Member.id,1000]);
|
|
const roles=createResearchSpecialists(f.db,f.keys,{clock:()=>2000}),input={owner,userId:f.people.Member.id,revision:0,requestId:randomUUID(),confirmed:true};
|
|
return{...f,owner,roles,input};
|
|
}
|
|
test('a sealed deed-authorised appointment grants only specialist production, once per request',async()=>{
|
|
const f=await fixture();try{
|
|
assert.equal(await researchAuthority(f.db,f.db,f.people.Member,f.owner),false);
|
|
const result=await f.roles.appoint(f.people.Owner,f.input);assert.equal(result.specialist.id,f.people.Member.id);assert.equal(result.revision,1);assert.equal((await f.roles.appoint(f.people.Owner,f.input)).replayed,true);
|
|
assert.equal(await researchAuthority(f.db,f.db,f.people.Member,f.owner),true);const member=await f.roles.read(f.people.Member,f.owner);assert.equal(member.canManage,false);assert.equal(member.candidates.length,0);
|
|
await assert.rejects(f.roles.appoint(f.people.Member,{...f.input,requestId:randomUUID(),revision:1,userId:f.people.Other.id}),/management/);
|
|
const rows=await f.db.query("SELECT s.key_id,k.used_at,k.key_type FROM jarvis_research_specialists s JOIN organisation_keys k ON k.id=s.key_id");assert.equal(rows.length,1);assert.equal(rows[0].used_at,null);assert.equal(rows[0].key_type,'organisation-action');
|
|
await assert.rejects(f.roles.appoint(f.people.Owner,{...f.input,userId:f.people.Other.id}),/reference conflict/);
|
|
}finally{f.db.close();}
|
|
});
|
|
test('membership rebinding, ownership changes and explicit removal revoke specialist authority',async()=>{
|
|
const f=await fixture();try{
|
|
await f.roles.appoint(f.people.Owner,f.input);await f.db.query('UPDATE site_company_members SET joined_at=3000 WHERE company_id=? AND user_id=?',[f.owner.id,f.people.Member.id]);assert.equal(await isResearchSpecialist(f.db,f.db,f.people.Member,f.owner),false);
|
|
await f.roles.appoint(f.people.Owner,{...f.input,revision:1,requestId:randomUUID()});assert.equal(await researchAuthority(f.db,f.db,f.people.Member,f.owner),true);
|
|
await f.roles.appoint(f.people.Owner,{...f.input,userId:null,revision:2,requestId:randomUUID()});assert.equal(await researchAuthority(f.db,f.db,f.people.Member,f.owner),false);
|
|
await assert.rejects(f.roles.appoint(f.people.Owner,{...f.input,revision:1,requestId:randomUUID()}),/changed/);
|
|
await f.roles.appoint(f.people.Owner,{...f.input,revision:3,requestId:randomUUID()});
|
|
await f.db.query('INSERT INTO site_company_members(company_id,user_id,joined_at) VALUES(?,?,?)',[f.owner.id,f.people.Other.id,4000]);await f.db.query('UPDATE site_company_deeds SET owner_account_id=? WHERE company_id=?',[f.people.Other.id,f.owner.id]);assert.equal(await researchAuthority(f.db,f.db,f.people.Member,f.owner),false);
|
|
}finally{f.db.close();}
|
|
});
|
|
test('the role API rejects cross-origin writes, identity overrides and outsiders',async()=>{
|
|
const f=await fixture();try{let actor=f.people.Owner;const api=createResearchSpecialistAPI(f.db,f.keys,{}, {authenticate:async()=>actor});
|
|
const get=owner=>api(new Request('https://cp.dunerealms.com/api/items/research-specialist?kind='+owner.kind+'&id='+owner.id));
|
|
const post=(body,origin='https://cp.dunerealms.com')=>api(new Request('https://cp.dunerealms.com/api/items/research-specialist',{method:'POST',headers:{origin,'content-type':'application/json'},body:JSON.stringify(body)}));
|
|
assert.equal((await post(f.input,'https://example.com')).status,403);assert.equal((await post({...f.input,accountId:f.people.Other.id})).status,400);assert.equal((await post({...f.input,userId:f.people.Other.id})).status,403);
|
|
assert.equal((await post(f.input)).status,200);actor=f.people.Other;assert.equal((await get(f.owner)).status,403);actor=null;assert.equal((await get(f.owner)).status,401);
|
|
}finally{f.db.close();}
|
|
});
|
|
|
|
test('an office specialist follows the current charter, term and membership across office/POL workshops',async()=>{
|
|
const f=await companyFixture();try{
|
|
await f.db.query('DROP TABLE political_office_terms');
|
|
for(const sql of [...officeLedgerSchema,...POLITICAL_APPOINTMENT_SCHEMA])await f.db.query(sql.replace(/ ENGINE=InnoDB(?: DEFAULT CHARSET=utf8mb4)?/g,''));
|
|
await ensurePoliticalOfficeCharters(f.db,f.keys);
|
|
const term=randomUUID(),owner={kind:'politics',id:'emperor'},clock=()=>2000;
|
|
await f.db.query("INSERT INTO political_office_terms(id,kind,faction,election_start,round_id,user_id,office,starts_at,expires_at,outcome,streak) VALUES(?,'imperial','imperium',0,?,?,'emperor',0,10000,'winner',1)",[term,randomUUID(),f.people.Owner.id]);
|
|
const appointments=createPoliticalAppointments(f.db,{clock:()=>1000});
|
|
const membership=await appointments.appoint(f.people.Owner,{officeKey:'emperor',userId:f.people.Member.id,requestId:randomUUID(),title:'Imperial Specialist',type:'job',expiresAt:9000});
|
|
await initializeBoardStates(f.db);await initializeResearchSpecialists(f.db);const roles=createResearchSpecialists(f.db,f.keys,{clock});
|
|
await roles.appoint(f.people.Owner,{owner,userId:f.people.Member.id,revision:0,requestId:randomUUID(),confirmed:true});
|
|
assert.equal(await researchAuthority(f.db,f.db,f.people.Member,owner,{clock}),true);
|
|
assert.equal(await researchAuthority(f.db,f.db,f.people.Member,{kind:'office',id:'emperor'},{clock}),true);
|
|
assert.equal((await roles.read(f.people.Member,owner)).canManage,false);
|
|
await appointments.revoke(f.people.Owner,{deedId:membership.id});
|
|
assert.equal(await researchAuthority(f.db,f.db,f.people.Member,owner,{clock}),false);
|
|
await appointments.appoint(f.people.Owner,{officeKey:'emperor',userId:f.people.Member.id,requestId:randomUUID(),title:'Imperial Specialist',type:'job',expiresAt:9000});
|
|
await roles.appoint(f.people.Owner,{owner,userId:f.people.Member.id,revision:1,requestId:randomUUID(),confirmed:true});
|
|
assert.equal(await researchAuthority(f.db,f.db,f.people.Member,owner,{clock}),true);
|
|
await f.db.query("UPDATE political_office_terms SET outcome='resigned' WHERE id=?",[term]);
|
|
assert.equal(await researchAuthority(f.db,f.db,f.people.Member,owner,{clock}),false);
|
|
}finally{f.db.close();}
|
|
});
|

Staff