From fa373e0956144c0844f7e1b54222a669c2c16c20 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 22 Feb 2022 10:54:06 +0700 Subject: [PATCH] Added ContentAPITestAgent refs https://github.com/TryGhost/Toolbox/issues/215 - The ContentAPI needs it's own test agent, so we can write e2e tests. - The main method mostly to be used by the test suites is "authenticate" - it add necessary authentication keys to the request. The agent is not authenticated by default because there are suites that need to test the "non authenticated" requests. Also, there's a need to have the default API key inserted from fixtures level before authenticating (it's not strictly necessary because the key is not dynamic, but I think coupling this point would be a bad move) --- test/utils/content-api-test-agent.js | 33 ++++++++++++++++++++++++++++ test/utils/e2e-framework.js | 23 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 test/utils/content-api-test-agent.js diff --git a/test/utils/content-api-test-agent.js b/test/utils/content-api-test-agent.js new file mode 100644 index 0000000000..b5ee0b6c19 --- /dev/null +++ b/test/utils/content-api-test-agent.js @@ -0,0 +1,33 @@ +const TestAgent = require('./test-agent'); +const DataGenerator = require('./fixtures/data-generator'); + +const defaultContentAPISecretKey = DataGenerator.Content.api_keys[1].secret; + +/** + * @constructor + * @param {Object} app Ghost express app instance + * @param {Object} options + * @param {String} options.apiURL + * @param {String} options.originURL + */ +class ContentAPITestAgent extends TestAgent { + constructor(app, options) { + super(app, options); + } + + async authenticateWithSecret(secret) { + this.defaults.queryParams = { + key: secret + }; + } + + /** + * + * @description Authenticate with default content api keys + */ + authenticate() { + return this.authenticateWithSecret(defaultContentAPISecretKey); + } +} + +module.exports = ContentAPITestAgent; diff --git a/test/utils/e2e-framework.js b/test/utils/e2e-framework.js index b224c75cd4..ff99417cee 100644 --- a/test/utils/e2e-framework.js +++ b/test/utils/e2e-framework.js @@ -29,6 +29,7 @@ const mockManager = require('./e2e-framework-mock-manager'); 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 db = require('./db-utils'); // Services that need resetting @@ -142,6 +143,27 @@ const resetData = async () => { await db.reset({truncate: true}); }; +/** + * Creates a ContentAPITestAgent which is a drop-in substitution for supertest. + * It is automatically hooked up to the Content API so you can make requests to e.g. + * agent.get('/posts/') without having to worry about URL paths + * @returns {Promise} agent + */ +const getContentAPIAgent = async () => { + try { + const app = await startGhost(); + const originURL = configUtils.config.get('url'); + + return new ContentAPITestAgent(app, { + apiURL: '/ghost/api/canary/content/', + originURL + }); + } catch (error) { + error.message = `Unable to create test agent. ${error.message}`; + throw error; + } +}; + /** * Creates a AdminAPITestAgent which is a drop-in substitution for supertest. * It is automatically hooked up to the Admin API so you can make requests to e.g. @@ -237,6 +259,7 @@ module.exports = { agentProvider: { getAdminAPIAgent, getMembersAPIAgent, + getContentAPIAgent, getAgentsForMembers },