web/website/tests/system-board-access.test.mjs

43 lines
5.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {projectSystemBoardAccess} from '../node/system-board-access.mjs';
const clan={id:'ours',active:1,user_id:'member',owner_id:'leader',clan_role:'member'};
test('verified organisation names accompany board grants for automatic provisioning',()=>{
assert.equal(projectSystemBoardAccess({userId:'member',clans:[{...clan,name:'Current clan'}]}).organisations[0].name,'Current clan');
});
test('private board membership excludes other users, inactive clans and departed users',()=>{
const access=projectSystemBoardAccess({userId:'member',clans:[clan,{...clan,id:'other',user_id:'other'},{...clan,id:'closed',active:0}]});
assert.deepEqual(access.organisations,[{kind:'clan',id:'ours',read:true,post:true,manage:false}]);
assert.equal(projectSystemBoardAccess({userId:'member',clans:[]}).organisations.length,0);
});
test('only verified current clan ownership grants subtree management',()=>{
for(const role of ['member','officer','owner']){
const result=projectSystemBoardAccess({userId:'member',clans:[{...clan,clan_role:role}]}).organisations[0];
assert.equal(result.manage,false);
}
assert.equal(projectSystemBoardAccess({userId:'member',clans:[{...clan,owner_id:'member',clan_role:'owner'}]}).organisations[0].manage,true);
});
test('company founder and CEO access remain distinct; unrelated and inactive companies are excluded',()=>{
const company={id:'company',status:'active',founded_by:'founder',ceo_account_id:'ceo'};
const view=userId=>projectSystemBoardAccess({userId,companies:[company]}).organisations;
assert.equal(view('founder')[0].manage,false);assert.equal(view('ceo')[0].manage,true);assert.deepEqual(view('stranger'),[]);
assert.deepEqual(projectSystemBoardAccess({userId:'ceo',companies:[{...company,status:'closed'}]}).organisations,[]);
});
test('inspection is not moderation, and neither can be enabled with truthy strings or an anonymous identity',()=>{
assert.deepEqual(projectSystemBoardAccess({inspectAll:true,moderateAll:true}),{version:1,inspectAll:false,moderateAll:false,organisations:[]});
assert.equal(projectSystemBoardAccess({userId:'owner',inspectAll:true}).moderateAll,false);
assert.equal(projectSystemBoardAccess({userId:'member',inspectAll:'true',moderateAll:1}).inspectAll,false);
assert.equal(projectSystemBoardAccess({userId:'mod',moderateAll:true}).moderateAll,true);
});
test('Company boards follow the current deed and roster, including removal after a transfer',async()=>{const {companyFixture}=await import('./company-deed-fixture.mjs'),{loadSystemBoardAccess}=await import('../node/system-board-access.mjs');const f=await companyFixture();try{const id=await f.create(),read=async name=>(await loadSystemBoardAccess(f.db,f.people[name])).organisations.filter(o=>o.kind==='company');assert.equal((await read('Owner'))[0].manage,true);assert.equal((await read('Other')).length,0);await f.db.query('INSERT INTO site_company_members(company_id,user_id,joined_at) VALUES(?,?,1)',[id,f.people.Member.id]);assert.equal((await read('Member'))[0].post,true);assert.equal((await read('Member'))[0].manage,false);await f.db.query('DELETE FROM site_company_members WHERE company_id=? AND user_id=?',[id,f.people.Member.id]);assert.equal((await read('Member')).length,0);await f.db.query('INSERT INTO site_company_members(company_id,user_id,joined_at) VALUES(?,?,1)',[id,f.people.Other.id]);await f.db.query('UPDATE site_company_deeds SET owner_account_id=? WHERE company_id=?',[f.people.Other.id,id]);await f.db.query('DELETE FROM site_company_members WHERE company_id=? AND user_id=?',[id,f.people.Owner.id]);assert.equal((await read('Owner')).length,0);assert.equal((await read('Other'))[0].manage,true);}finally{f.db.close();}});
test('mother company deed grants no child board access without direct membership',async()=>{
const {companyFixture}=await import('./company-deed-fixture.mjs'),{loadSystemBoardAccess}=await import('../node/system-board-access.mjs'),{randomUUID}=await import('node:crypto');const f=await companyFixture();try{
const parent=await f.create(),child=randomUUID(),now=Date.now();await f.db.query("UPDATE site_company_deeds SET charter_kind='royal' WHERE company_id=?",[parent]);
await f.db.query("INSERT INTO corrino_companies(id,slug,name,description,founded_by,status,created_at,updated_at) VALUES(?,'private-child','Private Child','Private',?,'active',?,?)",[child,f.people.Other.id,now,now]);
await f.db.query("INSERT INTO site_company_deeds(company_id,owner_account_id,charter_kind,version,created_at,updated_at) VALUES(?,?,'royal',1,?,?)",[child,f.people.Other.id,now,now]);await f.db.query('INSERT INTO site_company_members(company_id,user_id,joined_at) VALUES(?,?,?)',[child,f.people.Other.id,now]);await f.db.query("INSERT INTO site_company_relations(company_id,parent_company_id,relation_origin,updated_at) VALUES(?,?,'created',?)",[child,parent,now]);
assert.equal((await loadSystemBoardAccess(f.db,f.people.Owner)).organisations.some(o=>o.id===child),false);
await f.db.query('INSERT INTO site_company_members(company_id,user_id,joined_at) VALUES(?,?,?)',[child,f.people.Owner.id,now]);const grant=(await loadSystemBoardAccess(f.db,f.people.Owner)).organisations.find(o=>o.id===child);assert.equal(grant.read,true);assert.equal(grant.manage,false);
}finally{f.db.close();}
});