Skip to content

Commit

Permalink
Merge 7552db5 into 63b2a89
Browse files Browse the repository at this point in the history
  • Loading branch information
chiomadans1759 committed Sep 8, 2019
2 parents 63b2a89 + 7552db5 commit e161759
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 55 deletions.
86 changes: 32 additions & 54 deletions src/controllers/request.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,61 +53,39 @@ export default class RequestController {
.catch((err) => Response.InternalServerError(res, err));
}

/**
* @param {object} req The data which contains dates, reason, origin, destination etc
* @param {object} res Response returned to the user
* @returns {object} An object containing the request submitted into the db
*/
static TripRequests(req, res) {
const destination = [];
const {
user_id, category, origin, departure_date, reason, booking_id
} = req.body;
destination.push(req.body.destination);
const newRequest = {
user_id, category, origin, destination, departure_date, reason, booking_id
};
// persist booking to database
Request.create(newRequest)
.then((data) => Response.Success(res, data, 201))
.catch((error) => Response.CustomError(res, 500, 'error',
'Request failed. Please see information below.',
error.message));
}

/**
/**
* @param {object} req
* @param {object} res
* @returns {json} request
*/
static async createMultiCityRequest(req, res) {
try {
const { id: user_id } = req.currentUser.dataValues;
const {
category, origin, destination, departure_date, return_date, reason, room_id
} = req.body;
const bookingData = {
departure_date, return_date, user_id, room_id
};
const booking = await models.Booking.create(bookingData);
const { id: booking_id } = booking;
const requestData = {
user_id, category, origin, destination: destination.split(', '), departure_date, return_date, reason, booking_id
};
const request = await models.Request.create(requestData);
return res.status(201).json({
status: 'success',
data: { request, booking }
});
} catch (error) {
return res.status(500)
.json({
status: 'error',
error: 'Internal server error',
});
}
}
static async createMultiCityRequest(req, res) {
try {
const { id: user_id } = req.currentUser.dataValues;
const {
category, origin, destination, departure_date, return_date, reason, room_id
} = req.body;
const bookingData = {
departure_date, return_date, user_id, room_id
};
const booking = await models.Booking.create(bookingData);
const { id: booking_id } = booking;
const requestData = {
user_id, category, origin, destination: destination.split(', '), departure_date, return_date, reason, booking_id
};
const request = await models.Request.create(requestData);
return res.status(201).json({
status: 'success',
data: { request, booking }
});
} catch (error) {
return res.status(500)
.json({
status: 'error',
error: 'Internal server error',
});
}
}

/**
* @method createReturnTripRequest
Expand All @@ -117,7 +95,7 @@ export default class RequestController {
*
* @returns {objet} status and message
*/
static async createReturnTripRequest(req, res){
static async createReturnTripRequest(req, res) {
try {
const { id: user_id } = req.user;
const {
Expand All @@ -133,7 +111,7 @@ export default class RequestController {
const bookingExists = await Booking.findByPk(booking_id);

if (!bookingExists) {
return res
return res
.status(400)
.json({
status: 'error',
Expand All @@ -155,14 +133,14 @@ export default class RequestController {
});

return Response.Success(res, newRequest.dataValues, 201);
} catch(error){
} catch (error) {
Response.CustomError(
res,
500,
'error',
'Request failed. Please see information below.',
error.message
)
error.message
);
}
}

Expand Down
107 changes: 107 additions & 0 deletions src/test/comment.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import chai from 'chai';
import app from '../index';
import JWTService from '../services/jwt.service';
import sinon from 'sinon';
import Sinonchai from 'sinon-chai';

import CommentController from '../controllers/comment.controller';


chai.should();
chai.use(Sinonchai);

describe('Comments', () => {
// Test for deleting comment
Expand Down Expand Up @@ -73,4 +79,105 @@ describe('Comments', () => {
});
});
});

describe('/populate comment for a request', () => {
it('fakes server error', (done) => {
const req = { body: {} };
const res = {
status() { },
send() { }
};

sinon.stub(res, 'status').returnsThis();

CommentController.deleteComment(req, res);
(res.status).should.have.callCount(1);
done();
});
});

// Test for populating comments for a request
describe('/populate comment for a request', () => {
it('should be able to retrieve comment for a request when an authorize user makes a request', (done) => {
chai.request(app)
.get('/api/v1/comment/1')
.set('token', JWTService.generateToken({ id: 1, role: 'requester' }))
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('status').eql('success');
res.body.should.have.property('data');
done();
});
});
});

describe('/populate comment for a request', () => {
it('Should not be able to populate comment for a request if the user is not logged in', (done) => {
chai.request(app)
.get('/api/v1/comment/1')
.end((err, res) => {
res.should.have.status(401);
res.body.should.be.an('object');
res.body.should.have.property('status').eql('error');
done();
});
});
});
describe('/populate comment for a request', () => {
it('Should not be able to populate comment for a request if the user supplies invalid token', (done) => {
chai.request(app)
.get('/api/v1/comment/1')
.set('token', '24jh3bchjbeureu3f33')
.end((err, res) => {
res.should.have.status(401);
res.body.should.be.an('object');
res.body.should.have.property('status').eql('error');
done();
});
});
});
describe('/populate comment for a request', () => {
it('should not allow an unauthorized user to populate comments for a request', (done) => {
chai.request(app)
.get('/api/v1/comment/1')
.set('token', JWTService.generateToken({ id: 2, role: 'requester' }))
.end((err, res) => {
res.should.have.status(401);
res.body.should.be.an('object');
res.body.should.have.property('status').eql('error');
done();
});
});
});
describe('/populate comment for a request', () => {
it('Should not be able to populate comment for a request if the user supplies invalid request_id', (done) => {
chai.request(app)
.get('/api/v1/comment/a')
.set('token', JWTService.generateToken({ id: 1, role: 'requester' }))
.end((err, res) => {
res.should.have.status(500);
res.body.should.be.an('object');
res.body.should.have.property('status').eql('error');
done();
});
});
});

describe('/populate comment for a request', () => {
it('fakes server error', (done) => {
const req = { body: {} };
const res = {
status() { },
send() { }
};

sinon.stub(res, 'status').returnsThis();

CommentController.getComments(req, res);
(res.status).should.have.callCount(1);
done();
});
});

});
2 changes: 1 addition & 1 deletion src/test/signup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Users', () => {
});
});

describe('/GET Requests', () => {
describe('/Validate Signup Inputs', () => {
it('should not register a new user with an already existing email', (done) => {
chai.request(app)
.post('/api/v1/auth/signup')
Expand Down

0 comments on commit e161759

Please sign in to comment.