// Every staff/supervisor route must say how it handles campus-scoped staff (utils/campusScope.js): // scopeEvent(...), scopeList, unscopedOnly or campusIndependent('reason'). A route that says // nothing would show a campus-limited supervisor every campus's data, so this fails on it. const fs = require('fs'); const path = require('path'); const { staff, supervisor } = require('../src/middleware/authMiddleware'); const ROUTES_DIR = path.join(__dirname, '..', 'src', 'routes'); function staffRoutes() { const out = []; for (const file of fs.readdirSync(ROUTES_DIR).filter(f => f.endsWith('.js'))) { const router = require(path.join(ROUTES_DIR, file)); for (const layer of router.stack || []) { if (!layer.route) continue; const handles = layer.route.stack.map(s => s.handle); const role = handles.includes(staff) || handles.includes(supervisor); if (!role) continue; for (const method of Object.keys(layer.route.methods)) { // router.route('/x').get(...).put(...) puts every method's handlers on one route. const forMethod = layer.route.stack.filter(s => s.method === method || s.method === undefined).map(s => s.handle); const marker = forMethod.find(h => h && h.campusScope); out.push({ file, method: method.toUpperCase(), path: layer.route.path, marker: marker ? marker.campusScope : null }); } } } return out; } describe('campus scope on staff and supervisor routes', () => { const routes = staffRoutes(); test('finds the staff and supervisor routes', () => { expect(routes.length).toBeGreaterThan(100); }); test('every one says how it handles campus-scoped staff', () => { const missing = routes.filter(r => !r.marker).map(r => `${r.file} ${r.method} ${r.path}`); expect(missing).toEqual([]); }); test('a campus-independent route gives a reason', () => { for (const r of routes.filter(x => x.marker && x.marker.startsWith('independent'))) { expect(r.marker.length).toBeGreaterThan('independent: '.length + 5); } }); });