Skip to content

Commit

Permalink
Merge pull request #38 from andela/ft-return-trip-167727634
Browse files Browse the repository at this point in the history
#167727634-Return-Trip-Request
  • Loading branch information
Daymorelah committed Aug 29, 2019
2 parents 8cdb644 + 54b919b commit aeb158c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 38 deletions.
32 changes: 32 additions & 0 deletions src/controllers/RequestController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ class RequestController {
return HelperMethods.serverError(res);
}
}

/**
* Book a Return Trip
* Route: POST: /request
* @param {object} req - HTTP Request object
* @param {object} res - HTTP Response object
* @return {res} res - HTTP Response object
* @memberof RequestController
*/
static async bookAReturnTrip(req, res) {
try {
const { id } = req.decoded;
const { returnDate } = req.body;
if (!returnDate) {
return HelperMethods.clientError(res, 'The returnDate field is required.');
}
const { dataValues } = await Request.create({ ...req.body, userId: id });
if (dataValues.id) {
return HelperMethods.requestSuccessful(res, {
success: true,
message: 'Trip booked successfully',
tripCreated: dataValues,
}, 201);
}
return HelperMethods.clientError(res,
'Could not book your return trip. please try again.',
400);
} catch (error) {
if (error.errors) return HelperMethods.sequelizeValidationError(res, error);
return HelperMethods.serverError(res);
}
}
}

export default RequestController;
1 change: 0 additions & 1 deletion src/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ class UserController {
}
return HelperMethods.clientError(res, 'User does not exist', 404);
} catch (error) {
console.log('err is ==> ', error);
if (error.errors) return HelperMethods.sequelizeValidationError(res, error);
return HelperMethods.serverError(res);
}
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/03-create-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
},
returnDate: {
type: Sequelize.DATEONLY,
allowNull: false,
allowNull: true,
},
reason: {
type: Sequelize.ENUM,
Expand Down
5 changes: 4 additions & 1 deletion src/models/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export default (sequelize, DataTypes) => {
}
}
},
returnDate: DataTypes.DATEONLY,
returnDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
accommodationId: {
type: DataTypes.INTEGER
},
Expand Down
5 changes: 5 additions & 0 deletions src/routes/requestRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const requestRoutes = app => {
Validate.validateUserInput,
Authorization.checkToken,
RequestController.bookATrip);

app.post('/api/v1/request/book_return_trip',
Authorization.checkToken,
Validate.validateUserInput,
RequestController.bookAReturnTrip);
};

export default requestRoutes;
112 changes: 77 additions & 35 deletions src/test/integrationTests/requestController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ chai.use(chaiHttp);
const { expect } = chai;

describe('Integration tests for the request controller', () => {
const tripDetails = {
origin: 'Lagos',
destination: 'Kaduna',
flightDate: '2019-06-21',
returnDate: '2019-03-21',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
reason: 'EXPEDITION',
};
let token;
before('login with an existing user details from the seeded data', async () => {
const response = await chai.request(app).post('/api/v1/auth/login')
Expand All @@ -15,41 +24,74 @@ describe('Integration tests for the request controller', () => {
});
token = response.body.data.userDetails.token;
});
it('should allow a registered user to book a trip', async () => {
const tripDetails = {
origin: 'Onipan',
destination: 'Okoko',
flightDate: '2019-06-21',
returnDate: '2019-03-21',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
reason: 'EXPEDITION',
};
const response = await chai.request(app).post('/api/v1/request/book_trip')
.set('x-access-token', token).send(tripDetails);
expect(response.status).to.equal(201);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.equal('Trip booked successfully');
expect(response.body.data).to.have.property('tripCreated');
expect(response.body.data.tripCreated).to.be.an('object');
expect(response.body.data).to.have.property('success');
expect(response.body.data.success).to.equal(true);
describe('Authentication tests', () => {
it('should return an error if the authentication token is missing', async () => {
const response = await chai
.request(app)
.post('/api/v1/request/book_return_trip')
.send(tripDetails);
expect(response.status).to.equal(401);
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
expect(response.body).to.have.property('message');
});
it('should return error if the authentication token is invalid', async () => {
const response = await chai
.request(app)
.post('/api/v1/request/book_return_trip')
.set('x-access-token', 'hbhfbdhhabdkh')
.send(tripDetails);
expect(response.status).to.equal(401);
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
expect(response.body).to.have.property('message');
});
});
it('should not book a trip when a required detail is missing', async () => {
const tripDetails = {
origin: 'Onipan',
destination: 'Okoko',
returnDate: '2019-03-21',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
reason: 'EXPEDITION',
};
const response = await chai.request(app).post('/api/v1/request/book_trip')
.set('x-access-token', token).send(tripDetails);
expect(response.status).to.equal(400);
expect(response.body).to.have.property('message');
expect(response.body.message).to.equal('The "flightDate" field is required');
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
describe('Tests for the book a trip features', () => {
it('should allow a registered user to book a one way trip', async () => {
const response = await chai.request(app).post('/api/v1/request/book_trip')
.set('x-access-token', token)
.send(tripDetails);
expect(response.status).to.equal(201);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.equal('Trip booked successfully');
expect(response.body.data).to.have.property('tripCreated');
expect(response.body.data.tripCreated).to.be.an('object');
expect(response.body.data).to.have.property('success');
expect(response.body.data.success).to.equal(true);
});
it('should allow a registered user to book a return trip', async () => {
const response = await chai.request(app).post('/api/v1/request/book_return_trip')
.set('x-access-token', token).send(tripDetails);
expect(response.status).to.equal(201);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.equal('Trip booked successfully');
expect(response.body.data).to.have.property('tripCreated');
expect(response.body.data.tripCreated).to.be.an('object');
expect(response.body.data).to.have.property('success');
expect(response.body.data.success).to.equal(true);
});
});
describe('Validation tests for the book a trip features', () => {
it('should not book a one way trip when a required detail is missing', async () => {
delete tripDetails.flightDate;
const response = await chai.request(app).post('/api/v1/request/book_trip')
.set('x-access-token', token).send(tripDetails);
expect(response.status).to.equal(400);
expect(response.body).to.have.property('message');
expect(response.body.message).to.equal('The "flightDate" field is required');
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
});
it('should not book a return trip when a required detail is missing', async () => {
delete tripDetails.returnDate;
const response = await chai.request(app).post('/api/v1/request/book_return_trip')
.set('x-access-token', token).send(tripDetails);
expect(response.status).to.equal(400);
expect(response.body).to.have.property('message');
expect(response.body.message).to.equal('The returnDate field is required.');
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
});
});
});

0 comments on commit aeb158c

Please sign in to comment.