0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Checked for existence of a resource to determine page existence

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

The existing implementation was a very basic check to get us to the
first milestone. By checking if the page points to a resource we can
know for sure the URL exists on the site.
This commit is contained in:
Fabien "egg" O'Carroll 2023-01-24 14:08:42 +07:00
parent a22799e133
commit 919d0a80c0
3 changed files with 89 additions and 39 deletions

View file

@ -1,5 +1,6 @@
/** /**
* @typedef {import('@tryghost/webmentions/lib/MentionsAPI').IRoutingService} IRoutingService * @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} */ /** @typedef {URL} */
#siteUrl; #siteUrl;
/** @typedef {IResourceService} */
#resourceService;
/** /**
* @param {object} deps * @param {object} deps
* @param {URL} deps.siteUrl * @param {URL} deps.siteUrl
* @param {IResourceService} deps.resourceService
* @param {import('got')} deps.externalRequest;
*/ */
constructor(deps) { constructor(deps) {
this.#siteUrl = deps.siteUrl; this.#siteUrl = deps.siteUrl;
this.#resourceService = deps.resourceService;
} }
/** /**
@ -35,7 +42,13 @@ module.exports = class RoutingService {
return false; return false;
} }
return true; const resource = await this.#resourceService.getByURL(url);
if (resource) {
return true;
}
return false;
} }
}; };

View file

@ -35,7 +35,8 @@ module.exports = {
urlService urlService
}); });
const routingService = new RoutingService({ const routingService = new RoutingService({
siteUrl: new URL(urlUtils.getSiteUrl()) siteUrl: new URL(urlUtils.getSiteUrl()),
resourceService
}); });
const api = new MentionsAPI({ const api = new MentionsAPI({

View file

@ -1,55 +1,91 @@
const assert = require('assert'); const assert = require('assert');
const sinon = require('sinon');
const ObjectID = require('bson-objectid').default;
const RoutingService = require('../../../../../core/server/services/mentions/RoutingService'); const RoutingService = require('../../../../../core/server/services/mentions/RoutingService');
describe('RoutingService', function () { describe('RoutingService', function () {
describe('pageExists', function () { describe('pageExists', function () {
it('Returns false if the url is from a different origin', async function () { describe('URL checks', function () {
const siteUrl = new URL('https://website.com'); it('Returns false if the url is from a different origin', async function () {
const routingService = new RoutingService({ const siteUrl = new URL('https://website.com');
siteUrl 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 () { describe('Resource checks', function () {
const siteUrl = new URL('https://website.com/subdir'); it('Returns true if a resource exists for the URL', async function () {
const routingService = new RoutingService({ const siteUrl = new URL('https://website.com/subdir');
siteUrl 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: { it('Returns false if the url is on the correct origin and subdirectory and a resource does not exist', async function () {
const result = await routingService.pageExists(new URL('https://website.com')); const siteUrl = new URL('https://website.com/subdir');
assert.equal(result, false); const resourceService = {
break checkNoSubdomain; getByURL: sinon.stub()
} };
const routingService = new RoutingService({
siteUrl,
resourceService
});
checkIncorrectSubdomain: { resourceService.getByURL.resolves(null);
const result = await routingService.pageExists(new URL('https://website.com/different'));
assert.equal(result, false);
break checkIncorrectSubdomain;
}
});
it('Returns true if the url is on the correct origin and subdirectory', async function () { checkJustSubdomain: {
const siteUrl = new URL('https://website.com/subdir'); const result = await routingService.pageExists(new URL('https://website.com/subdir'));
const routingService = new RoutingService({ assert.equal(result, false);
siteUrl 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;
}
}); });
}); });
}); });