2022-02-07 11:02:04 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-12-09 06:10:06 -05:00
|
|
|
const sinon = require('sinon');
|
2022-02-07 14:43:33 -05:00
|
|
|
const assert = require('assert');
|
2021-12-09 06:10:06 -05:00
|
|
|
|
2022-02-08 15:21:03 -05:00
|
|
|
// Helper services
|
|
|
|
const configUtils = require('./configUtils');
|
|
|
|
|
2022-02-07 11:02:04 -05:00
|
|
|
let mocks = {};
|
2022-02-07 12:28:53 -05:00
|
|
|
let emailCount = 0;
|
2022-02-07 11:02:04 -05:00
|
|
|
|
2022-02-08 15:21:03 -05:00
|
|
|
// Mockable services
|
2021-12-09 06:10:06 -05:00
|
|
|
const mailService = require('../../core/server/services/mail/index');
|
2022-02-08 15:21:03 -05:00
|
|
|
const labs = require('../../core/shared/labs');
|
2021-12-09 06:10:06 -05:00
|
|
|
|
2022-02-07 11:02:04 -05:00
|
|
|
const mockMail = () => {
|
|
|
|
mocks.mail = sinon
|
2021-12-09 06:10:06 -05:00
|
|
|
.stub(mailService.GhostMailer.prototype, 'send')
|
|
|
|
.resolves('Mail is disabled');
|
2022-02-07 11:02:04 -05:00
|
|
|
|
|
|
|
return mocks.mail;
|
|
|
|
};
|
|
|
|
|
2022-02-08 15:21:03 -05:00
|
|
|
const mockLabsEnabled = (flag, alpha = true) => {
|
|
|
|
mocks.labs = mocks.labs || {};
|
|
|
|
|
|
|
|
// We assume we should enable alpha experiments unless explicitly told not to!
|
|
|
|
if (!alpha) {
|
|
|
|
configUtils.set('enableDeveloperExperiments', true);
|
|
|
|
}
|
|
|
|
|
2022-02-10 07:03:47 -05:00
|
|
|
if (mocks.labs[flag]) {
|
|
|
|
mocks.labs[flag].returns(true);
|
|
|
|
} else {
|
|
|
|
mocks.labs[flag] = sinon.stub(labs, 'isSet').withArgs(flag).returns(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockLabsDisabled = (flag, alpha = true) => {
|
|
|
|
mocks.labs = mocks.labs || {};
|
|
|
|
|
|
|
|
// We assume we should enable alpha experiments unless explicitly told not to!
|
|
|
|
if (!alpha) {
|
|
|
|
configUtils.set('enableDeveloperExperiments', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mocks.labs[flag]) {
|
|
|
|
mocks.labs[flag].returns(false);
|
|
|
|
} else {
|
|
|
|
mocks.labs[flag] = sinon.stub(labs, 'isSet').withArgs(flag).returns(false);
|
|
|
|
}
|
2022-02-08 15:21:03 -05:00
|
|
|
};
|
|
|
|
|
2022-02-07 12:28:53 -05:00
|
|
|
const sentEmailCount = (count) => {
|
2022-02-07 11:02:04 -05:00
|
|
|
if (!mocks.mail) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 'Cannot assert on mail when mail has not been mocked'
|
|
|
|
});
|
|
|
|
}
|
2022-02-07 12:28:53 -05: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 06:10:06 -05:00
|
|
|
};
|
|
|
|
|
2022-02-07 11:02:04 -05:00
|
|
|
const restore = () => {
|
2022-02-08 15:21:03 -05:00
|
|
|
configUtils.restore();
|
2022-02-07 11:02:04 -05:00
|
|
|
sinon.restore();
|
|
|
|
mocks = {};
|
2022-02-07 12:28:53 -05:00
|
|
|
emailCount = 0;
|
2022-02-07 11:02:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
mockMail,
|
2022-02-08 15:21:03 -05:00
|
|
|
mockLabsEnabled,
|
2022-02-10 07:03:47 -05:00
|
|
|
mockLabsDisabled,
|
2022-02-07 12:28:53 -05:00
|
|
|
restore,
|
|
|
|
assert: {
|
|
|
|
sentEmailCount,
|
|
|
|
sentEmail
|
|
|
|
}
|
2022-02-07 11:02:04 -05:00
|
|
|
};
|