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.
This commit is contained in:
Simon Backx 2023-01-11 13:52:52 +01:00
parent ee36a0fce9
commit 8a2303413d
4 changed files with 108 additions and 6 deletions

View File

@ -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);

View File

@ -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'

View File

@ -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",

View File

@ -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: '<html><body>Hi {{name}}</body></html>',
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: '<html><body>Hi {{name}}</body></html>',
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);
});
});
});