Skip to content

Commit

Permalink
Merge 13efd6d into ecf04c5
Browse files Browse the repository at this point in the history
  • Loading branch information
fantastic-genius committed Jul 19, 2019
2 parents ecf04c5 + 13efd6d commit 7e96a12
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
18 changes: 7 additions & 11 deletions controllers/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export default {
voteArticle: async (req, res) => {
const { slug } = req.params;
let { status } = req.body;
status = status === 'true' || status === 'false' ? JSON.parse(status) : status;
status = JSON.parse(status);
const article = await db.Article.findOne({
where: {
slug
Expand Down Expand Up @@ -308,16 +308,12 @@ export default {
if (!vote) {
await db.ArticleVote.create(voteDetails);
} else {
switch (status) {
case true:
case false:
await vote.updateArticleVote(status);
resStatus = 200;
break;
default:
await vote.deleteArticleVote();
message = 'You have unvote this article';
resStatus = 200;
resStatus = 200;
if (status === vote.status) {
await vote.deleteArticleVote();
message = 'You have unvote this article';
} else {
await vote.updateArticleVote(status);
}
}

Expand Down
8 changes: 7 additions & 1 deletion routes/v1/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ router.delete(
Article.deleteArticle,
);

router.post('/vote/:slug', Middleware.authenticate, Middleware.isblackListedToken, Article.voteArticle);
router.post(
'/vote/:slug',
Middleware.authenticate,
Middleware.isblackListedToken,
Validation.voteArticle,
Article.voteArticle
);

router.get(
'/rate/:slug',
Expand Down
39 changes: 36 additions & 3 deletions tests/routes/articles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,15 @@ describe('ARTICLES TEST', () => {
expect(res.body.message).to.equal('You downvote this article');
});

it('Should unvote an article', async () => {
it('Should unvote an article already upvoted', async () => {
await createArticleVote(upvote);
const res = await chai
.request(app)
.post(`/api/v1/articles/vote/${articleSlug}`)
.set('x-access-token', userToken);
.set('x-access-token', userToken)
.send({
status: true
});
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
expect(res.body.message).to.equal('You have unvote this article');
Expand All @@ -553,12 +556,42 @@ describe('ARTICLES TEST', () => {
const res = await chai
.request(app)
.post(`/api/v1/articles/vote/${wrongSlug}`)
.set('x-access-token', userToken);
.set('x-access-token', userToken)
.send({
status: false
});
expect(res.status).to.equal(404);
expect(res.body).to.be.an('object');
expect(res.body.error).to.equal('This article does not exist');
});

it('Should return error message when status is not true or false', async () => {
const res = await chai
.request(app)
.post(`/api/v1/articles/vote/${articleSlug}`)
.set('x-access-token', userToken)
.send({
status: 'ade'
});
expect(res.status).to.equal(400);
expect(res.body).to.be.an('object');
expect(res.body.error).to.equal('Wrong status field provided');
});

it('Should return error message when status is set to null', async () => {
const res = await chai
.request(app)
.post(`/api/v1/articles/vote/${articleSlug}`)
.set('x-access-token', userToken)
.send({
status: null
});
expect(res.status).to.equal(400);
expect(res.body).to.be.an('object');
expect(res.body.error[0].message).to.equal('Input your status');
});
});

describe('Get Article Ratings', () => {
let articleData;
beforeEach(async () => {
Expand Down
27 changes: 26 additions & 1 deletion validators/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,30 @@ export default {
message: e,
});
}
}
},

voteArticle: async (req, res, next) => {
let voteData;
const rules = {
status: 'required|boolean',
slug: 'required|string'
};

try {
voteData = { status: JSON.parse(req.body.status), ...req.params };
} catch (error) {
return res.status(400).send({
error: 'Wrong status field provided',
});
}

try {
await validatorInstance.validateAll(voteData, rules, messages);
next();
} catch (e) {
return res.status(400).send({
error: e,
});
}
},
};

0 comments on commit 7e96a12

Please sign in to comment.