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)
This commit is contained in:
Naz 2022-02-22 10:54:06 +07:00
parent 3c3ead8648
commit fa373e0956
2 changed files with 56 additions and 0 deletions

View File

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

View File

@ -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<ContentAPITestAgent>} 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
},