Skip to content

Commit

Permalink
Merge 3bff7a1 into 5791108
Browse files Browse the repository at this point in the history
  • Loading branch information
tvpeter committed Sep 16, 2019
2 parents 5791108 + 3bff7a1 commit 482ca4b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 77 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"@babel/polyfill": "^7.4.4",
"@sendgrid/mail": "^6.4.0",
"bcryptjs": "^2.4.3",
"cloudinary": "1.14.0",
"cors": "^2.8.4",
"date-fns": "^2.0.1",
"dotenv": "^6.2.0",
Expand All @@ -54,7 +53,6 @@
"method-override": "^2.3.10",
"methods": "^1.1.2",
"morgan": "^1.9.1",
"multer": "1.4.2",
"passport": "^0.4.0",
"passport-facebook": "3.0.0",
"passport-google-oauth": "2.0.0",
Expand Down
24 changes: 8 additions & 16 deletions src/controllers/Accomodation.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import dotenv from 'dotenv';
import cloudinary from 'cloudinary';
import Response from '../helpers/Response';
import db from '../database/models';

dotenv.config();
cloudinary.v2.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_API,
api_secret: process.env.CLOUDINARY_SECRET
});
const { Accomodation, Room, Booking, User } = db;
const {
Accomodation, Room, Booking, User
} = db;

