From 5210e84b265441da3e1e75b2e84c4f7eb045df21 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 7 Feb 2022 16:02:04 +0000 Subject: [PATCH] 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 --- .../api/admin/authentication.test.js | 45 +++++++------------ test/regression/api/admin/site.test.js | 4 +- test/utils/e2e-framework-mock-utils.js | 28 ++++++++++-- test/utils/e2e-framework.js | 23 ++++++---- 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/test/regression/api/admin/authentication.test.js b/test/regression/api/admin/authentication.test.js index 87926ab77f..cb2a3da557 100644 --- a/test/regression/api/admin/authentication.test.js +++ b/test/regression/api/admin/authentication.test.js @@ -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'); }); }); }); diff --git a/test/regression/api/admin/site.test.js b/test/regression/api/admin/site.test.js index 45abcd9116..8992b40893 100644 --- a/test/regression/api/admin/site.test.js +++ b/test/regression/api/admin/site.test.js @@ -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 () { diff --git a/test/utils/e2e-framework-mock-utils.js b/test/utils/e2e-framework-mock-utils.js index 1fc2a7506e..ac3002851f 100644 --- a/test/utils/e2e-framework-mock-utils.js +++ b/test/utils/e2e-framework-mock-utils.js @@ -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 +}; diff --git a/test/utils/e2e-framework.js b/test/utils/e2e-framework.js index e61c2af814..309b33ff17 100644 --- a/test/utils/e2e-framework.js +++ b/test/utils/e2e-framework.js @@ -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 + } +};