Skip to content

Commit 9a2b386

Browse files
author
Rushabh Sancheti
committed
feat: merging main into feat/multi-stage-docker
2 parents d742947 + 4319277 commit 9a2b386

File tree

9 files changed

+161
-33
lines changed

9 files changed

+161
-33
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ typings/
5858
# dotenv environment variables file
5959
.env
6060
.env.local
61+
.env.development
62+
.env.docker
6163

6264
# next.js build output
6365
.next

__tests__/server/api/requestGenerators.test.js

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ describe('requestGenerators tests', () => {
3232
beforeAll(() => {
3333
ENDPOINT = '/products';
3434
});
35+
36+
const mockProduct = {
37+
_id: '62bd7bd6151f31799cc670a6',
38+
name: 'Bat',
39+
price: 29,
40+
category: 'Sports',
41+
quantity: 10
42+
};
3543
describe('generatePostRequest tests', () => {
44+
const reqBody = mockProduct;
3645
it('should generatePostRoute should perform success response', async () => {
37-
const reqBody = {
38-
name: 'Jane Doe'
39-
};
46+
delete reqBody['_id'];
4047
const createSpy = jest
4148
.spyOn(daoUtils, 'createItem')
4249
.mockImplementation((_, body) => Promise.resolve(body));
@@ -50,7 +57,7 @@ describe('requestGenerators tests', () => {
5057
.send(reqBody);
5158
expect(res.statusCode).toBe(200);
5259
expect(createSpy).toBeCalled();
53-
expect(res.body.data).toEqual(reqBody);
60+
expect(res.body.data).toEqual(mockProduct);
5461
});
5562

5663
it('should generatePostRoute should perform error response', async () => {
@@ -65,10 +72,23 @@ describe('requestGenerators tests', () => {
6572
Accept: 'application/json',
6673
Authorization: 'Bearer dummy-token'
6774
})
68-
.send({});
75+
.send(reqBody);
6976
expect(res.statusCode).toBe(500);
7077
expect(res.body.error).toEqual(error.message);
7178
});
79+
80+
it('should return an error from validateSchema middleware', async () => {
81+
const res = await supertest(app)
82+
.post(ENDPOINT)
83+
.set({
84+
Accept: 'application/json',
85+
Authorization: 'Bearer dummy-token'
86+
})
87+
.send({
88+
name: 'Bat'
89+
});
90+
expect(res.statusCode).toBe(500);
91+
});
7292
});
7393

7494
describe('generatePatchRequest tests', () => {
@@ -77,7 +97,7 @@ describe('requestGenerators tests', () => {
7797
jest.spyOn(daoUtils, 'updateItem').mockResolvedValue(returnItem);
7898

7999
const res = await supertest(app)
80-
.patch(`${ENDPOINT}/7`)
100+
.patch(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
81101
.set({
82102
Accept: 'application/json',
83103
Authorization: 'Bearer dummy-token'
@@ -92,15 +112,43 @@ describe('requestGenerators tests', () => {
92112
jest.spyOn(daoUtils, 'updateItem').mockRejectedValue(error);
93113

94114
const res = await supertest(app)
95-
.patch(`${ENDPOINT}/7`)
115+
.patch(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
96116
.set({
97117
Accept: 'application/json',
98118
Authorization: 'Bearer dummy-token'
99119
})
100-
.send({});
120+
.send({ name: 'Jane Doe' });
101121
expect(res.statusCode).toBe(500);
102122
expect(res.body).toEqual({ error: error.message });
103123
});
124+
125+
it('should return error from validateObjectId middleware', async () => {
126+
const res = await supertest(app)
127+
.patch(`${ENDPOINT}/62bd7bd6151f31799cc670a`)
128+
.set({
129+
Accept: 'application/json',
130+
Authorization: 'Bearer dummy-token'
131+
})
132+
.send({ name: 'Jane Doe' });
133+
expect(res.statusCode).toBe(500);
134+
expect(res.error.text).toEqual(
135+
'{"error":{"message":"Invalid ObjectId"}}'
136+
);
137+
});
138+
139+
it('should return error from validateReqBody middleware', async () => {
140+
const res = await supertest(app)
141+
.patch(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
142+
.set({
143+
Accept: 'application/json',
144+
Authorization: 'Bearer dummy-token'
145+
})
146+
.send({ names: 'Jane Doe' });
147+
expect(res.statusCode).toBe(500);
148+
expect(res.error.text).toEqual(
149+
'{"error":{"message":"Request schema is invalid"}}'
150+
);
151+
});
104152
});
105153

106154
describe('generateFetchAllRequest tests', () => {
@@ -137,45 +185,61 @@ describe('requestGenerators tests', () => {
137185

138186
describe('generateFetchOneRequest tests', () => {
139187
it('should generate get route that fetch single item', async () => {
140-
const returnItem = { name: 'sasuke' };
141-
jest.spyOn(daoUtils, 'fetchItem').mockResolvedValue(returnItem);
188+
jest.spyOn(daoUtils, 'fetchItem').mockResolvedValue(mockProduct);
142189

143190
const res = await supertest(app)
144-
.get(`${ENDPOINT}/7`)
191+
.get(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
145192
.set({
146193
Accept: 'application/json',
147194
Authorization: 'Bearer dummy-token'
148-
})
149-
.send({ name: 'shikamaru' });
195+
});
196+
150197
expect(res.statusCode).toBe(200);
151-
expect(res.body).toEqual({ data: returnItem });
198+
expect(res.body).toEqual({ data: mockProduct });
152199
});
153200

154201
it('should generate get route that could return error response', async () => {
155202
const error = new Error('update failed');
156203
jest.spyOn(daoUtils, 'fetchItem').mockRejectedValue(error);
157204

158205
const res = await supertest(app)
159-
.get(`${ENDPOINT}/7`)
206+
.get(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
160207
.set({
161208
Accept: 'application/json',
162209
Authorization: 'Bearer dummy-token'
163-
})
164-
.send({});
210+
});
165211
expect(res.statusCode).toBe(500);
166212
expect(res.body).toEqual({ error: error.message });
167213
});
214+
215+
it('should return error from validateObjectId', async () => {
216+
const error = new Error('update failed');
217+
jest.spyOn(daoUtils, 'fetchItem').mockRejectedValue(error);
218+
219+
const res = await supertest(app)
220+
.get(`${ENDPOINT}/62bd7bd6151f31799cc670a`)
221+
.set({
222+
Accept: 'application/json',
223+
Authorization: 'Bearer dummy-token'
224+
});
225+
expect(res.statusCode).toBe(500);
226+
expect(res.error.text).toEqual(
227+
'{"error":{"message":"Invalid ObjectId"}}'
228+
);
229+
});
168230
});
169231

170232
describe('generateDeleteRequest tests', () => {
171233
it('should generate delete route that could delete item', async () => {
172-
const deleteRes = 'item delete sucess';
234+
const deleteRes = 'item delete success';
173235
jest.spyOn(daoUtils, 'deleteItem').mockResolvedValue(deleteRes);
174236

175-
const res = await supertest(app).delete(`${ENDPOINT}/7`).set({
176-
Accept: 'application/json',
177-
Authorization: 'Bearer dummy-token'
178-
});
237+
const res = await supertest(app)
238+
.delete(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
239+
.set({
240+
Accept: 'application/json',
241+
Authorization: 'Bearer dummy-token'
242+
});
179243
expect(res.statusCode).toBe(200);
180244
expect(res.body).toEqual({ data: deleteRes });
181245
});
@@ -184,12 +248,27 @@ describe('requestGenerators tests', () => {
184248
const error = new Error('delete failed');
185249
jest.spyOn(daoUtils, 'deleteItem').mockRejectedValue(error);
186250

187-
const res = await supertest(app).delete(`${ENDPOINT}/7`).set({
188-
Accept: 'application/json',
189-
Authorization: 'Bearer dummy-token'
190-
});
251+
const res = await supertest(app)
252+
.delete(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
253+
.set({
254+
Accept: 'application/json',
255+
Authorization: 'Bearer dummy-token'
256+
});
191257
expect(res.statusCode).toBe(500);
192258
expect(res.body).toEqual({ error: error.message });
193259
});
260+
261+
it('should return error from validateObjectId', async () => {
262+
const res = await supertest(app)
263+
.delete(`${ENDPOINT}/62bd7bd6151f31799cc670a`)
264+
.set({
265+
Accept: 'application/json',
266+
Authorization: 'Bearer dummy-token'
267+
});
268+
expect(res.statusCode).toBe(500);
269+
expect(res.error.text).toEqual(
270+
'{"error":{"message":"Invalid ObjectId"}}'
271+
);
272+
});
194273
});
195274
});

badges/badge-branches.svg

Lines changed: 1 addition & 1 deletion
Loading

badges/badge-functions.svg

Lines changed: 1 addition & 1 deletion
Loading

badges/badge-lines.svg

Lines changed: 1 addition & 1 deletion
Loading

badges/badge-statements.svg

Lines changed: 1 addition & 1 deletion
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-mongo-express",
3-
"version": "10.0.31",
3+
"version": "10.0.32",
44
"description": "A basic starter web app with node, express and mongoose",
55
"main": "index.js",
66
"scripts": {

server/api/requestGenerators.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@ import {
1414
import { clientCredentialsGrant, managementClient } from 'utils/auth0';
1515
import { REQUEST_TYPES } from './customApisMapper';
1616
import config from 'config';
17-
import { checkJwt } from 'middlewares/auth';
17+
import { validateObjectId, validateReqBody, validateSchema } from 'utils';
1818

1919
export const generateRequest = (type, router, model, validator) => {
20-
const middlewares = [...createValidatorMiddlewares(validator), checkJwt];
20+
const middlewares = [...createValidatorMiddlewares(validator)];
2121
switch (type) {
2222
case REQUEST_TYPES.create:
23+
middlewares.push(validateSchema(model));
2324
generatePostRequest({ router, model, middlewares });
2425
break;
2526
case REQUEST_TYPES.update:
27+
middlewares.push(validateObjectId, validateReqBody(model));
2628
generatePatchRequest({ router, model, middlewares });
2729
break;
2830
case REQUEST_TYPES.fetchOne:
31+
middlewares.push(validateObjectId);
2932
generateFetchOneRequest({ router, model, middlewares });
3033
break;
3134
case REQUEST_TYPES.fetchAll:
3235
generateFetchAllRequest({ router, model, middlewares });
3336
break;
3437
case REQUEST_TYPES.remove:
38+
middlewares.push(validateObjectId);
3539
generateDeleteRequest({ router, model, middlewares });
3640
break;
3741
}

0 commit comments

Comments
 (0)