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
This commit is contained in:
Naz 2022-02-21 11:19:07 +07:00
parent 4b5852fab1
commit 178e7db066
3 changed files with 31 additions and 18 deletions

View File

@ -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) {

View File

@ -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);
}
}

22
test/utils/test-agent.js Normal file
View File

@ -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;