diff --git a/test/regression/api/canary/admin/authentication.test.js b/test/regression/api/canary/admin/authentication.test.js index 64eb00dd08..5c9d8306a9 100644 --- a/test/regression/api/canary/admin/authentication.test.js +++ b/test/regression/api/canary/admin/authentication.test.js @@ -1,6 +1,5 @@ const should = require('should'); const sinon = require('sinon'); -const supertest = require('supertest'); const localUtils = require('./utils'); const testUtils = require('../../../../utils/index'); const models = require('../../../../../core/server/models/index'); @@ -132,7 +131,10 @@ describe('Authentication API canary', function () { describe('Invitation', function () { before(async function () { - request = await localUtils.getAuthenticatedAgent(undefined, 'invites'); + request = await localUtils.getAgent(); + // NOTE: this order of fixture initialization boggles me. Ideally should not depend on agent/login sequence + await localUtils.initFixtures('invites'); + await localUtils.login(request); }); it('check invite with invalid email', function () { @@ -221,7 +223,10 @@ describe('Authentication API canary', function () { const user = testUtils.DataGenerator.forModel.users[0]; before(async function () { - request = await localUtils.getAuthenticatedAgent({forceStart: true}); + request = await localUtils.getAgent({forceStart: true}); + // NOTE: this order of fixture initialization boggles me. Ideally should not depend on agent/login sequence + await localUtils.initFixtures(); + await localUtils.login(request); }); beforeEach(function () { @@ -381,7 +386,10 @@ describe('Authentication API canary', function () { describe('Reset all passwords', function () { let sendEmail; before(async function () { - request = await localUtils.getAuthenticatedAgent({forceStart: true}); + request = await localUtils.getAgent({forceStart: true}); + // NOTE: this order of fixture initialization boggles me. Ideally should not depend on agent/login sequence + await localUtils.initFixtures(); + await localUtils.login(request); }); beforeEach(function () { diff --git a/test/regression/api/canary/admin/utils.js b/test/regression/api/canary/admin/utils.js index 76573d2924..8845d1c491 100644 --- a/test/regression/api/canary/admin/utils.js +++ b/test/regression/api/canary/admin/utils.js @@ -2,6 +2,8 @@ const url = require('url'); const _ = require('lodash'); const supertest = require('supertest'); const testUtils = require('../../../../utils'); +const fixtures = require('../../../../utils/fixture-utils'); +const {sequence} = require('@tryghost/promise'); const API_URL = '/ghost/api/canary/admin/'; @@ -214,19 +216,35 @@ module.exports = { return await testUtils.startGhost(Object.assign(defaults, overrides)); }, - async getAgent({forceStart}) { + async getAgent({forceStart} = {}) { const app = await this.startGhost({forceStart}); return supertest.agent(app); }, - async getAuthenticatedAgent({forceStart} = {}, fixtureOptions) { - const request = await this.getAgent({forceStart}); - await this.doAuth(request, fixtureOptions); + async login(request) { + return testUtils.API.login(request, `${API_URL}session/`); + }, + async getAuthenticatedAgent({forceStart} = {}) { + const request = await this.getAgent({forceStart}); + await this.login(request); return request; }, + async initFixtures(...options) { + // No DB setup, but override the owner + options = _.merge({'owner:post': true}, _.transform(options, function (result, val) { + if (val) { + result[val] = true; + } + })); + + const fixtureOps = fixtures.getFixtureOps(options); + + return sequence(fixtureOps); + }, + getValidAdminToken(endpoint, key) { const jwt = require('jsonwebtoken'); key = key || testUtils.DataGenerator.Content.api_keys[0];