Added update check notificaitons test coverage

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

- This is a continuation of the test coverage for the UpdateCheckService.
- Covers scpecial cases of notification processing within Update Check
- The refactor inside the update check service was a convenience to get rid or the Bluebird dependency completely. Also, some minor preventative code added to avoid errors from referencing undefined objects
This commit is contained in:
Naz 2021-06-01 16:34:58 +04:00
parent e9cbf3451a
commit a78e034643
2 changed files with 145 additions and 11 deletions

View File

@ -238,7 +238,7 @@ class UpdateCheckService {
// CASE: filter out messages based on your groups
return _.includes(notificationGroups.map(function (groupIdentifier) {
if (notification.version.match(new RegExp(groupIdentifier))) {
if (notification && notification.version && notification.version.match(new RegExp(groupIdentifier))) {
return true;
}
@ -247,7 +247,9 @@ class UpdateCheckService {
});
}
return Promise.each(notifications, this.createCustomNotification);
for (const notification of notifications) {
await this.createCustomNotification(notification);
}
}
/**

View File

@ -1,19 +1,14 @@
const _ = require('lodash');
const Promise = require('bluebird');
const should = require('should');
const rewire = require('rewire');
const sinon = require('sinon');
const moment = require('moment');
const uuid = require('uuid');
const testUtils = require('../../utils');
const configUtils = require('../../utils/configUtils');
const urlUtils = require('../../utils/urlUtils');
const packageInfo = require('../../../package.json');
const api = require('../../../core/server/api').v2;
const mailService = require('../../../core/server/services/mail/');
let ghostVersion = rewire('../../../core/server/lib/ghost-version');
const UpdateCheckService = rewire('../../../core/server/update-check-service');
const UpdateCheckService = require('../../../core/server/update-check-service');
describe('Update Check', function () {
const internal = {context: {internal: true}};
@ -77,7 +72,9 @@ describe('Update Check', function () {
urlUtils: urlUtilsStub,
request: requestStub,
ghostVersion,
ghostMailer: mailService
ghostMailer: {
send: sinon.stub().resolves()
}
});
await updateCheckService.check();
@ -135,7 +132,9 @@ describe('Update Check', function () {
urlUtils: urlUtilsStub,
request: requestStub,
ghostVersion,
ghostMailer: mailService
ghostMailer: {
send: sinon.stub().resolves()
}
});
await updateCheckService.check();
@ -186,7 +185,9 @@ describe('Update Check', function () {
urlUtils: urlUtilsStub,
request: requestStub,
ghostVersion,
ghostMailer: mailService
ghostMailer: {
send: sinon.stub().resolves()
}
});
await updateCheckService.check();
@ -210,4 +211,135 @@ describe('Update Check', function () {
data.npm_version.should.not.be.empty();
});
});
describe('Notifications', function () {
it('should create a release notification for target version', async function () {
const notification = {
id: 1,
custom: 0,
messages: [{
id: uuid.v4(),
version: '999.9.x',
content: '<p>Hey there! This is for 999.9.0 version</p>',
dismissible: true,
top: true
}]
};
const notificationsAPIAddStub = sinon.stub().resolves();
const updateCheckService = new UpdateCheckService({
api: {
settings: {
read: settingsStub,
edit: settingsStub
},
users: {
browse: sinon.stub().resolves({
users: [{
roles: [{
name: 'Owner'
}]
}]
})
},
posts: {
browse: sinon.stub().resolves()
},
notifications: {
add: notificationsAPIAddStub
}
},
config: configUtils.config,
i18n: i18nStub,
logging: loggingStub,
urlUtils: urlUtilsStub,
request: sinon.stub().resolves({
body: {
notifications: [notification]
}
}),
ghostVersion,
ghostMailer: {
send: sinon.stub().resolves()
}
});
await updateCheckService.check();
notificationsAPIAddStub.calledOnce.should.equal(true);
notificationsAPIAddStub.args[0][0].notifications.length.should.equal(1);
const targetNotification = notificationsAPIAddStub.args[0][0].notifications[0];
targetNotification.dismissible.should.eql(notification.messages[0].dismissible);
targetNotification.id.should.eql(notification.messages[0].id);
targetNotification.top.should.eql(notification.messages[0].top);
targetNotification.type.should.eql('info');
targetNotification.message.should.eql(notification.messages[0].content);
});
it('should send an email for critical notification', async function () {
const notification = {
id: 1,
messages: [{
id: uuid.v4(),
version: 'custom1',
content: '<p>Critical message. Upgrade your site!</p>',
dismissible: false,
top: true,
type: 'alert'
}]
};
const notificationsAPIAddStub = sinon.stub().resolves();
const mailServiceStub = {
send: sinon.stub().resolves()
};
const updateCheckService = new UpdateCheckService({
api: {
settings: {
read: settingsStub,
edit: settingsStub
},
users: {
browse: sinon.stub().resolves({
users: [{
email: 'jbloggs@example.com',
roles: [{
name: 'Owner'
}]
}]
})
},
posts: {
browse: sinon.stub().resolves()
},
notifications: {
add: notificationsAPIAddStub
}
},
config: configUtils.config,
i18n: i18nStub,
logging: loggingStub,
urlUtils: urlUtilsStub,
request: sinon.stub().resolves({
body: [notification]
}),
ghostVersion,
ghostMailer: mailServiceStub
});
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);
notificationsAPIAddStub.calledOnce.should.equal(true);
notificationsAPIAddStub.args[0][0].notifications.length.should.equal(1);
});
});
});