Ghost/ghost/api-version-compatibility-service/test/api-version-compatibility-service.test.js
Naz 8414e927f9 Added handling of multiple owners to notify
refs https://github.com/TryGhost/Toolbox/issues/292

- There can be multiple users in the Ghost instance that should be notifiied about version mismatch. Following the logic of the security notifications these are users with 'Owner' and 'Administrator' roles. To have the most up to date list of the emails to notify the emails fetching was made dinamic and is now passed in as a 'fetchEmailsToNotify' function.
- Also fixed the subject of the email to match the final copy
2022-04-21 15:57:43 +08:00

117 lines
4.9 KiB
JavaScript

const assert = require('assert');
const sinon = require('sinon');
const APIVersionCompatibilityService = require('../index');
describe('APIVersionCompatibilityService', function () {
afterEach(function () {
sinon.reset();
});
it('Sends an email to the instance owners when fresh accept-version header mismatch detected', async function () {
const sendEmail = sinon.spy();
const fetchHandled = sinon.spy();
const saveHandled = sinon.spy();
const compatibilityService = new APIVersionCompatibilityService({
sendEmail,
fetchEmailsToNotify: async () => ['test_env@example.com'],
fetchHandled,
saveHandled
});
await compatibilityService.handleMismatch({
acceptVersion: 'v4.5',
contentVersion: 'v5.1',
userAgent: 'Elaborate Fox'
});
assert.equal(sendEmail.called, true);
assert.equal(sendEmail.args[0][0].to, 'test_env@example.com');
assert.equal(sendEmail.args[0][0].subject, `Attention required: Your Elaborate Fox integration has failed`);
assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version: v4.5/);
assert.match(sendEmail.args[0][0].html, /Current Ghost version: v5.1/);
});
it('Does NOT send an email to the instance owner when previously handled accept-version header mismatch is detected', async function () {
const sendEmail = sinon.spy();
const fetchHandled = sinon.stub()
.onFirstCall().resolves(null)
.onSecondCall().resolves({});
const saveHandled = sinon.stub().resolves({});
const compatibilityService = new APIVersionCompatibilityService({
sendEmail,
fetchEmailsToNotify: async () => ['test_env@example.com'],
fetchHandled,
saveHandled
});
await compatibilityService.handleMismatch({
acceptVersion: 'v4.5',
contentVersion: 'v5.1',
userAgent: 'Elaborate Fox'
});
assert.equal(sendEmail.calledOnce, true);
assert.equal(sendEmail.args[0][0].to, 'test_env@example.com');
assert.equal(sendEmail.args[0][0].subject, `Attention required: Your Elaborate Fox integration has failed`);
assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version: v4.5/);
assert.match(sendEmail.args[0][0].html, /Current Ghost version: v5.1/);
await compatibilityService.handleMismatch({
acceptVersion: 'v4.5',
contentVersion: 'v5.1',
userAgent: 'Elaborate Fox'
});
assert.equal(sendEmail.calledTwice, false);
});
it('Does send multiple emails to the instance owners when previously unhandled accept-version header mismatch is detected', async function () {
const sendEmail = sinon.spy();
const fetchHandled = sinon.stub()
.onFirstCall().resolves(null)
.onSecondCall().resolves(null);
const saveHandled = sinon.stub().resolves({});
const compatibilityService = new APIVersionCompatibilityService({
sendEmail,
fetchEmailsToNotify: async () => ['test_env@example.com', 'test_env2@example.com'],
fetchHandled,
saveHandled
});
await compatibilityService.handleMismatch({
acceptVersion: 'v4.5',
contentVersion: 'v5.1',
userAgent: 'Elaborate Fox'
});
assert.equal(sendEmail.calledTwice, true);
assert.equal(sendEmail.args[0][0].to, 'test_env@example.com');
assert.equal(sendEmail.args[0][0].subject, `Attention required: Your Elaborate Fox integration has failed`);
assert.match(sendEmail.args[0][0].html, /Elaborate Fox integration expected Ghost version: v4.5/);
assert.match(sendEmail.args[0][0].html, /Current Ghost version: v5.1/);
assert.equal(sendEmail.calledTwice, true);
assert.equal(sendEmail.args[1][0].to, 'test_env2@example.com');
assert.equal(sendEmail.args[1][0].subject, `Attention required: Your Elaborate Fox integration has failed`);
assert.match(sendEmail.args[1][0].html, /Elaborate Fox integration expected Ghost version: v4.5/);
assert.match(sendEmail.args[1][0].html, /Current Ghost version: v5.1/);
await compatibilityService.handleMismatch({
acceptVersion: 'v4.8',
contentVersion: 'v5.1',
userAgent: 'Elaborate Fox'
});
assert.equal(sendEmail.callCount, 4);
assert.equal(sendEmail.args[2][0].to, 'test_env@example.com');
assert.equal(sendEmail.args[2][0].subject, `Attention required: Your Elaborate Fox integration has failed`);
assert.match(sendEmail.args[2][0].html, /Elaborate Fox integration expected Ghost version: v4.8/);
assert.match(sendEmail.args[2][0].html, /Current Ghost version: v5.1/);
});
});