diff --git a/test/regression/api/canary/admin/authentication.test.js b/test/regression/api/canary/admin/authentication.test.js index b38d207baf..d439d763ad 100644 --- a/test/regression/api/canary/admin/authentication.test.js +++ b/test/regression/api/canary/admin/authentication.test.js @@ -3,19 +3,17 @@ const {expect} = require('chai'); const {any} = require('expect'); const chaiJestSnapshot = require('@ethanresnick/chai-jest-snapshot'); -const should = require('should'); -const sinon = require('sinon'); const testUtils = require('../../../../utils/index'); const framework = require('../../../../utils/e2e-framework'); const models = require('../../../../../core/server/models/index'); const security = require('@tryghost/security'); const settingsCache = require('../../../../../core/shared/settings-cache'); const config = require('../../../../../core/shared/config/index'); -const mailService = require('../../../../../core/server/services/mail/index'); - -let request; describe('Authentication API canary', function () { + let request; + let emailStub; + describe('Blog setup', function () { before(async function () { chaiJestSnapshot.resetSnapshotRegistry(); @@ -28,11 +26,11 @@ describe('Authentication API canary', function () { beforeEach(function () { chaiJestSnapshot.configureUsingMochaContext(this); - sinon.stub(mailService.GhostMailer.prototype, 'send').resolves('Mail is disabled'); + emailStub = framework.stubMail(); }); afterEach(function () { - sinon.restore(); + framework.restoreMocks(); }); it('is setup? no', async function () { @@ -74,8 +72,7 @@ describe('Authentication API canary', function () { etag: any(String) }); - mailService.GhostMailer.prototype.send.called.should.be.true(); - mailService.GhostMailer.prototype.send.args[0][0].to.should.equal('test@example.com'); + expect(emailStub.called).to.be.true; }); it('is setup? yes', async function () { @@ -244,11 +241,11 @@ describe('Authentication API canary', function () { }); beforeEach(function () { - sinon.stub(mailService.GhostMailer.prototype, 'send').resolves('Mail is disabled'); + emailStub = framework.stubMail(); }); afterEach(function () { - sinon.restore(); + framework.restoreMocks(); }); it('reset password', async function () { @@ -406,11 +403,11 @@ describe('Authentication API canary', function () { }); beforeEach(function () { - sendEmail = sinon.stub(mailService.GhostMailer.prototype, 'send').resolves('Mail is disabled'); + emailStub = framework.stubMail(); }); afterEach(function () { - sinon.restore(); + framework.restoreMocks(); }); it('reset all passwords returns 200', async function () { @@ -429,16 +426,16 @@ describe('Authentication API canary', function () { // All users locked const users = await models.User.fetchAll(); for (const user of users) { - user.get('status').should.be.eql('locked'); + expect(user.get('status')).to.equal('locked'); } // No session left const sessions = await models.Session.fetchAll(); - sessions.length.should.be.eql(0); + expect(sessions.length).to.equal(0); - sendEmail.callCount.should.be.eql(2); - sendEmail.firstCall.args[0].subject.should.be.eql('Reset Password'); - sendEmail.secondCall.args[0].subject.should.be.eql('Reset Password'); + 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'); }); }); }); diff --git a/test/utils/e2e-framework-mock-utils.js b/test/utils/e2e-framework-mock-utils.js new file mode 100644 index 0000000000..1fc2a7506e --- /dev/null +++ b/test/utils/e2e-framework-mock-utils.js @@ -0,0 +1,12 @@ +const sinon = require('sinon'); + +const mailService = require('../../core/server/services/mail/index'); + +const stubMail = () => { + return sinon + .stub(mailService.GhostMailer.prototype, 'send') + .resolves('Mail is disabled'); +}; + +module.exports.stubMail = stubMail; +module.exports.restoreMocks = () => sinon.restore(); diff --git a/test/utils/e2e-framework.js b/test/utils/e2e-framework.js index 63c99a774a..a4fca20688 100644 --- a/test/utils/e2e-framework.js +++ b/test/utils/e2e-framework.js @@ -18,9 +18,11 @@ const fs = require('fs-extra'); const path = require('path'); const os = require('os'); const uuid = require('uuid'); + const fixtures = require('./fixture-utils'); const redirectsUtils = require('./redirects'); const configUtils = require('./configUtils'); +const mockUtils = require('./e2e-framework-mock-utils'); const boot = require('../../core/boot'); const TestAgent = require('./test-agent'); @@ -106,6 +108,8 @@ const resetDb = async () => { await db.teardown(); }; + + /** * Creates a TestAgent which is a drop-in substitution for supertest hooked into Ghost. * @param {String} apiURL @@ -120,7 +124,9 @@ const getAgent = async (apiURL) => { // request agent module.exports.getAgent = getAgent; -// state building +// state manipulation module.exports.initFixtures = initFixtures; module.exports.getFixture = getFixture; module.exports.resetDb = resetDb; +module.exports.stubMail = mockUtils.stubMail; +module.exports.restoreMocks = mockUtils.restoreMocks;