2022-02-21 07:19:07 +03:00
|
|
|
const TestAgent = require('./test-agent');
|
2021-12-07 19:21:48 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2022-02-06 19:18:12 +03:00
|
|
|
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
|
|
|
|
*/
|
2022-02-21 07:19:07 +03:00
|
|
|
class AdminAPITestAgent extends TestAgent {
|
2022-02-06 19:18:12 +03:00
|
|
|
constructor(app, options) {
|
2022-02-21 07:19:07 +03:00
|
|
|
super(app, options);
|
2021-12-07 19:21:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async loginAs(email, password) {
|
2022-06-01 08:34:20 +03:00
|
|
|
const res = await this.post('/session/')
|
2022-02-06 19:18:12 +03:00
|
|
|
.body({
|
2021-12-07 19:21:48 +03:00
|
|
|
grant_type: 'password',
|
|
|
|
username: email,
|
|
|
|
password: password
|
2022-06-01 08:34:20 +03:00
|
|
|
});
|
2021-12-07 19:21:48 +03:00
|
|
|
|
2022-06-01 08:34:20 +03:00
|
|
|
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
|
2021-12-07 19:21:48 +03:00
|
|
|
});
|
2022-06-01 08:34:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.headers['set-cookie'];
|
2021-12-07 19:21:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async loginAsOwner() {
|
2022-02-06 19:18:12 +03:00
|
|
|
await this.loginAs(ownerUser.email, ownerUser.password);
|
2021-12-07 19:21:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 07:04:47 +03:00
|
|
|
module.exports = AdminAPITestAgent;
|