diff --git a/ghost/core/core/server/services/mentions/RoutingService.js b/ghost/core/core/server/services/mentions/RoutingService.js index 737231fdc6..33520b8f58 100644 --- a/ghost/core/core/server/services/mentions/RoutingService.js +++ b/ghost/core/core/server/services/mentions/RoutingService.js @@ -1,5 +1,6 @@ /** * @typedef {import('@tryghost/webmentions/lib/MentionsAPI').IRoutingService} IRoutingService + * @typedef {import('@tryghost/webmentions/lib/MentionsAPI').IResourceService} IResourceService */ /** @@ -15,12 +16,18 @@ module.exports = class RoutingService { /** @typedef {URL} */ #siteUrl; + /** @typedef {IResourceService} */ + #resourceService; + /** * @param {object} deps * @param {URL} deps.siteUrl + * @param {IResourceService} deps.resourceService + * @param {import('got')} deps.externalRequest; */ constructor(deps) { this.#siteUrl = deps.siteUrl; + this.#resourceService = deps.resourceService; } /** @@ -35,7 +42,13 @@ module.exports = class RoutingService { return false; } - return true; + const resource = await this.#resourceService.getByURL(url); + + if (resource) { + return true; + } + + return false; } }; diff --git a/ghost/core/core/server/services/mentions/service.js b/ghost/core/core/server/services/mentions/service.js index cd6e4af202..6c2f908fb9 100644 --- a/ghost/core/core/server/services/mentions/service.js +++ b/ghost/core/core/server/services/mentions/service.js @@ -35,7 +35,8 @@ module.exports = { urlService }); const routingService = new RoutingService({ - siteUrl: new URL(urlUtils.getSiteUrl()) + siteUrl: new URL(urlUtils.getSiteUrl()), + resourceService }); const api = new MentionsAPI({ diff --git a/ghost/core/test/unit/server/services/mentions/RoutingService.test.js b/ghost/core/test/unit/server/services/mentions/RoutingService.test.js index 4465827abf..b09c32c6b0 100644 --- a/ghost/core/test/unit/server/services/mentions/RoutingService.test.js +++ b/ghost/core/test/unit/server/services/mentions/RoutingService.test.js @@ -1,55 +1,91 @@ const assert = require('assert'); +const sinon = require('sinon'); +const ObjectID = require('bson-objectid').default; const RoutingService = require('../../../../../core/server/services/mentions/RoutingService'); describe('RoutingService', function () { describe('pageExists', function () { - it('Returns false if the url is from a different origin', async function () { - const siteUrl = new URL('https://website.com'); - const routingService = new RoutingService({ - siteUrl + describe('URL checks', function () { + it('Returns false if the url is from a different origin', async function () { + const siteUrl = new URL('https://website.com'); + const resourceService = { + getByURL: sinon.stub() + }; + const routingService = new RoutingService({ + siteUrl, + resourceService + }); + + const result = await routingService.pageExists(new URL('https://different-website.com')); + + assert.equal(result, false); }); - const result = await routingService.pageExists(new URL('https://different-website.com')); + it('Returns false if the url is not on the correct subdirectory', async function () { + const siteUrl = new URL('https://website.com/subdir'); + const resourceService = { + getByURL: sinon.stub() + }; + const routingService = new RoutingService({ + siteUrl, + resourceService + }); - assert.equal(result, false); + checkNoSubdomain: { + const result = await routingService.pageExists(new URL('https://website.com')); + assert.equal(result, false); + break checkNoSubdomain; + } + + checkIncorrectSubdomain: { + const result = await routingService.pageExists(new URL('https://website.com/different')); + assert.equal(result, false); + break checkIncorrectSubdomain; + } + }); }); - it('Returns false if the url is not on the correct subdirectory', async function () { - const siteUrl = new URL('https://website.com/subdir'); - const routingService = new RoutingService({ - siteUrl + describe('Resource checks', function () { + it('Returns true if a resource exists for the URL', async function () { + const siteUrl = new URL('https://website.com/subdir'); + const resourceService = { + getByURL: sinon.stub() + }; + const routingService = new RoutingService({ + siteUrl, + resourceService + }); + + resourceService.getByURL.resolves({type: 'post', id: new ObjectID}); + + const result = await routingService.pageExists(new URL('https://website.com/subdir/post')); + assert.equal(result, true); }); - checkNoSubdomain: { - const result = await routingService.pageExists(new URL('https://website.com')); - assert.equal(result, false); - break checkNoSubdomain; - } + it('Returns false if the url is on the correct origin and subdirectory and a resource does not exist', async function () { + const siteUrl = new URL('https://website.com/subdir'); + const resourceService = { + getByURL: sinon.stub() + }; + const routingService = new RoutingService({ + siteUrl, + resourceService + }); - checkIncorrectSubdomain: { - const result = await routingService.pageExists(new URL('https://website.com/different')); - assert.equal(result, false); - break checkIncorrectSubdomain; - } - }); + resourceService.getByURL.resolves(null); - it('Returns true if the url is on the correct origin and subdirectory', async function () { - const siteUrl = new URL('https://website.com/subdir'); - const routingService = new RoutingService({ - siteUrl + checkJustSubdomain: { + const result = await routingService.pageExists(new URL('https://website.com/subdir')); + assert.equal(result, false); + break checkJustSubdomain; + } + + checkLongerPath: { + const result = await routingService.pageExists(new URL('https://website.com/subdir/page')); + assert.equal(result, false); + break checkLongerPath; + } }); - - checkJustSubdomain: { - const result = await routingService.pageExists(new URL('https://website.com/subdir')); - assert.equal(result, true); - break checkJustSubdomain; - } - - checkLongerPath: { - const result = await routingService.pageExists(new URL('https://website.com/subdir/page')); - assert.equal(result, true); - break checkLongerPath; - } }); }); });