mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
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
This commit is contained in:
parent
258b0acc51
commit
8414e927f9
2 changed files with 33 additions and 23 deletions
|
@ -3,29 +3,33 @@ class APIVersionCompatibilityService {
|
|||
*
|
||||
* @param {Object} options
|
||||
* @param {Function} options.sendEmail - email sending function
|
||||
* @param {String} options.emailTo - email address to receive notifications
|
||||
* @param {() => Promise<any>} options.fetchEmailsToNotify - email address to receive notifications
|
||||
* @param {(acceptVersion: String) => Promise<any>} options.fetchHandled - retrives already handled compatibility notifications
|
||||
* @param {(acceptVersion: String) => Promise<any>} options.saveHandled - persists already handled compatibility notifications
|
||||
*/
|
||||
constructor({sendEmail, emailTo, fetchHandled, saveHandled}) {
|
||||
constructor({sendEmail, fetchEmailsToNotify, fetchHandled, saveHandled}) {
|
||||
this.sendEmail = sendEmail;
|
||||
this.emailTo = emailTo;
|
||||
this.fetchEmailsToNotify = fetchEmailsToNotify;
|
||||
this.fetchHandled = fetchHandled;
|
||||
this.saveHandled = saveHandled;
|
||||
}
|
||||
|
||||
async handleMismatch({acceptVersion, contentVersion, userAgent}) {
|
||||
async handleMismatch({acceptVersion, contentVersion, userAgent = ''}) {
|
||||
if (!await this.fetchHandled(acceptVersion)) {
|
||||
const emailTemplate = `
|
||||
${userAgent} integration expected Ghost version: ${acceptVersion}
|
||||
Current Ghost version: ${contentVersion}
|
||||
`;
|
||||
|
||||
await this.sendEmail({
|
||||
subject: `Ghost has noticed that your ${userAgent} integration is no longer working as expected`,
|
||||
to: this.emailTo,
|
||||
html: emailTemplate
|
||||
});
|
||||
const emails = await this.fetchEmailsToNotify();
|
||||
for (const email of emails) {
|
||||
await this.sendEmail({
|
||||
subject: `Ghost has noticed that your ${userAgent} integration is no longer working as expected`,
|
||||
to: email,
|
||||
html: emailTemplate
|
||||
});
|
||||
}
|
||||
|
||||
await this.saveHandled(acceptVersion);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,14 @@ describe('APIVersionCompatibilityService', function () {
|
|||
sinon.reset();
|
||||
});
|
||||
|
||||
it('Sends an email to the instance owner when fresh accept-version header mismatch detected', async function () {
|
||||
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,
|
||||
emailTo: 'test_env@example.com',
|
||||
fetchEmailsToNotify: async () => ['test_env@example.com'],
|
||||
fetchHandled,
|
||||
saveHandled
|
||||
});
|
||||
|
@ -27,7 +27,7 @@ describe('APIVersionCompatibilityService', function () {
|
|||
|
||||
assert.equal(sendEmail.called, true);
|
||||
assert.equal(sendEmail.args[0][0].to, 'test_env@example.com');
|
||||
assert.equal(sendEmail.args[0][0].subject, `Ghost has noticed that your Elaborate Fox integration is no longer working as expected`);
|
||||
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/);
|
||||
});
|
||||
|
@ -42,7 +42,7 @@ describe('APIVersionCompatibilityService', function () {
|
|||
|
||||
const compatibilityService = new APIVersionCompatibilityService({
|
||||
sendEmail,
|
||||
emailTo: 'test_env@example.com',
|
||||
fetchEmailsToNotify: async () => ['test_env@example.com'],
|
||||
fetchHandled,
|
||||
saveHandled
|
||||
});
|
||||
|
@ -55,7 +55,7 @@ describe('APIVersionCompatibilityService', function () {
|
|||
|
||||
assert.equal(sendEmail.calledOnce, true);
|
||||
assert.equal(sendEmail.args[0][0].to, 'test_env@example.com');
|
||||
assert.equal(sendEmail.args[0][0].subject, `Ghost has noticed that your Elaborate Fox integration is no longer working as expected`);
|
||||
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/);
|
||||
|
||||
|
@ -68,7 +68,7 @@ describe('APIVersionCompatibilityService', function () {
|
|||
assert.equal(sendEmail.calledTwice, false);
|
||||
});
|
||||
|
||||
it('Does send multiple emails to the instance owner when previously unhandled accept-version header mismatch is detected', async function () {
|
||||
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)
|
||||
|
@ -78,7 +78,7 @@ describe('APIVersionCompatibilityService', function () {
|
|||
|
||||
const compatibilityService = new APIVersionCompatibilityService({
|
||||
sendEmail,
|
||||
emailTo: 'test_env@example.com',
|
||||
fetchEmailsToNotify: async () => ['test_env@example.com', 'test_env2@example.com'],
|
||||
fetchHandled,
|
||||
saveHandled
|
||||
});
|
||||
|
@ -89,22 +89,28 @@ describe('APIVersionCompatibilityService', function () {
|
|||
userAgent: 'Elaborate Fox'
|
||||
});
|
||||
|
||||
assert.equal(sendEmail.calledOnce, true);
|
||||
assert.equal(sendEmail.calledTwice, true);
|
||||
assert.equal(sendEmail.args[0][0].to, 'test_env@example.com');
|
||||
assert.equal(sendEmail.args[0][0].subject, `Ghost has noticed that your Elaborate Fox integration is no longer working as expected`);
|
||||
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.calledTwice, true);
|
||||
assert.equal(sendEmail.args[0][0].to, 'test_env@example.com');
|
||||
assert.equal(sendEmail.args[0][0].subject, `Ghost has noticed that your Elaborate Fox integration is no longer working as expected`);
|
||||
assert.match(sendEmail.args[1][0].html, /Elaborate Fox integration expected Ghost version: v4.8/);
|
||||
assert.match(sendEmail.args[1][0].html, /Current Ghost version: v5.1/);
|
||||
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/);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue