Skip to content

Commit

Permalink
feat(editCommentHistory): add feature to get edit history for a comment
Browse files Browse the repository at this point in the history
- Created feature to get edit history of a comment.
  GET /api/articles/<slug>/comment/<comment-id>/history
  * Authorization required
  * Admin priviledge required
- Wrote tests
[Finishes #164608984]
  • Loading branch information
danprocoder committed Mar 18, 2019
1 parent 70cdda1 commit 686e0b5
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
47 changes: 47 additions & 0 deletions server/controllers/articleComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,53 @@ class Comment {
return response(res).serverError({ errors: { server: ['database error'] } });
}
}

/**
* @description returns edit history of a particular comment.
* @param {*} req The request object
* @param {*} res The response object
* @returns {undefined}
*/
static async getEditHistory(req, res) {
const { commentId } = req.params;
const { article } = req;

const comment = await ArticleComment.findOne({
where: {
articleId: article.id, // This ensures the comment belongs to the specified article
id: commentId
},
attributes: ['comment', 'createdAt']
});
if (!comment) {
return response(res).notFound('Comment does not exists');
}

// Get comment history
const dbHistory = await CommentEditHistory.findAll({
where: {
commentId,
},
attributes: [['previousComment', 'comment'], 'createdAt'],
order: [
['id', 'DESC']
]
});

let original, history;
if (dbHistory.length === 0) {
original = comment;
history = [];
} else {
original = dbHistory.pop();
history = dbHistory;
history.unshift(comment);
}
response(res).success({
original,
history
});
}
}

export default Comment;
6 changes: 6 additions & 0 deletions server/routes/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ router.delete('/articles/:slug/comment/:commentId',
ValidateComment.validateComment,
Comment.deleteComment);

router.get('/articles/:slug/comment/:commentId/history',
AuthenticateUser.verifyUser,
AuthenticateUser.isAdmin,
AuthenticateArticle.verifyArticle,
Comment.getEditHistory);

export default router;
133 changes: 133 additions & 0 deletions server/test/commentEditHistory.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import dotenv from 'dotenv';
import app from '../app';

dotenv.config();
chai.use(chaiHttp);

let userToken, adminToken, commentId;
const articleSlug = 'this-is-an-article-1';

describe('Test endpoint to return edit history', () => {
before('it should get admin user', (done) => {
chai
.request(app)
.post('/api/users/login')
.send({
name: process.env.SUPER_ADMIN_USERNAME,
password: process.env.SUPER_ADMIN_PASSWORD
})
.end((err, res) => {
expect(res.status).to.equal(200);
const { token } = res.body.user;
adminToken = token;
done();
});
});

before('it should create a regular user', (done) => {
chai
.request(app)
.post('/api/users')
.send({
firstname: 'Comment',
lastname: 'Bot',
username: 'commentbot',
email: 'commentbot@authorshaven.com',
password: '1234567890'
})
.end((err, res) => {
expect(res.status).to.equal(201);
const { token } = res.body.user;
userToken = token;
done();
});
});

it('should create a new comment', (done) => {
chai
.request(app)
.post(`/api/articles/${articleSlug}/comment`)
.set('authorization', `Bearer ${userToken}`)
.send({
comment: 'This is original comment'
})
.end((err, res) => {
expect(res.status).to.equal(201);
const { userComment } = res.body;
commentId = userComment.id;
done();
});
});

it('should edit the above comment', (done) => {
chai
.request(app)
.patch(`/api/articles/${articleSlug}/comment/${commentId}`)
.set('authorization', `Bearer ${userToken}`)
.send({
comment: 'This is the edited version of the comment'
})
.end((err, res) => {
expect(res.status).to.equal(200);
done();
});
});

it('should edit the above comment again', (done) => {
chai
.request(app)
.patch(`/api/articles/${articleSlug}/comment/${commentId}`)
.set('authorization', `Bearer ${userToken}`)
.send({
comment: 'This is the second time the comment is editted'
})
.end((err, res) => {
expect(res.status).to.equal(200);
done();
});
});

it('should return a 401 if user trying to get edit history is not an admin', (done) => {
chai
.request(app)
.get(`/api/articles/${articleSlug}/comment/${commentId}/history`)
.set('authorization', `bearer ${userToken}`)
.end((err, res) => {
expect(res.status).to.equal(401);
done();
});
});

it('should return a 404 admin is trying to get history for a comment that does not exists', (done) => {
chai
.request(app)
.get(`/api/articles/${articleSlug}/comment/9876545/history`)
.set('authorization', `Bearer ${adminToken}`)
.end((err, res) => {
expect(res.status).to.equal(404);
done();
});
});

it('should get a edit history of the comment of the article', (done) => {
chai
.request(app)
.get(`/api/articles/${articleSlug}/comment/${commentId}/history`)
.set('authorization', `bearer ${adminToken}`)
.end((err, res) => {
expect(res.status).to.equal(200);

const { original, history } = res.body;
expect(original.comment).to.be.a('string');
expect(history).to.be.an('array');
expect(history.length).to.equal(2);
expect(original.comment).to.equal('This is original comment');
// History is sorted starting from most recent editted.
expect(history[0].comment).to.equal('This is the second time the comment is editted');
expect(history[1].comment).to.equal('This is the edited version of the comment');
done();
});
});
});
1 change: 1 addition & 0 deletions server/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import './report.spec';
import './articleSearch.spec';
import './admin.spec';
import './notification.spec';
import './commentEditHistory.spec';

0 comments on commit 686e0b5

Please sign in to comment.