2022-02-07 19:02:04 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-12-09 14:10:06 +03:00
|
|
|
const sinon = require('sinon');
|
2022-02-07 22:43:33 +03:00
|
|
|
const assert = require('assert');
|
2022-02-15 16:08:58 +03:00
|
|
|
const nock = require('nock');
|
2021-12-09 14:10:06 +03:00
|
|
|
|
2022-02-08 23:21:03 +03:00
|
|
|
// Helper services
|
|
|
|
const configUtils = require('./configUtils');
|
|
|
|
|
2022-02-07 19:02:04 +03:00
|
|
|
let mocks = {};
|
2022-02-07 20:28:53 +03:00
|
|
|
let emailCount = 0;
|
2022-02-07 19:02:04 +03:00
|
|
|
|
2022-02-08 23:21:03 +03:00
|
|
|
// Mockable services
|
2021-12-09 14:10:06 +03:00
|
|
|
const mailService = require('../../core/server/services/mail/index');
|
2022-02-08 23:21:03 +03:00
|
|
|
const labs = require('../../core/shared/labs');
|
2021-12-09 14:10:06 +03:00
|
|
|
|
2022-02-07 19:02:04 +03:00
|
|
|
const mockMail = () => {
|
|
|
|
mocks.mail = sinon
|
2021-12-09 14:10:06 +03:00
|
|
|
.stub(mailService.GhostMailer.prototype, 'send')
|
|
|
|
.resolves('Mail is disabled');
|
2022-02-07 19:02:04 +03:00
|
|
|
|
|
|
|
return mocks.mail;
|
|
|
|
};
|
|
|
|
|
2022-02-15 16:08:58 +03:00
|
|
|
const mockStripe = () => {
|
|
|
|
nock.disableNetConnect();
|
|
|
|
};
|
|
|
|
|
2022-02-08 23:21:03 +03:00
|
|
|
const mockLabsEnabled = (flag, alpha = true) => {
|
|
|
|
// We assume we should enable alpha experiments unless explicitly told not to!
|
|
|
|
if (!alpha) {
|
|
|
|
configUtils.set('enableDeveloperExperiments', true);
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:12:31 +03:00
|
|
|
if (!mocks.labs) {
|
|
|
|
mocks.labs = sinon.stub(labs, 'isSet');
|
2022-02-10 15:03:47 +03:00
|
|
|
}
|
2022-02-18 13:12:31 +03:00
|
|
|
|
|
|
|
mocks.labs.withArgs(flag).returns(true);
|
2022-02-10 15:03:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockLabsDisabled = (flag, alpha = true) => {
|
|
|
|
// We assume we should enable alpha experiments unless explicitly told not to!
|
|
|
|
if (!alpha) {
|
|
|
|
configUtils.set('enableDeveloperExperiments', true);
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:12:31 +03:00
|
|
|
if (!mocks.labs) {
|
|
|
|
mocks.labs = sinon.stub(labs, 'isSet');
|
2022-02-10 15:03:47 +03:00
|
|
|
}
|
2022-02-18 13:12:31 +03:00
|
|
|
|
|
|
|
mocks.labs.withArgs(flag).returns(false);
|
2022-02-08 23:21:03 +03:00
|
|
|
};
|
|
|
|
|
2022-02-07 20:28:53 +03:00
|
|
|
const sentEmailCount = (count) => {
|
2022-02-07 19:02:04 +03:00
|
|
|
if (!mocks.mail) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 'Cannot assert on mail when mail has not been mocked'
|
|
|
|
});
|
|
|
|
}
|
2022-02-07 20:28:53 +03:00
|
|
|
|
|
|
|
sinon.assert.callCount(mocks.mail, count);
|
|
|
|
};
|
|
|
|
|
|
|
|
const sentEmail = (matchers) => {
|
|
|
|
if (!mocks.mail) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 'Cannot assert on mail when mail has not been mocked'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let spyCall = mocks.mail.getCall(emailCount);
|
|
|
|
|
|
|
|
// We increment here so that the messaging has an index of 1, whilst getting the call has an index of 0
|
|
|
|
emailCount += 1;
|
|
|
|
|
|
|
|
sinon.assert.called(mocks.mail);
|
|
|
|
|
|
|
|
Object.keys(matchers).forEach((key) => {
|
|
|
|
let value = matchers[key];
|
|
|
|
|
|
|
|
// We use assert, rather than sinon.assert.calledWith, as we end up with much better error messaging
|
|
|
|
assert.notEqual(spyCall.args[0][key], undefined, `Expected email to have property ${key}`);
|
|
|
|
assert.equal(spyCall.args[0][key], value, `Expected Email ${emailCount} to have ${key} of ${value}`);
|
|
|
|
});
|
2021-12-09 14:10:06 +03:00
|
|
|
};
|
|
|
|
|
2022-02-07 19:02:04 +03:00
|
|
|
const restore = () => {
|
2022-02-08 23:21:03 +03:00
|
|
|
configUtils.restore();
|
2022-02-07 19:02:04 +03:00
|
|
|
sinon.restore();
|
|
|
|
mocks = {};
|
2022-02-07 20:28:53 +03:00
|
|
|
emailCount = 0;
|
2022-02-15 16:08:58 +03:00
|
|
|
nock.cleanAll();
|
|
|
|
nock.enableNetConnect();
|
2022-02-07 19:02:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
mockMail,
|
2022-02-15 16:08:58 +03:00
|
|
|
mockStripe,
|
2022-02-08 23:21:03 +03:00
|
|
|
mockLabsEnabled,
|
2022-02-10 15:03:47 +03:00
|
|
|
mockLabsDisabled,
|
2022-02-07 20:28:53 +03:00
|
|
|
restore,
|
|
|
|
assert: {
|
|
|
|
sentEmailCount,
|
|
|
|
sentEmail
|
|
|
|
}
|
2022-02-07 19:02:04 +03:00
|
|
|
};
|