Events, payments, registrations, scan locks and tickets marked; list controllers filtered (registrations, payments, tickets, scans, stats, coupons, scheduled messages, form responses, funnel). Owner-or-staff routes check staff against the record's event (ownerOk). Scanner names the campus of another campus's ticket. Route test passes; unit tests for the rules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
5.8 KiB
JavaScript
125 lines
5.8 KiB
JavaScript
const cs = require('../src/utils/campusScope');
|
|
|
|
const north = { campusIds: ['north'], names: ['North'] };
|
|
|
|
describe('campus scope rules', () => {
|
|
test('unscoped sees and manages everything', () => {
|
|
expect(cs.canView(null, 'south')).toBe(true);
|
|
expect(cs.canManage(null, null)).toBe(true);
|
|
expect(cs.eventWhere(null)).toEqual({});
|
|
});
|
|
|
|
test('scoped: own campus and church-wide events are visible, other campuses are not', () => {
|
|
expect(cs.canView(north, 'north')).toBe(true);
|
|
expect(cs.canView(north, null)).toBe(true);
|
|
expect(cs.canView(north, 'south')).toBe(false);
|
|
});
|
|
|
|
test('scoped: only own campus events can be managed — not church-wide ones', () => {
|
|
expect(cs.canManage(north, 'north')).toBe(true);
|
|
expect(cs.canManage(north, null)).toBe(false);
|
|
expect(cs.canManage(north, 'south')).toBe(false);
|
|
});
|
|
|
|
test('list filters, also through relations', () => {
|
|
const ev = { OR: [{ campusId: { in: ['north'] } }, { campusId: null }] };
|
|
expect(cs.eventWhere(north)).toEqual(ev);
|
|
expect(cs.manageWhere(north)).toEqual({ campusId: { in: ['north'] } });
|
|
expect(cs.relWhere(north, 'registration.event')).toEqual({ registration: { event: ev } });
|
|
expect(cs.relWhere(null, 'event')).toEqual({});
|
|
});
|
|
|
|
test('withScope never overwrites the other filter', () => {
|
|
const own = { OR: [{ a: 1 }, { b: 2 }] };
|
|
const scope = cs.eventWhere(north);
|
|
expect(cs.withScope(own, scope)).toEqual({ AND: [own, scope] });
|
|
expect(cs.withScope(own, {})).toBe(own);
|
|
expect(cs.withScope({}, scope)).toBe(scope);
|
|
});
|
|
|
|
test('coupons: only when limited to events, all at own campuses', () => {
|
|
expect(cs.couponAllowed(null, [])).toBe(true);
|
|
expect(cs.couponAllowed(north, [])).toBe(false); // every event
|
|
expect(cs.couponAllowed(north, ['north'])).toBe(true);
|
|
expect(cs.couponAllowed(north, ['north', null])).toBe(false); // a church-wide event
|
|
expect(cs.couponAllowed(north, ['north', 'south'])).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('campus scope middleware', () => {
|
|
// The scope is preset on the request (scopeFor caches it there) and the resolvers are stubbed,
|
|
// so these run without a database.
|
|
const saved = { ...cs.RESOLVERS };
|
|
const events = {
|
|
n1: { id: 'n1', campusId: 'north', campusName: 'North', owners: ['u-owner'] },
|
|
s1: { id: 's1', campusId: 'south', campusName: 'South', owners: ['u-owner'] },
|
|
all: { id: 'all', campusId: null, campusName: null, owners: [] },
|
|
};
|
|
beforeAll(() => {
|
|
for (const k of ['event', 'registration', 'ticketQr', 'payment']) cs.RESOLVERS[k] = async id => events[id] || null;
|
|
});
|
|
afterAll(() => Object.assign(cs.RESOLVERS, saved));
|
|
|
|
const run = async (mw, { scope = north, params = {}, body = {}, userId = 'u-staff' } = {}) => {
|
|
const req = { _campusScope: scope, params, body, user: { id: userId, role: 'supervisor' } };
|
|
const out = { status: 200, body: null, next: false };
|
|
const res = { status(c) { out.status = c; return this; }, json(b) { out.body = b; return this; } };
|
|
await mw(req, res, () => { out.next = true; });
|
|
return out;
|
|
};
|
|
|
|
test('scopeEvent: own campus and church-wide pass, another campus is "not found"', async () => {
|
|
const mw = cs.scopeEvent();
|
|
expect((await run(mw, { params: { id: 'n1' } })).next).toBe(true);
|
|
expect((await run(mw, { params: { id: 'all' } })).next).toBe(true);
|
|
const other = await run(mw, { params: { id: 's1' } });
|
|
expect(other.next).toBe(false);
|
|
expect(other.status).toBe(404);
|
|
});
|
|
|
|
test('scopeEvent: unscoped always passes; unknown records pass to the controller', async () => {
|
|
expect((await run(cs.scopeEvent(), { scope: null, params: { id: 's1' } })).next).toBe(true);
|
|
expect((await run(cs.scopeEvent(), { params: { id: 'nope' } })).next).toBe(true);
|
|
});
|
|
|
|
test("scopeEvent need 'manage': church-wide events are refused with a reason", async () => {
|
|
const mw = cs.scopeEvent({ need: 'manage' });
|
|
expect((await run(mw, { params: { id: 'n1' } })).next).toBe(true);
|
|
const all = await run(mw, { params: { id: 'all' } });
|
|
expect(all.status).toBe(403);
|
|
expect(all.body.code).toBe('CHURCH_WIDE_EVENT');
|
|
});
|
|
|
|
test('scopeEvent ownerOk: the owner of another campus\'s record still gets through', async () => {
|
|
const mw = cs.scopeEvent({ resolve: 'registration', ownerOk: true });
|
|
expect((await run(mw, { params: { id: 's1' }, userId: 'u-owner' })).next).toBe(true);
|
|
expect((await run(mw, { params: { id: 's1' } })).status).toBe(404);
|
|
});
|
|
|
|
test('the scanner names the campus of another campus\'s ticket', async () => {
|
|
const mw = cs.scopeEvent({ resolve: 'ticketQr', from: r => r.params.qrCode, nameCampus: true });
|
|
const out = await run(mw, { params: { qrCode: 's1' } });
|
|
expect(out.status).toBe(403);
|
|
expect(out.body).toEqual({ code: 'OTHER_CAMPUS', message: 'This ticket is for an event at South.' });
|
|
});
|
|
|
|
test('scopeBody: every target and every id in a list is checked', async () => {
|
|
const mw = cs.scopeBody([['registrationId', 'registration'], ['ticketIds', 'ticket']]);
|
|
cs.RESOLVERS.ticket = async id => events[id] || null;
|
|
expect((await run(mw, { body: { ticketIds: ['n1', 'all'] } })).next).toBe(true);
|
|
expect((await run(mw, { body: { ticketIds: ['n1', 's1'] } })).status).toBe(404);
|
|
});
|
|
|
|
test("scopeBody orphan 'deny': a payment not linked to any event is unscoped-only", async () => {
|
|
const mw = cs.scopeBody([['registrationId', 'registration'], ['eventId', 'event']], { orphan: 'deny' });
|
|
expect((await run(mw, { body: {} })).status).toBe(403);
|
|
expect((await run(mw, { body: { eventId: 'n1' } })).next).toBe(true);
|
|
expect((await run(mw, { scope: null, body: {} })).next).toBe(true);
|
|
});
|
|
|
|
test('unscopedOnly refuses scoped users', async () => {
|
|
expect((await run(cs.unscopedOnly)).status).toBe(403);
|
|
expect((await run(cs.unscopedOnly, { scope: null })).next).toBe(true);
|
|
});
|
|
});
|