// Every staff/supervisor route on /api/v2 must really check campus-scoped staff (utils/campusScope.js). // A route has to declare `campusScope` (start-up fails otherwise), but the declaration is only text: // the check itself is a marked middleware — scopeEvent(...), scopeBody(...), scopeList, // unscopedOnly, campusIndependent('reason') or scopeBroadcast(...). A route without one would show // a campus-limited supervisor every campus's data, so this fails on it. // // (Until 2.0 the same test ran over the v1 route files, which held the markers.) // // The routes below check campuses inside their TypeScript service or handler instead, as their // `campusScope` text says. Adding a route here means its handler does the check itself. const CHECKED_IN_HANDLER = new Set([ 'GET /events/manage', 'POST /events', 'GET /sections', 'POST /uploads/event-image', 'GET /series', 'POST /series', 'PATCH /series/:id', 'GET /series/:id/compare', 'GET /season-passes', 'POST /season-passes', 'PATCH /season-passes/:id', 'DELETE /season-passes/:id', 'GET /coupons', 'GET /coupons/redemptions', 'GET /coupons/:id', 'POST /coupons', 'PATCH /coupons/:id', 'GET /campuses/:id/audience', 'GET /registrations', 'GET /saved-filters', 'POST /saved-filters', 'PUT /saved-filters/:id', 'DELETE /saved-filters/:id', 'GET /checkin/:qrCode', 'POST /checkin/:qrCode', ]); /* eslint-disable @typescript-eslint/no-require-imports */ const { v2Router } = require('../src/v2'); const { routes } = require('../src/v2/route'); type Route = { method: string; path: string; access: string; campusScope?: string; middleware?: { campusScope?: string }[] }; describe('campus scope on v2 staff and supervisor routes', () => { v2Router(); const staffRoutes = (routes as Route[]).filter(r => r.access === 'staff' || r.access === 'supervisor'); const key = (r: Route) => `${r.method.toUpperCase()} ${r.path}`; const marker = (r: Route) => (r.middleware || []).find(h => h && h.campusScope)?.campusScope; test('finds the staff and supervisor routes', () => { expect(staffRoutes.length).toBeGreaterThan(150); }); test('each one has a campus-scope middleware, or checks in its handler', () => { const missing = staffRoutes.filter(r => !marker(r) && !CHECKED_IN_HANDLER.has(key(r))).map(key); expect(missing).toEqual([]); }); test('the handler-checked list holds only real staff routes without a middleware', () => { const real = new Set(staffRoutes.filter(r => !marker(r)).map(key)); expect([...CHECKED_IN_HANDLER].filter(k => !real.has(k))).toEqual([]); }); test('a campus-independent route gives a reason', () => { for (const r of staffRoutes) { const m = marker(r); if (m && m.startsWith('independent')) expect(m.length).toBeGreaterThan('independent: '.length + 5); } }); });