Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix for highlight text and comment #72

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions controllers/comments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import Notification from '../../utils/notifications';

export default {
addComment: async (req, res) => {
const { params: { slug }, body: { content }, user } = req;
let { highlightedTextObj } = req.body;
const { params: { slug }, body: { content, highlightedText }, user } = req;
try {
const foundArticle = await db.Article.findOne({
where: { slug },
Expand All @@ -14,12 +13,12 @@ export default {
error: 'Article does not exist'
});
}
highlightedTextObj = JSON.stringify(highlightedTextObj);

const comment = await db.Comment.create({
articleId: foundArticle.id,
userId: user.id,
content,
highlightedText: highlightedTextObj
highlightedText,
});

Notification.articleNotification({
Expand Down
24 changes: 1 addition & 23 deletions tests/routes/articles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1186,34 +1186,12 @@ describe('ARTICLES TEST', () => {
.set('x-access-token', userToken)
.send({
content: 'This is my first comment',
highlightedTextObj: {
text: article.body.substring(9, 22),
startPosition: 9,
endPosition: 22
}
highlightedText: 'is my first co'
});
expect(res.statusCode).to.equal(201);
expect(res.body.message).to.be.equal('Comment added successfully');
expect(res.body.comment).to.be.an('object');
expect(res.body.comment.content).to.be.a('string');
// test response here
});

it('should add an highlighted comment if user is authenticated', async () => {
const res = await chai
.request(app)
.post(`/api/v1/articles/${articleSlug}/comments`)
.set('x-access-token', userToken)
.send({
content: 'This is my first comment',
highlightedTextObj: {
text: 'invalid text',
startPosition: 11,
endPosition: 25
}
});
expect(res.statusCode).to.equal(400);
expect(res.body.error).to.be.equal('Invalid highlighted text');
});
});

Expand Down
23 changes: 3 additions & 20 deletions validators/articles/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { sanitize } from 'indicative';
import { messages, validatorInstance, sanitizeRules } from '../../utils';
import db from '../../db/models';

export default {
createArticle: async (req, res, next) => {
Expand Down Expand Up @@ -121,26 +120,10 @@ export default {
const rules = {
content: 'required|string',
slug: 'required|string',
highlightedTextObj: 'object'
highlightedText: 'string'
};
const { params: { slug }, body: { highlightedTextObj } } = req;
const data = { ...req.body, slug };

try {
if (highlightedTextObj) {
const { startPosition, endPosition, text } = highlightedTextObj;
const { body } = await db.Article.findOne({
where: { slug: req.params.slug }
});
if (text !== body.substring(startPosition, endPosition)) {
throw new Error('Invalid highlighted text');
}
}
} catch (e) {
return res.status(400).json({
error: e.message,
});
}
const { params: { slug }, body } = req;
const data = { ...body, slug };

try {
await validatorInstance.validateAll(data, rules, messages);
Expand Down