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

Moved email stubbing logic into e2e-framework utils

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

- Moving common mocking/stubbing/spying logic into an outside utils module allows test suites to keep agnostic towards which framework powers mocking etc. Should also substitute email service stubbing used in multiple places
- Bonus, got rid of should dependency, which is deprecated
This commit is contained in:
Naz 2021-12-09 15:10:06 +04:00
parent dc6e7a690a
commit 309e14b8c9
3 changed files with 34 additions and 19 deletions

View file

@ -3,19 +3,17 @@ const {expect} = require('chai');
const {any} = require('expect'); const {any} = require('expect');
const chaiJestSnapshot = require('@ethanresnick/chai-jest-snapshot'); const chaiJestSnapshot = require('@ethanresnick/chai-jest-snapshot');
const should = require('should');
const sinon = require('sinon');
const testUtils = require('../../../../utils/index'); const testUtils = require('../../../../utils/index');
const framework = require('../../../../utils/e2e-framework'); const framework = require('../../../../utils/e2e-framework');
const models = require('../../../../../core/server/models/index'); const models = require('../../../../../core/server/models/index');
const security = require('@tryghost/security'); const security = require('@tryghost/security');
const settingsCache = require('../../../../../core/shared/settings-cache'); const settingsCache = require('../../../../../core/shared/settings-cache');
const config = require('../../../../../core/shared/config/index'); const config = require('../../../../../core/shared/config/index');
const mailService = require('../../../../../core/server/services/mail/index');
let request;
describe('Authentication API canary', function () { describe('Authentication API canary', function () {
let request;
let emailStub;
describe('Blog setup', function () { describe('Blog setup', function () {
before(async function () { before(async function () {
chaiJestSnapshot.resetSnapshotRegistry(); chaiJestSnapshot.resetSnapshotRegistry();
@ -28,11 +26,11 @@ describe('Authentication API canary', function () {
beforeEach(function () { beforeEach(function () {
chaiJestSnapshot.configureUsingMochaContext(this); chaiJestSnapshot.configureUsingMochaContext(this);
sinon.stub(mailService.GhostMailer.prototype, 'send').resolves('Mail is disabled'); emailStub = framework.stubMail();
}); });
afterEach(function () { afterEach(function () {
sinon.restore(); framework.restoreMocks();
}); });
it('is setup? no', async function () { it('is setup? no', async function () {
@ -74,8 +72,7 @@ describe('Authentication API canary', function () {
etag: any(String) etag: any(String)
}); });
mailService.GhostMailer.prototype.send.called.should.be.true(); expect(emailStub.called).to.be.true;
mailService.GhostMailer.prototype.send.args[0][0].to.should.equal('test@example.com');
}); });
it('is setup? yes', async function () { it('is setup? yes', async function () {
@ -244,11 +241,11 @@ describe('Authentication API canary', function () {
}); });
beforeEach(function () { beforeEach(function () {
sinon.stub(mailService.GhostMailer.prototype, 'send').resolves('Mail is disabled'); emailStub = framework.stubMail();
}); });
afterEach(function () { afterEach(function () {
sinon.restore(); framework.restoreMocks();
}); });
it('reset password', async function () { it('reset password', async function () {
@ -406,11 +403,11 @@ describe('Authentication API canary', function () {
}); });
beforeEach(function () { beforeEach(function () {
sendEmail = sinon.stub(mailService.GhostMailer.prototype, 'send').resolves('Mail is disabled'); emailStub = framework.stubMail();
}); });
afterEach(function () { afterEach(function () {
sinon.restore(); framework.restoreMocks();
}); });
it('reset all passwords returns 200', async function () { it('reset all passwords returns 200', async function () {
@ -429,16 +426,16 @@ describe('Authentication API canary', function () {
// All users locked // All users locked
const users = await models.User.fetchAll(); const users = await models.User.fetchAll();
for (const user of users) { for (const user of users) {
user.get('status').should.be.eql('locked'); expect(user.get('status')).to.equal('locked');
} }
// No session left // No session left
const sessions = await models.Session.fetchAll(); const sessions = await models.Session.fetchAll();
sessions.length.should.be.eql(0); expect(sessions.length).to.equal(0);
sendEmail.callCount.should.be.eql(2); expect(emailStub.callCount).to.equal(2);
sendEmail.firstCall.args[0].subject.should.be.eql('Reset Password'); expect(emailStub.firstCall.args[0].subject).to.equal('Reset Password');
sendEmail.secondCall.args[0].subject.should.be.eql('Reset Password'); expect(emailStub.secondCall.args[0].subject).to.equal('Reset Password');
}); });
}); });
}); });

View file

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

View file

@ -18,9 +18,11 @@ const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const os = require('os'); const os = require('os');
const uuid = require('uuid'); const uuid = require('uuid');
const fixtures = require('./fixture-utils'); const fixtures = require('./fixture-utils');
const redirectsUtils = require('./redirects'); const redirectsUtils = require('./redirects');
const configUtils = require('./configUtils'); const configUtils = require('./configUtils');
const mockUtils = require('./e2e-framework-mock-utils');
const boot = require('../../core/boot'); const boot = require('../../core/boot');
const TestAgent = require('./test-agent'); const TestAgent = require('./test-agent');
@ -106,6 +108,8 @@ const resetDb = async () => {
await db.teardown(); await db.teardown();
}; };
/** /**
* Creates a TestAgent which is a drop-in substitution for supertest hooked into Ghost. * Creates a TestAgent which is a drop-in substitution for supertest hooked into Ghost.
* @param {String} apiURL * @param {String} apiURL
@ -120,7 +124,9 @@ const getAgent = async (apiURL) => {
// request agent // request agent
module.exports.getAgent = getAgent; module.exports.getAgent = getAgent;
// state building // state manipulation
module.exports.initFixtures = initFixtures; module.exports.initFixtures = initFixtures;
module.exports.getFixture = getFixture; module.exports.getFixture = getFixture;
module.exports.resetDb = resetDb; module.exports.resetDb = resetDb;
module.exports.stubMail = mockUtils.stubMail;
module.exports.restoreMocks = mockUtils.restoreMocks;