Removed need for 'Origin' header in test suites

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"
This commit is contained in:
Naz 2021-12-10 11:05:37 +04:00
parent fc4d8d7de0
commit f4ad3b23ba
4 changed files with 23 additions and 31 deletions

View File

@ -6,7 +6,6 @@ const testUtils = require('../../../../utils/index');
const framework = require('../../../../utils/e2e-framework'); const framework = require('../../../../utils/e2e-framework');
const models = require('../../../../../core/server/models/index'); const models = require('../../../../../core/server/models/index');
const settingsCache = require('../../../../../core/shared/settings-cache'); const settingsCache = require('../../../../../core/shared/settings-cache');
const config = require('../../../../../core/shared/config/index');
describe('Authentication API canary', function () { describe('Authentication API canary', function () {
let request; let request;
@ -32,7 +31,6 @@ describe('Authentication API canary', function () {
it('is setup? no', async function () { it('is setup? no', async function () {
const res = await request const res = await request
.get('authentication/setup') .get('authentication/setup')
.set('Origin', config.get('url'))
.expect(200); .expect(200);
expect(res.body).to.matchSnapshot(); expect(res.body).to.matchSnapshot();
@ -45,7 +43,6 @@ describe('Authentication API canary', function () {
it('complete setup', async function () { it('complete setup', async function () {
const res = await request const res = await request
.post('authentication/setup') .post('authentication/setup')
.set('Origin', config.get('url'))
.send({ .send({
setup: [{ setup: [{
name: 'test user', name: 'test user',
@ -73,8 +70,7 @@ describe('Authentication API canary', function () {
it('is setup? yes', async function () { it('is setup? yes', async function () {
const res = await request const res = await request
.get('authentication/setup') .get('authentication/setup');
.set('Origin', config.get('url'));
expect(res.body).to.matchSnapshot(); expect(res.body).to.matchSnapshot();
expect(res.headers).to.matchSnapshot({ expect(res.headers).to.matchSnapshot({
@ -86,7 +82,6 @@ describe('Authentication API canary', function () {
it('complete setup again', function () { it('complete setup again', function () {
return request return request
.post('authentication/setup') .post('authentication/setup')
.set('Origin', config.get('url'))
.send({ .send({
setup: [{ setup: [{
name: 'test user', name: 'test user',
@ -105,7 +100,6 @@ describe('Authentication API canary', function () {
const res = await request const res = await request
.put('authentication/setup') .put('authentication/setup')
.set('Origin', config.get('url'))
.send({ .send({
setup: [{ setup: [{
name: 'test user edit', name: 'test user edit',
@ -146,7 +140,6 @@ describe('Authentication API canary', function () {
it('check invite with invalid email', function () { it('check invite with invalid email', function () {
return request return request
.get('authentication/invitation?email=invalidemail') .get('authentication/invitation?email=invalidemail')
.set('Origin', config.get('url'))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(400); .expect(400);
}); });
@ -154,7 +147,6 @@ describe('Authentication API canary', function () {
it('check valid invite', async function () { it('check valid invite', async function () {
const res = await request const res = await request
.get(`authentication/invitation?email=${testUtils.DataGenerator.forKnex.invites[0].email}`) .get(`authentication/invitation?email=${testUtils.DataGenerator.forKnex.invites[0].email}`)
.set('Origin', config.get('url'))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
@ -164,7 +156,6 @@ describe('Authentication API canary', function () {
it('check invalid invite', async function () { it('check invalid invite', async function () {
const res = await request const res = await request
.get(`authentication/invitation?email=notinvited@example.org`) .get(`authentication/invitation?email=notinvited@example.org`)
.set('Origin', config.get('url'))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
@ -174,7 +165,6 @@ describe('Authentication API canary', function () {
it('try to accept without invite', function () { it('try to accept without invite', function () {
return request return request
.post('authentication/invitation') .post('authentication/invitation')
.set('Origin', config.get('url'))
.send({ .send({
invitation: [{ invitation: [{
token: 'lul11111', token: 'lul11111',
@ -190,7 +180,6 @@ describe('Authentication API canary', function () {
it('try to accept with invite and existing email address', function () { it('try to accept with invite and existing email address', function () {
return request return request
.post('authentication/invitation') .post('authentication/invitation')
.set('Origin', config.get('url'))
.send({ .send({
invitation: [{ invitation: [{
token: testUtils.DataGenerator.forKnex.invites[0].token, token: testUtils.DataGenerator.forKnex.invites[0].token,
@ -206,7 +195,6 @@ describe('Authentication API canary', function () {
it('try to accept with invite', async function () { it('try to accept with invite', async function () {
const res = await request const res = await request
.post('authentication/invitation') .post('authentication/invitation')
.set('Origin', config.get('url'))
.send({ .send({
invitation: [{ invitation: [{
token: testUtils.DataGenerator.forKnex.invites[0].token, token: testUtils.DataGenerator.forKnex.invites[0].token,
@ -255,7 +243,6 @@ describe('Authentication API canary', function () {
}); });
const res = await request.put('authentication/passwordreset') const res = await request.put('authentication/passwordreset')
.set('Origin', config.get('url'))
.set('Accept', 'application/json') .set('Accept', 'application/json')
.send({ .send({
passwordreset: [{ passwordreset: [{
@ -276,7 +263,6 @@ describe('Authentication API canary', function () {
it('reset password: invalid token', async function () { it('reset password: invalid token', async function () {
const res = await request const res = await request
.put('authentication/passwordreset') .put('authentication/passwordreset')
.set('Origin', config.get('url'))
.set('Accept', 'application/json') .set('Accept', 'application/json')
.send({ .send({
passwordreset: [{ passwordreset: [{
@ -311,7 +297,6 @@ describe('Authentication API canary', function () {
const res = await request const res = await request
.put('authentication/passwordreset') .put('authentication/passwordreset')
.set('Origin', config.get('url'))
.set('Accept', 'application/json') .set('Accept', 'application/json')
.send({ .send({
passwordreset: [{ passwordreset: [{
@ -343,7 +328,6 @@ describe('Authentication API canary', function () {
const res = await request const res = await request
.put('authentication/passwordreset') .put('authentication/passwordreset')
.set('Origin', config.get('url'))
.set('Accept', 'application/json') .set('Accept', 'application/json')
.send({ .send({
passwordreset: [{ passwordreset: [{
@ -368,7 +352,6 @@ describe('Authentication API canary', function () {
it('reset password: generate reset token', async function () { it('reset password: generate reset token', async function () {
const res = await request const res = await request
.post('authentication/passwordreset') .post('authentication/passwordreset')
.set('Origin', config.get('url'))
.set('Accept', 'application/json') .set('Accept', 'application/json')
.send({ .send({
passwordreset: [{ passwordreset: [{
@ -408,7 +391,6 @@ describe('Authentication API canary', function () {
it('reset all passwords returns 200', async function () { it('reset all passwords returns 200', async function () {
const res = await request.post('authentication/reset_all_passwords') const res = await request.post('authentication/reset_all_passwords')
.set('Origin', config.get('url'))
.set('Accept', 'application/json') .set('Accept', 'application/json')
.send({}) .send({})
.expect(200); .expect(200);

View File

@ -2,7 +2,6 @@ const {expect} = require('chai');
const {any, stringMatching} = require('expect'); const {any, stringMatching} = require('expect');
const framework = require('../../../../utils/e2e-framework'); const framework = require('../../../../utils/e2e-framework');
const config = require('../../../../../core/shared/config');
describe('Config API', function () { describe('Config API', function () {
let request; let request;
@ -13,8 +12,7 @@ describe('Config API', function () {
it('can retrieve config and all expected properties', async function () { it('can retrieve config and all expected properties', async function () {
const res = await request const res = await request
.get('site/') .get('site/');
.set('Origin', config.get('url'));
expect(res.body.site).to.matchSnapshot({ expect(res.body.site).to.matchSnapshot({
version: stringMatching(/\d+\.\d+/) version: stringMatching(/\d+\.\d+/)

View File

@ -115,8 +115,13 @@ const resetDb = async () => {
*/ */
const getAgent = async (apiURL) => { const getAgent = async (apiURL) => {
const app = await startGhost(); const app = await startGhost();
const originURL = configUtils.config.get('url');
return new TestAgent(apiURL, app); return new TestAgent({
apiURL,
app,
originURL
});
}; };
// request agent // request agent

View File

@ -4,12 +4,15 @@ const errors = require('@tryghost/errors');
class TestAgent { class TestAgent {
/** /**
* @constructor * @constructor
* @param {String} API_URL * @param {Object} options
* @param {Object} app Ghost express app instance * @param {String} options.apiURL
* @param {String} options.originURL
* @param {Object} options.app Ghost express app instance
*/ */
constructor(API_URL, app) { constructor({apiURL, app, originURL}) {
this.API_URL = API_URL; this.API_URL = apiURL;
this.app = app; this.app = app;
this.originURL = originURL;
this.request = supertest.agent(app); this.request = supertest.agent(app);
} }
@ -26,19 +29,23 @@ class TestAgent {
// Forward get(), post(), put(), and delete() straight to the request agent & handle the URL // Forward get(), post(), put(), and delete() straight to the request agent & handle the URL
get(url) { get(url) {
return this.request.get(this.makeUrl(url)); return this.request.get(this.makeUrl(url))
.set('Origin', this.originURL);
} }
post(url) { post(url) {
return this.request.post(this.makeUrl(url)); return this.request.post(this.makeUrl(url))
.set('Origin', this.originURL);
} }
put(url) { put(url) {
return this.request.put(this.makeUrl(url)); return this.request.put(this.makeUrl(url))
.set('Origin', this.originURL);
} }
delete(url) { delete(url) {
return this.request.delete(this.makeUrl(url)); return this.request.delete(this.makeUrl(url))
.set('Origin', this.originURL);
} }
async loginAs(email, password) { async loginAs(email, password) {