0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Refactored request initialization with supertest

refs https://github.com/TryGhost/Toolbox/issues/152
refs 5bea089dfe
refs 16ad5f73c4

- The refactor is heavily inspired by the referenced commit from @ErisDS
- This refactor is meant to serve as a template for further refactors and unification of the abstraction over "request". Should allow us to be more agnostic towards the library that's powering the request thing. For example, mock-express style of request handling could substitute or get substututed easily if all tests are behind similar "get(Authenticated)Agent" interface
This commit is contained in:
Naz 2021-12-06 18:34:35 +04:00 committed by naz
parent b7231875a0
commit 05a4fbfd2a
3 changed files with 19 additions and 18 deletions

View file

@ -14,8 +14,7 @@ let request;
describe('Authentication API canary', function () { describe('Authentication API canary', function () {
describe('Blog setup', function () { describe('Blog setup', function () {
before(async function () { before(async function () {
const app = await localUtils.startGhost({forceStart: true}); request = await localUtils.getAgent({forceStart: true});
request = supertest.agent(app);
}); });
beforeEach(function () { beforeEach(function () {
@ -133,10 +132,7 @@ describe('Authentication API canary', function () {
describe('Invitation', function () { describe('Invitation', function () {
before(async function () { before(async function () {
const app = await localUtils.startGhost(); request = await localUtils.getAuthenticatedRequestAgent(null, 'invites');
request = supertest.agent(app);
await localUtils.doAuth(request, 'invites');
}); });
it('check invite with invalid email', function () { it('check invite with invalid email', function () {
@ -225,10 +221,7 @@ describe('Authentication API canary', function () {
const user = testUtils.DataGenerator.forModel.users[0]; const user = testUtils.DataGenerator.forModel.users[0];
before(async function () { before(async function () {
const app = await localUtils.startGhost({forceStart: true}); request = await localUtils.getAuthenticatedRequestAgent({forceStart: true});
request = supertest.agent(app);
await localUtils.doAuth(request);
}); });
beforeEach(function () { beforeEach(function () {
@ -388,10 +381,7 @@ describe('Authentication API canary', function () {
describe('Reset all passwords', function () { describe('Reset all passwords', function () {
let sendEmail; let sendEmail;
before(async function () { before(async function () {
const app = await localUtils.startGhost({forceStart: true}); request = await localUtils.getAuthenticatedRequestAgent({forceStart: true});
request = supertest.agent(app);
await localUtils.doAuth(request);
}); });
beforeEach(function () { beforeEach(function () {

View file

@ -1,5 +1,4 @@
const should = require('should'); const should = require('should');
const supertest = require('supertest');
const testUtils = require('../../../../utils'); const testUtils = require('../../../../utils');
const localUtils = require('./utils'); const localUtils = require('./utils');
const config = require('../../../../../core/shared/config'); const config = require('../../../../../core/shared/config');
@ -8,9 +7,7 @@ describe('Config API', function () {
let request; let request;
before(async function () { before(async function () {
const app = await localUtils.startGhost(); request = await localUtils.getAuthenticatedRequestAgent();
request = supertest.agent(app);
await localUtils.doAuth(request);
}); });
it('can retrieve config and all expected properties', async function () { it('can retrieve config and all expected properties', async function () {

View file

@ -1,5 +1,6 @@
const url = require('url'); const url = require('url');
const _ = require('lodash'); const _ = require('lodash');
const supertest = require('supertest');
const testUtils = require('../../../../utils'); const testUtils = require('../../../../utils');
const API_URL = '/ghost/api/canary/admin/'; const API_URL = '/ghost/api/canary/admin/';
@ -213,6 +214,19 @@ module.exports = {
return await testUtils.startGhost(Object.assign(defaults, overrides)); return await testUtils.startGhost(Object.assign(defaults, overrides));
}, },
async getAgent(options) {
const app = await this.startGhost(options);
return supertest.agent(app);
},
async getAuthenticatedAgent(bootOptions, fixtureOptions) {
const request = await this.getAgent(bootOptions);
await this.doAuth(request, fixtureOptions);
return request;
},
getValidAdminToken(endpoint, key) { getValidAdminToken(endpoint, key) {
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
key = key || testUtils.DataGenerator.Content.api_keys[0]; key = key || testUtils.DataGenerator.Content.api_keys[0];