Skip to content

Commit

Permalink
Ft(Fix Create Accomodation): Remove file upload creating accomodation
Browse files Browse the repository at this point in the history
[Refactors #167891598]
  • Loading branch information
tvpeter committed Sep 14, 2019
1 parent 5c804b0 commit e2c4cbb
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 62 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
22 changes: 7 additions & 15 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,23 +17,21 @@ 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,
201,
'Successfully created acoomodation center',
dataValues
));
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.

20 changes: 6 additions & 14 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,30 +15,23 @@ 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) => {
describe('Create accomodation facility', () => {
it('should create accomodation facility', (done) => {
const token = jwtHelper.generateToken(travelAdmin);
const filePath = `${__dirname}/hotel.jpg`;
const stub = sinon.stub(cloudinary.uploader, 'upload').returns('thisistheimageurl');

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.status).to.eq(201);
expect(res.body.message).to.eq('Successfully created acoomodation center');
stub.restore();
done();
});
});
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 @@ -545,4 +545,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 e2c4cbb

Please sign in to comment.