Ghost/ghost/email-service/test/mailgun-email-provider.test.js
Hannah Wolfe 6161f94910
Updated to use assert/strict everywhere (#17047)
refs: https://github.com/TryGhost/Toolbox/issues/595

We're rolling out new rules around the node assert library, the first of which is enforcing the use of assert/strict. This means we don't need to use the strict version of methods, as the standard version will work that way by default.

This caught some gotchas in our existing usage of assert where the lack of strict mode had unexpected results:
- Url matching needs to be done on `url.href` see aa58b354a4
- Null and undefined are not the same thing,  there were a few cases of this being confused
- Particularly questionable changes in [PostExporter tests](c1a468744b) tracked [here](https://github.com/TryGhost/Team/issues/3505).
- A typo see eaac9c293a

Moving forward, using assert strict should help us to catch unexpected behaviour, particularly around nulls and undefineds during implementation.
2023-06-21 09:56:59 +01:00

234 lines
8.1 KiB
JavaScript

const MailgunEmailProvider = require('../lib/MailgunEmailProvider');
const sinon = require('sinon');
const should = require('should');
const assert = require('assert/strict');
describe('Mailgun Email Provider', function () {
describe('send', function () {
let mailgunClient;
let sendStub;
beforeEach(function () {
sendStub = sinon.stub().resolves({
id: 'provider-123'
});
mailgunClient = {
send: sendStub
};
});
afterEach(function () {
sinon.restore();
});
it('calls mailgun client with correct data', async function () {
const mailgunEmailProvider = new MailgunEmailProvider({
mailgunClient,
errorHandler: () => {}
});
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'
}
]
}, {
clickTrackingEnabled: true,
openTrackingEnabled: true
});
should(response.id).eql('provider-123');
should(sendStub.calledOnce).be.true();
sendStub.calledWith(
{
subject: 'Hi',
html: '<html><body>Hi %recipient.name%</body></html>',
plaintext: 'Hi',
from: 'ghost@example.com',
replyTo: 'ghost@example.com',
id: '123',
track_opens: true,
track_clicks: true
},
{'member@example.com': {name: 'John'}},
[]
).should.be.true();
});
it('handles mailgun client error correctly', async function () {
const mailgunErr = new Error('Bad Request');
mailgunErr.details = 'Invalid domain';
mailgunErr.status = 400;
sendStub = sinon.stub().throws({
error: mailgunErr,
messageData: {}
});
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('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.equal(provider.getMaximumRecipients(), 1000);
});
});
});