0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Fixed authentication test suites

refs https://github.com/TryGhost/Toolbox/issues/152

- This refactor fixes the rest of authentication test suite and outlines the state setup steps that are absolutely needed for tests that rely on any db state. It's still counterintuitive why the fixture initialization has to be called and in that specific order, so next will be refactoring in this area to simplify the initi code
This commit is contained in:
Naz 2021-12-06 20:53:23 +04:00 committed by naz
parent 25d75f69db
commit a18fa18ff3
2 changed files with 34 additions and 8 deletions

View file

@ -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 () {

View file

@ -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];