forked from gfk-ba/meteor-mailgun-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailgun-api_tests.js
411 lines (325 loc) · 10.5 KB
/
mailgun-api_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
(function () {
'use strict';
//<editor-fold desc="Instantiation tests">
describe('Mailgun - Test API', function () {
var sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
it('Should create a object for .api', function () {
var testMailgun = new Mailgun({});
expect(testMailgun.api).to.be.a('object');
});
it('When given options - expect apiKey to be set', function () {
var testApiKey = 'Test',
testMailgun = new Mailgun({ apiKey: testApiKey, domain: 'mail.somewhere.com'});
expect(testMailgun.api.apiKey).to.equal(testApiKey);
});
it('When given options - expect Domain to be set', function () {
var testApiKey = 'Test',
testDomain = 'mail.somewhere.com',
testMailgun = new Mailgun({ apiKey: testApiKey, domain: testDomain});
expect(testMailgun.api.domain).to.equal(testDomain);
});
});
//</editor-fold>
//<editor-fold desc="Send tests">
describe('Mailgun - #Send', function () {
var sandbox, instance;
beforeEach(function () {
sandbox = sinon.sandbox.create();
instance = new Mailgun({ apiKey: 'Test', domain: 'mail.somewhere.com'});
});
afterEach(function () {
sandbox.restore();
});
it('Expect testmode to be passed on', function () {
var send = sandbox.stub(instance, '_send').returns({wait: function () { return {response: '', error: ''};}});
instance.send({from:'[email protected]'}, {
testmode: true
});
expect(send.args[0][0]['o:testmode']).to.equal(true);
});
it('Should create a valid emailobject', function () {
var send = sandbox.stub(instance, '_send').returns({wait: function () { return {response: '', error: ''};}});
var testPayload = {
"to": "[email protected]",
"from": "[email protected]",
"html": "<html><head></head><body>This is a test</body></html>",
"text": "This is a test",
"subject": "testSubject",
"tags": [
"some",
"test",
"tags"
]
};
instance.send(_.clone(testPayload));
var expected = _.clone(testPayload);
expected['o:tag'] = expected.tags;
delete expected.tags;
expect(send).to.have.been.calledWith(expected);
});
// it('Should save email to disk when options.saveEmailTo is defined', function () {
// sandbox.stub(Npm, 'require').returns(sinon.stub());
//
// var send = sandbox.stub(instance, '_send').returns({wait: function () { return {response: '', error: ''};}});
//
// var testPayload = {
// "to": "[email protected]",
// "from": "[email protected]",
// "html": "<html><head></head><body>This is a test</body></html>",
// "text": "This is a test",
// "subject": "testSubject",
// "tags": [
// "some",
// "test",
// "tags"
// ]
// };
//
// var randomNumber = Math.floor(100000 + Math.random() * 900000);
//
// var testOptions = {
// saveEmailTo: '/some' + randomNumber + '/test/dir/bla.html'
// };
//
// instance.send(_.clone(testPayload), testOptions);
//
// var expected = testOptions.saveEmailTo.split('/');
// expected.pop();
// expected = expected.join('/');
// console.log(writeFile.args);
//
// expect(mkdirpStub).to.have.been.calledWith(expected);
// expect(writeFile).to.have.been.calledWith(testOptions.saveEmailTo, testPayload.html);
// });
});
//</editor-fold>
//<editor-fold desc="getEvents tests">
describe('Mailgun - #getEvents', function () {
var sandbox, instance;
beforeEach(function () {
sandbox = sinon.sandbox.create();
instance = new Mailgun({ apiKey: 'Test', domain: 'mail.somewhere.com'});
});
afterEach(function () {
sandbox.restore();
});
it('Should have constants for the different events', function () {
expect(instance.CONST).to.be.a('object');
expect(instance.CONST.EVENTTYPES).to.be.a('object');
expect(_.size(instance.CONST.EVENTTYPES)).to.be.at.least(1);
});
it('Should return the events in the response', function () {
var testError = new Error(['Testing errors']),
testResponse = {
items: [
{
event: instance.CONST.EVENTTYPES.ACCEPTED,
timestamp: '14234991213.01'
}
]
};
instance.api.events().__proto__.get = function (filter, cb) {
cb(testError);
};
var result1 = instance.getEvents({}).wait();
expect(result1).to.eql({error: testError, items:[]});
instance.api.events().__proto__.get = function (filter, cb) {
cb(undefined, testResponse);
};
var result2 = instance.getEvents({}).wait();
expect(result2).to.eql({
error: undefined,
items: testResponse.items
});
expect((result2.items[0].date / 1000).toString()).to.equal(testResponse.items[0].timestamp);
});
it('When given a filter - When called with beginDate/endDate - Should convert the beginDate and endDate to a begin and end timestamp', function () {
var testResponse = {
items: [
{
event: instance.CONST.EVENTTYPES.ACCEPTED
}
]
},
beginDate = new Date() - 1000,
endDate = new Date(),
actualFilter;
instance.api.events().__proto__.get = function (filter, cb) {
actualFilter = filter;
cb(undefined, testResponse);
};
instance.getEvents({
beginDate: beginDate,
endDate: new Date()
}).wait();
expect(actualFilter.begin).to.equal((beginDate / 1000).toString());
expect(actualFilter.end).to.equal((endDate / 1000).toString());
expect(actualFilter.beginDate).to.be.an('undefined');
expect(actualFilter.endDate).to.be.an('undefined');
});
it('When given a filter - When the given filter contains a value for ascending - Should convert true/false to yes/no', function () {
var yes = 'yes', no = 'no', valueForAscending;
instance.api.events().__proto__.get = function (filter, cb) {
valueForAscending = filter.ascending;
cb();
};
instance.getEvents({
ascending: true
}).wait();
expect(valueForAscending).to.equal(yes);
instance.getEvents({
ascending: false
}).wait();
expect(valueForAscending).to.equal(no);
});
it('When given a filter - When the given filter contains a value for ascending - Should not convert yes or no', function () {
var yes = 'yes', no = 'no', valueForAscending;
instance.api.events().__proto__.get = function (filter, cb) {
valueForAscending = filter.ascending;
cb();
};
instance.getEvents({
ascending: yes
}).wait();
expect(valueForAscending).to.equal(yes);
instance.getEvents({
ascending: no
}).wait();
expect(valueForAscending).to.equal(no);
});
it('When given a filter - When the given filter contains a value for pretty - Should convert true/false to yes/no', function () {
var yes = 'yes', no = 'no', valueForPretty;
instance.api.events().__proto__.get = function (filter, cb) {
valueForPretty = filter.pretty;
cb();
};
instance.getEvents({
pretty: true
}).wait();
expect(valueForPretty).to.equal(yes);
instance.getEvents({
pretty: false
}).wait();
expect(valueForPretty).to.equal(no);
});
it('When given a filter - When the given filter contains a value for pretty - Should not convert yes or no', function () {
var yes = 'yes', no = 'no', valueForPretty;
instance.api.events().__proto__.get = function (filter, cb) {
valueForPretty = filter.pretty;
cb();
};
instance.getEvents({
pretty: yes
}).wait();
expect(valueForPretty).to.equal(yes);
instance.getEvents({
pretty: no
}).wait();
expect(valueForPretty).to.equal(no);
});
});
//</editor-fold>
//<editor-fold desc="handleEvents tests">
describe('Mailgun - #handleEvents', function () {
var sandbox, instance, testResponse, testResponseItem, getEvents, testFilter, testHandlers, eventTypes, testItems;
beforeEach(function () {
sandbox = sinon.sandbox.create();
instance = new Mailgun({ apiKey: 'Test', domain: 'mail.somewhere.com'});
testResponse = {
"items": [
],
"errors": undefined
};
testResponseItem = {
"tags": [],
"envelope": {
"transport": "smtp",
"sender": "[email protected]",
"sending-ip": "184.173.153.199"
},
"delivery-status": {
"message": "",
"code": 0,
"description": null
},
"campaigns": [],
"user-variables": {},
"flags": {
"is-authenticated": true,
"is-test-mode": false
},
"timestamp": 1377208314.173742,
"message": {
"headers": {
"to": "[email protected]",
"message-id": "[email protected]",
"from": "[email protected]",
"subject": "Sample Message"
},
"attachments": [],
"recipients": [
],
"size": 31143
},
"recipient": "[email protected]",
"event": "delivered"
};
getEvents = sandbox.stub(instance, 'getEvents').returns({
wait: function () {
return testResponse;
}
});
testFilter = {};
testHandlers = {};
_.each(eventTypes, function (event) {
testHandlers[event] = sinon.spy();
});
testHandlers.before = sinon.spy();
eventTypes = instance.CONST.EVENTTYPES;
testItems = testResponse.items;
});
afterEach(function () {
sandbox.restore();
});
it('Should call getEvents', function () {
testFilter = {begin:123};
instance.handleEvents(testFilter);
expect(getEvents).to.have.been.calledWith(testFilter);
});
it('When called with eventHandlers - Should call the appropiate handler for the appropiate event', function () {
var testData = {};
_.each(eventTypes, function (event) {
var testItem = _.clone(testResponseItem);
testItem.event = event;
testItems.push(testItem);
testData[event] = testItem;
});
instance.handleEvents(testFilter, testHandlers);
_.each(eventTypes, function (event) {
expect(testHandlers.before).to.have.been.calledBefore(testHandlers[event]);
expect(testHandlers.before).to.have.been.calledWith(testData[event]);
expect(testHandlers[event]).to.have.been.calledWith(testData[event]);
});
});
it('When called with eventHandlers - When getEvents returns a error - It should return that error', function () {
var testError = new Error('Foobar');
testResponse = {
error: testError
};
var res = instance.handleEvents(testFilter, testHandlers);
expect(res.error).to.eql(testError);
});
it('When called without eventHandlers - it should return a error', function () {
var res = instance.handleEvents({});
expect(_.isString(res.error.message)).to.equal(true);
});
});
//</editor-fold>
} ());