2022-07-25 12:41:33 +03:00
|
|
|
const assert = require('assert');
|
2022-09-02 17:57:59 +03:00
|
|
|
const {agentProvider, mockManager, fixtureManager, matchers, configUtils} = require('../../utils/e2e-framework');
|
2022-08-24 10:28:20 +03:00
|
|
|
const {anyEtag, anyObjectId, anyLocationFor, anyISODateTime, anyErrorId, anyUuid, anyNumber, anyBoolean} = matchers;
|
2022-07-22 13:03:05 +03:00
|
|
|
const should = require('should');
|
|
|
|
const models = require('../../../core/server/models');
|
2022-07-25 18:35:46 +03:00
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const settingsCache = require('../../../core/shared/settings-cache');
|
2022-07-27 15:56:16 +03:00
|
|
|
const sinon = require('sinon');
|
2022-09-02 17:57:59 +03:00
|
|
|
const settingsService = require('../../../core/server/services/settings/settings-service');
|
2022-07-06 11:41:38 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
let membersAgent, membersAgent2, postId, postTitle, commentId;
|
|
|
|
|
|
|
|
async function getPaidProduct() {
|
|
|
|
return await models.Product.findOne({type: 'paid'});
|
2022-11-02 08:47:20 +03:00
|
|
|
}
|
2022-07-06 11:41:38 +03:00
|
|
|
|
2022-07-06 18:47:21 +03:00
|
|
|
const commentMatcher = {
|
2022-07-06 16:50:11 +03:00
|
|
|
id: anyObjectId,
|
|
|
|
created_at: anyISODateTime,
|
|
|
|
member: {
|
2022-07-06 18:45:53 +03:00
|
|
|
id: anyObjectId,
|
|
|
|
uuid: anyUuid
|
2022-07-06 17:44:20 +03:00
|
|
|
},
|
2022-08-10 17:12:35 +03:00
|
|
|
count: {
|
|
|
|
likes: anyNumber
|
|
|
|
},
|
2022-07-06 17:44:20 +03:00
|
|
|
liked: anyBoolean
|
2022-07-06 16:50:11 +03:00
|
|
|
};
|
|
|
|
|
2022-08-10 17:12:35 +03:00
|
|
|
/**
|
2022-08-24 10:28:20 +03:00
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {number} [options.replies]
|
|
|
|
* @returns
|
2022-08-10 17:12:35 +03:00
|
|
|
*/
|
|
|
|
function commentMatcherWithReplies(options = {replies: 0}) {
|
|
|
|
return {
|
2022-07-20 16:25:38 +03:00
|
|
|
id: anyObjectId,
|
2022-08-10 17:12:35 +03:00
|
|
|
created_at: anyISODateTime,
|
|
|
|
member: {
|
|
|
|
id: anyObjectId,
|
|
|
|
uuid: anyUuid
|
|
|
|
},
|
|
|
|
replies: new Array(options?.replies ?? 0).fill(commentMatcher),
|
|
|
|
count: {
|
|
|
|
likes: anyNumber,
|
|
|
|
replies: anyNumber
|
|
|
|
},
|
|
|
|
liked: anyBoolean
|
|
|
|
};
|
|
|
|
}
|
2022-07-06 18:47:21 +03:00
|
|
|
|
2022-07-20 16:25:38 +03:00
|
|
|
async function sleep(ms) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-25 17:27:38 +03:00
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
async function testCanCommentOnPost(member) {
|
|
|
|
await models.Member.edit({last_seen_at: null, last_commented_at: null}, {id: member.get('id')});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {body} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
html: '<div></div><p></p><p>This is a <strong>message</strong></p><p></p><p></p><p>New line</p><p></p>'
|
|
|
|
}]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('comments')
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcher]
|
|
|
|
});
|
|
|
|
// Save for other tests
|
|
|
|
commentId = body.comments[0].id;
|
|
|
|
|
|
|
|
// Check if author got an email
|
|
|
|
mockManager.assert.sentEmailCount(1);
|
|
|
|
mockManager.assert.sentEmail({
|
|
|
|
subject: '💬 New comment on your post: ' + postTitle,
|
|
|
|
to: fixtureManager.get('users', 0).email,
|
|
|
|
// Note that the <strong> tag is removed by the sanitizer
|
|
|
|
html: new RegExp(escapeRegExp('<p>This is a message</p><p></p><p>New line</p>'))
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for the dispatched events (because this happens async)
|
|
|
|
await sleep(200);
|
|
|
|
|
|
|
|
// Check last_updated_at changed?
|
|
|
|
member = await models.Member.findOne({id: member.id});
|
|
|
|
should.notEqual(member.get('last_seen_at'), null, 'The member should have a `last_seen_at` property after posting a comment.');
|
|
|
|
|
|
|
|
// Check last_commented_at changed?
|
|
|
|
should.notEqual(member.get('last_commented_at'), null, 'The member should have a `last_commented_at` property after posting a comment.');
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:57:59 +03:00
|
|
|
async function testCanReply(member, emailMatchers = {}) {
|
2022-08-30 18:38:58 +03:00
|
|
|
const date = new Date(0);
|
|
|
|
await models.Member.edit({last_seen_at: date, last_commented_at: date}, {id: member.get('id')});
|
|
|
|
|
|
|
|
const {body} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: fixtureManager.get('comments', 0).id,
|
|
|
|
html: 'This is a reply'
|
|
|
|
}]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('comments')
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcher]
|
|
|
|
});
|
|
|
|
|
|
|
|
mockManager.assert.sentEmailCount(2);
|
|
|
|
mockManager.assert.sentEmail({
|
|
|
|
subject: '💬 New comment on your post: ' + postTitle,
|
|
|
|
to: fixtureManager.get('users', 0).email
|
|
|
|
});
|
|
|
|
|
|
|
|
mockManager.assert.sentEmail({
|
2022-09-02 17:57:59 +03:00
|
|
|
...emailMatchers,
|
2022-08-30 18:38:58 +03:00
|
|
|
subject: '↪️ New reply to your comment on Ghost',
|
|
|
|
to: fixtureManager.get('members', 0).email
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for the dispatched events (because this happens async)
|
|
|
|
await sleep(250);
|
|
|
|
|
|
|
|
// Check last_updated_at changed?
|
|
|
|
member = await models.Member.findOne({id: member.id});
|
|
|
|
should.notEqual(member.get('last_seen_at').getTime(), date.getTime(), 'Should update `last_seen_at` property after posting a comment.');
|
|
|
|
|
|
|
|
// Check last_commented_at changed?
|
|
|
|
should.notEqual(member.get('last_commented_at').getTime(), date.getTime(), 'Should update `last_commented_at` property after posting a comment.');
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:20:55 +03:00
|
|
|
async function testCannotCommentOnPost(status = 403) {
|
2022-08-30 18:38:58 +03:00
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
html: '<div></div><p></p><p>This is a <strong>message</strong></p><p></p><p></p><p>New line</p><p></p>'
|
|
|
|
}]})
|
2022-09-06 18:20:55 +03:00
|
|
|
.expectStatus(status)
|
2022-08-30 18:38:58 +03:00
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:20:55 +03:00
|
|
|
async function testCannotReply(status = 403) {
|
2022-08-30 18:38:58 +03:00
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: fixtureManager.get('comments', 0).id,
|
|
|
|
html: 'This is a reply'
|
|
|
|
}]})
|
2022-09-06 18:20:55 +03:00
|
|
|
.expectStatus(status)
|
2022-08-30 18:38:58 +03:00
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:41:38 +03:00
|
|
|
describe('Comments API', function () {
|
2022-08-30 18:38:58 +03:00
|
|
|
let member;
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-07-06 11:41:38 +03:00
|
|
|
before(async function () {
|
|
|
|
membersAgent = await agentProvider.getMembersAPIAgent();
|
2022-11-18 16:58:35 +03:00
|
|
|
membersAgent2 = membersAgent.duplicate();
|
2022-07-06 11:41:38 +03:00
|
|
|
|
2022-07-06 16:50:11 +03:00
|
|
|
await fixtureManager.init('posts', 'members', 'comments');
|
2022-07-06 11:41:38 +03:00
|
|
|
|
|
|
|
postId = fixtureManager.get('posts', 0).id;
|
2022-08-02 18:17:33 +03:00
|
|
|
postTitle = fixtureManager.get('posts', 0).title;
|
2022-07-06 11:41:38 +03:00
|
|
|
});
|
|
|
|
|
2022-07-20 16:25:38 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
mockManager.mockMail();
|
|
|
|
});
|
|
|
|
|
2022-07-06 11:41:38 +03:00
|
|
|
afterEach(function () {
|
2022-09-02 17:57:59 +03:00
|
|
|
configUtils.restore();
|
2022-07-06 11:41:38 +03:00
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('when commenting enabled for all', function () {
|
|
|
|
describe('when not authenticated', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
const getStub = sinon.stub(settingsCache, 'get');
|
|
|
|
getStub.callsFake((key, options) => {
|
|
|
|
if (key === 'comments_enabled') {
|
|
|
|
return 'all';
|
|
|
|
}
|
|
|
|
return getStub.wrappedMethod.call(settingsCache, key, options);
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-09-02 17:57:59 +03:00
|
|
|
afterEach(async function () {
|
2022-08-30 18:38:58 +03:00
|
|
|
sinon.restore();
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can browse all comments of a post', async function () {
|
|
|
|
const {body} = await membersAgent
|
|
|
|
.get(`/api/comments/?filter=post_id:${postId}`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcherWithReplies({replies: 1})]
|
|
|
|
});
|
|
|
|
});
|
2022-09-06 18:20:55 +03:00
|
|
|
|
|
|
|
it('cannot comment on a post', async function () {
|
|
|
|
await testCannotCommentOnPost(401);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cannot reply on a post', async function () {
|
|
|
|
await testCannotReply(401);
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('cannot report a comment', async function () {
|
|
|
|
commentId = fixtureManager.get('comments', 0).id;
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Create a temporary comment
|
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/${commentId}/report/`)
|
|
|
|
.expectStatus(401)
|
2022-09-06 18:20:55 +03:00
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cannot like a comment', async function () {
|
|
|
|
// Create a temporary comment
|
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/${commentId}/like/`)
|
|
|
|
.expectStatus(401)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cannot unlike a comment', async function () {
|
|
|
|
// Create a temporary comment
|
|
|
|
await membersAgent
|
|
|
|
.delete(`/api/comments/${commentId}/like/`)
|
|
|
|
.expectStatus(401)
|
2022-08-30 18:38:58 +03:00
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyUuid
|
|
|
|
}]
|
|
|
|
});
|
2022-07-27 15:56:16 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('when authenticated', function () {
|
2022-09-02 17:57:59 +03:00
|
|
|
let getStub;
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
before(async function () {
|
|
|
|
await membersAgent.loginAs('member@example.com');
|
|
|
|
member = await models.Member.findOne({email: 'member@example.com'}, {require: true});
|
|
|
|
await membersAgent2.loginAs('member2@example.com');
|
|
|
|
});
|
2022-09-14 18:02:13 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
beforeEach(function () {
|
2022-09-02 17:57:59 +03:00
|
|
|
getStub = sinon.stub(settingsCache, 'get');
|
2022-08-30 18:38:58 +03:00
|
|
|
getStub.callsFake((key, options) => {
|
|
|
|
if (key === 'comments_enabled') {
|
|
|
|
return 'all';
|
|
|
|
}
|
|
|
|
return getStub.wrappedMethod.call(settingsCache, key, options);
|
2022-07-22 13:03:05 +03:00
|
|
|
});
|
2022-08-30 18:38:58 +03:00
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
afterEach(async function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can comment on a post', async function () {
|
|
|
|
await testCanCommentOnPost(member);
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can browse all comments of a post', async function () {
|
|
|
|
const {body} = await membersAgent
|
|
|
|
.get(`/api/comments/?filter=post_id:${postId}`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcherWithReplies({replies: 1}), commentMatcher]
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can reply to your own comment', async function () {
|
|
|
|
// Should not update last_seen_at or last_commented_at when both are already set to a value on the same day
|
|
|
|
const timezone = settingsCache.get('timezone');
|
|
|
|
const date = moment.utc(new Date()).tz(timezone).startOf('day').toDate();
|
|
|
|
await models.Member.edit({last_seen_at: date, last_commented_at: date}, {id: member.get('id')});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {body} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: commentId,
|
|
|
|
html: 'This is a reply'
|
|
|
|
}]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('comments')
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcher]
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check only the author got an email (because we are the author of this parent comment)
|
|
|
|
mockManager.assert.sentEmailCount(1);
|
|
|
|
mockManager.assert.sentEmail({
|
|
|
|
subject: '💬 New comment on your post: ' + postTitle,
|
|
|
|
to: fixtureManager.get('users', 0).email
|
2022-07-22 13:03:05 +03:00
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Wait for the dispatched events (because this happens async)
|
|
|
|
await sleep(200);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check last updated_at is not changed?
|
|
|
|
member = await models.Member.findOne({id: member.id});
|
|
|
|
should.equal(member.get('last_seen_at').getTime(), date.getTime(), 'The member should not update `last_seen_at` if last seen at is same day');
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check last_commented_at changed?
|
|
|
|
should.equal(member.get('last_commented_at').getTime(), date.getTime(), 'The member should not update `last_commented_at` f last seen at is same day');
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can reply to a comment', async function () {
|
|
|
|
await testCanReply(member);
|
|
|
|
});
|
2022-09-02 17:57:59 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
let testReplyId;
|
|
|
|
it('Limits returned replies to 3', async function () {
|
|
|
|
const parentId = fixtureManager.get('comments', 0).id;
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check initial status: two replies before test
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/comments/${parentId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcherWithReplies({replies: 2})]
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.comments[0].count.replies.should.eql(2);
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Add some replies
|
|
|
|
for (let index = 0; index < 3; index++) {
|
|
|
|
const {body: reply} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: parentId,
|
|
|
|
html: 'This is a reply ' + index
|
|
|
|
}]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('comments')
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcher]
|
|
|
|
});
|
|
|
|
if (index === 0) {
|
|
|
|
testReplyId = reply.comments[0].id;
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check if we have count.replies = 4, and replies.length == 3
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/comments/${parentId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [commentMatcherWithReplies({replies: 3})]
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.comments[0].count.replies.should.eql(5);
|
|
|
|
});
|
|
|
|
});
|
2022-09-02 17:57:59 +03:00
|
|
|
|
|
|
|
it('Can reply to a comment with www domain', async function () {
|
|
|
|
// Test that the www. is stripped from the default
|
|
|
|
configUtils.set('url', 'http://www.domain.example/');
|
|
|
|
await testCanReply(member, {from: 'noreply@domain.example'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can reply to a comment with custom support email', async function () {
|
|
|
|
// Test that the www. is stripped from the default
|
|
|
|
getStub.callsFake((key, options) => {
|
|
|
|
if (key === 'members_support_address') {
|
|
|
|
return 'support@example.com';
|
|
|
|
}
|
|
|
|
if (key === 'comments_enabled') {
|
|
|
|
return 'all';
|
|
|
|
}
|
|
|
|
return getStub.wrappedMethod.call(settingsCache, key, options);
|
|
|
|
});
|
|
|
|
await testCanReply(member, {from: 'support@example.com'});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can like a comment', async function () {
|
|
|
|
// Check not liked
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/comments/${commentId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: new Array(1).fill(commentMatcherWithReplies({replies: 1}))
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.comments[0].liked.should.eql(false);
|
|
|
|
body.comments[0].count.likes.should.eql(0);
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Create a temporary comment
|
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/${commentId}/like/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expectEmptyBody();
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check liked
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/comments/${commentId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: new Array(1).fill(commentMatcherWithReplies({replies: 1}))
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.comments[0].liked.should.eql(true);
|
|
|
|
body.comments[0].count.likes.should.eql(1);
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Cannot like a comment multiple times', async function () {
|
|
|
|
// Create a temporary comment
|
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/${commentId}/like/`)
|
|
|
|
.expectStatus(400)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can like a reply', async function () {
|
|
|
|
// Check initial status: two replies before test
|
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/${testReplyId}/like/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expectEmptyBody();
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check liked
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/comments/${testReplyId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: new Array(1).fill(commentMatcherWithReplies({replies: 0}))
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.comments[0].liked.should.eql(true);
|
|
|
|
body.comments[0].count.likes.should.eql(1);
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can return replies', async function () {
|
|
|
|
const parentId = fixtureManager.get('comments', 0).id;
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check initial status: two replies before test
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/comments/${parentId}/replies/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2022-09-02 17:57:59 +03:00
|
|
|
comments: new Array(7).fill(commentMatcher)
|
2022-08-30 18:38:58 +03:00
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
should(body.comments[0].count.replies).be.undefined();
|
2022-09-02 17:57:59 +03:00
|
|
|
should(body.meta.pagination.total).eql(7);
|
2022-08-30 18:38:58 +03:00
|
|
|
should(body.meta.pagination.next).eql(null);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check liked + likes working for replies too
|
|
|
|
should(body.comments[2].id).eql(testReplyId);
|
|
|
|
should(body.comments[2].count.likes).eql(1);
|
|
|
|
should(body.comments[2].liked).eql(true);
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-09-02 17:57:59 +03:00
|
|
|
it('Can request last page of replies', async function () {
|
2022-08-30 18:38:58 +03:00
|
|
|
const parentId = fixtureManager.get('comments', 0).id;
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check initial status: two replies before test
|
|
|
|
await membersAgent
|
2022-09-02 17:57:59 +03:00
|
|
|
.get(`/api/comments/${parentId}/replies/?page=3&limit=3`)
|
2022-08-30 18:38:58 +03:00
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2022-09-02 17:57:59 +03:00
|
|
|
comments: new Array(1).fill(commentMatcher)
|
2022-08-30 18:38:58 +03:00
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
should(body.comments[0].count.replies).be.undefined();
|
2022-09-02 17:57:59 +03:00
|
|
|
should(body.meta.pagination.total).eql(7);
|
2022-08-30 18:38:58 +03:00
|
|
|
should(body.meta.pagination.next).eql(null);
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can remove a like (unlike)', async function () {
|
|
|
|
// Unlike
|
|
|
|
await membersAgent
|
|
|
|
.delete(`/api/comments/${commentId}/like/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expectEmptyBody();
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check not liked
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/comments/${commentId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: new Array(1).fill(commentMatcherWithReplies({replies: 1}))
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.comments[0].liked.should.eql(false);
|
|
|
|
body.comments[0].count.likes.should.eql(0);
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Cannot unlike a comment if it has not been liked', async function () {
|
|
|
|
// Remove like
|
|
|
|
await membersAgent
|
|
|
|
.delete(`/api/comments/${commentId}/like/`)
|
|
|
|
//.expectStatus(404)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can report a comment', async function () {
|
|
|
|
// Create a temporary comment
|
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/${commentId}/report/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expectEmptyBody();
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check report
|
|
|
|
const reports = await models.CommentReport.findAll({filter: 'comment_id:' + commentId});
|
|
|
|
reports.models.length.should.eql(1);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const report = reports.models[0];
|
|
|
|
report.get('member_id').should.eql(member.id);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
mockManager.assert.sentEmail({
|
|
|
|
subject: '🚩 A comment has been reported on your post',
|
|
|
|
to: fixtureManager.get('users', 0).email,
|
|
|
|
html: new RegExp(escapeRegExp('<p>This is a message</p><p></p><p>New line</p>')),
|
|
|
|
text: new RegExp(escapeRegExp('This is a message\n\nNew line'))
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Cannot report a comment twice', async function () {
|
|
|
|
// Create a temporary comment
|
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/${commentId}/report/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expectEmptyBody();
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Check report should be the same (no extra created)
|
|
|
|
const reports = await models.CommentReport.findAll({filter: 'comment_id:' + commentId});
|
|
|
|
reports.models.length.should.eql(1);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const report = reports.models[0];
|
|
|
|
report.get('member_id').should.eql(member.id);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
mockManager.assert.sentEmailCount(0);
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can edit a comment on a post', async function () {
|
|
|
|
const {body} = await await membersAgent
|
|
|
|
.put(`/api/comments/${commentId}`)
|
|
|
|
.body({comments: [{
|
|
|
|
html: 'Updated comment'
|
|
|
|
}]})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [{
|
|
|
|
...commentMatcherWithReplies({replies: 1}),
|
|
|
|
edited_at: anyISODateTime
|
|
|
|
}]
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
assert(body.comments[0].edited_at, 'The edited_at field should be populated');
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not edit a comment post_id', async function () {
|
|
|
|
const anotherPostId = fixtureManager.get('posts', 1).id;
|
|
|
|
await membersAgent
|
|
|
|
.put(`/api/comments/${commentId}`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: anotherPostId
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {body} = await membersAgent
|
|
|
|
.get(`/api/comments/?filter=post_id:${anotherPostId}`);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
assert(!body.comments.find(comment => comment.id === commentId), 'The comment should not have moved post');
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not edit a comment which does not belong to you', async function () {
|
|
|
|
await membersAgent2
|
|
|
|
.put(`/api/comments/${commentId}`)
|
|
|
|
.body({comments: [{
|
|
|
|
html: 'Illegal comment update'
|
|
|
|
}]})
|
|
|
|
.expectStatus(403)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
type: 'NoPermissionError',
|
|
|
|
id: anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not edit a comment as a member who is not you', async function () {
|
|
|
|
const memberId = fixtureManager.get('members', 1).id;
|
|
|
|
await membersAgent
|
|
|
|
.put(`/api/comments/${commentId}`)
|
|
|
|
.body({comments: [{
|
|
|
|
html: 'Illegal comment update',
|
|
|
|
member_id: memberId
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [
|
|
|
|
comment
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} = await membersAgent.get(`/api/comments/${commentId}`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
comments: [{
|
|
|
|
...commentMatcherWithReplies({replies: 1}),
|
|
|
|
edited_at: anyISODateTime
|
|
|
|
}]
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
assert(comment.member.id !== memberId);
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not reply to a reply', async function () {
|
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [{
|
|
|
|
id: parentId
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
html: 'Parent'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [{
|
|
|
|
id: replyId
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: parentId,
|
|
|
|
html: 'Reply'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: replyId,
|
|
|
|
html: 'Reply to a reply!'
|
|
|
|
}]})
|
|
|
|
.expectStatus(400)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
type: 'BadRequestError',
|
|
|
|
id: anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not edit a replies parent', async function () {
|
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [{
|
|
|
|
id: parentId
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
html: 'Parent'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [{
|
|
|
|
id: newParentId
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
html: 'New Parent'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [{
|
|
|
|
id: replyId
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
parent_id: parentId,
|
|
|
|
html: 'Reply'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Attempt to edit the parent
|
|
|
|
await membersAgent
|
|
|
|
.put(`/api/comments/${replyId}/`)
|
|
|
|
.body({comments: [{
|
|
|
|
parent_id: newParentId,
|
|
|
|
html: 'Changed parent'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {body: {comments: [comment]}} = await membersAgent.get(`api/comments/${newParentId}`);
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
assert(comment.replies.length === 0, 'The parent comment should not have changed');
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can fetch counts', async function () {
|
2022-11-02 08:47:20 +03:00
|
|
|
const ids = [
|
|
|
|
fixtureManager.get('posts', 0).id,
|
|
|
|
fixtureManager.get('posts', 1).id,
|
|
|
|
fixtureManager.get('posts', 2).id
|
|
|
|
];
|
2022-08-30 18:38:58 +03:00
|
|
|
await membersAgent
|
2022-11-02 08:47:20 +03:00
|
|
|
.get(`api/comments/counts/?ids=${ids.join(',')}`)
|
2022-08-30 18:38:58 +03:00
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot();
|
|
|
|
});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can delete a comment, and it is redacted from', async function () {
|
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [{
|
|
|
|
id: commentToDeleteId
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
html: 'Comment to delete'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
const {
|
|
|
|
body: {
|
|
|
|
comments: [deletedComment]
|
|
|
|
}
|
|
|
|
} = await membersAgent
|
|
|
|
.put(`/api/comments/${commentToDeleteId}`)
|
|
|
|
.body({comments: [{
|
|
|
|
status: 'deleted'
|
|
|
|
}]});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
assert(!deletedComment.html);
|
|
|
|
});
|
2022-07-22 13:03:05 +03:00
|
|
|
});
|
2022-07-06 11:41:38 +03:00
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('when commenting disabled', function () {
|
2022-07-27 15:56:16 +03:00
|
|
|
beforeEach(async function () {
|
|
|
|
await membersAgent.loginAs('member@example.com');
|
|
|
|
const getStub = sinon.stub(settingsCache, 'get');
|
|
|
|
getStub.callsFake((key, options) => {
|
|
|
|
if (key === 'comments_enabled') {
|
|
|
|
return 'off';
|
|
|
|
}
|
|
|
|
return getStub.wrappedMethod.call(settingsCache, key, options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not comment on a post', async function () {
|
2022-07-27 15:56:16 +03:00
|
|
|
const {body} = await membersAgent
|
|
|
|
.post(`/api/comments/`)
|
|
|
|
.body({comments: [{
|
|
|
|
post_id: postId,
|
|
|
|
html: '<p>This is a <strong>message</strong></p><p>New line</p>'
|
|
|
|
}]})
|
|
|
|
.expectStatus(405);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('when paid only commenting', function () {
|
|
|
|
beforeEach(async function () {
|
2022-07-27 15:56:16 +03:00
|
|
|
const getStub = sinon.stub(settingsCache, 'get');
|
|
|
|
getStub.callsFake((key, options) => {
|
|
|
|
if (key === 'comments_enabled') {
|
2022-08-30 18:38:58 +03:00
|
|
|
return 'paid';
|
2022-07-27 15:56:16 +03:00
|
|
|
}
|
|
|
|
return getStub.wrappedMethod.call(settingsCache, key, options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
2022-07-06 11:41:38 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('Members with access', function () {
|
|
|
|
before(async function () {
|
|
|
|
await membersAgent.loginAs('paid@example.com');
|
|
|
|
member = await models.Member.findOne({email: 'paid@example.com'}, {require: true});
|
|
|
|
|
|
|
|
const product = await getPaidProduct();
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Attach comped subscription to this member
|
|
|
|
await models.Member.edit({
|
|
|
|
status: 'comped',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: product.id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, {id: member.id});
|
2022-07-20 16:25:38 +03:00
|
|
|
});
|
2022-07-25 18:35:46 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can comment on a post', async function () {
|
|
|
|
await testCanCommentOnPost(member);
|
2022-07-20 16:25:38 +03:00
|
|
|
});
|
2022-07-25 18:35:46 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can reply to a comment', async function () {
|
|
|
|
await testCanReply(member);
|
|
|
|
});
|
2022-07-20 16:25:38 +03:00
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('Members without access', function () {
|
|
|
|
before(async function () {
|
|
|
|
await membersAgent.loginAs('free@example.com');
|
2022-07-20 16:25:38 +03:00
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not comment on a post', async function () {
|
|
|
|
await testCannotCommentOnPost();
|
2022-07-20 16:25:38 +03:00
|
|
|
});
|
2022-07-25 18:35:46 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not reply to a comment', async function () {
|
|
|
|
await testCannotReply();
|
2022-07-25 12:41:33 +03:00
|
|
|
});
|
2022-07-22 13:03:05 +03:00
|
|
|
});
|
2022-08-30 18:38:58 +03:00
|
|
|
});
|
2022-07-22 13:03:05 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Only allow members with access to a given post to comment on that post
|
|
|
|
describe('Tier-only posts', function () {
|
|
|
|
let post;
|
2022-11-02 08:47:20 +03:00
|
|
|
let product;
|
2022-07-25 12:41:33 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
before(async function () {
|
|
|
|
product = await getPaidProduct();
|
2022-07-25 12:41:33 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Limit post access
|
|
|
|
post = await models.Post.findOne({id: postId}, {require: true});
|
2022-07-25 12:41:33 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
await models.Post.edit({
|
|
|
|
visibility: 'tiers',
|
|
|
|
tiers: [
|
|
|
|
{
|
|
|
|
id: product.id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, {id: post.id});
|
2022-07-25 12:41:33 +03:00
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
const getStub = sinon.stub(settingsCache, 'get');
|
|
|
|
getStub.callsFake((key, options) => {
|
|
|
|
if (key === 'comments_enabled') {
|
|
|
|
return 'all';
|
2022-07-25 12:41:33 +03:00
|
|
|
}
|
2022-08-30 18:38:58 +03:00
|
|
|
return getStub.wrappedMethod.call(settingsCache, key, options);
|
|
|
|
});
|
2022-07-25 12:41:33 +03:00
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
afterEach(async function () {
|
|
|
|
sinon.restore();
|
2022-07-25 12:41:33 +03:00
|
|
|
});
|
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('Members with access', function () {
|
|
|
|
before(async function () {
|
|
|
|
await membersAgent.loginAs('member-premium@example.com');
|
|
|
|
member = await models.Member.findOne({email: 'member-premium@example.com'}, {require: true});
|
2022-11-02 08:47:20 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
// Attach comped subscription to this member
|
|
|
|
await models.Member.edit({
|
|
|
|
status: 'comped',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: product.id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, {id: member.id});
|
|
|
|
});
|
2022-07-25 12:41:33 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can comment on a post', async function () {
|
|
|
|
await testCanCommentOnPost(member);
|
|
|
|
});
|
2022-08-01 10:48:55 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can reply to a comment', async function () {
|
|
|
|
await testCanReply(member);
|
|
|
|
});
|
2022-08-01 10:48:55 +03:00
|
|
|
});
|
2022-08-08 15:29:27 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
describe('Members without access', function () {
|
|
|
|
before(async function () {
|
|
|
|
await membersAgent.loginAs('member-not-premium@example.com');
|
|
|
|
});
|
2022-08-08 15:29:27 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not comment on a post', async function () {
|
|
|
|
await testCannotCommentOnPost();
|
|
|
|
});
|
2022-08-08 15:29:27 +03:00
|
|
|
|
2022-08-30 18:38:58 +03:00
|
|
|
it('Can not reply to a comment', async function () {
|
|
|
|
await testCannotReply();
|
|
|
|
});
|
2022-08-08 15:29:27 +03:00
|
|
|
});
|
2022-07-06 11:41:38 +03:00
|
|
|
});
|
|
|
|
});
|