From 067228d0eb4cca4ac4f92459190db3093dd4726c Mon Sep 17 00:00:00 2001 From: minlinx Date: Tue, 10 Sep 2019 12:31:45 +0100 Subject: [PATCH] adds a controller for users to book an accommodation facility --- src/controllers/AccommodationController.js | 40 ++++++++++ src/controllers/index.js | 3 +- src/models/Accommodation.js | 3 +- src/routes/accommodationRoute.js | 14 ++++ src/routes/index.js | 2 + .../accommodationController.test.js | 57 +++++++++++++++ src/test/integrationTests/index.js | 1 + .../unitTests/accomodationController.test.js | 35 +++++++++ src/test/unitTests/index.js | 2 + src/test/unitTests/userController.test.js | 73 +++++++++++++++++++ 10 files changed, 227 insertions(+), 3 deletions(-) create mode 100644 src/controllers/AccommodationController.js create mode 100644 src/routes/accommodationRoute.js create mode 100644 src/test/integrationTests/accommodationController.test.js create mode 100644 src/test/unitTests/accomodationController.test.js create mode 100644 src/test/unitTests/userController.test.js diff --git a/src/controllers/AccommodationController.js b/src/controllers/AccommodationController.js new file mode 100644 index 0000000..524ec1c --- /dev/null +++ b/src/controllers/AccommodationController.js @@ -0,0 +1,40 @@ +import models from '../models'; +import { HelperMethods } from '../utils'; + +const { Accommodation } = models; + +/** + * Class representing the Accomodation controller + * @class AccommodationController + * @description accommodation controller + */ +class AccommodationController { + /** + * Book An Accommodation. + * Route: POST: /accommodation + * @param {object} req - HTTP Request object + * @param {object} res - HTTP Response object + * @return {res} res - HTTP Response object + * @memberof AccommodationController + */ + static async bookAnAccommodation(req, res) { + try { + const { body } = req; + const { dataValues } = await Accommodation.create({ ...body }); + if (dataValues.id) { + HelperMethods.requestSuccessful(res, { + success: true, + message: 'Accommodation booked successfully', + accommodationCreated: dataValues, + }, 201); + } + return HelperMethods.serverError(res, + 'Could not create an accommodation. Please try again'); + } catch (error) { + if (error.errors) return HelperMethods.sequelizeValidationError(res, error); + return HelperMethods.serverError(res); + } + } +} + +export default AccommodationController; diff --git a/src/controllers/index.js b/src/controllers/index.js index da0033f..ca0797c 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,4 +1,5 @@ import UserController from './UserController'; import RequestController from './RequestController'; +import AccommodationController from './AccommodationController'; -export { UserController, RequestController }; +export { UserController, RequestController, AccommodationController }; diff --git a/src/models/Accommodation.js b/src/models/Accommodation.js index a939b9d..3b7e6b3 100644 --- a/src/models/Accommodation.js +++ b/src/models/Accommodation.js @@ -34,12 +34,11 @@ export default (sequelize, DataTypes) => { } } }, - }); + }, { tableName: 'Accommodations' }); Accommodation.associate = models => { Accommodation.hasMany(models.Request, { foreignKey: 'accommodationId', - as: 'accommodation', }); }; diff --git a/src/routes/accommodationRoute.js b/src/routes/accommodationRoute.js new file mode 100644 index 0000000..1cab3d4 --- /dev/null +++ b/src/routes/accommodationRoute.js @@ -0,0 +1,14 @@ +import { AccommodationController } from '../controllers'; +import { Authorization } from '../middlewares'; +import Validate from '../validation'; + +const accommodationRoute = app => { + app.post( + '/api/v1/accommodation', + Validate.validateUserInput, + Authorization.checkToken, + AccommodationController.bookAnAccommodation + ); +}; + +export default accommodationRoute; diff --git a/src/routes/index.js b/src/routes/index.js index debcbfc..884cc95 100755 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,5 +1,6 @@ import authRoute from './authRoute'; import requestRoute from './requestRoute'; +import accommodationRoute from './accommodationRoute'; import userRoute from './userRoute'; import socialAuthRoute from './socialAuthRoute'; /** @@ -19,6 +20,7 @@ const routes = app => { requestRoute(app); userRoute(app); socialAuthRoute(app); + accommodationRoute(app); }; export default routes; diff --git a/src/test/integrationTests/accommodationController.test.js b/src/test/integrationTests/accommodationController.test.js new file mode 100644 index 0000000..440a5a8 --- /dev/null +++ b/src/test/integrationTests/accommodationController.test.js @@ -0,0 +1,57 @@ +import chai from 'chai'; +import chaiHttp from 'chai-http'; +import app from '../../index'; + +chai.use(chaiHttp); +const { expect } = chai; + +describe('Integration Tests For Accommodation Controller', () => { + describe('Test book accommodation controller', () => { + const accommodationDetails = { + name: 'Southern Sun Ikoyi Hotel', + address: '174, Owolabi street, Yaba', + roomName: 'C3', + roomType: '2 bedroom', + vacantNumber: '1', + }; + let token; + before('Get Token', async () => { + const loginResponse = await chai.request(app).post('/api/v1/auth/login') + .send({ + email: 'demo2@demo.com', + password: 'password', + }); + token = loginResponse.body.data.userDetails.token; + }); + it('should book an accommodation for a user', async () => { + const response = await chai.request(app).post('/api/v1/accommodation') + .send(accommodationDetails).set('x-access-token', token); + expect(response.status).to.deep.equal(201); + expect(response.body.data).to.have.property('success'); + expect(response.body.data.success).to.equal(true); + expect(response.body.data).to.have.property('message'); + expect(response.body.data.message) + .to.equal('Accommodation booked successfully'); + }); + it('should return client error when some details are missing', async () => { + const response = await chai.request(app).post('/api/v1/accommodation') + .send().set({ 'x-access-token': token }); + expect(response.status).to.deep.equal(400); + expect(response.body).to.have.property('success'); + expect(response.body.success).to.equal(false); + expect(response.body).to.have.property('message'); + expect(response.body.message) + .to.equal('The "name" field is required'); + }); + it('should return error for missing token', async () => { + const response = await chai.request(app).post('/api/v1/accommodation') + .send(accommodationDetails); + expect(response.status).to.deep.equal(401); + expect(response.body).to.have.property('success'); + expect(response.body.success).to.equal(false); + expect(response.body).to.have.property('message'); + expect(response.body.message) + .to.equal('User not authorized'); + }); + }); +}); diff --git a/src/test/integrationTests/index.js b/src/test/integrationTests/index.js index e64e102..89f7306 100644 --- a/src/test/integrationTests/index.js +++ b/src/test/integrationTests/index.js @@ -1,3 +1,4 @@ import './requestController.test'; import './userController.test'; import './socialController.test'; +import './accommodationController.test'; diff --git a/src/test/unitTests/accomodationController.test.js b/src/test/unitTests/accomodationController.test.js new file mode 100644 index 0000000..3000860 --- /dev/null +++ b/src/test/unitTests/accomodationController.test.js @@ -0,0 +1,35 @@ +import sinon from 'sinon'; +import chai from 'chai'; +import { AccommodationController } from '../../controllers'; +import models from '../../models'; + +const { expect } = chai; +const { Accommodation } = models; + +const req = { decoded: { id: 'some id' } }; + +const res = { + status() { + return this; + }, + json(obj) { + return obj; + } +}; + +describe('unit test for the Accomodation Controller', () => { + let stubbedMethod; + afterEach(() => { + if (stubbedMethod.restore) stubbedMethod.restore(); + }); + it('should return a server error when an unexpected error happens', async () => { + stubbedMethod = sinon.stub(Accommodation, 'create').throws({ + dataValues: 'some thing' + }); + const response = await AccommodationController.bookAnAccommodation(req, res); + expect(response).to.have.property('message'); + expect(response.message).to.equal('Internal server error'); + expect(response).to.have.property('success'); + expect(response.success).to.equal(false); + }); +}); diff --git a/src/test/unitTests/index.js b/src/test/unitTests/index.js index 08f07cd..f51ab09 100644 --- a/src/test/unitTests/index.js +++ b/src/test/unitTests/index.js @@ -2,5 +2,7 @@ import './authentication.test'; import './sendEmail.test'; import './helperMethods.test'; import './requestController.test'; +import './accomodationController.test'; +import './userController.test'; import './middleware.test'; import './searchRequestsMiddleware.test'; diff --git a/src/test/unitTests/userController.test.js b/src/test/unitTests/userController.test.js new file mode 100644 index 0000000..603809e --- /dev/null +++ b/src/test/unitTests/userController.test.js @@ -0,0 +1,73 @@ +import sinon from 'sinon'; +import chai from 'chai'; +import { UserController } from '../../controllers'; +import models from '../../models'; + +const { expect } = chai; +const { User } = models; + +const req = { decoded: { id: 'some id' } }; + +const res = { + status() { + return this; + }, + json(obj) { + return obj; + } +}; + +describe('unit test for the User Controller', () => { + let stubbedMethod; + afterEach(() => { + if (stubbedMethod.restore) stubbedMethod.restore(); + }); + it('should return a server error when an unexpected error happens', async () => { + stubbedMethod = sinon.stub(User, 'findByPk').throws({ + dataValues: 'some thing' + }); + const response = await UserController.updateProfile(req, res); + expect(response).to.have.property('message'); + expect(response.message).to.equal('Internal server error'); + expect(response).to.have.property('success'); + expect(response.success).to.equal(false); + }); + it('should return a server error when an unexpected error happens', async () => { + stubbedMethod = sinon.stub(User, 'findByPk').throws({ + dataValues: 'some thing' + }); + const response = await UserController.getProfile(req, res); + expect(response).to.have.property('message'); + expect(response.message).to.equal('Internal server error'); + expect(response).to.have.property('success'); + expect(response.success).to.equal(false); + }); + it('should return a server error when an unexpected error happens', async () => { + stubbedMethod = sinon.stub(User, 'findByPk').throws({ + dataValues: 'some thing' + }); + const response = await UserController.verifyEmail(req, res); + expect(response).to.have.property('message'); + expect(response.message).to.equal('Internal server error'); + expect(response).to.have.property('success'); + expect(response.success).to.equal(false); + }); + it('should return a server error when an unexpected error happens', async () => { + stubbedMethod = sinon.stub(User, 'findOne').throws({ + dataValues: 'some thing' + }); + const response = await UserController.resetPassword(req, res); + expect(response).to.have.property('message'); + expect(response.message).to.equal('Internal server error'); + expect(response).to.have.property('success'); + expect(response.success).to.equal(false); + }); + it('should only allow managers reject a User', async () => { + stubbedMethod = sinon.stub(User, 'update').throws({ dataValues: 'some thing' }); + const response = await UserController.rememberUserDetails(req, res); + expect(response).to.have.property('message'); + expect(response.message).to.equal('Internal server error'); + expect(response).to.have.property('success'); + expect(response.success).to.equal(false); + }); +});