From a18469a3be86e5b83c4636eebfabc7f1e5c77f32 Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 20 May 2022 13:17:00 +0800 Subject: [PATCH] Added root Ghost server/API test agent refs https://github.com/TryGhost/Team/issues/1640 - Some tests require making request to the root of the mounted server path like `GET /ghost/.well-known/jwks.json`. These are not stricly APIs like Admin, Content, and Members. They do need a separate agent to distinguish the configuration - for this agetn we can stop loading some parts like "server" in the future to speed things up (didn't work straight out of the box) --- test/utils/e2e-framework.js | 30 +++++++++++++++++++++++++++++- test/utils/ghost-api-test-agent.js | 19 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/utils/ghost-api-test-agent.js diff --git a/test/utils/e2e-framework.js b/test/utils/e2e-framework.js index b3aabbc15e..51c2853f04 100644 --- a/test/utils/e2e-framework.js +++ b/test/utils/e2e-framework.js @@ -30,6 +30,7 @@ const boot = require('../../core/boot'); const AdminAPITestAgent = require('./admin-api-test-agent'); const MembersAPITestAgent = require('./members-api-test-agent'); const ContentAPITestAgent = require('./content-api-test-agent'); +const GhostAPITestAgent = require('./ghost-api-test-agent'); const db = require('./db-utils'); // Services that need resetting @@ -219,6 +220,32 @@ const getMembersAPIAgent = async () => { } }; +/** + * Creates a GhostAPITestAgent, which is a drop-in substitution for supertest + * It is automatically hooked up to the Ghost API so you can make requests to e.g. + * agent.get('/well-known/jwks.json') without having to worry about URL paths + * + * @returns {Promise} agent + */ +const getGhostAPIAgent = async () => { + const bootOptions = { + frontend: false + }; + + try { + const app = await startGhost(bootOptions); + const originURL = configUtils.config.get('url'); + + return new GhostAPITestAgent(app, { + apiURL: '/ghost/', + originURL + }); + } catch (error) { + error.message = `Unable to create test agent. ${error.message}`; + throw error; + } +}; + /** * * @returns {Promise<{adminAgent: AdminAPITestAgent, membersAgent: MembersAPITestAgent}>} agents @@ -260,7 +287,8 @@ module.exports = { getAdminAPIAgent, getMembersAPIAgent, getContentAPIAgent, - getAgentsForMembers + getAgentsForMembers, + getGhostAPIAgent }, // Mocks and Stubs diff --git a/test/utils/ghost-api-test-agent.js b/test/utils/ghost-api-test-agent.js new file mode 100644 index 0000000000..b279135ec5 --- /dev/null +++ b/test/utils/ghost-api-test-agent.js @@ -0,0 +1,19 @@ +const TestAgent = require('./test-agent'); + +/** + * NOTE: this class is not doing much at the moment. It's rather a placeholder to test + * any Ghost API specific functionality, like /.well-known. If there is none in the nearest + * future, it would make sense to remove it alltogether. + * @constructor + * @param {Object} app Ghost express app instance + * @param {Object} options + * @param {String} options.apiURL + * @param {String} options.originURL + */ +class GhostAPITestAgent extends TestAgent { + constructor(app, options) { + super(app, options); + } +} + +module.exports = GhostAPITestAgent;