From 178e7db066cae5a5f3dc7e336b2e0eab51dd99e7 Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 21 Feb 2022 11:19:07 +0700 Subject: [PATCH] Extracted TestAgent class for test utils refs https://github.com/TryGhost/Toolbox/issues/214 - The constructor logic was duplicated across test agents, so made sence to extract the logic into a common class --- test/utils/admin-api-test-agent.js | 12 +++--------- test/utils/members-api-test-agent.js | 15 ++++++--------- test/utils/test-agent.js | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 test/utils/test-agent.js diff --git a/test/utils/admin-api-test-agent.js b/test/utils/admin-api-test-agent.js index c290a94494..d8b26b0252 100644 --- a/test/utils/admin-api-test-agent.js +++ b/test/utils/admin-api-test-agent.js @@ -1,4 +1,4 @@ -const Agent = require('@tryghost/express-test'); +const TestAgent = require('./test-agent'); const errors = require('@tryghost/errors'); const DataGenerator = require('./fixtures/data-generator'); @@ -14,15 +14,9 @@ const ownerUser = { * @param {String} options.apiURL * @param {String} options.originURL */ -class AdminAPITestAgent extends Agent { +class AdminAPITestAgent extends TestAgent { constructor(app, options) { - super(app, { - baseUrl: options.apiURL, - headers: { - host: options.originURL.replace(/http:\/\//, ''), - origin: options.originURL - } - }); + super(app, options); } async loginAs(email, password) { diff --git a/test/utils/members-api-test-agent.js b/test/utils/members-api-test-agent.js index 336820c6c8..c76def0ba5 100644 --- a/test/utils/members-api-test-agent.js +++ b/test/utils/members-api-test-agent.js @@ -1,21 +1,18 @@ -const Agent = require('@tryghost/express-test'); +const TestAgent = require('./test-agent'); /** + * NOTE: this class is not doing much at the moment. It's rather a placeholder to put + * any Members API specific functionality into. 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 MembersAPITestAgent extends Agent { +class MembersAPITestAgent extends TestAgent { constructor(app, options) { - super(app, { - baseUrl: options.apiURL, - headers: { - host: options.originURL.replace(/http:\/\//, ''), - origin: options.originURL - } - }); + super(app, options); } } diff --git a/test/utils/test-agent.js b/test/utils/test-agent.js new file mode 100644 index 0000000000..d7a62a91b6 --- /dev/null +++ b/test/utils/test-agent.js @@ -0,0 +1,22 @@ +const Agent = require('@tryghost/express-test'); + +/** + * @constructor + * @param {Object} app Ghost express app instance + * @param {Object} options + * @param {String} options.apiURL + * @param {String} options.originURL + */ +class TestAgent extends Agent { + constructor(app, options) { + super(app, { + baseUrl: options.apiURL, + headers: { + host: options.originURL.replace(/http:\/\//, ''), + origin: options.originURL + } + }); + } +} + +module.exports = TestAgent;