web/website/tests/company-admin.test.mjs
2026-11-21 20:22:37 +01:00

14 lines
3.4 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {createCompanyAdmin,accountPrefixPattern} from '../node/company-admin.mjs';
const origin='https://cp.dunerealms.com';const req=(body,headers={origin})=>new Request(origin+'/api/companies/manage',{method:'POST',headers:{'Content-Type':'application/json',...headers},body:JSON.stringify(body)});
const make=(user={id:'staff'},nodes=['web.accounts.assign'])=>{const calls=[];const handler=createCompanyAdmin({},origin,{authenticate:async()=>user,authorize:async()=>({nodes}),register:async(db,body)=>{calls.push(body);return {id:'new-company'};},appoint:async(db,body)=>{calls.push(body);return {companyId:body.companyId};}});return {handler,calls};};
test('guest cannot register company',async()=>{const x=make(null);assert.equal((await x.handler(req({action:'register'}))).status,401);assert.equal(x.calls.length,0);});
test('unauthorized account cannot manage company',async()=>{const x=make({id:'member'},[]);assert.equal((await x.handler(req({action:'register'}))).status,403);assert.equal(x.calls.length,0);});
test('cross-origin request is rejected',async()=>{const x=make();assert.equal((await x.handler(req({action:'register'},{origin:'https://evil.example'}))).status,403);assert.equal(x.calls.length,0);});
test('authorized registration delegates actor and server-side permission',async()=>{const x=make();const res=await x.handler(req({action:'register',slug:'company',name:'Company',founderId:'founder'}));assert.equal(res.status,201);assert.equal(x.calls[0].actorId,'staff');assert.equal(x.calls[0].authorized,true);});
test('authorized CEO appointment delegates validated actor',async()=>{const x=make();const res=await x.handler(req({action:'appoint-ceo',companyId:'company',ceoId:'member'}));assert.equal(res.status,200);assert.equal(x.calls[0].actorId,'staff');});
test('account search requires staff and returns only username and id',async()=>{const db={prepare(sql){assert.match(sql,/SELECT id,username/);return {bind(){return {all:async()=>({results:[{id:'id-1',username:'player'}]})};}};}};const handler=createCompanyAdmin(db,origin,{authenticate:async()=>({id:'staff'}),authorize:async()=>({nodes:['web.accounts.assign']})});const response=await handler(new Request(origin+'/api/companies/manage?account=play'));assert.equal(response.status,200);assert.deepEqual(await response.json(),{accounts:[{id:'id-1',username:'player'}]});});
test('guest account search remains forbidden',async()=>{const x=make(null);const r=await x.handler(new Request(origin+'/api/companies/manage?account=play'));assert.equal(r.status,401);});
test('short or malformed account search fails before SQL',async()=>{const handler=createCompanyAdmin({},origin,{authenticate:async()=>({id:'staff'}),authorize:async()=>({nodes:['web.accounts.assign']})});const r=await handler(new Request(origin+'/api/companies/manage?account=%25'));assert.equal(r.status,400);});
test('underscore in username is escaped as literal in SQL LIKE',async()=>{assert.equal(accountPrefixPattern('user_name'),'user!_name%');let captured;const db={prepare(sql){assert.match(sql,/ESCAPE '!'/);return {bind(pattern){captured=pattern;return {all:async()=>({results:[]})};}};}};const handler=createCompanyAdmin(db,origin,{authenticate:async()=>({id:'staff'}),authorize:async()=>({nodes:['web.accounts.assign']})});assert.equal((await handler(new Request(origin+'/api/companies/manage?account=user_name'))).status,200);assert.equal(captured,'user!_name%');});