mailgun.js 4.1.2 → 4.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,392 @@
1
+ import formData from 'form-data';
2
+
3
+ import nock from 'nock';
4
+ import Request from '../lib/request';
5
+ import RequestOptions from '../lib/interfaces/RequestOptions';
6
+ import { InputFormData } from '../lib/interfaces/IFormData';
7
+ import { DomainCredentialsList, DomainCredentialsResult } from '../lib/interfaces/DomainCredentials';
8
+ import DomainTemplatesClient from '../lib/domainsTemplates';
9
+ import { DomainTemplateUpdateVersionData, DomainTemplateVersionData } from '../lib/interfaces/DomainTemplates';
10
+
11
+ // TODO: fix types
12
+ describe('DomainsTemplatesClient', function () {
13
+ let client: DomainTemplatesClient;
14
+ let api: nock.Scope;
15
+
16
+ beforeEach(function () {
17
+ const reqObject = new Request({ url: 'https://api.mailgun.net' } as RequestOptions, formData as InputFormData);
18
+ client = new DomainTemplatesClient(reqObject);
19
+ api = nock('https://api.mailgun.net');
20
+ });
21
+
22
+ afterEach(function () {
23
+ api.done();
24
+ });
25
+
26
+ describe('list', function () {
27
+ it('fetches all templates for domain', async () => {
28
+ api.get('/v3/testDomain/templates').reply(200, {
29
+ items: [
30
+ {
31
+ name: 'test_template',
32
+ description: 'test_template description',
33
+ createdAt: 'Mon, 20 Dec 2021 14:47:51 UTC',
34
+ createdBy: '',
35
+ id: 'someId'
36
+ }
37
+ ],
38
+ paging: {
39
+ first: 'first page url',
40
+ last: 'last page url',
41
+ next: 'next page url',
42
+ previous: 'previous page url',
43
+ }
44
+ });
45
+
46
+ const templatesList = await client.list('testDomain');
47
+ templatesList.should.be.an('object').to.have.property('items');
48
+ templatesList.items.should.be.an('array').to.have.property('length').to.be.equal(1);
49
+ templatesList.items[0].should.eql({
50
+ name: 'test_template',
51
+ description: 'test_template description',
52
+ createdAt: new Date('Mon, 20 Dec 2021 14:47:51 UTC'),
53
+ createdBy: '',
54
+ id: 'someId'
55
+ });
56
+ });
57
+ });
58
+
59
+ describe('get', function () {
60
+ it('fetches all templates for domain', async () => {
61
+ api.get('/v3/testDomain/templates/testTemplateName').reply(200, {
62
+ template: {
63
+ name: 'test_template',
64
+ description: 'test_template description',
65
+ createdAt: 'Mon, 20 Dec 2021 14:47:51 UTC',
66
+ createdBy: '',
67
+ id: 'someId'
68
+ }
69
+ });
70
+
71
+ const template = await client.get('testDomain', 'testTemplateName');
72
+ template.should.be.an('object');
73
+ template.should.eql({
74
+ name: 'test_template',
75
+ description: 'test_template description',
76
+ createdAt: new Date('Mon, 20 Dec 2021 14:47:51 UTC'),
77
+ createdBy: '',
78
+ id: 'someId'
79
+ });
80
+ });
81
+ });
82
+
83
+ describe('create', function () {
84
+ it('creates domain template', async () => {
85
+ const templateData = {
86
+ name: 'test_template1',
87
+ description: 'test_template1 description',
88
+ template: '%recipient.title%',
89
+ tag: 'v1',
90
+ comment: 'dummy comment'
91
+ };
92
+
93
+ api.post('/v3/testDomain/templates').reply(200, {
94
+ message: 'template has been stored',
95
+ template: {
96
+ name: 'test_template1',
97
+ description: 'test_template description 1',
98
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
99
+ createdBy: '',
100
+ id: 'someId',
101
+ version: {
102
+ tag: 'v1',
103
+ template: '%recipient.title%',
104
+ engine: 'handlebars',
105
+ mjml: '',
106
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
107
+ comment: 'dummy comment',
108
+ active: true,
109
+ id: 'someId2'
110
+ }
111
+ }
112
+ });
113
+
114
+ const template = await client.create('testDomain', templateData);
115
+ template.should.be.an('object');
116
+ template.should.eql({
117
+ name: 'test_template1',
118
+ description: 'test_template description 1',
119
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
120
+ createdBy: '',
121
+ id: 'someId',
122
+ version: {
123
+ tag: 'v1',
124
+ template: '%recipient.title%',
125
+ engine: 'handlebars',
126
+ mjml: '',
127
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
128
+ comment: 'dummy comment',
129
+ active: true,
130
+ id: 'someId2'
131
+ }
132
+ });
133
+ });
134
+ });
135
+
136
+ describe('update', function () {
137
+ it('updates domain template', async () => {
138
+ api.put('/v3/testDomain/templates/testTemplateName').reply(200, {
139
+ message: 'template has been updated',
140
+ template: { name: 'test_template1' }
141
+ });
142
+
143
+ const updatedTemplate = await client.update('testDomain', 'testTemplateName', {
144
+ description: 'updated test_template description'
145
+ });
146
+ updatedTemplate.should.be.an('object');
147
+ updatedTemplate.should.eql({
148
+ status: 200,
149
+ message: 'template has been updated',
150
+ templateName: 'test_template1'
151
+ });
152
+ });
153
+ });
154
+
155
+ describe('destroy', function () {
156
+ it('deletes domain template', async () => {
157
+ api.delete('/v3/testDomain/templates/testTemplateName').reply(200, {
158
+ message: 'template has been deleted',
159
+ template: { name: 'test_template1' }
160
+ });
161
+
162
+ const deletedTemplate = await client.destroy('testDomain', 'testTemplateName');
163
+ deletedTemplate.should.eql({
164
+ status: 200,
165
+ message: 'template has been deleted',
166
+ templateName: 'test_template1'
167
+ });
168
+ });
169
+ });
170
+
171
+ describe('destroyAll', function () {
172
+ it('deletes all domain templates', async () => {
173
+ api.delete('/v3/testDomain/templates').reply(200, { message: 'templates have been deleted' });
174
+
175
+ const deletedTemplate = await client.destroyAll('testDomain');
176
+ deletedTemplate.should.eql({ status: 200, message: 'templates have been deleted' });
177
+ });
178
+ });
179
+
180
+ describe('createVersion', function () {
181
+ it('creates new version of domain template', async () => {
182
+ const templateVersionData = {
183
+ template: '%recipient.title%',
184
+ tag: 'v3',
185
+ comment: 'comment',
186
+ active: 'yes'
187
+ } as DomainTemplateVersionData;
188
+
189
+ api.post('/v3/testDomain/templates/testTemplateName/versions').reply(200, {
190
+ message: 'new version of the template has been stored',
191
+ template: {
192
+ name: 'test_template1',
193
+ description: 'test_template description 1',
194
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
195
+ createdBy: '',
196
+ id: 'someId',
197
+ version: {
198
+ tag: 'v1',
199
+ template: '%recipient.title%',
200
+ engine: 'handlebars',
201
+ mjml: '',
202
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
203
+ comment: 'comment',
204
+ active: true,
205
+ id: 'someId2'
206
+ }
207
+ }
208
+ });
209
+
210
+ const templateVersion = await client.createVersion('testDomain', 'testTemplateName', templateVersionData);
211
+ templateVersion.should.be.an('object');
212
+ templateVersion.should.eql({
213
+ status: 200,
214
+ message: 'new version of the template has been stored',
215
+ template: {
216
+ name: 'test_template1',
217
+ description: 'test_template description 1',
218
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
219
+ createdBy: '',
220
+ id: 'someId',
221
+ version: {
222
+ tag: 'v1',
223
+ template: '%recipient.title%',
224
+ engine: 'handlebars',
225
+ mjml: '',
226
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
227
+ comment: 'comment',
228
+ active: true,
229
+ id: 'someId2'
230
+ }
231
+ }
232
+ });
233
+ });
234
+ });
235
+
236
+ describe('getVersion', function () {
237
+ it('fetches version of domain template', async () => {
238
+ api.get('/v3/testDomain/templates/testTemplateName/versions/v1').reply(200, {
239
+ template: {
240
+ name: 'test_template',
241
+ description: 'test_template description',
242
+ createdAt: 'Wed, 22 Dec 2021 09:13:27 UTC',
243
+ createdBy: '',
244
+ id: 'someId',
245
+ version: {
246
+ tag: 'v1',
247
+ template: '%recipient.title%',
248
+ engine: 'handlebars',
249
+ mjml: '',
250
+ createdAt: 'Wed, 22 Dec 2021 09:13:27 UTC',
251
+ comment: 'dummy comment',
252
+ active: false,
253
+ id: 'someId2'
254
+ }
255
+ }
256
+ });
257
+
258
+ const template = await client.getVersion('testDomain', 'testTemplateName', 'v1');
259
+ template.should.be.an('object');
260
+ template.should.eql({
261
+ name: 'test_template',
262
+ description: 'test_template description',
263
+ createdAt: new Date('2021-12-22T09:13:27.000Z'),
264
+ createdBy: '',
265
+ id: 'someId',
266
+ version: {
267
+ tag: 'v1',
268
+ template: '%recipient.title%',
269
+ engine: 'handlebars',
270
+ mjml: '',
271
+ createdAt: new Date('2021-12-22T09:13:27.000Z'),
272
+ comment: 'dummy comment',
273
+ active: false,
274
+ id: 'someId2',
275
+ }
276
+ });
277
+ });
278
+ });
279
+
280
+ describe('updateVersion', function () {
281
+ it('updates version of domain template', async () => {
282
+ api.put('/v3/testDomain/templates/testTemplateName/versions/v2').reply(200, {
283
+ message: 'version has been updated',
284
+ template: { name: 'test_template', version: { tag: 'v2' } }
285
+ });
286
+
287
+ const updatedTemplate = await client.updateVersion('testDomain', 'testTemplateName', 'v2', {
288
+ template: '%recipient.title%',
289
+ comment: 'updated comment 2',
290
+ active: 'yes'
291
+ } as DomainTemplateUpdateVersionData);
292
+ updatedTemplate.should.be.an('object');
293
+ updatedTemplate.should.eql({
294
+ status: 200,
295
+ message: 'version has been updated',
296
+ templateName: 'test_template',
297
+ templateVersion: { tag: 'v2' }
298
+ });
299
+ });
300
+ });
301
+
302
+ describe('destroyVersion', function () {
303
+ it('deletes version of domain template', async () => {
304
+ api.delete('/v3/testDomain/templates/testTemplateName/versions/v1').reply(200, {
305
+ message: 'version has been deleted',
306
+ template: { name: 'test_template', version: { tag: 'v1' } }
307
+ });
308
+
309
+ const deletedTemplate = await client.destroyVersion('testDomain', 'testTemplateName', 'v1');
310
+ deletedTemplate.should.eql({
311
+ status: 200,
312
+ message: 'version has been deleted',
313
+ templateName: 'test_template',
314
+ templateVersion: { tag: 'v1' }
315
+ });
316
+ });
317
+ });
318
+
319
+ describe('listVersions', function () {
320
+ it('gets list of version for domain template', async () => {
321
+ api.get('/v3/testDomain/templates/testTemplateName/versions').reply(200, {
322
+ paging: {
323
+ first: 'first page url',
324
+ last: 'last page url',
325
+ next: 'next page url',
326
+ previous: 'previous page url',
327
+ },
328
+ template: {
329
+ name: 'test_template',
330
+ description: 'test_template description',
331
+ createdAt: 'Wed, 22 Dec 2021 09:13:27 UTC',
332
+ createdBy: '',
333
+ id: 'someId',
334
+ versions: [{
335
+ tag: 'v2',
336
+ engine: 'handlebars',
337
+ mjml: '',
338
+ createdAt: 'Wed, 22 Dec 2021 09:51:07 UTC',
339
+ comment: 'updated comment 2',
340
+ active: true,
341
+ id: 'someId1'
342
+ },
343
+ {
344
+ tag: 'v5',
345
+ engine: 'handlebars',
346
+ mjml: '',
347
+ createdAt: 'Mon, 20 Dec 2021 16:36:55 UTC',
348
+ comment: 'updated comment',
349
+ active: false,
350
+ id: 'someId2'
351
+ }]
352
+ }
353
+ });
354
+
355
+ const templatesList = await client.listVersions('testDomain', 'testTemplateName');
356
+ templatesList.should.be.an('object');
357
+ templatesList.should.eql({
358
+ pages: {
359
+ first: 'first page url',
360
+ last: 'last page url',
361
+ next: 'next page url',
362
+ previous: 'previous page url',
363
+ },
364
+ template: {
365
+ name: 'test_template',
366
+ description: 'test_template description',
367
+ createdAt: new Date('Wed, 22 Dec 2021 09:13:27 UTC'),
368
+ createdBy: '',
369
+ id: 'someId',
370
+ versions: [{
371
+ tag: 'v2',
372
+ engine: 'handlebars',
373
+ mjml: '',
374
+ createdAt: new Date('Wed, 22 Dec 2021 09:51:07 UTC'),
375
+ comment: 'updated comment 2',
376
+ active: true,
377
+ id: 'someId1'
378
+ },
379
+ {
380
+ tag: 'v5',
381
+ engine: 'handlebars',
382
+ mjml: '',
383
+ createdAt: new Date('Mon, 20 Dec 2021 16:36:55 UTC'),
384
+ comment: 'updated comment',
385
+ active: false,
386
+ id: 'someId2'
387
+ }]
388
+ }
389
+ });
390
+ });
391
+ });
392
+ });