From 8a2303413de11be067ffb7e7e2fc524b6d7429aa Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Wed, 11 Jan 2023 13:52:52 +0100 Subject: [PATCH] Restricted email-service package to 100% test coverage fixes https://github.com/TryGhost/Team/issues/2339 The email service is now fully covered by tests, and this commit also forces the test coverage to remain 100% after future changes. --- .../email-service/batch-sending.test.js | 2 +- .../lib/mailgun-email-provider.js | 6 +- ghost/email-service/package.json | 2 +- .../test/mailgun-email-provider.test.js | 104 +++++++++++++++++- 4 files changed, 108 insertions(+), 6 deletions(-) diff --git a/ghost/core/test/integration/services/email-service/batch-sending.test.js b/ghost/core/test/integration/services/email-service/batch-sending.test.js index c4d87e941c..cbdd0ad4aa 100644 --- a/ghost/core/test/integration/services/email-service/batch-sending.test.js +++ b/ghost/core/test/integration/services/email-service/batch-sending.test.js @@ -397,7 +397,7 @@ describe('Batch sending tests', function () { assert.equal(batch.get('provider_id'), null); assert.equal(batch.get('status'), 'failed'); assert.equal(batch.get('error_status_code'), 500); - assert.equal(batch.get('error_message'), 'Internal server error:Something went wrong'); + assert.equal(batch.get('error_message'), 'Internal server error: Something went wrong'); const errorData = JSON.parse(batch.get('error_data')); assert.equal(errorData.error.status, 500); assert.deepEqual(errorData.messageData.to.length, 1); diff --git a/ghost/email-service/lib/mailgun-email-provider.js b/ghost/email-service/lib/mailgun-email-provider.js index 7087bfc573..7712d0f32e 100644 --- a/ghost/email-service/lib/mailgun-email-provider.js +++ b/ghost/email-service/lib/mailgun-email-provider.js @@ -78,7 +78,7 @@ class MailgunEmailProvider { * @returns {string} */ #createMailgunErrorMessage(error) { - const message = (error?.message || '') + ':' + (error?.details || ''); + const message = (error?.message || 'Mailgun Error') + (error?.details ? (': ' + error.details) : ''); return message.slice(0, 2000); } @@ -148,7 +148,7 @@ class MailgunEmailProvider { let ghostError; if (e.error && e.messageData) { const {error, messageData} = e; - + // REF: possible mailgun errors https://documentation.mailgun.com/en/latest/api-intro.html#status-codes ghostError = new errors.EmailError({ statusCode: error.status, @@ -161,7 +161,7 @@ class MailgunEmailProvider { } else { ghostError = new errors.EmailError({ statusCode: undefined, - message: e.message, + message: this.#createMailgunErrorMessage(e), errorDetails: undefined, context: e.context || 'Mailgun Error', code: 'BULK_EMAIL_SEND_FAILED' diff --git a/ghost/email-service/package.json b/ghost/email-service/package.json index b9e19b9557..4e83c55398 100644 --- a/ghost/email-service/package.json +++ b/ghost/email-service/package.json @@ -7,7 +7,7 @@ "main": "index.js", "scripts": { "dev": "echo \"Implement me!\"", - "test:unit": "NODE_ENV=testing c8 --all --reporter text --reporter cobertura mocha './test/**/*.test.js'", + "test:unit": "NODE_ENV=testing c8 --all --check-coverage --100 --reporter text --reporter cobertura mocha './test/**/*.test.js'", "test": "yarn test:unit", "lint:code": "eslint *.js lib/ --ext .js --cache", "lint": "yarn lint:code && yarn lint:test", diff --git a/ghost/email-service/test/mailgun-email-provider.test.js b/ghost/email-service/test/mailgun-email-provider.test.js index 841a582927..7f21c5d662 100644 --- a/ghost/email-service/test/mailgun-email-provider.test.js +++ b/ghost/email-service/test/mailgun-email-provider.test.js @@ -1,6 +1,7 @@ const MailgunEmailProvider = require('../lib/mailgun-email-provider'); const sinon = require('sinon'); const should = require('should'); +const assert = require('assert'); describe('Mailgun Email Provider', function () { describe('send', function () { @@ -122,10 +123,111 @@ describe('Mailgun Email Provider', function () { }, {}); should(response).be.undefined(); } catch (e) { - should(e.message).eql('Bad Request:Invalid domain'); + should(e.message).eql('Bad Request: Invalid domain'); should(e.statusCode).eql(400); should(e.errorDetails).eql('{"error":{"details":"Invalid domain","status":400},"messageData":{}}'); } }); + + it('handles unknown error correctly', async function () { + const mailgunErr = new Error('Unknown Error'); + sendStub = sinon.stub().throws(mailgunErr); + + mailgunClient = { + send: sendStub + }; + + const mailgunEmailProvider = new MailgunEmailProvider({ + mailgunClient, + errorHandler: () => {} + }); + try { + const response = await mailgunEmailProvider.send({ + subject: 'Hi', + html: 'Hi {{name}}', + plaintext: 'Hi', + from: 'ghost@example.com', + replyTo: 'ghost@example.com', + emailId: '123', + recipients: [ + { + email: 'member@example.com', + replacements: [ + { + id: 'name', + token: '{{name}}', + value: 'John' + } + ] + } + ], + replacementDefinitions: [ + { + id: 'name', + token: '{{name}}', + getValue: () => 'John' + } + ] + }, {}); + should(response).be.undefined(); + } catch (e) { + should(e.message).eql('Unknown Error'); + should(e.errorDetails).eql(undefined); + } + }); + + it('handles empty error correctly', async function () { + const mailgunErr = new Error(''); + sendStub = sinon.stub().throws(mailgunErr); + + mailgunClient = { + send: sendStub + }; + + const mailgunEmailProvider = new MailgunEmailProvider({ + mailgunClient, + errorHandler: () => {} + }); + try { + const response = await mailgunEmailProvider.send({ + subject: 'Hi', + html: 'Hi {{name}}', + plaintext: 'Hi', + from: 'ghost@example.com', + replyTo: 'ghost@example.com', + emailId: '123', + recipients: [ + { + email: 'member@example.com', + replacements: [ + { + id: 'name', + token: '{{name}}', + value: 'John' + } + ] + } + ], + replacementDefinitions: [ + { + id: 'name', + token: '{{name}}', + getValue: () => 'John' + } + ] + }, {}); + should(response).be.undefined(); + } catch (e) { + should(e.message).eql('Mailgun Error'); + should(e.errorDetails).eql(undefined); + } + }); + }); + + describe('getMaximumRecipients', function () { + it('returns 1000', function () { + const provider = new MailgunEmailProvider({}); + assert.strictEqual(provider.getMaximumRecipients(), 1000); + }); }); });