mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-30 14:22:07 +03:00
f4ad3b23ba
refs https://github.com/TryGhost/Toolbox/issues/129 - The "Origin" header is require in Admin API requests so it makes sense to bake it into a default request made by most tests. Reduces unnecesary fluf around test request setup and removes a "config" dependency in each tests suite using the "e2e framework"
91 lines
2.8 KiB
JavaScript
91 lines
2.8 KiB
JavaScript
const supertest = require('supertest');
|
|
const errors = require('@tryghost/errors');
|
|
|
|
class TestAgent {
|
|
/**
|
|
* @constructor
|
|
* @param {Object} options
|
|
* @param {String} options.apiURL
|
|
* @param {String} options.originURL
|
|
* @param {Object} options.app Ghost express app instance
|
|
*/
|
|
constructor({apiURL, app, originURL}) {
|
|
this.API_URL = apiURL;
|
|
this.app = app;
|
|
this.originURL = originURL;
|
|
this.request = supertest.agent(app);
|
|
}
|
|
|
|
/**
|
|
* Helper method to concatenate urls
|
|
* @NOTE: this is essentially a duplicate of our internal urljoin tool that is stuck in the too-big url-utils package atm
|
|
* @param {string} url
|
|
* @returns
|
|
*/
|
|
makeUrl(url) {
|
|
// Join the base URL and the main url and remove any duplicate slashes
|
|
return `${this.API_URL}/${url}`.replace(/(^|[^:])\/\/+/g, '$1/');
|
|
}
|
|
|
|
// Forward get(), post(), put(), and delete() straight to the request agent & handle the URL
|
|
get(url) {
|
|
return this.request.get(this.makeUrl(url))
|
|
.set('Origin', this.originURL);
|
|
}
|
|
|
|
post(url) {
|
|
return this.request.post(this.makeUrl(url))
|
|
.set('Origin', this.originURL);
|
|
}
|
|
|
|
put(url) {
|
|
return this.request.put(this.makeUrl(url))
|
|
.set('Origin', this.originURL);
|
|
}
|
|
|
|
delete(url) {
|
|
return this.request.delete(this.makeUrl(url))
|
|
.set('Origin', this.originURL);
|
|
}
|
|
|
|
async loginAs(email, password) {
|
|
await this.post('/session/')
|
|
.send({
|
|
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() {
|
|
// temporary copy-pasta
|
|
let user = {
|
|
// owner (owner is still id 1 because of permissions)
|
|
id: '1',
|
|
name: 'Joe Bloggs',
|
|
slug: 'joe-bloggs',
|
|
email: 'jbloggs@example.com',
|
|
password: 'Sl1m3rson99',
|
|
profile_image: 'https://example.com/super_photo.jpg'
|
|
};
|
|
|
|
await this.loginAs(user.email, user.password);
|
|
}
|
|
}
|
|
|
|
module.exports = TestAgent;
|