From a78e03464355d400a92e772ddcbf8e11adcc646f Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 1 Jun 2021 16:34:58 +0400 Subject: [PATCH] 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 --- core/server/update-check-service.js | 6 +- .../update-check/update_check_spec.js | 150 ++++++++++++++++-- 2 files changed, 145 insertions(+), 11 deletions(-) diff --git a/core/server/update-check-service.js b/core/server/update-check-service.js index 1da0f8980c..fdb739309e 100644 --- a/core/server/update-check-service.js +++ b/core/server/update-check-service.js @@ -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); + } } /** diff --git a/test/regression/update-check/update_check_spec.js b/test/regression/update-check/update_check_spec.js index 81465e1bde..9786d335c4 100644 --- a/test/regression/update-check/update_check_spec.js +++ b/test/regression/update-check/update_check_spec.js @@ -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: '

Hey there! This is for 999.9.0 version

', + 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: '

Critical message. Upgrade your site!

', + 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('

Critical message. Upgrade your site!

'); + mailServiceStub.send.args[0][0].forceTextContent.should.equal(true); + + notificationsAPIAddStub.calledOnce.should.equal(true); + notificationsAPIAddStub.args[0][0].notifications.length.should.equal(1); + }); + }); });