mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
4b5852fab1
refs https://github.com/TryGhost/Toolbox/issues/214 - TestAgent was used to initialize both Admin & Member API agents, which is somewhat confusing because Member API does not have the same "loginAs" functionality like Admin API does - Having distinct agents for each API makes the class API cleaner with possibility to extract common functionality even further
56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
const Agent = require('@tryghost/express-test');
|
|
const errors = require('@tryghost/errors');
|
|
const DataGenerator = require('./fixtures/data-generator');
|
|
|
|
const ownerUser = {
|
|
email: DataGenerator.Content.users[0].email,
|
|
password: DataGenerator.Content.users[0].password
|
|
};
|
|
|
|
/**
|
|
* @constructor
|
|
* @param {Object} app Ghost express app instance
|
|
* @param {Object} options
|
|
* @param {String} options.apiURL
|
|
* @param {String} options.originURL
|
|
*/
|
|
class AdminAPITestAgent extends Agent {
|
|
constructor(app, options) {
|
|
super(app, {
|
|
baseUrl: options.apiURL,
|
|
headers: {
|
|
host: options.originURL.replace(/http:\/\//, ''),
|
|
origin: options.originURL
|
|
}
|
|
});
|
|
}
|
|
|
|
async loginAs(email, password) {
|
|
await this.post('/session/')
|
|
.body({
|
|
grant_type: 'password',
|
|
username: email,
|
|
password: password
|
|
})
|
|
.then(function then(res) {
|
|
if (res.statusCode === 302) {
|
|
// This can happen if you already have an instance running e.g. if you've been using Ghost CLI recently
|
|
throw new errors.IncorrectUsageError({
|
|
message: 'Ghost is redirecting, do you have an instance already running on port 2369?'
|
|
});
|
|
} else if (res.statusCode !== 200 && res.statusCode !== 201) {
|
|
throw new errors.IncorrectUsageError({
|
|
message: res.body.errors[0].message
|
|
});
|
|
}
|
|
|
|
return res.headers['set-cookie'];
|
|
});
|
|
}
|
|
|
|
async loginAsOwner() {
|
|
await this.loginAs(ownerUser.email, ownerUser.password);
|
|
}
|
|
}
|
|
|
|
module.exports = AdminAPITestAgent;
|