-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathdata.controller.read.test.js
More file actions
104 lines (90 loc) · 2.8 KB
/
Copy pathdata.controller.read.test.js
File metadata and controls
104 lines (90 loc) · 2.8 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
'use strict';
const mockFind = jest.fn();
const mockAnd = jest.fn();
const mockFindOne = jest.fn();
const mockQueryLean = jest.fn().mockResolvedValue([]);
// 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; },
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, lean: mockQueryLean };
},
findOne: (...args) => {
mockFindOne(...args);
return { lean: jest.fn().mockResolvedValue({ _id: 'doc_1' }) };
},
})),
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();
});
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();
});
});