Improved e2e-framework API

- encapsulated concerns within individual objects
- this will allow us to refactor these into classes or move them around later
- also makes it clearer how methods like restore relate to other methods
- e.g mocks.restore() restores mocks, whilst fixtureManager.restore() restores the database
This commit is contained in:
Hannah Wolfe 2022-02-07 16:02:04 +00:00
parent 82d2228bca
commit 5210e84b26
No known key found for this signature in database
GPG Key ID: AB586C3B5AE5C037
4 changed files with 58 additions and 42 deletions

View File

@ -3,7 +3,7 @@ const {any} = require('expect');
const security = require('@tryghost/security');
const testUtils = require('../../../utils');
const framework = require('../../../utils/e2e-framework');
const {agentProvider, mockManager, fixtureManager} = require('../../../utils/e2e-framework');
const models = require('../../../../core/server/models');
const settingsCache = require('../../../../core/shared/settings-cache');
@ -17,15 +17,15 @@ describe('Authentication API canary', function () {
describe('Blog setup', function () {
before(async function () {
agent = await framework.getAgent('/ghost/api/canary/admin/');
agent = await agentProvider.getAgent('/ghost/api/canary/admin/');
});
beforeEach(function () {
emailStub = framework.stubMail();
emailStub = mockManager.mockMail();
});
afterEach(function () {
framework.restoreMocks();
mockManager.restore();
});
it('is setup? no', async function () {
@ -106,7 +106,7 @@ describe('Authentication API canary', function () {
});
it('update setup', async function () {
await framework.initFixtures();
await fixtureManager.init();
await agent.loginAsOwner();
const res = await agent
@ -138,16 +138,12 @@ describe('Authentication API canary', function () {
describe('Invitation', function () {
before(async function () {
agent = await framework.getAgent('/ghost/api/canary/admin/');
agent = await agentProvider.getAgent('/ghost/api/canary/admin/');
// NOTE: this order of fixture initialization boggles me. Ideally should not depend on agent/login sequence
await framework.initFixtures('invites');
await fixtureManager.init('invites');
await agent.loginAsOwner();
});
after(async function () {
await framework.resetDb();
});
it('check invite with invalid email', function () {
return agent
.get('authentication/invitation?email=invalidemail')
@ -225,22 +221,18 @@ describe('Authentication API canary', function () {
const user = testUtils.DataGenerator.forModel.users[0];
before(async function () {
agent = await framework.getAgent('/ghost/api/canary/admin/');
agent = await agentProvider.getAgent('/ghost/api/canary/admin/');
// NOTE: this order of fixture initialization boggles me. Ideally should not depend on agent/login sequence
await framework.initFixtures('invites');
await fixtureManager.init('invites');
await agent.loginAsOwner();
});
after(async function () {
await framework.resetDb();
});
beforeEach(function () {
emailStub = framework.stubMail();
emailStub = mockManager.mockMail();
});
afterEach(function () {
framework.restoreMocks();
mockManager.restore();
});
it('reset password', async function () {
@ -380,24 +372,19 @@ describe('Authentication API canary', function () {
});
describe('Reset all passwords', function () {
let sendEmail;
before(async function () {
agent = await framework.getAgent('/ghost/api/canary/admin/');
agent = await agentProvider.getAgent('/ghost/api/canary/admin/');
// NOTE: this order of fixture initialization boggles me. Ideally should not depend on agent/login sequence
await framework.initFixtures('invites');
await fixtureManager.init('invites');
await agent.loginAsOwner();
});
after(async function () {
await framework.resetDb();
});
beforeEach(function () {
emailStub = framework.stubMail();
emailStub = mockManager.mockMail();
});
afterEach(function () {
framework.restoreMocks();
mockManager.restore();
});
it('reset all passwords returns 200', async function () {
@ -425,6 +412,8 @@ describe('Authentication API canary', function () {
expect(emailStub.callCount).to.equal(2);
expect(emailStub.firstCall.args[0].subject).to.equal('Reset Password');
expect(emailStub.secondCall.args[0].subject).to.equal('Reset Password');
expect(emailStub.firstCall.args[0].to).to.equal('jbloggs@example.com');
expect(emailStub.secondCall.args[0].to).to.equal('ghost-author@example.com');
});
});
});

View File

@ -1,13 +1,13 @@
const {expect} = require('chai');
const {any, stringMatching} = require('expect');
const framework = require('../../../utils/e2e-framework');
const {agentProvider} = require('../../../utils/e2e-framework');
describe('Config API', function () {
let agent;
before(async function () {
agent = await framework.getAgent('/ghost/api/canary/admin/');
agent = await agentProvider.getAgent('/ghost/api/canary/admin/');
});
it('can retrieve config and all expected properties', async function () {

View File

@ -1,12 +1,32 @@
const errors = require('@tryghost/errors');
const sinon = require('sinon');
let mocks = {};
const mailService = require('../../core/server/services/mail/index');
const stubMail = () => {
return sinon
const mockMail = () => {
mocks.mail = sinon
.stub(mailService.GhostMailer.prototype, 'send')
.resolves('Mail is disabled');
return mocks.mail;
};
module.exports.stubMail = stubMail;
module.exports.restoreMocks = () => sinon.restore();
const assertMailSentTo = (email) => {
if (!mocks.mail) {
throw new errors.IncorrectUsageError({
message: 'Cannot assert on mail when mail has not been mocked'
});
}
};
const restore = () => {
sinon.restore();
mocks = {};
};
module.exports = {
mockMail,
restore
};

View File

@ -130,12 +130,19 @@ const getAgent = async (apiURL) => {
}
};
// request agent
module.exports.getAgent = getAgent;
module.exports = {
// request agent
agentProvider: {
getAgent
},
// state manipulation
module.exports.initFixtures = initFixtures;
module.exports.getFixture = getFixture;
module.exports.resetDb = resetDb;
module.exports.stubMail = mockUtils.stubMail;
module.exports.restoreMocks = mockUtils.restoreMocks;
// Mocks and Stubs
mockManager: mockUtils,
// DB State Manipulation
fixtureManager: {
get: getFixture,
init: initFixtures,
reset: resetDb
}
};