/**
* Accomodation class
Expand All @@ -23,24 +17,22 @@ class AccomodationController {
*/
static async createAccomodation(req, res) {
const {
name, branchId, capacity, address
name, branchId, capacity, address, imgUrl
} = req.body;

try {
const image = req.file ? await cloudinary.uploader.upload(req.file.path) : null;
const imgurl = image ? image.public_id : null;
const { dataValues } = await Accomodation.create({
name,
branchId,
capacity,
status: 'available',
imgurl,
imgurl: imgUrl,
address
});
return res.status(200).json(new Response(
return res.status(201).json(new Response(
true,
200,
'Successfully created acoomodation center',
201,
'Successfully created accomodation center',
dataValues
));
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Token {
req.payload = decoded;
return next();
} catch (error) {
return res.status(500).json(new Response(false, 500, 'There\'s an error processing your token'));
return res.status(401).json(new Response(false, 401, 'Unauthorized, your token is invalid or expired'));
}
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/routes/accomodation.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import AccomodationController from '../controllers/Accomodation';
import validator from '../middlewares/validator';
import middleware from '../middlewares/AuthMiddlewares';
import { createAccomodation, addRoom, accomodationBooking } from '../validation/accomodationSchema';
import upload from '../utils/upload';

const accomodationRoutes = Router();

accomodationRoutes.post('/create',
Token.verifyAdminToken('travel admin'), upload.single('imgurl'), validator(createAccomodation), AccomodationController.createAccomodation);
Token.verifyAdminToken('travel admin'), validator(createAccomodation), AccomodationController.createAccomodation);

accomodationRoutes.post('/addroom',
Token.verifyAdminToken('travel admin'), validator(addRoom), AccomodationController.addRoom);
Expand Down
26 changes: 0 additions & 26 deletions src/utils/upload.js

This file was deleted.

39 changes: 11 additions & 28 deletions test/accomodation.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import cloudinary from 'cloudinary';
import app from '../src/index';
import jwtHelper from '../src/helpers/Token';
import users from './mockData/mockAuth';
Expand All @@ -16,35 +15,28 @@ const { travelAdmin } = users;
const {
accomodationWithWrongBranchId,
accomodationWrongCompany, existingAccomodation, addRoom,
accomodationComplete, roomWithWrongAccId, InvalidCharacterAcctn,
roomWithWrongAccId, InvalidCharacterAcctn, accomodationComplete,
addRoom1, roomInAnotherCompany, roomWithoutName, accdtnWithoutName
} = accomodation;

describe('Travel Admin Creates Accomodation', () => {
describe('Create accomodation facility', ()=> {
it('should create an accomodation facility', (done) => {
const token = jwtHelper.generateToken(travelAdmin);
const filePath = `${__dirname}/hotel.jpg`;
const stub = sinon.stub(cloudinary.uploader, 'upload').returns('thisistheimageurl');
describe('Create accomodation facility', () => {
const token = jwtHelper.generateToken(travelAdmin);

it('should create accomodation facility', (done) => {

chai
.request(app)
.post(`${baseUrl}/create`)
.send(accomodationComplete)
.set('x-access-token', token)
.set('Content-Type', 'Multipart/form-data')
.attach('imgurl', filePath, 'hotel.jpg')
.field('name', accomodationComplete.name)
.field('branchId', accomodationComplete.branchId)
.field('capacity', accomodationComplete.capacity)
.field('address', accomodationComplete.address)
.end((err, res) => {
expect(res.status).to.eq(200);
expect(res.body.message).to.eq('Successfully created acoomodation center');
stub.restore();
expect(res.status).to.eq(201);
expect(res.body.message).to.eq('Successfully created accomodation center');
done();
});
});
it('should return error if invalid branch is supplied', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -59,7 +51,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if unauthorized branchid is supplied', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -74,7 +65,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if accomodation facility name is not supplied', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -88,7 +78,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if a registered accomodation is sent', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -103,7 +92,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error 500 if there is server error', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

const stub = sinon.stub(Accomodation, 'create')
.rejects(new Error('There\'s error processing your request, try again'));
Expand All @@ -123,8 +111,9 @@ describe('Travel Admin Creates Accomodation', () => {
});

describe('Add rooms to accomodation facility', () => {
const token = jwtHelper.generateToken(travelAdmin);

it('should create a new room', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -138,7 +127,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if accomodation is not found', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -152,7 +140,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if user attempts to add a room to another company\'s accomodation', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -166,7 +153,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if room is already registered', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -180,7 +166,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if name of room is not supplied', (done) => {
const token = jwtHelper.generateToken(travelAdmin);

chai
.request(app)
Expand All @@ -194,7 +179,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if number of rooms is equal to the capacity of the accomodation', (done) => {
const token = jwtHelper.generateToken(travelAdmin);
const stub = sinon.stub(Room, 'count')
.returns(12);

Expand All @@ -211,7 +195,6 @@ describe('Travel Admin Creates Accomodation', () => {
});
});
it('should return error if there is server error', (done) => {
const token = jwtHelper.generateToken(travelAdmin);
const stub = sinon.stub(Room, 'create')
.rejects(new Error('There\'s error processing your request, try again'));

Expand Down
4 changes: 2 additions & 2 deletions test/admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('Admin controller', () => {
});
});

it('should return a 500 error when an error occurs on the server', (done) => {
it('should return a 401 error when an error occurs on the server', (done) => {
const token = jwtHelper.generateToken(superAdminLogin);
const stub = sinon.stub(User, 'findAll')
.rejects(new Error('Server error, Please try again later'));
Expand All @@ -183,7 +183,7 @@ describe('Admin controller', () => {
.get(`${baseUrl}/user/all`)
.set('authorization', token)
.end((err, res) => {
expect(res.status).to.equal(500);
expect(res.status).to.equal(401);
stub.restore();
done(err);
});
Expand Down
12 changes: 12 additions & 0 deletions test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,16 @@ describe('EMAIL VERIFICATION TEST', () => {
done();
});
});
it('should return error if there is server error', (done) => {
const stub = sinon.stub(User, 'findOne').rejects('Server error');
chai
.request(app)
.patch(`${baseURL}/verify?token=${token}`)
.end((err, res) => {
expect(res).to.have.status(500);
expect(res.body.message).to.eq('Server error, Please try again later');
stub.restore();
done();
});
});
});
1 change: 1 addition & 0 deletions test/mockData/mockAccomodation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const accomodationComplete = {
name: 'Martinez Guest Inn Housing',
branchId: '3dd3b34a-7554-455e-a688-36afda199624',
capacity: '20',
imgUrl: 'image.png',
address: 'This is the address of the center',
};
const accdtnWithoutName = {
Expand Down

0 comments on commit 482ca4b

Please sign in to comment.