notifications-node-client 8.2.1 → 8.3.1

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.
Files changed (35) hide show
  1. package/client/api_client.js +60 -67
  2. package/client/notification.js +289 -265
  3. package/package.json +13 -3
  4. package/tsconfig.json +18 -0
  5. package/.github/PULL_REQUEST_TEMPLATE.md +0 -18
  6. package/CHANGELOG.md +0 -292
  7. package/CONTRIBUTING.md +0 -59
  8. package/Dockerfile +0 -14
  9. package/Makefile +0 -41
  10. package/scripts/run_with_docker.sh +0 -21
  11. package/scripts/test_send.js +0 -57
  12. package/spec/api_client.js +0 -156
  13. package/spec/authentication.js +0 -32
  14. package/spec/integration/schemas/v1/GET_notifications_return.json +0 -37
  15. package/spec/integration/schemas/v1/POST_notification_return_email.json +0 -27
  16. package/spec/integration/schemas/v1/POST_notification_return_sms.json +0 -26
  17. package/spec/integration/schemas/v1/definitions.json +0 -12
  18. package/spec/integration/schemas/v1/email_notification.json +0 -106
  19. package/spec/integration/schemas/v1/sms_notification.json +0 -104
  20. package/spec/integration/schemas/v2/GET_notification_response.json +0 -50
  21. package/spec/integration/schemas/v2/GET_notifications_response.json +0 -29
  22. package/spec/integration/schemas/v2/GET_received_text_response.json +0 -23
  23. package/spec/integration/schemas/v2/GET_received_texts_response.json +0 -29
  24. package/spec/integration/schemas/v2/GET_template_by_id.json +0 -30
  25. package/spec/integration/schemas/v2/GET_templates_response.json +0 -15
  26. package/spec/integration/schemas/v2/POST_notification_email_response.json +0 -18
  27. package/spec/integration/schemas/v2/POST_notification_letter_response.json +0 -19
  28. package/spec/integration/schemas/v2/POST_notification_precompiled_letter_response.json +0 -19
  29. package/spec/integration/schemas/v2/POST_notification_sms_response.json +0 -18
  30. package/spec/integration/schemas/v2/POST_template_preview.json +0 -14
  31. package/spec/integration/schemas/v2/definitions.json +0 -51
  32. package/spec/integration/test.js +0 -415
  33. package/spec/integration/test_files/one_page_pdf.pdf +0 -0
  34. package/spec/notification.js +0 -633
  35. package/spec/test_files/simple.csv +0 -2
