Skip to content

Commit 2b40a4f

Browse files
feat: add tests for middlewares/auth/paths.js
1 parent dda5fbd commit 2b40a4f

File tree

2 files changed

+206
-2
lines changed

2 files changed

+206
-2
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import { authMiddlewareFunc } from 'server/middlewares/auth/utils';
2+
import { paths } from 'server/middlewares/auth/paths';
3+
import { SCOPE_TYPE } from 'utils/constants';
4+
import { isEqual } from 'lodash';
5+
6+
jest.mock('server/middlewares/auth/utils', () => ({
7+
authMiddlewareFunc: jest.fn()
8+
}));
9+
10+
const testPaths = [
11+
{
12+
path: '/assign-roles',
13+
scopes: [SCOPE_TYPE.SUPER_ADMIN],
14+
method: 'PUT'
15+
},
16+
{
17+
path: '/roles',
18+
scopes: [SCOPE_TYPE.SUPER_ADMIN],
19+
method: 'POST'
20+
},
21+
{
22+
path: '/stores',
23+
scopes: [SCOPE_TYPE.SUPER_ADMIN],
24+
method: 'POST'
25+
},
26+
{
27+
path: '/aggregate/order-amount',
28+
scopes: [SCOPE_TYPE.SUPER_ADMIN],
29+
method: 'GET'
30+
},
31+
{
32+
path: '/orders',
33+
method: 'POST'
34+
},
35+
{
36+
path: '/orders/:_id',
37+
method: 'GET'
38+
},
39+
{
40+
path: '/orders',
41+
method: 'GET'
42+
},
43+
{
44+
path: '/referenced-orders',
45+
method: 'GET'
46+
},
47+
{
48+
path: '/unsharded-orders',
49+
method: 'GET'
50+
},
51+
{
52+
path: '/unsharded-referenced-orders',
53+
method: 'GET'
54+
},
55+
{
56+
path: '/stores',
57+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
58+
method: 'GET',
59+
hasCustomAuth: true
60+
},
61+
{
62+
path: '/stores/:_id',
63+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
64+
method: 'GET',
65+
hasCustomAuth: true
66+
},
67+
{
68+
path: '/stores/:_id',
69+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
70+
method: 'PATCH',
71+
hasCustomAuth: true
72+
},
73+
74+
{
75+
path: '/stores/:_id',
76+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
77+
method: 'DELETE',
78+
hasCustomAuth: true
79+
},
80+
{
81+
path: '/store-products',
82+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
83+
method: 'POST',
84+
hasCustomAuth: true
85+
},
86+
{
87+
path: '/store-products',
88+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
89+
method: 'GET',
90+
hasCustomAuth: true
91+
},
92+
{
93+
path: '/store-products/:_id',
94+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
95+
method: 'GET',
96+
hasCustomAuth: true
97+
},
98+
{
99+
path: '/store-products/:_id',
100+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.STORE_ADMIN],
101+
method: 'DELETE',
102+
hasCustomAuth: true
103+
},
104+
{
105+
path: '/suppliers',
106+
scopes: [SCOPE_TYPE.SUPER_ADMIN],
107+
method: 'POST'
108+
},
109+
{
110+
path: '/suppliers/:_id',
111+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
112+
method: 'GET',
113+
hasCustomAuth: true
114+
},
115+
{
116+
path: '/suppliers',
117+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
118+
method: 'GET',
119+
hasCustomAuth: true
120+
},
121+
{
122+
path: '/suppliers/:_id',
123+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
124+
method: 'PATCH',
125+
hasCustomAuth: true
126+
},
127+
{
128+
path: '/suppliers/:_id',
129+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
130+
method: 'DELETE',
131+
hasCustomAuth: true
132+
},
133+
{
134+
path: '/supplier-products',
135+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
136+
method: 'POST',
137+
hasCustomAuth: true
138+
},
139+
{
140+
path: '/supplier-products',
141+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
142+
method: 'GET',
143+
hasCustomAuth: true
144+
},
145+
{
146+
path: '/supplier-products/:_id',
147+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
148+
method: 'GET',
149+
hasCustomAuth: true
150+
},
151+
{
152+
path: '/supplier-products/:_id',
153+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
154+
method: 'PATCH',
155+
hasCustomAuth: true
156+
},
157+
{
158+
path: '/supplier-products/:_id',
159+
scopes: [SCOPE_TYPE.SUPER_ADMIN, SCOPE_TYPE.SUPPLIER_ADMIN],
160+
method: 'DELETE',
161+
hasCustomAuth: true
162+
}
163+
];
164+
describe('paths', () => {
165+
it('check if all the paths are present', async () => {
166+
let i = 0;
167+
function checkIfPathMatches(path, testPath) {
168+
return path.path.toUpperCase() === testPath.path.toUpperCase();
169+
}
170+
function checkIfMethodMatches(path, testPath) {
171+
return path.method.toUpperCase() === testPath.method.toUpperCase();
172+
}
173+
function checkIfScopesMatch(path, testPath) {
174+
return isEqual(path.scopes, testPath.scopes);
175+
}
176+
await Promise.all(
177+
testPaths.map(async testPath => {
178+
let foundPath = false;
179+
await Promise.all(
180+
paths.map(async path => {
181+
if (
182+
checkIfMethodMatches(path, testPath) &&
183+
checkIfPathMatches(path, testPath) &&
184+
checkIfScopesMatch(path, testPath)
185+
) {
186+
foundPath = true;
187+
188+
if (testPath.hasCustomAuth) {
189+
path.authMiddleware(
190+
{ params: {}, user: {}, body: {} },
191+
{},
192+
() => {}
193+
);
194+
expect(
195+
authMiddlewareFunc
196+
).toHaveBeenCalledTimes(++i);
197+
}
198+
}
199+
})
200+
);
201+
expect(foundPath).toBe(true);
202+
})
203+
);
204+
});
205+
});

jest.config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
"!**/node_modules/**",
77
"!**/dist/**",
88
"!**/models/**",
9-
"__tests__/__load__/libs/**/*.*"
9+
"!__tests__/__load__/libs/**/*.*"
1010
],
11-
"testRegex": "(/__tests__/.*\\.test)\\.js$",
1211
"coverageReporters": ["json-summary", "text", "lcov"],
1312
"testPathIgnorePatterns": ["<rootDir>/dist/"],
1413
"moduleNameMapper": {

0 commit comments

Comments
 (0)