-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add article routes - add seed to initialise a user, category and article - create controller to handle comment posts - create middleware validate user comments - update documentation to capture new route - write tests to test endpoint functionality - refactor usersValidations to confirm with ES6 syntax and not to send descriptive errors when logging in [Delivers ##159987407] feat(comment): user can comment - add article routes - add seed to initialise a user, category and article - create controller to handle comment posts - create middleware validate user comments - update documentation to capture new route - write tests to test endpoint functionality [Delivers ##159987407] feat(comment): user can comment - add article routes - add seed to initialise a user, category and article - create controller to handle comment posts - create middleware validate user comments - update documentation to capture new route - write tests to test endpoint functionality - refactor usersValidations to confirm with ES6 syntax and not to send descriptive errors when logging in [Delivers ##159987407]
- Loading branch information
1 parent
b37f5d4
commit a5b2603
Showing
13 changed files
with
303 additions
and
60 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
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,31 @@ | ||
import validator from 'validator'; | ||
import models from '../models/index'; | ||
|
||
const { Users, Comments } = models; | ||
|
||
const commentsController = { | ||
create: (req, res) => { | ||
const { articleId } = req.params; | ||
const { content } = req.body; | ||
const userId = req.currentUser.id; | ||
|
||
const userComment = { | ||
content: validator.escape(content.trim()), | ||
articleId: validator.escape(articleId), | ||
userId | ||
}; | ||
|
||
Comments | ||
.create(userComment) | ||
.then(comment => Users.findById(userId, { | ||
attributes: ['id', 'username', 'firstname', 'lastname', 'createdAt', 'updatedAt'] | ||
}) | ||
.then(user => res.status(201).jsend.success({ user, comment }))) | ||
.catch(error => res.status(500).jsend.error({ | ||
message: 'There was a problem processing your request', | ||
error | ||
})); | ||
} | ||
}; | ||
|
||
export default commentsController; |
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,22 @@ | ||
import helpers from '../helpers/helpers'; | ||
|
||
const articlesValidations = { | ||
/** @description This method validates users' comments and replies | ||
* @param {object} req The request object | ||
* @param {object} res The response object | ||
* @param {object} next the next middleware | ||
* @returns {object} json response | ||
*/ | ||
|
||
validateComments: (req, res, next) => { | ||
const { content } = req.body; | ||
const { validString } = helpers; | ||
|
||
if (!validString(content)) { | ||
return res.status(400).jsend.fail('Comment is empty'); | ||
} | ||
return next(); | ||
} | ||
}; | ||
|
||
export default articlesValidations; |
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
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,13 @@ | ||
import express from 'express'; | ||
import commentsController from '../controllers/commentsController'; | ||
import articlesValidations from '../middleware/articlesValidations'; | ||
import auth from '../middleware/auth'; | ||
|
||
const articlesRoutes = express.Router(); | ||
|
||
const { create } = commentsController; | ||
const { validateComments } = articlesValidations; | ||
|
||
articlesRoutes.post('/:articleId', auth, validateComments, create); | ||
|
||
export default articlesRoutes; |
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,17 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Users', [{ | ||
username: 'johndoe', | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
email: 'johndoe@gmail.com', | ||
password: 'johndoe', | ||
bio: 'I like to eat', | ||
image: 'someimgurl', | ||
premium: true, | ||
isVerified: true, | ||
interests: ['Entertainment', 'Science'], | ||
createdAt: '2018-09-05', | ||
updatedAt: '2018-09-05' | ||
}]), | ||
down: queryInterface => queryInterface.bulkDelete('Users') | ||
}; |
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 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Categories', [{ | ||
name: 'Science', | ||
createdAt: '2018-09-05', | ||
updatedAt: '2018-09-05' | ||
}]), | ||
down: queryInterface => queryInterface.bulkDelete('Categories') | ||
}; |
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,13 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Articles', [{ | ||
slug: 'slug-here', | ||
title: 'My Life', | ||
description: 'About my life', | ||
body: 'This is the content of the story of my life', | ||
userId: 1, | ||
categoryId: 1, | ||
createdAt: '2018-09-05', | ||
updatedAt: '2018-09-05' | ||
}]), | ||
down: queryInterface => queryInterface.bulkDelete('Articles') | ||
}; |
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,103 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../server/app'; | ||
|
||
chai.use(chaiHttp); | ||
const { should } = chai; | ||
should(); | ||
|
||
let token; | ||
|
||
describe('Tests for Articles', () => { | ||
describe('Create comment tests', () => { | ||
before((done) => { | ||
chai | ||
.request(app) | ||
.post('/api/v1/users/auth/signup') | ||
.send({ | ||
username: 'enjames', | ||
email: 'enjames@james.com', | ||
password: 'pasS1234', | ||
}) | ||
.end((err, res) => { | ||
token = res.body.data.token; | ||
res.body.should.have.property('data'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('User should not be able to comment if not authenticated', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/v1/articles/1') | ||
.send({ | ||
content: 'Your post was not inspiring.' | ||
}) | ||
.end((err, res) => { | ||
res.body.status.should.equal('error'); | ||
res.body.message.should.equal('No token provided'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('User should receive an error if authentication fails', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/v1/articles/1') | ||
.set('Authorization', '8beccb8ef75986c7096888907ddf4165889255315b67be782a3333eeeeee') | ||
.send({ | ||
content: 'Your post was not inspiring.' | ||
}) | ||
.end((err, res) => { | ||
res.body.status.should.equal('error'); | ||
res.body.message.should.equal('Failed to authenticate token! Valid token required'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('It should return an object containing a user and a comment object', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/v1/articles/1') | ||
.set('Authorization', token) | ||
.send({ | ||
content: '' | ||
}) | ||
.end((err, res) => { | ||
res.body.status.should.equal('fail'); | ||
res.body.data.should.equal('Comment is empty'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('It should return an object containing a user and a comment object', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/v1/articles/1') | ||
.set('Authorization', token) | ||
.send({ | ||
content: 'Your post was not inspiring.' | ||
}) | ||
.end((err, res) => { | ||
res.body.data.should.have.property('user'); | ||
res.body.data.comment.content.should.equal('Your post was not inspiring.'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('It should return an object containing a user and a comment object', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/v1/articles/13431') | ||
.set('Authorization', token) | ||
.send({ | ||
content: 'Your post was not inspiring.' | ||
}) | ||
.end((err, res) => { | ||
res.body.status.should.equal('error'); | ||
res.body.message.should.equal('There was a problem processing your request'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.