Repository navigation
Expand file tree
/
Copy pathdata.controller.read.test.js
More file actions
168 lines (142 loc) · 5.15 KB
/
Copy pathdata.controller.read.test.js
File metadata and controls
168 lines (142 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const mockFind = jest.fn();
const mockAnd = jest.fn();
const mockFindOne = jest.fn();
const mockQueryLean = jest.fn().mockResolvedValue([]);
const mockPopulate = jest.fn();
const mockEnginePopulate = jest.fn();
// mockAnd returns an object with .lean() so features.query.lean() works after .and()
mockAnd.mockReturnValue({ lean: mockQueryLean });
const mockQueryEngine = jest.fn((query) => {
const engine = {
query,
filter() { return engine; },
sort() { return engine; },
populate() { mockEnginePopulate(...arguments); return engine; },
paginate() { return engine; },
};
return engine;
});
jest.mock('@urbackend/common', () => ({
sanitize: (v) => v,
Project: {},
getConnection: jest.fn().mockResolvedValue({}),
getCompiledModel: jest.fn(() => ({
find: (...args) => {
mockFind(...args);
return {
and: mockAnd,
populate: (...pArgs) => {
mockPopulate(...pArgs);
return { lean: mockQueryLean };
},
lean: mockQueryLean
};
},
findOne: (...args) => {
mockFindOne(...args);
const chainable = {
populate: (...pArgs) => {
mockPopulate(...pArgs);
return chainable;
},
lean: jest.fn().mockResolvedValue({ _id: 'doc_1' }),
};
return chainable;
},
})),
QueryEngine: mockQueryEngine,
validateData: jest.fn(),
validateUpdateData: jest.fn(),
}));
const { getAllData, getSingleDoc } = require('../controllers/data.controller');
function makeReq(overrides = {}) {
return {
params: { collectionName: 'posts', id: '507f1f77bcf86cd799439011' },
project: {
_id: 'proj_1',
resources: { db: { isExternal: false } },
collections: [{ name: 'posts', model: [] }],
},
query: {},
rlsFilter: {},
...overrides,
};
}
function makeRes() {
const res = {
statusCode: null,
body: null,
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
res.status.mockImplementation((code) => {
res.statusCode = code;
return res;
});
res.json.mockImplementation((data) => {
res.body = data;
return res;
});
return res;
}
describe('data.controller read RLS filters', () => {
beforeEach(() => {
jest.clearAllMocks();
mockEnginePopulate.mockClear();
});
test('getAllData applies rlsFilter to find()', async () => {
const req = makeReq({ rlsFilter: { userId: 'user_1' } });
const res = makeRes();
await getAllData(req, res);
expect(mockFind).toHaveBeenCalledWith();
expect(mockAnd).toHaveBeenCalledWith([{ userId: 'user_1' }]);
expect(res.json).toHaveBeenCalled();
});
test('getSingleDoc applies rlsFilter to findOne()', async () => {
const req = makeReq({ rlsFilter: { userId: 'user_1' } });
const res = makeRes();
await getSingleDoc(req, res);
expect(mockFindOne).toHaveBeenCalledWith({
$and: [
{ _id: '507f1f77bcf86cd799439011' },
{ userId: 'user_1' },
],
});
expect(res.json).toHaveBeenCalled();
});
test('getAllData calls engine.populate() when populate param is provided', async () => {
const req = makeReq({ query: { populate: 'author,comments' } });
const res = makeRes();
await getAllData(req, res);
expect(mockQueryEngine).toHaveBeenCalled();
expect(mockEnginePopulate).toHaveBeenCalled();
});
test('getAllData does not forward populate/expand to Mongo filter', async () => {
const req = makeReq({ query: { populate: 'author', expand: 'category', title: 'hello' } });
const res = makeRes();
await getAllData(req, res);
// mockFind is called with the raw Mongoose query (no args for Model.find())
// The real filter exclusion is tested via the QueryEngine directly,
// but we confirm find() was invoked and the request did not error out.
expect(mockFind).toHaveBeenCalledWith();
expect(res.json).toHaveBeenCalled();
expect(res.status).not.toHaveBeenCalledWith(500);
});
test('getSingleDoc calls populate on the query', async () => {
const req = makeReq({ query: { populate: 'author' } });
const res = makeRes();
await getSingleDoc(req, res);
expect(mockPopulate).toHaveBeenCalledWith('author');
expect(res.json).toHaveBeenCalled();
});
test('getSingleDoc handles array-format populate param without crashing', async () => {
const req = makeReq({ query: { populate: ['author', 'category'] } });
const res = makeRes();
await getSingleDoc(req, res);
expect(mockPopulate).toHaveBeenCalledWith('author');
expect(mockPopulate).toHaveBeenCalledWith('category');
expect(res.json).toHaveBeenCalled();
expect(res.status).not.toHaveBeenCalledWith(500);
});
});