forked from unimatrix/web
8 lines
2.1 KiB
JavaScript
8 lines
2.1 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {createStaffBureauReadApi} from '../node/staff-bureau-read-api.mjs';
|
|
const url=path=>'https://cp.dunerealms.com'+path;
|
|
const make=(authenticate,authorize)=>{const calls=[];const db={prepare(sql){calls.push(sql);return {bind(){return this},first:async()=>({account_id:'u',department_key:'operations',staff_level:'staff',status:'active',appointed_at:123,version:1}),all:async()=>({results:[]})}}};return {api:createStaffBureauReadApi(db,{authenticate,authorize}),calls};};
|
|
test('unauthenticated staff endpoint rejects before database access',async()=>{const {api,calls}=make(async()=>null,async()=>true);const r=await api(new Request(url('/api/v1/staff/me')));assert.equal(r.status,401);assert.equal(calls.length,0);});
|
|
test('ordinary staff cannot enumerate directory or departments',async()=>{const {api,calls}=make(async()=>({id:'u'}),async()=>false);for(const path of ['/api/v1/staff/directory','/api/v1/staff/departments']){const r=await api(new Request(url(path)));assert.equal(r.status,403);}assert.equal(calls.length,0);});
|
|
test('self service exposes only own shadow position',async()=>{const {api,calls}=make(async()=>({id:'u'}),async()=>false);const r=await api(new Request(url('/api/v1/staff/me')));assert.equal(r.status,200);const data=await r.json();assert.equal(data.mode,'shadow');assert.equal(data.active,false);assert.equal(data.position.accountId,'u');assert.ok(calls[0].includes('WHERE account_id=?'));});
|
|
test('authorized department listing is fixed and read-only',async()=>{const {api}=make(async()=>({id:'u'}),async()=>true);const r=await api(new Request(url('/api/v1/staff/departments')));assert.equal(r.status,200);const data=await r.json();assert.equal(data.departments.length,4);assert.equal(data.mode,'shadow');});
|
|
test('unknown staff API routes return 404, mutations return 405',async()=>{const {api}=make(async()=>({id:'u'}),async()=>true);assert.equal((await api(new Request(url('/api/v1/staff/unknown')))).status,404);assert.equal((await api(new Request(url('/api/v1/staff/me'),{method:'POST'}))).status,405);});
|

Staff