|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const authorizeReadOperation = require('../middlewares/authorizeReadOperation'); |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Helpers |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +function makeProject(rlsOverrides = {}) { |
| 10 | + return { |
| 11 | + _id: 'proj_1', |
| 12 | + collections: [ |
| 13 | + { |
| 14 | + name: 'posts', |
| 15 | + rls: { |
| 16 | + enabled: true, |
| 17 | + mode: 'public-read', |
| 18 | + ownerField: 'userId', |
| 19 | + requireAuthForWrite: true, |
| 20 | + ...rlsOverrides, |
| 21 | + }, |
| 22 | + }, |
| 23 | + ], |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function makeReq(overrides = {}) { |
| 28 | + return { |
| 29 | + keyRole: 'publishable', |
| 30 | + params: { collectionName: 'posts' }, |
| 31 | + project: makeProject(), |
| 32 | + authUser: null, |
| 33 | + rlsFilter: undefined, |
| 34 | + ...overrides, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function makeRes() { |
| 39 | + const res = { |
| 40 | + statusCode: null, |
| 41 | + body: null, |
| 42 | + status: jest.fn().mockReturnThis(), |
| 43 | + json: jest.fn().mockReturnThis(), |
| 44 | + }; |
| 45 | + res.status.mockImplementation((code) => { |
| 46 | + res.statusCode = code; |
| 47 | + return res; |
| 48 | + }); |
| 49 | + res.json.mockImplementation((data) => { |
| 50 | + res.body = data; |
| 51 | + return res; |
| 52 | + }); |
| 53 | + return res; |
| 54 | +} |
| 55 | + |
| 56 | +// --------------------------------------------------------------------------- |
| 57 | +// Tests |
| 58 | +// --------------------------------------------------------------------------- |
| 59 | + |
| 60 | +describe('authorizeReadOperation middleware', () => { |
| 61 | + let next; |
| 62 | + |
| 63 | + beforeEach(() => { |
| 64 | + next = jest.fn(); |
| 65 | + }); |
| 66 | + |
| 67 | + test('secret key bypass sets empty filter', async () => { |
| 68 | + const req = makeReq({ keyRole: 'secret' }); |
| 69 | + const res = makeRes(); |
| 70 | + |
| 71 | + await authorizeReadOperation(req, res, next); |
| 72 | + |
| 73 | + expect(next).toHaveBeenCalledTimes(1); |
| 74 | + expect(req.rlsFilter).toEqual({}); |
| 75 | + }); |
| 76 | + |
| 77 | + test('returns 404 when collection is not found', async () => { |
| 78 | + const req = makeReq({ params: { collectionName: 'unknown' } }); |
| 79 | + const res = makeRes(); |
| 80 | + |
| 81 | + await authorizeReadOperation(req, res, next); |
| 82 | + |
| 83 | + expect(res.statusCode).toBe(404); |
| 84 | + expect(res.body.error).toBe('Collection not found'); |
| 85 | + expect(next).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + test('rls disabled allows public read', async () => { |
| 89 | + const req = makeReq({ project: makeProject({ enabled: false }) }); |
| 90 | + const res = makeRes(); |
| 91 | + |
| 92 | + await authorizeReadOperation(req, res, next); |
| 93 | + |
| 94 | + expect(next).toHaveBeenCalledTimes(1); |
| 95 | + expect(req.rlsFilter).toEqual({}); |
| 96 | + }); |
| 97 | + |
| 98 | + test('public-read allows read without auth', async () => { |
| 99 | + const req = makeReq({ authUser: null }); |
| 100 | + const res = makeRes(); |
| 101 | + |
| 102 | + await authorizeReadOperation(req, res, next); |
| 103 | + |
| 104 | + expect(next).toHaveBeenCalledTimes(1); |
| 105 | + expect(req.rlsFilter).toEqual({}); |
| 106 | + }); |
| 107 | + |
| 108 | + test('owner-write-only is treated as public-read', async () => { |
| 109 | + const req = makeReq({ project: makeProject({ mode: 'owner-write-only' }) }); |
| 110 | + const res = makeRes(); |
| 111 | + |
| 112 | + await authorizeReadOperation(req, res, next); |
| 113 | + |
| 114 | + expect(next).toHaveBeenCalledTimes(1); |
| 115 | + expect(req.rlsFilter).toEqual({}); |
| 116 | + }); |
| 117 | + |
| 118 | + test('private mode requires auth', async () => { |
| 119 | + const req = makeReq({ project: makeProject({ mode: 'private' }) }); |
| 120 | + const res = makeRes(); |
| 121 | + |
| 122 | + await authorizeReadOperation(req, res, next); |
| 123 | + |
| 124 | + expect(res.statusCode).toBe(401); |
| 125 | + expect(res.body.error).toBe('Authentication required'); |
| 126 | + expect(next).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + test('private mode sets owner filter when authed', async () => { |
| 130 | + const req = makeReq({ |
| 131 | + project: makeProject({ mode: 'private', ownerField: 'userId' }), |
| 132 | + authUser: { userId: 'user_abc' }, |
| 133 | + }); |
| 134 | + const res = makeRes(); |
| 135 | + |
| 136 | + await authorizeReadOperation(req, res, next); |
| 137 | + |
| 138 | + expect(next).toHaveBeenCalledTimes(1); |
| 139 | + expect(req.rlsFilter).toEqual({ userId: 'user_abc' }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments