0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Added comments API tests

refs https://github.com/TryGhost/Team/issues/1664

Note: we put it in the members-comments folder because some issue with test ordering
- refs https://ghost.slack.com/archives/C02G9E68C/p1657538586658649?thread_ts=1657522575.865029&cid=C02G9E68C
- There is something wrong with the url service reset when running multiple tests
- Currently we are doing a soft reset, this needs investigating
- Changing the order so that the comments API tests are executed after the content API tests, fixes the issue too.

Co-authored-by: Simon Backx <simon@ghost.org>
This commit is contained in:
Kevin Ansfield 2022-07-06 10:41:38 +02:00 committed by Simon Backx
parent 78c15933e6
commit 25c0b75426
3 changed files with 185 additions and 0 deletions

View file

@ -0,0 +1,91 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Comments API when authenticated Can browse all comments of a post 1: [body] 1`] = `
Object {
"comments": Array [
Object {
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
"edited_at": null,
"html": "This is a message",
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"member": Object {
"avatar_image": null,
"bio": null,
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
"email": "member@example.com",
"email_count": 0,
"email_open_rate": null,
"email_opened_count": 0,
"enable_comment_notifications": true,
"geolocation": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"last_commented_at": null,
"last_seen_at": null,
"name": null,
"note": null,
"status": "free",
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
"uuid": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/,
},
"member_id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"parent_id": null,
"post_id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"status": "published",
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
},
],
"meta": Object {
"pagination": Object {
"limit": 15,
"next": null,
"page": 1,
"pages": 1,
"prev": null,
"total": 1,
},
},
}
`;
exports[`Comments API when authenticated Can browse all comments of a post 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "*",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "805",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Accept-Encoding",
"x-powered-by": "Express",
}
`;
exports[`Comments API when authenticated Can comment on a post 1: [body] 1`] = `
Object {
"comments": Array [
Object {
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
"edited_at": null,
"html": "This is a message",
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"member_id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"parent_id": null,
"post_id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"status": "published",
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
},
],
}
`;
exports[`Comments API when authenticated Can comment on a post 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "*",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "286",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"location": StringMatching /https\\?:\\\\/\\\\/\\.\\*\\?\\\\/comments\\\\/\\[a-f0-9\\]\\{24\\}\\\\//,
"vary": "Accept-Encoding",
"x-powered-by": "Express",
}
`;

View file

@ -0,0 +1,76 @@
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyLocationFor, anyISODateTime, anyUuid} = matchers;
let membersAgent, membersService, postId;
describe('Comments API', function () {
before(async function () {
membersAgent = await agentProvider.getMembersAPIAgent();
await fixtureManager.init('posts', 'members');
//await fixtureManager.init('comments');
postId = fixtureManager.get('posts', 0).id;
});
afterEach(function () {
mockManager.restore();
});
describe('when not authenticated', function () {
it('can browse posts');
});
describe('when authenticated', function () {
before(async function () {
await membersAgent.loginAs('member@example.com');
});
it('Can comment on a post', async function () {
const {body} = await membersAgent
.post(`/api/comments/`)
.body({comments: [{
post_id: postId,
html: 'This is a message'
}]})
.expectStatus(201)
.matchHeaderSnapshot({
etag: anyEtag,
location: anyLocationFor('comments')
})
.matchBodySnapshot({
comments: [{
id: anyObjectId,
member_id: anyObjectId,
post_id: anyObjectId,
created_at: anyISODateTime,
updated_at: anyISODateTime
}]
});
});
it('Can browse all comments of a post', async function () {
const {body} = await membersAgent
.get(`/api/comments/?filter=post_id:${postId}&include=member`)
.expectStatus(200)
.matchHeaderSnapshot({
etag: anyEtag
})
.matchBodySnapshot({
comments: [{
id: anyObjectId,
member_id: anyObjectId,
member: {
id: anyObjectId,
created_at: anyISODateTime,
updated_at: anyISODateTime,
uuid: anyUuid
},
post_id: anyObjectId,
created_at: anyISODateTime,
updated_at: anyISODateTime
}]
});
});
});
});

View file

@ -1,4 +1,5 @@
const TestAgent = require('./test-agent');
const errors = require('@tryghost/errors');
/**
* NOTE: this class is not doing much at the moment. It's rather a placeholder to put
@ -14,6 +15,23 @@ class MembersAPITestAgent extends TestAgent {
constructor(app, options) {
super(app, options);
}
async loginAs(email) {
const membersService = require('../../core/server/services/members');
const magicLink = await membersService.api.getMagicLink(email);
const magicLinkUrl = new URL(magicLink);
const token = magicLinkUrl.searchParams.get('token');
const res = await this.get(`/?token=${token}`);
if (res.statusCode !== 302) {
throw new errors.IncorrectUsageError({
message: res.body.errors[0].message
});
}
return res.headers['set-cookie'];
}
}
module.exports = MembersAPITestAgent;