-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
227 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
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,4 +1,5 @@ | ||
import UserController from './UserController'; | ||
import RequestController from './RequestController'; | ||
import AccommodationController from './AccommodationController'; | ||
|
||
export { UserController, RequestController }; | ||
export { UserController, RequestController, AccommodationController }; |
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 |
---|---|---|
@@ -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; |
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 |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
}); |
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,3 +1,4 @@ | ||
import './requestController.test'; | ||
import './userController.test'; | ||
import './socialController.test'; | ||
import './accommodationController.test'; |
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,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); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |