From 477295a2624e8553556f3d35be533069dc45f029 Mon Sep 17 00:00:00 2001 From: Steve Larson <9larsons@gmail.com> Date: Mon, 30 Jan 2023 07:59:00 -0600 Subject: [PATCH] added mentions admin browse api e2e tests (#16177) refs TryGhost/Team#2468 -added simple browse api tests for webmentions -need to add user access tests and possibly site access (private sites) --- .../admin/__snapshots__/mentions.test.js.snap | 46 +++++++++++++++++ .../core/test/e2e-api/admin/mentions.test.js | 40 +++++++++++++++ ghost/core/test/utils/fixture-utils.js | 9 ++++ .../test/utils/fixtures/data-generator.js | 51 +++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 ghost/core/test/e2e-api/admin/__snapshots__/mentions.test.js.snap create mode 100644 ghost/core/test/e2e-api/admin/mentions.test.js diff --git a/ghost/core/test/e2e-api/admin/__snapshots__/mentions.test.js.snap b/ghost/core/test/e2e-api/admin/__snapshots__/mentions.test.js.snap new file mode 100644 index 0000000000..d9f402c7d2 --- /dev/null +++ b/ghost/core/test/e2e-api/admin/__snapshots__/mentions.test.js.snap @@ -0,0 +1,46 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Mentions API Can browse with limits 1: [body] 1`] = ` +Object { + "mentions": Array [ + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "payload": null, + "resource_id": null, + "source": Any, + "source_author": null, + "source_excerpt": null, + "source_favicon": null, + "source_featured_image": null, + "source_site_title": null, + "source_title": Any, + "target": Any, + "timestamp": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "payload": null, + "resource_id": null, + "source": Any, + "source_author": null, + "source_excerpt": null, + "source_favicon": null, + "source_featured_image": null, + "source_site_title": null, + "source_title": Any, + "target": Any, + "timestamp": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, + ], + "meta": Object { + "pagination": Object { + "limit": 2, + "next": 2, + "page": 1, + "pages": 2, + "prev": null, + "total": 3, + }, + }, +} +`; diff --git a/ghost/core/test/e2e-api/admin/mentions.test.js b/ghost/core/test/e2e-api/admin/mentions.test.js new file mode 100644 index 0000000000..d02ec91ac8 --- /dev/null +++ b/ghost/core/test/e2e-api/admin/mentions.test.js @@ -0,0 +1,40 @@ +const {agentProvider, fixtureManager, mockManager, matchers} = require('../../utils/e2e-framework'); +const {anyObjectId, anyISODateTime, anyString} = matchers; + +const matchMentionShallowIncludes = { + id: anyObjectId, + source: anyString, + target: anyString, + timestamp: anyISODateTime, + source_title: anyString +}; + +describe('Mentions API', function () { + let agent; + + before(async function () { + agent = await agentProvider.getAdminAPIAgent(); + mockManager.mockLabsEnabled('webmentions'); + // TODO: test various users' access + await fixtureManager.init('users','mentions'); + await agent.loginAsOwner(); + }); + + afterEach(function () { + mockManager.restore(); + }); + + it('Can browse with limits', async function () { + const res = await agent.get('mentions/?limit=2') + .expectStatus(200) + .matchBodySnapshot({ + mentions: new Array(2).fill(matchMentionShallowIncludes) + }); + }); + + it('Cannot browse when lab disabled', async function () { + mockManager.mockLabsDisabled('webmentions'); + const res = await agent.get('mentions/') + .expectStatus(404); + }); +}); diff --git a/ghost/core/test/utils/fixture-utils.js b/ghost/core/test/utils/fixture-utils.js index 2fee9c4ed2..b0ba5633b4 100644 --- a/ghost/core/test/utils/fixture-utils.js +++ b/ghost/core/test/utils/fixture-utils.js @@ -460,6 +460,12 @@ const fixtures = { })); }, + insertMentions: function insertMentions() { + return Promise.all(DataGenerator.forKnex.mentions.map((mention) => { + return models.Mention.add(mention, context.internal); + })); + }, + insertEmails: function insertEmails() { return Promise.all(DataGenerator.forKnex.emails.map((email) => { return models.Email.add(email, context.internal); @@ -826,6 +832,9 @@ const toDoList = { }, links: function insertLinks() { return fixtures.insertLinks(); + }, + mentions: function insertMentions() { + return fixtures.insertMentions(); } }; diff --git a/ghost/core/test/utils/fixtures/data-generator.js b/ghost/core/test/utils/fixtures/data-generator.js index 78407d283b..c578968a4a 100644 --- a/ghost/core/test/utils/fixtures/data-generator.js +++ b/ghost/core/test/utils/fixtures/data-generator.js @@ -890,6 +890,42 @@ DataGenerator.Content = { created_at: null, updated_at: null } + ], + + // TODO: build mentions for testing browse endpoint + mentions: [ + // source: webmention.source, + // target: webmention.target, + // timestamp: new Date(), + // payload: webmention.payload, + // resourceId: resourceInfo.type === 'post' ? resourceInfo.id : null, + // sourceTitle: metadata.title, + // sourceSiteTitle: metadata.siteTitle, + // sourceAuthor: metadata.author, + // sourceExcerpt: metadata.excerpt, + // sourceFavicon: metadata.favicon, + // sourceFeaturedImage: metadata.image + { + id: ObjectId().toHexString(), + source: 'https://source.com', + target: 'https://target.com', + sourceTitle: 'Title!', + sourceExcerpt: 'Excerpt!' + }, + { + id: ObjectId().toHexString(), + source: 'https://anothersource.com', + target: 'https://anothertarget.com', + sourceTitle: 'Another Title!', + sourceExcerpt: 'Another Excerpt!' + }, + { + id: ObjectId().toHexString(), + source: 'https://src.com', + target: 'https://target.com', + sourceTitle: 'Title goes here', + sourceExcerpt: 'Excerpt goes here' + } ] }; @@ -1340,6 +1376,13 @@ DataGenerator.forKnex = (function () { }); } + function createMention(overrides) { + const newObj = _.cloneDeep(overrides); + return _.defaults(newObj, { + created_at: new Date() + }); + } + const posts = [ createPost(DataGenerator.Content.posts[0]), createPost(DataGenerator.Content.posts[1]), @@ -1849,6 +1892,12 @@ DataGenerator.forKnex = (function () { createLink(DataGenerator.Content.links[2]) ]; + const mentions = [ + createMention(DataGenerator.Content.mentions[0]), + createMention(DataGenerator.Content.mentions[1]), + createMention(DataGenerator.Content.mentions[2]) + ]; + return { createPost, createGenericPost, @@ -1880,6 +1929,7 @@ DataGenerator.forKnex = (function () { createNewsletter, createOffer, createComment, + createMention, invites, posts, @@ -1912,6 +1962,7 @@ DataGenerator.forKnex = (function () { comments, redirects, links, + mentions, members_paid_subscription_events, members_created_events,