Skip to content

Commit fb14eaa

Browse files
Merge pull request #61 from wednesday-solutions/feat/auth
feat: move all auth to paths.js
2 parents 637ce4a + 2b40a4f commit fb14eaa

File tree

10 files changed

+364
-34
lines changed

10 files changed

+364
-34
lines changed

.eslintignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
.DS_Store
2+
# Logs
3+
logs
4+
*.log
5+
npm-debug.log*
6+
yarn-debug.log*
7+
yarn-error.log*
8+
9+
# Runtime data
10+
pids
11+
*.pid
12+
*.seed
13+
*.pid.lock
14+
15+
# Directory for instrumented libs generated by jscoverage/JSCover
16+
lib-cov
17+
18+
# Coverage directory used by tools like istanbul
19+
coverage
20+
21+
# nyc test coverage
22+
.nyc_output
23+
24+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
25+
.grunt
26+
27+
# Bower dependency directory (https://bower.io/)
28+
bower_components
29+
30+
# node-waf configuration
31+
.lock-wscript
32+
33+
# Compiled binary addons (https://nodejs.org/api/addons.html)
34+
build/Release
35+
36+
# Dependency directories
37+
node_modules/
38+
jspm_packages/
39+
40+
# TypeScript v1 declaration files
41+
typings/
42+
43+
# Optional npm cache directory
44+
.npm
45+
46+
# Optional eslint cache
47+
.eslintcache
48+
49+
# Optional REPL history
50+
.node_repl_history
51+
52+
# Output of 'npm pack'
53+
*.tgz
54+
55+
# Yarn Integrity file
56+
.yarn-integrity
57+
58+
# dotenv environment variables file
59+
.env
60+
.env.local
61+
62+
# next.js build output
63+
.next
64+
__tests__/__load__/libs

__tests__/server/api/orders/index.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { redis } from 'services/redis';
1414
import { mockData } from 'utils/mockData';
1515
const { MOCK_ORDER_DETAILS: mockOrderDetails, MOCK_ORDER: mockOrder } =
1616
mockData;
17-
17+
jest.mock('middlewares/auth', () => ({
18+
checkJwt: (req, res, next) => {
19+
next();
20+
}
21+
}));
1822
describe('Order tests', () => {
1923
const date = '1994-10-24';
2024
const amt = 25000;

__tests__/server/api/referencedOrders/index.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import app from 'server';
44
import { mockData } from 'utils/mockData';
55

66
const { MOCK_UNSHARDED_REFERENCED_ORDERS: mockReferencedOrders } = mockData;
7-
7+
jest.mock('middlewares/auth', () => ({
8+
checkJwt: (req, res, next) => {
9+
next();
10+
}
11+
}));
812
describe('fetchAllReferencedOrders tests', () => {
913
let MODEL_NAME;
1014
let ENDPOINT;

__tests__/server/api/unshardedReferencedOrders/index.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import kebabCase from 'lodash/kebabCase';
55

66
const { MOCK_UNSHARDED_REFERENCED_ORDERS: mockUnshardedReferencedOrders } =
77
mockData;
8-
8+
jest.mock('middlewares/auth', () => ({
9+
checkJwt: (req, res, next) => {
10+
next();
11+
}
12+
}));
913
describe('fetchAllUnshardedReferencedOrders tests', () => {
1014
let MODEL_NAME;
1115
let ENDPOINT;
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"**/server/**",
66
"!**/node_modules/**",
77
"!**/dist/**",
8-
"!**/models/**"
8+
"!**/models/**",
9+
"!__tests__/__load__/libs/**/*.*"
910
],
1011
"coverageReporters": ["json-summary", "text", "lcov"],
1112
"testPathIgnorePatterns": ["<rootDir>/dist/"],

server/api/routes/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
aggregatedOrderAmountValidator,
88
fetchAggregatedOrderAmount
99
} from 'api/aggregate/orders';
10-
import { checkJwt } from 'middlewares/auth';
1110
import { rateLimiter as limiter } from 'middlewares/rateLimiter';
1211

1312
const router = express.Router();
@@ -20,13 +19,12 @@ const rateLimiter = limiter({
2019
});
2120

2221
router.post('/login', loginValidator, rateLimiter, login);
23-
router.post('/roles', rateLimiter, checkJwt, roleValidator, roles);
24-
router.put('/assign-roles', checkJwt, assignRoleValidator, assignRoles);
22+
router.post('/roles', rateLimiter, roleValidator, roles);
23+
router.put('/assign-roles', assignRoleValidator, assignRoles);
2524
router.post('/cron-job', cronJobValidator, cronJob);
2625

2726
router.get(
2827
'/aggregate/order-amount',
29-
checkJwt,
3028
aggregatedOrderAmountValidator,
3129
fetchAggregatedOrderAmount
3230
);

server/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import log from 'utils/logger';
1111
import { isTestEnv } from 'utils';
1212
import { initQueues } from 'utils/queue';
1313
import { injectRequestId } from 'middlewares/injectRequestId';
14+
import { checkJwt } from 'middlewares/auth';
1415
import { middleware as contextMiddleware } from 'express-http-context';
1516

1617
/**
@@ -28,6 +29,7 @@ app.use(express.json());
2829
// get information from html forms
2930
app.use(bodyParser.json({ limit: '10mb' }));
3031
app.use(bodyParser.urlencoded({ extended: true }));
32+
app.use(checkJwt);
3133

3234
// used for getting and setting request-scoped context anywhere
3335
app.use(contextMiddleware);

0 commit comments

Comments
 (0)