web/website/tests/company-workspace.test.mjs

15 lines
1.7 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {LocalD1} from './d1-adapter.mjs';
import {createCompanyWorkspace} from '../node/company-workspace.mjs';
const mine='11111111-1111-4111-8111-111111111111',other='22222222-2222-4222-8222-222222222222';
test('Company CP reads only founder/CEO companies and rejects company/account overrides',async()=>{
const db=new LocalD1('CREATE TABLE site_accounts(id TEXT,username TEXT);CREATE TABLE corrino_companies(id TEXT,slug TEXT,name TEXT,description TEXT,status TEXT,founded_by TEXT,ceo_account_id TEXT);');
try{
db.sqlite.exec(`INSERT INTO site_accounts VALUES('owner','Owner'),('stranger','Other');INSERT INTO corrino_companies VALUES('${mine}','mine','Mine','My charter','active','owner',NULL),('${other}','other','Other','Private company','active','stranger',NULL)`);
const api=createCompanyWorkspace(db,{authenticate:async()=>({id:'owner'})}),request=query=>api(new Request('https://cp.dunerealms.com/api/companies/workspace'+query));
const data=await(await request('')).json();assert.deepEqual(data.companies.map(c=>c.id),[mine]);assert.equal(data.selected,mine);assert.equal(data.companies[0].role,'Founder');
assert.equal((await request('?company='+other)).status,404);assert.equal((await request('?user=stranger')).status,400);assert.equal((await request('?company='+mine+'&company='+other)).status,400);
assert.equal((await api(new Request('https://cp.dunerealms.com/api/companies/workspace',{method:'POST'}))).status,405);
assert.equal((await createCompanyWorkspace(db,{authenticate:async()=>null})(new Request('https://cp.dunerealms.com/api/companies/workspace'))).status,401);
}finally{db.close();}
});