diff --git a/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap b/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap new file mode 100644 index 0000000000..e6fe38a6fd --- /dev/null +++ b/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap @@ -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", +} +`; diff --git a/test/e2e-api/members-comments/comments.test.js b/test/e2e-api/members-comments/comments.test.js new file mode 100644 index 0000000000..ceb224c8e0 --- /dev/null +++ b/test/e2e-api/members-comments/comments.test.js @@ -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 + }] + }); + }); + }); +}); diff --git a/test/utils/members-api-test-agent.js b/test/utils/members-api-test-agent.js index c76def0ba5..11847f3cf5 100644 --- a/test/utils/members-api-test-agent.js +++ b/test/utils/members-api-test-agent.js @@ -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;