Ghost/ghost/mailgun-client/test/mailgun-client.test.js
Daniel Lockyer bf254b9c6a Extracted Mailgun client to separate package
refs https://github.com/TryGhost/Toolbox/issues/363

- this commit pulls all code involving the Mailgun client SDK into one
  new package called `mailgun-client`
- this means we should be able to replace `mailgun-js` (deprecated) with
  `mailgun.js` (the new, official one) without editing code all over the
  place
- this also lays some groundwork for better testing of smaller
  components
2022-08-10 17:12:37 +02:00

80 lines
2.5 KiB
JavaScript

const assert = require('assert');
const sinon = require('sinon');
// module under test
const MailgunClient = require('../');
describe('MailgunClient', function () {
let config, settings;
beforeEach(function () {
// options objects that can be stubbed or spied
config = {get() {}};
settings = {get() {}};
});
afterEach(function () {
sinon.restore();
});
it('can connect via config', function () {
const configStub = sinon.stub(config, 'get');
configStub.withArgs('bulkEmail').returns({
mailgun: {
apiKey: 'apiKey',
domain: 'domain.com',
baseUrl: 'https://api.mailgun.net/v3'
}
});
const mailgunClient = new MailgunClient({config, settings});
assert.equal(mailgunClient.isConfigured(), true);
});
it('can connect via settings', function () {
const settingsStub = sinon.stub(settings, 'get');
settingsStub.withArgs('mailgun_api_key').returns('settingsApiKey');
settingsStub.withArgs('mailgun_domain').returns('settingsdomain.com');
settingsStub.withArgs('mailgun_base_url').returns('https://example.com/v3');
const mailgunClient = new MailgunClient({config, settings});
assert.equal(mailgunClient.isConfigured(), true);
});
it('cannot configure Mailgun if config/settings missing', function () {
const mailgunClient = new MailgunClient({config, settings});
assert.equal(mailgunClient.isConfigured(), false);
});
describe('normalizeEvent()', function () {
it('works', function () {
const event = {
event: 'testEvent',
severity: 'testSeverity',
recipient: 'testRecipient',
timestamp: 1614275662,
message: {
headers: {
'message-id': 'testProviderId'
}
},
'user-variables': {
'email-id': 'testEmailId'
}
};
const mailgunClient = new MailgunClient({config, settings});
const result = mailgunClient.normalizeEvent(event);
assert.deepStrictEqual(result, {
type: 'testEvent',
severity: 'testSeverity',
recipientEmail: 'testRecipient',
emailId: 'testEmailId',
providerId: 'testProviderId',
timestamp: new Date('2021-02-25T17:54:22.000Z')
});
});
});
});