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 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');
});
});
});

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