-
Notifications
You must be signed in to change notification settings - Fork 10
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
6 changed files
with
177 additions
and
0 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,49 @@ | ||
import models from '../database/models'; | ||
import Response from '../helpers/Response'; | ||
|
||
const { Comment, Trip } = models; | ||
|
||
/** | ||
* @class | ||
* @description controller class for comments | ||
* @exports CommentController | ||
*/ | ||
class CommentController { | ||
/** | ||
* Post a Comment | ||
* Route: POST: /trips/comments | ||
* @param {object} req - HTTP Request object | ||
* @param {object} res - HTTP Response object | ||
* @return {res} res - HTTP Response object | ||
* @memberof CommentController | ||
*/ | ||
static async createComment(req, res) { | ||
const { payload: { id: userId } } = req.payload; | ||
const { tripId } = req.params; | ||
const { body } = req.body; | ||
try { | ||
const getTrip = await Trip.findOne({ | ||
where: { id: tripId }, | ||
returning: true | ||
}); | ||
if (!getTrip) { | ||
const response = new Response(false, 404, 'Trip does not exist'); | ||
return res.status(response.status).json(response); | ||
} | ||
|
||
const newComment = await Comment.create({ userId, tripId, body }); | ||
const response = new Response( | ||
true, | ||
201, | ||
'Comment posted successfully', | ||
newComment | ||
); | ||
return res.status(response.code).json(response); | ||
} catch (error) { | ||
const response = new Response(false, 500, error.message); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
} | ||
|
||
export default CommentController; |
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,18 @@ | ||
import { Router } from 'express'; | ||
import CommentController from '../controllers/Comment'; | ||
import permit from '../middlewares/permission'; | ||
import Token from '../helpers/Token'; | ||
import validator from '../middlewares/validator'; | ||
import { createCommentSchema } from '../validation/commentSchema'; | ||
|
||
const commentRoute = Router(); | ||
|
||
commentRoute.post( | ||
'/:tripId/comment', | ||
Token.verifyToken, | ||
permit('staff', 'manager'), | ||
validator(createCommentSchema), | ||
CommentController.createComment, | ||
); | ||
|
||
export default commentRoute; |
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,30 @@ | ||
import { check } from 'express-validator'; | ||
import models from '../database/models'; | ||
|
||
const { Trip } = models; | ||
|
||
const createCommentSchema = [ | ||
check('body') | ||
.trim() | ||
.exists() | ||
.withMessage('Comment is required') | ||
.isLength({ min: 2 }) | ||
.withMessage('Comment should not be less than 2 characters'), | ||
|
||
check('userId') | ||
.isUUID(4) | ||
.optional(), | ||
|
||
check('tripId') | ||
.isUUID(4) | ||
.withMessage('Trip ID is not valid. Should be of type UUID') | ||
.custom(async (value) => { | ||
const getTrip = await Trip.findOne({ where: { id: value } }); | ||
if (!getTrip) { | ||
throw new Error('Trip ID does not exist'); | ||
} | ||
return true; | ||
}) | ||
]; | ||
|
||
export { createCommentSchema }; |
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,70 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import sinon from 'sinon'; | ||
import app from '../src/index'; | ||
import models from '../src/database/models'; | ||
import mockAuth from './mockData/mockAuth'; | ||
import mockComments from './mockData/mockComment'; | ||
|
||
chai.use(chaiHttp); | ||
const { expect } = chai; | ||
|
||
const { Comment } = models; | ||
const { user10 } = mockAuth; | ||
const { myComments } = mockComments; | ||
const baseURL = '/api/v1/trips'; | ||
const invalidID = 'ffe25dbe-29ea-4759-8461-ed116f6739da'; | ||
let userToken; | ||
|
||
describe('Comments API', () => { | ||
before('Login to get token', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/v1/auth/login') | ||
.send(user10) | ||
.end((err, res) => { | ||
const { token } = res.body.data.user; | ||
userToken = token; | ||
done(err); | ||
}); | ||
}); | ||
describe('POST /trips/:tripId/comment', () => { | ||
it('should successfully post a comment', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseURL}/${myComments[0].tripId}/comment`) | ||
.send(myComments[0]) | ||
.set('authorization', userToken) | ||
.end((err, res) => { | ||
expect(res).to.have.status(201); | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should throw an error if trip ID is invalid', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseURL}/${invalidID}/comment`) | ||
.send(myComments[0]) | ||
.set('authorization', userToken) | ||
.end((err, res) => { | ||
expect(res).to.have.status(400); | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should return a 500 error when an error occurs on the server', (done) => { | ||
const stub = sinon.stub(Comment, 'create') | ||
.rejects(new Error('Server error, Please try again later')); | ||
chai.request(app) | ||
.post(`${baseURL}/${myComments[0].tripId}/comment`) | ||
.send(myComments[0]) | ||
.set('authorization', userToken) | ||
.end((err, res) => { | ||
expect(res.status).to.equal(500); | ||
stub.restore(); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); |
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,8 @@ | ||
const myComments = [ | ||
{ | ||
tripId: 'ffe25dbe-29ea-4759-8461-ed116f6739df', | ||
body: 'Legend has it that to understand recursion, you must first understand recursion' | ||
} | ||
]; | ||
|
||
export default { myComments }; |