|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const mockAggregate = jest.fn(); |
| 4 | + |
| 5 | +jest.mock('@urbackend/common', () => ({ |
| 6 | + sanitize: (v) => v, |
| 7 | + Project: {}, |
| 8 | + getConnection: jest.fn().mockResolvedValue({}), |
| 9 | + getCompiledModel: jest.fn(() => ({ |
| 10 | + aggregate: mockAggregate, |
| 11 | + })), |
| 12 | + QueryEngine: jest.fn(), |
| 13 | + validateData: jest.fn(), |
| 14 | + validateUpdateData: jest.fn(), |
| 15 | + aggregateSchema: require('../../../../packages/common/src/utils/input.validation').aggregateSchema, |
| 16 | +})); |
| 17 | + |
| 18 | +const { aggregateData } = require('../controllers/data.controller'); |
| 19 | + |
| 20 | +function makeReq(overrides = {}) { |
| 21 | + return { |
| 22 | + params: { collectionName: 'posts' }, |
| 23 | + project: { |
| 24 | + _id: 'proj_1', |
| 25 | + resources: { db: { isExternal: false } }, |
| 26 | + collections: [{ name: 'posts', model: [] }], |
| 27 | + }, |
| 28 | + body: { pipeline: [{ $group: { _id: '$status', count: { $sum: 1 } } }] }, |
| 29 | + rlsFilter: {}, |
| 30 | + ...overrides, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function makeRes() { |
| 35 | + const res = { |
| 36 | + statusCode: null, |
| 37 | + body: null, |
| 38 | + status: jest.fn().mockReturnThis(), |
| 39 | + json: jest.fn().mockReturnThis(), |
| 40 | + }; |
| 41 | + res.status.mockImplementation((code) => { |
| 42 | + res.statusCode = code; |
| 43 | + return res; |
| 44 | + }); |
| 45 | + res.json.mockImplementation((data) => { |
| 46 | + res.body = data; |
| 47 | + return res; |
| 48 | + }); |
| 49 | + return res; |
| 50 | +} |
| 51 | + |
| 52 | +describe('aggregateData controller', () => { |
| 53 | + beforeEach(() => { |
| 54 | + jest.clearAllMocks(); |
| 55 | + mockAggregate.mockResolvedValue([{ _id: 'published', count: 2 }]); |
| 56 | + }); |
| 57 | + |
| 58 | + test('executes a valid aggregation pipeline', async () => { |
| 59 | + const req = makeReq(); |
| 60 | + const res = makeRes(); |
| 61 | + |
| 62 | + await aggregateData(req, res); |
| 63 | + |
| 64 | + expect(mockAggregate).toHaveBeenCalledWith([ |
| 65 | + { $group: { _id: '$status', count: { $sum: 1 } } }, |
| 66 | + ]); |
| 67 | + expect(res.statusCode).toBe(200); |
| 68 | + expect(res.body).toEqual({ |
| 69 | + success: true, |
| 70 | + data: [{ _id: 'published', count: 2 }], |
| 71 | + message: 'Aggregation executed successfully.', |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + test('prepends the RLS match stage when req.rlsFilter is set', async () => { |
| 76 | + const req = makeReq({ |
| 77 | + rlsFilter: { userId: 'user_1' }, |
| 78 | + body: { |
| 79 | + pipeline: [ |
| 80 | + { $match: { status: 'published' } }, |
| 81 | + { $sort: { createdAt: -1 } }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + }); |
| 85 | + const res = makeRes(); |
| 86 | + |
| 87 | + await aggregateData(req, res); |
| 88 | + |
| 89 | + expect(mockAggregate).toHaveBeenCalledWith([ |
| 90 | + { $match: { userId: 'user_1' } }, |
| 91 | + { $match: { status: 'published' } }, |
| 92 | + { $sort: { createdAt: -1 } }, |
| 93 | + ]); |
| 94 | + expect(res.statusCode).toBe(200); |
| 95 | + }); |
| 96 | + |
| 97 | + test('blocks write-capable aggregation stages', async () => { |
| 98 | + const req = makeReq({ |
| 99 | + body: { pipeline: [{ $match: { status: 'published' } }, { $out: 'archive' }] }, |
| 100 | + }); |
| 101 | + const res = makeRes(); |
| 102 | + |
| 103 | + await aggregateData(req, res); |
| 104 | + |
| 105 | + expect(mockAggregate).not.toHaveBeenCalled(); |
| 106 | + expect(res.statusCode).toBe(400); |
| 107 | + expect(res.body).toEqual({ |
| 108 | + success: false, |
| 109 | + data: {}, |
| 110 | + message: 'Aggregation pipeline contains blocked stage.', |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + test('rejects invalid pipeline payloads', async () => { |
| 115 | + const req = makeReq({ |
| 116 | + body: { pipeline: { $group: { _id: '$status' } } }, |
| 117 | + }); |
| 118 | + const res = makeRes(); |
| 119 | + |
| 120 | + await aggregateData(req, res); |
| 121 | + |
| 122 | + expect(mockAggregate).not.toHaveBeenCalled(); |
| 123 | + expect(res.statusCode).toBe(400); |
| 124 | + expect(res.body).toEqual({ |
| 125 | + success: false, |
| 126 | + data: {}, |
| 127 | + message: 'Invalid input: expected array, received object', |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments