-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bardarz/refactor-with-test
Refactor with test
- Loading branch information
Showing
16 changed files
with
221 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
|
||
// app.js | ||
/** app.js **/ | ||
const express = require('express'); | ||
const app = express(); | ||
|
||
//require('./config/dynamodb.config'); | ||
|
||
const corsOptions = { | ||
origin: '*', | ||
methods: 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', | ||
allowedHeaders: 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' | ||
}; | ||
/** CORS option if not using the express ones **/ | ||
// const corsOptions = { | ||
// origin: '*', | ||
// methods: 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', | ||
// allowedHeaders: 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' | ||
// }; | ||
|
||
// Integrate CORS options for all routes | ||
app.use(express.json()); | ||
|
||
|
||
// Injecting routes | ||
require('./routes/route.index.js')(app); | ||
require('./routes/index.js')(app); | ||
|
||
app.get('/', (req, res) => { | ||
return res.status(200).send({'message': 'YAY! Congratulations! Your first endpoint is working'}); | ||
}); | ||
|
||
|
||
module.exports = app; | ||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
'use strict'; | ||
|
||
import { ENV } from './env.config' | ||
|
||
|
||
// External libraries | ||
const dynamoose = require('dynamoose'); | ||
|
||
dynamoose.setDefaults( | ||
{ | ||
create: false, | ||
waitForActive: false | ||
}); | ||
// Config | ||
import { ENV } from './env.config' | ||
|
||
dynamoose.setDefaults({ | ||
create: false, | ||
waitForActive: false | ||
}); | ||
|
||
dynamoose.AWS.config.update({ | ||
region: "eu-west-1" | ||
}); | ||
|
||
if(!ENV.DYNAMO_DB.IS_OFFLINE) { | ||
if (!ENV.DYNAMO_DB.IS_OFFLINE) { | ||
console.log("is local"); | ||
dynamoose.local('http://localhost:4569') | ||
} | ||
|
||
|
||
|
||
module.exports = dynamoose; | ||
export default dynamoose; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
import * as Item from '../models/item.model' | ||
/** External libraries **/ | ||
import { INTERNAL_SERVER_ERROR, NOT_FOUND, OK } from 'http-status-codes'; | ||
// import * as _ from 'lodash'; | ||
|
||
const Get = async (req, res) => { | ||
console.log(req.params); | ||
import { isEmpty } from 'lodash'; | ||
|
||
await Item.get(req.params.ID).then((data) => { | ||
console.log(data); | ||
res.json(data); | ||
}); | ||
/** Item model **/ | ||
import Item from '../models/item.model' | ||
import { ErrItemNotFound } from '../errors'; | ||
|
||
}; | ||
const ItemController = { | ||
Get: async (req, res) => { | ||
const { ID } = req.params; | ||
|
||
try { | ||
const result = await Item.Get(ID); | ||
|
||
module.exports = { | ||
Get | ||
if (isEmpty(result)) { | ||
return res.status(NOT_FOUND).json(ErrItemNotFound); | ||
} | ||
|
||
return res.status(OK).json(result); | ||
} catch (e) { | ||
console.error(e); | ||
return res.status(INTERNAL_SERVER_ERROR).json(e); | ||
} | ||
} | ||
}; | ||
|
||
export default ItemController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** ID param not defined **/ | ||
export const ErrIdNotDefined = 'ErrorIdNotDefined'; | ||
|
||
/** Item not found**/ | ||
export const ErrItemNotFound = 'ErrItemNotFound'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
/** Third Party **/ | ||
const dynamoose = require('../config/dynamodb.config'); | ||
/** Item Dynamoose Schema **/ | ||
import ItemSchema from './schemas/item.schema'; | ||
|
||
/** Env **/ | ||
import { ENV } from '../config/env.config'; | ||
/** Errors **/ | ||
import { ErrIdNotDefined } from '../errors'; | ||
|
||
const Schema = dynamoose.Schema; | ||
const ItemModel = { | ||
Get: async (ID) => { | ||
if (!ID) { | ||
throw Error(ErrIdNotDefined); | ||
} | ||
|
||
const ItemSchema = new Schema({ | ||
set_ID: { | ||
type: String, | ||
hashKey: true | ||
}, | ||
created_at: String, | ||
updated_at: String, | ||
data: String | ||
return await ItemSchema.get(ID); | ||
} | ||
}; | ||
|
||
}, { | ||
useNativeBooleans: true, | ||
useDocumentTypes: true, | ||
}); | ||
|
||
module.exports = dynamoose.model(ENV.DYNAMO_DB.ITEM_TABLE, ItemSchema); | ||
export default ItemModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** Dynamoose config **/ | ||
import dynamoose from '../../config/dynamodb.config'; | ||
|
||
/** ENV config **/ | ||
import { ENV } from '../../config/env.config'; | ||
|
||
const ItemSchema = new dynamoose.Schema({ | ||
ID: { | ||
type: String, | ||
hashKey: true | ||
}, | ||
createdAt: String, | ||
updatedAt: String, | ||
data: String | ||
}, { | ||
useNativeBooleans: true, | ||
useDocumentTypes: true, | ||
}); | ||
|
||
export default dynamoose.model(ENV.DYNAMO_DB.ITEM_TABLE, ItemSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** External libraries **/ | ||
import { createValidator } from 'express-joi-validation'; | ||
const validator = createValidator({}); | ||
|
||
/** Controllers **/ | ||
import ItemController from '../controllers/item.controller'; | ||
|
||
/** Validators **/ | ||
import { itemParamsSchema } from '../validators/item.validators'; | ||
|
||
module.exports = (app) => { | ||
app.get('/api/item/:ID', validator.params(itemParamsSchema), ItemController.Get); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const Joi = require('@hapi/joi'); | ||
|
||
export const itemParamsSchema = Joi.object({ | ||
ID: Joi.string().guid().required() | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import ItemController from '../src/controllers/item.controller'; | ||
|
||
jest.mock('../src/models/item.model', () => ({ | ||
Get: jest.fn() | ||
})); | ||
|
||
import ItemModel from '../src/models/item.model'; | ||
import { Item, ItemID } from './mocks'; | ||
import { ErrItemNotFound } from '../src/errors'; | ||
|
||
describe('Item Controller', () => { | ||
const params = { ID: ItemID }; | ||
let mockRequest, mockResponse; | ||
|
||
beforeEach(() => { | ||
mockRequest = require('./mocks').mockRequest; | ||
mockResponse = require('./mocks').mockResponse; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('[GET] Should return the item', async () => { | ||
ItemModel.Get.mockImplementation(() => Item); | ||
|
||
const req = mockRequest({ params }); | ||
const res = mockResponse(); | ||
|
||
await ItemController.Get(req, res); | ||
|
||
expect(ItemModel.Get).toBeCalledTimes(1); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(res.json).toHaveBeenCalledWith(Item); | ||
}); | ||
|
||
it('[GET] Should return 404 if the item does not exist', async () => { | ||
ItemModel.Get.mockImplementation(() => {}); | ||
|
||
const req = mockRequest({ params }); | ||
const res = mockResponse(); | ||
|
||
await ItemController.Get(req, res); | ||
|
||
expect(ItemModel.Get).toBeCalledTimes(1); | ||
expect(res.status).toHaveBeenCalledWith(404); | ||
expect(res.json).toHaveBeenCalledWith(ErrItemNotFound); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const ItemID = 'b9894650-f038-46e5-94e2-1686e755ebb9'; | ||
|
||
const Item = { | ||
ID: ItemID, | ||
data: 'This is a mocked Item', | ||
createdAt: "2020-01-20T10:00:00.000Z", | ||
updatedAt: "2020-01-20T10:00:00.000Z" | ||
}; | ||
|
||
const mockRequest = (data) => ({ ...data }); | ||
const mockResponse = (data) => { | ||
const res = data || {}; | ||
|
||
res.status = jest.fn().mockReturnValue(res); | ||
res.json = jest.fn().mockReturnValue(res); | ||
|
||
return res; | ||
}; | ||
|
||
export { | ||
Item, | ||
ItemID, | ||
mockRequest, | ||
mockResponse | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import { expect } from 'chai'; | ||
import server from '../server'; | ||
|
||
describe('Server', ()=>{ | ||
it('tests that server is running current port', async () => { | ||
expect(server.port).to.equal(5000) | ||
|
||
it('Is running in current port', () => { | ||
expect(server.port).toEqual(5000) | ||
}) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { itemParamsSchema } from '../src/validators/item.validators'; | ||
import { ItemID } from './mocks'; | ||
|
||
describe('Item Joi validators', () => { | ||
const allowed = { ID: ItemID }; | ||
const notAllowed = { ID: '1' }; | ||
const empty = {}; | ||
|
||
describe('Params validation', () => { | ||
it('Should validate the UUID', () => { | ||
const { error } = itemParamsSchema.validate(allowed); | ||
|
||
expect(error).toBeUndefined(); | ||
}); | ||
|
||
it('Should invalidate with UUID not valid', () => { | ||
const { error } = itemParamsSchema.validate(notAllowed); | ||
const errorTypes = error.details.map(el => el.type); | ||
|
||
expect(error).toBeDefined(); | ||
expect(errorTypes.length).toBeGreaterThan(0); | ||
expect(errorTypes.includes('string.guid')).toBe(true); | ||
}); | ||
|
||
it('Should invalidate if empty', () => { | ||
const { error } = itemParamsSchema.validate(empty); | ||
const errorTypes = error.details.map(el => el.type); | ||
|
||
expect(error).toBeDefined(); | ||
expect(errorTypes.length).toBeGreaterThan(0); | ||
expect(errorTypes.includes('any.required')).toBe(true); | ||
}) | ||
}) | ||
}); |