Skip to content

Commit

Permalink
feat: merging main into feat/multi-stage-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushabh Sancheti committed Oct 25, 2022
2 parents d742947 + 4319277 commit 9a2b386
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ typings/
# dotenv environment variables file
.env
.env.local
.env.development
.env.docker

# next.js build output
.next
Expand Down
131 changes: 105 additions & 26 deletions __tests__/server/api/requestGenerators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ describe('requestGenerators tests', () => {
beforeAll(() => {
ENDPOINT = '/products';
});

const mockProduct = {
_id: '62bd7bd6151f31799cc670a6',
name: 'Bat',
price: 29,
category: 'Sports',
quantity: 10
};
describe('generatePostRequest tests', () => {
const reqBody = mockProduct;
it('should generatePostRoute should perform success response', async () => {
const reqBody = {
name: 'Jane Doe'
};
delete reqBody['_id'];
const createSpy = jest
.spyOn(daoUtils, 'createItem')
.mockImplementation((_, body) => Promise.resolve(body));
Expand All @@ -50,7 +57,7 @@ describe('requestGenerators tests', () => {
.send(reqBody);
expect(res.statusCode).toBe(200);
expect(createSpy).toBeCalled();
expect(res.body.data).toEqual(reqBody);
expect(res.body.data).toEqual(mockProduct);
});

it('should generatePostRoute should perform error response', async () => {
Expand All @@ -65,10 +72,23 @@ describe('requestGenerators tests', () => {
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
})
.send({});
.send(reqBody);
expect(res.statusCode).toBe(500);
expect(res.body.error).toEqual(error.message);
});

it('should return an error from validateSchema middleware', async () => {
const res = await supertest(app)
.post(ENDPOINT)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
})
.send({
name: 'Bat'
});
expect(res.statusCode).toBe(500);
});
});

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

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

const res = await supertest(app)
.patch(`${ENDPOINT}/7`)
.patch(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
})
.send({});
.send({ name: 'Jane Doe' });
expect(res.statusCode).toBe(500);
expect(res.body).toEqual({ error: error.message });
});

it('should return error from validateObjectId middleware', async () => {
const res = await supertest(app)
.patch(`${ENDPOINT}/62bd7bd6151f31799cc670a`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
})
.send({ name: 'Jane Doe' });
expect(res.statusCode).toBe(500);
expect(res.error.text).toEqual(
'{"error":{"message":"Invalid ObjectId"}}'
);
});

it('should return error from validateReqBody middleware', async () => {
const res = await supertest(app)
.patch(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
})
.send({ names: 'Jane Doe' });
expect(res.statusCode).toBe(500);
expect(res.error.text).toEqual(
'{"error":{"message":"Request schema is invalid"}}'
);
});
});

describe('generateFetchAllRequest tests', () => {
Expand Down Expand Up @@ -137,45 +185,61 @@ describe('requestGenerators tests', () => {

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

const res = await supertest(app)
.get(`${ENDPOINT}/7`)
.get(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
})
.send({ name: 'shikamaru' });
});

expect(res.statusCode).toBe(200);
expect(res.body).toEqual({ data: returnItem });
expect(res.body).toEqual({ data: mockProduct });
});

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

const res = await supertest(app)
.get(`${ENDPOINT}/7`)
.get(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
})
.send({});
});
expect(res.statusCode).toBe(500);
expect(res.body).toEqual({ error: error.message });
});

it('should return error from validateObjectId', async () => {
const error = new Error('update failed');
jest.spyOn(daoUtils, 'fetchItem').mockRejectedValue(error);

const res = await supertest(app)
.get(`${ENDPOINT}/62bd7bd6151f31799cc670a`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
});
expect(res.statusCode).toBe(500);
expect(res.error.text).toEqual(
'{"error":{"message":"Invalid ObjectId"}}'
);
});
});

describe('generateDeleteRequest tests', () => {
it('should generate delete route that could delete item', async () => {
const deleteRes = 'item delete sucess';
const deleteRes = 'item delete success';
jest.spyOn(daoUtils, 'deleteItem').mockResolvedValue(deleteRes);

const res = await supertest(app).delete(`${ENDPOINT}/7`).set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
});
const res = await supertest(app)
.delete(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
});
expect(res.statusCode).toBe(200);
expect(res.body).toEqual({ data: deleteRes });
});
Expand All @@ -184,12 +248,27 @@ describe('requestGenerators tests', () => {
const error = new Error('delete failed');
jest.spyOn(daoUtils, 'deleteItem').mockRejectedValue(error);

const res = await supertest(app).delete(`${ENDPOINT}/7`).set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
});
const res = await supertest(app)
.delete(`${ENDPOINT}/62bd7bd6151f31799cc670a6`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
});
expect(res.statusCode).toBe(500);
expect(res.body).toEqual({ error: error.message });
});

it('should return error from validateObjectId', async () => {
const res = await supertest(app)
.delete(`${ENDPOINT}/62bd7bd6151f31799cc670a`)
.set({
Accept: 'application/json',
Authorization: 'Bearer dummy-token'
});
expect(res.statusCode).toBe(500);
expect(res.error.text).toEqual(
'{"error":{"message":"Invalid ObjectId"}}'
);
});
});
});
2 changes: 1 addition & 1 deletion badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion badges/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion badges/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion badges/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mongo-express",
"version": "10.0.31",
"version": "10.0.32",
"description": "A basic starter web app with node, express and mongoose",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 6 additions & 2 deletions server/api/requestGenerators.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@ import {
import { clientCredentialsGrant, managementClient } from 'utils/auth0';
import { REQUEST_TYPES } from './customApisMapper';
import config from 'config';
import { checkJwt } from 'middlewares/auth';
import { validateObjectId, validateReqBody, validateSchema } from 'utils';

export const generateRequest = (type, router, model, validator) => {
const middlewares = [...createValidatorMiddlewares(validator), checkJwt];
const middlewares = [...createValidatorMiddlewares(validator)];
switch (type) {
case REQUEST_TYPES.create:
middlewares.push(validateSchema(model));
generatePostRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.update:
middlewares.push(validateObjectId, validateReqBody(model));
generatePatchRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.fetchOne:
middlewares.push(validateObjectId);
generateFetchOneRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.fetchAll:
generateFetchAllRequest({ router, model, middlewares });
break;
case REQUEST_TYPES.remove:
middlewares.push(validateObjectId);
generateDeleteRequest({ router, model, middlewares });
break;
}
Expand Down
Loading

0 comments on commit 9a2b386

Please sign in to comment.