11 lines
5.3 KiB
JavaScript
11 lines
5.3 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {readFileSync,mkdtempSync,rmSync} from 'node:fs';import {tmpdir} from 'node:os';import path from 'node:path';import {LocalD1} from './d1-adapter.mjs';import {handlePortal} from '../worker/portal-api.mjs';
|
|
const schema=['0000_forums','0001_profiles_tickets'].map(n=>readFileSync(new URL('../drizzle/'+n+'.sql',import.meta.url),'utf8')).join('\n');
|
|
function req(endpoint,user='alice',method='GET',body,email,origin='https://example.test'){return new Request('https://example.test/api/'+endpoint,{method,headers:{'Content-Type':'application/json',Origin:origin,...(user?{'oai-authenticated-user-id':user}:{}),...(email?{'oai-authenticated-user-email':email}:{})},...(body!==undefined?{body:JSON.stringify(body)}:{})})}
|
|
const profile={displayName:'Alice',minecraftName:'DesertPlayer',discordName:'OptionalHandle'};
|
|
async function setup(env){assert.equal((await handlePortal(req('profile','alice','POST',profile),env)).status,200);const r=await handlePortal(req('tickets','alice','POST',{category:'ban-appeal',subject:'Please review my ban',body:'I would like staff to review the reason for my ban.'}),env);assert.equal(r.status,201);return (await r.json()).id}
|
|
test('Portal data requires authentication and same-origin writes',async()=>{const DB=new LocalD1(schema);try{assert.equal((await handlePortal(req('profile',null),{DB})).status,401);assert.equal((await handlePortal(req('profile','alice','POST',profile,null,'https://evil.test'),{DB})).status,403);assert.equal((await handlePortal(req('profile','alice','POST',null),{DB})).status,400)}finally{DB.close()}});
|
|
test('Tickets are tied to profiles and appeals require a Minecraft name',async()=>{const DB=new LocalD1(schema);try{const body={category:'ban-appeal',subject:'Review my account ban',body:'Here is my explanation for staff review.'};assert.equal((await handlePortal(req('tickets','alice','POST',body),{DB})).status,422);await handlePortal(req('profile','alice','POST',{...profile,minecraftName:''}),{DB});assert.equal((await handlePortal(req('tickets','alice','POST',body),{DB})).status,422);body.category='staff-support';assert.equal((await handlePortal(req('tickets','alice','POST',body),{DB})).status,201)}finally{DB.close()}});
|
|
test('Other players cannot read or alter a private ticket',async()=>{const DB=new LocalD1(schema);try{const id=await setup({DB});assert.equal((await handlePortal(req('tickets/'+id,'bob'),{DB})).status,404);assert.equal((await handlePortal(req('tickets/'+id+'/replies','bob','POST',{body:'Forged reply'}),{DB})).status,404);assert.equal((await handlePortal(req('tickets/'+id+'/status','bob','POST',{status:'closed'}),{DB})).status,404);assert.deepEqual((await (await handlePortal(req('tickets','bob'),{DB})).json()).tickets,[]);assert.equal((await handlePortal(req('tickets?view=staff','bob'),{DB})).status,403);const own=await (await handlePortal(req('tickets/'+id),{DB})).json();assert.ok(!JSON.stringify(own).includes('user_id'));assert.equal(own.ticket.minecraft_name,'DesertPlayer')}finally{DB.close()}});
|
|
test('Only authorised staff can triage; replies carry verified staff roles',async()=>{const DB=new LocalD1(schema),env={DB,SUPPORT_STAFF_EMAILS:'staff@example.invalid'};try{const id=await setup(env);assert.equal((await handlePortal(req('tickets/'+id+'/status','alice','POST',{status:'resolved'}),env)).status,403);assert.equal((await handlePortal(req('tickets/'+id+'/status','moderator','POST',{status:'in_progress'},'staff@example.invalid'),env)).status,200);assert.equal((await handlePortal(req('tickets/'+id+'/replies','moderator','POST',{body:'We are reviewing your case.',author_role:'player'},'staff@example.invalid'),env)).status,201);const data=await (await handlePortal(req('tickets/'+id),env)).json();assert.equal(data.ticket.status,'in_progress');assert.equal(data.messages.find(m=>m.body==='We are reviewing your case.').author_role,'staff');assert.equal((await (await handlePortal(req('tickets?view=staff','moderator','GET',undefined,'staff@example.invalid'),env)).json()).tickets.length,1)}finally{DB.close()}});
|
|
test('Owners close and reopen tickets; closed cases block replies and rate limits apply',async()=>{const DB=new LocalD1(schema),env={DB};try{const id=await setup(env);await handlePortal(req('tickets/'+id+'/status','alice','POST',{status:'closed'}),env);assert.equal((await handlePortal(req('tickets/'+id+'/replies','alice','POST',{body:'Hello again'}),env)).status,409);await handlePortal(req('tickets/'+id+'/status','alice','POST',{status:'open'}),env);for(let i=0;i<5;i++)assert.equal((await handlePortal(req('tickets/'+id+'/replies','alice','POST',{body:'Reply number '+i}),env)).status,201);assert.equal((await handlePortal(req('tickets/'+id+'/replies','alice','POST',{body:'Too many replies'}),env)).status,429)}finally{DB.close()}});
|
|
test('Private profiles and ticket history persist after database restart',async()=>{const dir=mkdtempSync(path.join(tmpdir(),'dune-tickets-')),file=path.join(dir,'db.sqlite');let DB=new LocalD1(schema,file);try{const id=await setup({DB});DB.close();DB=new LocalD1(null,file);assert.equal((await (await handlePortal(req('tickets/'+id),{DB})).json()).ticket.subject,'Please review my ban');assert.equal((await (await handlePortal(req('tickets'),{DB})).json()).activeCount,1);assert.equal((await (await handlePortal(req('profile','bob'),{DB})).json()).profile,null)}finally{DB.close();rmSync(dir,{recursive:true,force:true})}});
|

Staff