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:
parent
a22799e133
commit
919d0a80c0
3 changed files with 89 additions and 39 deletions
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue