2021-02-25 23:04:17 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
|
2022-08-11 11:26:41 +03:00
|
|
|
const {EventProcessingResult} = require('@tryghost/email-analytics-service');
|
|
|
|
|
2021-02-25 23:04:17 +03:00
|
|
|
// module under test
|
|
|
|
const EmailAnalyticsProviderMailgun = require('../');
|
|
|
|
|
2022-08-11 11:26:41 +03:00
|
|
|
const SAMPLE_EVENTS = [
|
|
|
|
new EventProcessingResult({
|
|
|
|
delivered: 4,
|
|
|
|
opened: 2,
|
|
|
|
temporaryFailed: 0,
|
|
|
|
permanentFailed: 0,
|
|
|
|
unsubscribed: 0,
|
|
|
|
complained: 0,
|
|
|
|
unhandled: 0,
|
|
|
|
unprocessable: 0,
|
|
|
|
processingFailures: 0,
|
|
|
|
emailIds: [
|
|
|
|
'62f3aebaaa887b504a40519f',
|
|
|
|
'62f3c200e8e74e677ab5e1fa',
|
|
|
|
'62f3c606de193a6d00433dfc'
|
|
|
|
],
|
|
|
|
memberIds: ['62ed25f69ae8f1a8c22d1a2f']
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
2021-02-25 23:04:17 +03:00
|
|
|
describe('EmailAnalyticsProviderMailgun', function () {
|
2021-12-02 15:26:23 +03:00
|
|
|
let config, settings;
|
2021-02-25 23:04:17 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// options objects that can be stubbed or spied
|
|
|
|
config = {get() {}};
|
|
|
|
settings = {get() {}};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
describe('fetchLatest()', function () {
|
|
|
|
const LATEST_TIMESTAMP = new Date('Thu Feb 25 2021 12:00:00 GMT+0000');
|
|
|
|
const END_EXAMPLE = new Date('Thu Feb 25 2021 14:00:00 GMT+0000');
|
2022-08-11 11:26:41 +03:00
|
|
|
const MAILGUN_OPTIONS = {
|
|
|
|
event: 'delivered OR opened OR failed OR unsubscribed OR complained',
|
|
|
|
limit: 300,
|
2023-02-20 18:44:13 +03:00
|
|
|
tags: 'bulk-email',
|
|
|
|
begin: 1614254400,
|
|
|
|
end: undefined,
|
|
|
|
ascending: 'yes'
|
2022-08-11 11:26:41 +03:00
|
|
|
};
|
2021-02-25 23:04:17 +03:00
|
|
|
|
2022-08-11 11:26:41 +03:00
|
|
|
it('passes the correct parameters to mailgun-client', async function () {
|
2022-05-02 21:08:30 +03:00
|
|
|
const configStub = sinon.stub(config, 'get');
|
|
|
|
configStub.withArgs('bulkEmail').returns({
|
|
|
|
mailgun: {
|
|
|
|
apiKey: 'apiKey',
|
|
|
|
domain: 'domain.com',
|
2022-08-11 11:26:41 +03:00
|
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
2022-05-02 21:08:30 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
|
|
|
|
|
|
|
const batchHandler = sinon.spy();
|
2022-08-11 11:26:41 +03:00
|
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
2022-05-02 21:08:30 +03:00
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP});
|
|
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, MAILGUN_OPTIONS, batchHandler, {maxEvents: undefined});
|
2022-05-02 21:08:30 +03:00
|
|
|
});
|
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
it('can use end timestamp', async function () {
|
2021-02-25 23:04:17 +03:00
|
|
|
const configStub = sinon.stub(config, 'get');
|
|
|
|
configStub.withArgs('bulkEmail').returns({
|
|
|
|
mailgun: {
|
|
|
|
apiKey: 'apiKey',
|
|
|
|
domain: 'domain.com',
|
|
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-02 15:26:23 +03:00
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
2021-02-25 23:04:17 +03:00
|
|
|
|
|
|
|
const batchHandler = sinon.spy();
|
2022-08-11 11:26:41 +03:00
|
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
2021-02-25 23:04:17 +03:00
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP, end: END_EXAMPLE});
|
2021-02-25 23:04:17 +03:00
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
const END_EXAMPLE_UNIX = END_EXAMPLE.getTime() / 1000;
|
|
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {...MAILGUN_OPTIONS, end: END_EXAMPLE_UNIX}, batchHandler, {maxEvents: undefined});
|
2021-02-25 23:04:17 +03:00
|
|
|
});
|
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
it('can use end without begin', async function () {
|
|
|
|
const configStub = sinon.stub(config, 'get');
|
|
|
|
configStub.withArgs('bulkEmail').returns({
|
|
|
|
mailgun: {
|
|
|
|
apiKey: 'apiKey',
|
|
|
|
domain: 'domain.com',
|
|
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
|
|
}
|
|
|
|
});
|
2022-08-11 11:26:41 +03:00
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
|
|
|
|
|
|
|
const batchHandler = sinon.spy();
|
|
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
|
|
|
|
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {end: END_EXAMPLE});
|
|
|
|
|
|
|
|
const END_EXAMPLE_UNIX = END_EXAMPLE.getTime() / 1000;
|
|
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {...MAILGUN_OPTIONS, begin: undefined, end: END_EXAMPLE_UNIX}, batchHandler, {maxEvents: undefined});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can use max events', async function () {
|
2021-02-25 23:04:17 +03:00
|
|
|
const configStub = sinon.stub(config, 'get');
|
|
|
|
configStub.withArgs('bulkEmail').returns({
|
|
|
|
mailgun: {
|
|
|
|
apiKey: 'apiKey',
|
|
|
|
domain: 'domain.com',
|
|
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-02 15:26:23 +03:00
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
2021-02-25 23:04:17 +03:00
|
|
|
|
|
|
|
const batchHandler = sinon.spy();
|
2022-08-11 11:26:41 +03:00
|
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
2021-02-25 23:04:17 +03:00
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP, end: END_EXAMPLE, maxEvents: 1000});
|
|
|
|
|
|
|
|
const END_EXAMPLE_UNIX = END_EXAMPLE.getTime() / 1000;
|
|
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {...MAILGUN_OPTIONS, end: END_EXAMPLE_UNIX}, batchHandler, {maxEvents: 1000});
|
2022-05-02 21:08:30 +03:00
|
|
|
});
|
|
|
|
|
2021-02-25 23:04:17 +03:00
|
|
|
it('uses custom tags when supplied', async function () {
|
|
|
|
const configStub = sinon.stub(config, 'get');
|
|
|
|
configStub.withArgs('bulkEmail').returns({
|
|
|
|
mailgun: {
|
|
|
|
apiKey: 'apiKey',
|
|
|
|
domain: 'domain.com',
|
|
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
configStub.withArgs('bulkEmail:mailgun:tag').returns('custom-tag');
|
|
|
|
|
2021-12-02 15:26:23 +03:00
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
2021-02-25 23:04:17 +03:00
|
|
|
|
|
|
|
const batchHandler = sinon.spy();
|
2022-08-11 11:26:41 +03:00
|
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
2021-02-25 23:04:17 +03:00
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP});
|
2021-02-25 23:04:17 +03:00
|
|
|
|
2022-08-11 11:26:41 +03:00
|
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {
|
|
|
|
...MAILGUN_OPTIONS,
|
|
|
|
tags: 'bulk-email AND custom-tag'
|
2023-02-20 18:44:13 +03:00
|
|
|
}, batchHandler, {maxEvents: undefined});
|
2021-02-25 23:04:17 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|