Removed GhostMailer parameter from UpdateCheckService

refs https://github.com/TryGhost/Team/issues/728

- This is continuation of the previous commit. TLDR: Passing only the necessary parameter data makes it easier to reason about what dependencies the UpdateCheckService has to deal with
- Instead of passing in a whole GhostMailer instance passing only an email sending function, which again - makes things way more manageable to reason about
- The end of refactor, next will be a move of the UpdateCheckService into a separate module in tryghost/core
This commit is contained in:
Naz 2021-06-02 15:18:32 +04:00
parent a4e5436df7
commit 7121efb010
2 changed files with 14 additions and 24 deletions

View File

@ -40,14 +40,15 @@ class UpdateCheckService {
* @param {boolean} [options.config.forceUpdate]
* @param {string} options.config.ghostVersion - Ghost instance version
* @param {Function} options.request - a HTTP request proxy function
* @param {Function} options.sendEmail - function handling sending an email
*/
constructor({api, config, i18n, logging, request, ghostMailer}) {
constructor({api, config, i18n, logging, request, sendEmail}) {
this.api = api;
this.config = config;
this.i18n = i18n;
this.logging = logging;
this.request = request;
this.ghostMailer = ghostMailer;
this.sendEmail = sendEmail;
}
nextCheckTimestamp() {
@ -311,7 +312,7 @@ class UpdateCheckService {
if (toAdd.type === 'alert') {
for (const email of adminEmails) {
try {
this.ghostMailer.send({
this.sendEmail({
to: email,
subject: `Action required: Critical alert from Ghost instance ${siteUrl}`,
html: toAdd.message,

View File

@ -65,10 +65,7 @@ describe('Update Check', function () {
},
i18n: i18nStub,
logging: loggingStub,
request: requestStub,
ghostMailer: {
send: sinon.stub().resolves()
}
request: requestStub
});
await updateCheckService.check();
@ -129,10 +126,7 @@ describe('Update Check', function () {
},
i18n: i18nStub,
logging: loggingStub,
request: requestStub,
ghostMailer: {
send: sinon.stub().resolves()
}
request: requestStub
});
await updateCheckService.check();
@ -254,10 +248,7 @@ describe('Update Check', function () {
body: {
notifications: [notification]
}
}),
ghostMailer: {
send: sinon.stub().resolves()
}
})
});
await updateCheckService.check();
@ -287,9 +278,7 @@ describe('Update Check', function () {
};
const notificationsAPIAddStub = sinon.stub().resolves();
const mailServiceStub = {
send: sinon.stub().resolves()
};
const sendEmailStub = sinon.stub().resolves();
const updateCheckService = new UpdateCheckService({
api: {
@ -322,16 +311,16 @@ describe('Update Check', function () {
request: sinon.stub().resolves({
body: [notification]
}),
ghostMailer: mailServiceStub
sendEmail: sendEmailStub
});
await updateCheckService.check();
mailServiceStub.send.called.should.be.true();
mailServiceStub.send.args[0][0].to.should.equal('jbloggs@example.com');
mailServiceStub.send.args[0][0].subject.should.equal('Action required: Critical alert from Ghost instance http://127.0.0.1:2369');
mailServiceStub.send.args[0][0].html.should.equal('<p>Critical message. Upgrade your site!</p>');
mailServiceStub.send.args[0][0].forceTextContent.should.equal(true);
sendEmailStub.called.should.be.true();
sendEmailStub.args[0][0].to.should.equal('jbloggs@example.com');
sendEmailStub.args[0][0].subject.should.equal('Action required: Critical alert from Ghost instance http://127.0.0.1:2369');
sendEmailStub.args[0][0].html.should.equal('<p>Critical message. Upgrade your site!</p>');
sendEmailStub.args[0][0].forceTextContent.should.equal(true);
notificationsAPIAddStub.calledOnce.should.equal(true);
notificationsAPIAddStub.args[0][0].notifications.length.should.equal(1);