@@ -1,633 +0,0 @@
1
- const chai = require('chai');
2
- const chaiBytes = require('chai-bytes');
3
- chai.use(chaiBytes);
4
-
5
- let expect = require('chai').expect,
6
- NotifyClient = require('../client/notification.js').NotifyClient,
7
- MockDate = require('mockdate'),
8
- nock = require('nock'),
9
- createGovukNotifyToken = require('../client/authentication.js');
10
-
11
- MockDate.set(1234567890000);
12
-
13
- const baseUrl = 'http://localhost';
14
- const serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1';
15
- const apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3';
16
-
17
- function getNotifyClient() {
18
- let baseUrl = 'http://localhost';
19
- let notifyClient = new NotifyClient(baseUrl, serviceId, apiKeyId);
20
- return notifyClient;
21
- }
22
-
23
- function getNotifyAuthNock() {
24
- let notifyNock = nock(baseUrl, {
25
- reqheaders: {
26
- 'Authorization': 'Bearer ' + createGovukNotifyToken('POST', '/v2/notifications/', apiKeyId, serviceId)
27
- }
28
- })
29
- return notifyNock;
30
- }
31
-
32
- describe('notification api', () => {
33
-
34
- beforeEach(() => {
35
- MockDate.set(1234567890000);
36
- });
37
-
38
- afterEach(() => {
39
- MockDate.reset();
40
- });
41
-
42
- let notifyClient = getNotifyClient();
43
- let notifyAuthNock = getNotifyAuthNock();
44
-
45
- describe('sendEmail', () => {
46
- it('should send an email', () => {
47
-
48
- let email = 'dom@example.com',
49
- templateId = '123',
50
- options = {
51
- personalisation: {foo: 'bar'},
52
- },
53
- data = {
54
- template_id: templateId,
55
- email_address: email,
56
- personalisation: options.personalisation
57
- };
58
-
59
- notifyAuthNock
60
- .post('/v2/notifications/email', data)
61
- .reply(200, {hooray: 'bkbbk'});
62
-
63
- return notifyClient.sendEmail(templateId, email, options)
64
- .then(function (response) {
65
- expect(response.status).to.equal(200);
66
- });
67
-
68
- });
69
-
70
- it('should send an email with email_reply_to_id', () => {
71
-
72
- let email = 'dom@example.com',
73
- templateId = '123',
74
- options = {
75
- personalisation: {foo: 'bar'},
76
- emailReplyToId: '456',
77
- },
78
- data = {
79
- template_id: templateId,
80
- email_address: email,
81
- personalisation: options.personalisation,
82
- email_reply_to_id: options.emailReplyToId
83
- };
84
-
85
- notifyAuthNock
86
- .post('/v2/notifications/email', data)
87
- .reply(200, {hooray: 'bkbbk'});
88
-
89
- return notifyClient.sendEmail(templateId, email, options)
90
- .then((response) => {
91
- expect(response.status).to.equal(200);
92
- });
93
- });
94
-
95
- it('should send an email with a one click unsubscribe URL', () => {
96
-
97
- let email = 'me@example.com',
98
- templateId = '123',
99
- options = {
100
- personalisation: {foo: 'bar'},
101
- oneClickUnsubscribeURL: '456',
102
- },
103
- data = {
104
- template_id: templateId,
105
- email_address: email,
106
- personalisation: options.personalisation,
107
- one_click_unsubscribe_url: options.oneClickUnsubscribeURL
108
- };
109
-
110
- notifyAuthNock
111
- .post('/v2/notifications/email', data)
112
- .reply(200, {hooray: 'bkbbk'});
113
-
114
- return notifyClient.sendEmail(templateId, email, options)
115
- .then((response) => {
116
- expect(response.status).to.equal(200);
117
- });
118
- });
119
-
120
- it('should send an email with document upload', () => {
121
- let email = 'dom@example.com',
122
- templateId = '123',
123
- options = {
124
- personalisation: {documents:
125
- notifyClient.prepareUpload(Buffer.from("%PDF-1.5 testpdf"))
126
- },
127
- },
128
- data = {
129
- template_id: templateId,
130
- email_address: email,
131
- personalisation: options.personalisation,
132
- };
133
-
134
- notifyAuthNock
135
- .post('/v2/notifications/email', data)
136
- .reply(200, {hooray: 'bkbbk'});
137
-
138
- return notifyClient.sendEmail(templateId, email, options)
139
- .then((response) => {
140
- expect(response.status).to.equal(200);
141
- });
142
- });
143
-
144
- it('should send an email with a custom filename', () => {
145
- let email = 'dom@example.com',
146
- templateId = '123',
147
- options = {
148
- personalisation: {documents:
149
- notifyClient.prepareUpload(Buffer.from("a,b"), {filename: 'report.csv'})
150
- },
151
- },
152
- data = {
153
- template_id: templateId,
154
- email_address: email,
155
- personalisation: options.personalisation,
156
- };
157
-
158
- notifyAuthNock
159
- .post('/v2/notifications/email', data)
160
- .reply(200, {hooray: 'bkbbk'});
161
-
162
- return notifyClient.sendEmail(templateId, email, options)
163
- .then((response) => {
164
- expect(response.status).to.equal(200);
165
- expect(response.config.data).to.include('"filename":"report.csv"');
166
- });
167
- });
168
-
169
- it('should reject options dicts with unknown options', () => {
170
- let email = 'foo@bar.com',
171
- templateId = '123',
172
- // old personalisation dict
173
- options = {
174
- firstname: 'Fred',
175
- surname: 'Smith',
176
- reference: 'ABC123'
177
- };
178
- return notifyClient.sendEmail(templateId, email, options)
179
- .catch((err) => expect(err.message).to.include('["firstname","surname"]'));
180
- });
181
- });
182
-
183
- describe('prepareUpload', () => {
184
- it('should throw error when file bigger than 2MB is supplied', () => {
185
- let file = Buffer.alloc(3*1024*1024)
186
- expect(function(){
187
- notifyClient.prepareUpload(file);
188
- }).to.throw("File is larger than 2MB.")
189
- });
190
- it('should accept files as buffers (from fs.readFile with no encoding)', () => {
191
- let fs = require('fs');
192
- let file = fs.readFileSync('./spec/test_files/simple.csv');
193
- expect(typeof(file)).to.equal('object')
194
- expect(Buffer.isBuffer(file)).to.equal(true);
195
- expect(
196
- notifyClient.prepareUpload(file)
197
- ).contains({file: 'MSwyLDMKYSxiLGMK'})
198
- });
199
- it('should accept files as strings (from fs.readFile with an encoding)', () => {
200
- let fs = require('fs');
201
- let file = fs.readFileSync('./spec/test_files/simple.csv', 'binary');
202
- expect(typeof(file)).to.equal('string')
203
- expect(Buffer.isBuffer(file)).to.equal(false);
204
- expect(
205
- notifyClient.prepareUpload(file)
206
- ).contains({file: 'MSwyLDMKYSxiLGMK'})
207
- });
208
-
209
- it('should allow send a file email confirmation to be disabled', () => {
210
- let file = Buffer.alloc(2*1024*1024)
211
- expect(
212
- notifyClient.prepareUpload(file, {confirmEmailBeforeDownload: false})
213
- ).contains({confirm_email_before_download: false, retention_period: null})
214
- });
215
-
216
- it('should allow send a file email confirmation to be set', () => {
217
- let file = Buffer.alloc(2*1024*1024)
218
- expect(
219
- notifyClient.prepareUpload(file, {confirmEmailBeforeDownload: true})
220
- ).contains({confirm_email_before_download: true, retention_period: null})
221
- });
222
-
223
- it('should allow custom retention periods to be set', () => {
224
- let file = Buffer.alloc(2*1024*1024)
225
- expect(
226
- notifyClient.prepareUpload(file, {retentionPeriod: "52 weeks"})
227
- ).contains({confirm_email_before_download: null, retention_period: '52 weeks'})
228
- });
229
-
230
- it('should allow custom filenames to be set', () => {
231
- let file = Buffer.alloc(2*1024*1024)
232
- expect(
233
- notifyClient.prepareUpload(file, {filename: "report.csv"})
234
- ).contains({confirm_email_before_download: null, filename: "report.csv"})
235
- });
236
- });
237
-
238
- describe('sendSms', () => {
239
-
240
- it('should send an sms', () => {
241
-
242
- let phoneNo = '07525755555',
243
- templateId = '123',
244
- options = {
245
- personalisation: {foo: 'bar'},
246
- },
247
- data = {
248
- template_id: templateId,
249
- phone_number: phoneNo,
250
- personalisation: options.personalisation
251
- };
252
-
253
- notifyAuthNock
254
- .post('/v2/notifications/sms', data)
255
- .reply(200, {hooray: 'bkbbk'});
256
-
257
- return notifyClient.sendSms(templateId, phoneNo, options)
258
- .then(function (response) {
259
- expect(response.status).to.equal(200);
260
- });
261
- });
262
-
263
- it('should send an sms with smsSenderId', () => {
264
-
265
- let phoneNo = '07525755555',
266
- templateId = '123',
267
- options = {
268
- personalisation: {foo: 'bar'},
269
- smsSenderId: '456',
270
- },
271
- data = {
272
- template_id: templateId,
273
- phone_number: phoneNo,
274
- personalisation: options.personalisation,
275
- sms_sender_id: options.smsSenderId,
276
- };
277
-
278
- notifyAuthNock
279
- .post('/v2/notifications/sms', data)
280
- .reply(200, {hooray: 'bkbbk'});
281
-
282
- return notifyClient.sendSms(templateId, phoneNo, options)
283
- .then(function (response) {
284
- expect(response.status).to.equal(200);
285
- });
286
- });
287
-
288
- it('should reject options dicts with unknown options', () => {
289
- let phoneNumber = '07123456789',
290
- templateId = '123',
291
- // old personalisation dict
292
- options = {
293
- firstname: 'Fred',
294
- surname: 'Smith',
295
- reference: 'ABC123'
296
- };
297
- return notifyClient.sendSms(templateId, phoneNumber, options)
298
- .catch((err) => expect(err.message).to.include('["firstname","surname"]'));
299
- });
300
- });
301
-
302
- describe('sendLetter', () => {
303
-
304
- it('should send a letter', () => {
305
-
306
- let templateId = '123',
307
- options = {
308
- personalisation: {
309
- address_line_1: 'Mr Tester',
310
- address_line_2: '1 Test street',
311
- postcode: 'NW1 2UN'
312
- },
313
- },
314
- data = {
315
- template_id: templateId,
316
- personalisation: options.personalisation
317
- };
318
-
319
- notifyAuthNock
320
- .post('/v2/notifications/letter', data)
321
- .reply(200, {hooray: 'bkbbk'});
322
-
323
- return notifyClient.sendLetter(templateId, options)
324
- .then(function (response) {
325
- expect(response.status).to.equal(200);
326
- });
327
- });
328
-
329
- it('should reject options dicts with unknown options', () => {
330
- let templateId = '123',
331
- // old personalisation dict
332
- options = {
333
- address_line_1: 'Mr Tester',
334
- address_line_2: '1 Test street',
335
- postcode: 'NW1 2UN',
336
- reference: 'ABC123'
337
- };
338
- return notifyClient.sendLetter(templateId, options)
339
- .catch((err) => expect(err.message).to.include('["address_line_1","address_line_2","postcode"]'));
340
- });
341
-
342
- });
343
-
344
- it('should get notification by id', () => {
345
-
346
- let notificationId = 'wfdfdgf';
347
-
348
- notifyAuthNock
349
- .get('/v2/notifications/' + notificationId)
350
- .reply(200, {hooray: 'bkbbk'});
351
-
352
- return notifyClient.getNotificationById(notificationId)
353
- .then(function (response) {
354
- expect(response.status).to.equal(200);
355
- });
356
- });
357
-
358
- it('should get letter pdf by id', () => {
359
-
360
- let pdf_file = Buffer.from("%PDF-1.5 testpdf")
361
- let notificationId = 'wfdfdgf';
362
-
363
- notifyAuthNock
364
- .get('/v2/notifications/' + notificationId + '/pdf')
365
- .reply(200, pdf_file.toString());
366
-
367
- return notifyClient.getPdfForLetterNotification(notificationId)
368
- .then(function (response_buffer) {
369
- expect(response_buffer).to.equalBytes(pdf_file)
370
- });
371
- });
372
-
373
-
374
- describe('sendPrecompiledLetter', () => {
375
-
376
- it('should send a precompiled letter', () => {
377
- let pdf_file = Buffer.from("%PDF-1.5 testpdf"),
378
- reference = "HORK",
379
- data = {"reference": reference, "content": pdf_file.toString('base64')}
380
-
381
- notifyAuthNock
382
- .post('/v2/notifications/letter', data)
383
- .reply(200, {hiphip: 'hooray'});
384
-
385
- return notifyClient.sendPrecompiledLetter(reference, pdf_file)
386
- .then(function (response) {
387
- expect(response.status).to.equal(200);
388
- });
389
- });
390
-
391
-
392
- it('should be able to set postage when sending a precompiled letter', () => {
393
- let pdf_file = Buffer.from("%PDF-1.5 testpdf"),
394
- reference = "HORK",
395
- postage = "first"
396
- data = {"reference": reference, "content": pdf_file.toString('base64'), "postage": postage}
397
-
398
- notifyAuthNock
399
- .post('/v2/notifications/letter', data)
400
- .reply(200, {hiphip: 'hooray'});
401
-
402
- return notifyClient.sendPrecompiledLetter(reference, pdf_file, postage)
403
- .then(function (response) {
404
- expect(response.status).to.equal(200);
405
- });
406
- });
407
-
408
- it('should throw error when file bigger than 5MB is supplied', () => {
409
- let file = Buffer.alloc(6*1024*1024),
410
- reference = "HORK"
411
- expect(function(){
412
- notifyClient.sendPrecompiledLetter(reference, file);
413
- }).to.throw("File is larger than 5MB.")
414
- });
415
- });
416
-
417
-
418
- describe('getNotifications', () => {
419
-
420
- it('should get all notifications', () => {
421
-
422
- notifyAuthNock
423
- .get('/v2/notifications')
424
- .reply(200, {hooray: 'bkbbk'});
425
-
426
- return notifyClient.getNotifications()
427
- .then(function (response) {
428
- expect(response.status).to.equal(200);
429
- });
430
- });
431
-
432
- it('should get all notifications with a reference', () => {
433
-
434
- let reference = 'myref';
435
-
436
- notifyAuthNock
437
- .get('/v2/notifications?reference=' + reference)
438
- .reply(200, {hooray: 'bkbbk'});
439
-
440
- return notifyClient.getNotifications(undefined, undefined, reference)
441
- .then(function (response) {
442
- expect(response.status).to.equal(200);
443
- });
444
- });
445
-
446
- it('should get all failed notifications', () => {
447
-
448
- let status = 'failed';
449
-
450
- notifyAuthNock
451
- .get('/v2/notifications?status=' + status)
452
- .reply(200, {hooray: 'bkbbk'});
453
-
454
- return notifyClient.getNotifications(undefined, 'failed')
455
- .then(function (response) {
456
- expect(response.status).to.equal(200);
457
- });
458
- });
459
-
460
- it('should get all failed sms notifications', () => {
461
-
462
- let templateType = 'sms';
463
- let status = 'failed';
464
-
465
- notifyAuthNock
466
- .get('/v2/notifications?template_type=' + templateType + '&status=' + status)
467
- .reply(200, {hooray: 'bkbbk'});
468
-
469
- return notifyClient.getNotifications(templateType, status)
470
- .then(function (response) {
471
- expect(response.status).to.equal(200);
472
- });
473
- });
474
-
475
- it('should get all delivered sms notifications with a reference', () => {
476
-
477
- let templateType = 'sms';
478
- let status = 'delivered';
479
- let reference = 'myref';
480
-
481
- notifyAuthNock
482
- .get('/v2/notifications?template_type=' + templateType + '&status=' + status + '&reference=' + reference)
483
- .reply(200, {hooray: 'bkbbk'});
484
-
485
- return notifyClient.getNotifications(templateType, status, reference)
486
- .then(function (response) {
487
- expect(response.status).to.equal(200);
488
- });
489
- });
490
-
491
- it('should get all failed email notifications with a reference older than a given notification', () => {
492
-
493
- let templateType = 'sms';
494
- let status = 'delivered';
495
- let reference = 'myref';
496
- let olderThanId = '35836a9e-5a97-4d99-8309-0c5a2c3dbc72';
497
-
498
- notifyAuthNock
499
- .get('/v2/notifications?template_type=' + templateType +
500
- '&status=' + status +
501
- '&reference=' + reference +
502
- '&older_than=' + olderThanId
503
- )
504
- .reply(200, {hooray: 'bkbbk'});
505
-
506
- return notifyClient.getNotifications(templateType, status, reference, olderThanId)
507
- .then(function (response) {
508
- expect(response.status).to.equal(200);
509
- });
510
- });
511
- });
512
-
513
- describe('template funtions', () => {
514
-
515
- it('should get template by id', () => {
516
-
517
- let templateId = '35836a9e-5a97-4d99-8309-0c5a2c3dbc72';
518
-
519
- notifyAuthNock
520
- .get('/v2/template/' + templateId)
521
- .reply(200, {foo: 'bar'});
522
-
523
- return notifyClient.getTemplateById(templateId)
524
- .then(function (response) {
525
- expect(response.status).to.equal(200);
526
- });
527
-
528
- });
529
-
530
- it('should get template by id and version', () => {
531
-
532
- let templateId = '35836a9e-5a97-4d99-8309-0c5a2c3dbc72';
533
- let version = 10;
534
-
535
- notifyAuthNock
536
- .get('/v2/template/' + templateId + '/version/' + version)
537
- .reply(200, {foo: 'bar'});
538
-
539
- return notifyClient.getTemplateByIdAndVersion(templateId, version)
540
- .then(function (response) {
541
- expect(response.status).to.equal(200);
542
- });
543
-
544
- });
545
-
546
- it('should get all templates with unspecified template type', () => {
547
-
548
- notifyAuthNock
549
- .get('/v2/templates')
550
- .reply(200, {foo: 'bar'});
551
-
552
- return notifyClient.getAllTemplates()
553
- .then(function (response) {
554
- expect(response.status).to.equal(200);
555
- });
556
-
557
- });
558
-
559
- it('should get all templates with unspecified template type', () => {
560
-
561
- let templateType = 'sms'
562
-
563
- notifyAuthNock
564
- .get('/v2/templates?type=' + templateType)
565
- .reply(200, {foo: 'bar'});
566
-
567
- return notifyClient.getAllTemplates(templateType)
568
- .then(function (response) {
569
- expect(response.status).to.equal(200);
570
- });
571
-
572
- });
573
-
574
- it('should preview template by id with personalisation', () => {
575
-
576
- let templateId = '35836a9e-5a97-4d99-8309-0c5a2c3dbc72';
577
- let payload = {name: 'Foo' }
578
- let expectedPersonalisation = {personalisation: payload };
579
-
580
- notifyAuthNock
581
- .post('/v2/template/' + templateId + '/preview', expectedPersonalisation)
582
- .reply(200, {foo: 'bar'});
583
-
584
- return notifyClient.previewTemplateById(templateId, payload)
585
- .then(function (response) {
586
- expect(response.status).to.equal(200);
587
- });
588
-
589
- });
590
-
591
- it('should preview template by id without personalisation', () => {
592
-
593
- let templateId = '35836a9e-5a97-4d99-8309-0c5a2c3dbc72';
594
-
595
- notifyAuthNock
596
- .post('/v2/template/' + templateId + '/preview')
597
- .reply(200, {foo: 'bar'});
598
-
599
- return notifyClient.previewTemplateById(templateId)
600
- .then(function (response) {
601
- expect(response .status).to.equal(200);
602
- });
603
- });
604
- });
605
-
606
- it('should get latest 250 received texts', function() {
607
-
608
- notifyAuthNock
609
- .get('/v2/received-text-messages')
610
- .reply(200, {"foo":"bar"});
611
-
612
- return notifyClient.getReceivedTexts()
613
- .then(function(response){
614
- expect(response.status).to.equal(200);
615
- });
616
- });
617
-
618
- it('should get up to next 250 received texts with a reference older than a given message id', function() {
619
-
620
- var olderThanId = '35836a9e-5a97-4d99-8309-0c5a2c3dbc72';
621
-
622
- notifyAuthNock
623
- .get('/v2/received-text-messages?older_than=' + olderThanId)
624
- .reply(200, {"foo":"bar"});
625
-
626
- return notifyClient.getReceivedTexts(olderThanId)
627
- .then(function(response){
628
- expect(response.status).to.equal(200);
629
- });
630
- });
631
- });
632
-
633
- MockDate.reset();
@@ -1,2 +0,0 @@
1
- 1,2,3
2
- a,b,c