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').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,8 +42,14 @@ module.exports = class RoutingService {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resource = await this.#resourceService.getByURL(url);
|
||||||
|
|
||||||
|
if (resource) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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({
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
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 () {
|
||||||
|
describe('URL checks', function () {
|
||||||
it('Returns false if the url is from a different origin', async function () {
|
it('Returns false if the url is from a different origin', async function () {
|
||||||
const siteUrl = new URL('https://website.com');
|
const siteUrl = new URL('https://website.com');
|
||||||
|
const resourceService = {
|
||||||
|
getByURL: sinon.stub()
|
||||||
|
};
|
||||||
const routingService = new RoutingService({
|
const routingService = new RoutingService({
|
||||||
siteUrl
|
siteUrl,
|
||||||
|
resourceService
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await routingService.pageExists(new URL('https://different-website.com'));
|
const result = await routingService.pageExists(new URL('https://different-website.com'));
|
||||||
|
@ -16,8 +23,12 @@ describe('RoutingService', function () {
|
||||||
|
|
||||||
it('Returns false if the url is not on the correct subdirectory', async function () {
|
it('Returns false if the url is not on the correct subdirectory', async function () {
|
||||||
const siteUrl = new URL('https://website.com/subdir');
|
const siteUrl = new URL('https://website.com/subdir');
|
||||||
|
const resourceService = {
|
||||||
|
getByURL: sinon.stub()
|
||||||
|
};
|
||||||
const routingService = new RoutingService({
|
const routingService = new RoutingService({
|
||||||
siteUrl
|
siteUrl,
|
||||||
|
resourceService
|
||||||
});
|
});
|
||||||
|
|
||||||
checkNoSubdomain: {
|
checkNoSubdomain: {
|
||||||
|
@ -32,24 +43,49 @@ describe('RoutingService', function () {
|
||||||
break checkIncorrectSubdomain;
|
break checkIncorrectSubdomain;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
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
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
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
|
||||||
|
});
|
||||||
|
|
||||||
|
resourceService.getByURL.resolves(null);
|
||||||
|
|
||||||
checkJustSubdomain: {
|
checkJustSubdomain: {
|
||||||
const result = await routingService.pageExists(new URL('https://website.com/subdir'));
|
const result = await routingService.pageExists(new URL('https://website.com/subdir'));
|
||||||
assert.equal(result, true);
|
assert.equal(result, false);
|
||||||
break checkJustSubdomain;
|
break checkJustSubdomain;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkLongerPath: {
|
checkLongerPath: {
|
||||||
const result = await routingService.pageExists(new URL('https://website.com/subdir/page'));
|
const result = await routingService.pageExists(new URL('https://website.com/subdir/page'));
|
||||||
assert.equal(result, true);
|
assert.equal(result, false);
|
||||||
break checkLongerPath;
|
break checkLongerPath;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue