notifications-node-client 5.1.2 → 5.2.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,19 @@
1
+ ## 5.2.1 - 2022-10-19
2
+
3
+ * Support strings in calls to `prepareUpload`. `fs.readFile` can return strings if an encoding is provided, and the client didn't handle these correctly.
4
+
5
+ ## 5.2.0 - 2022-09-27
6
+
7
+ * Add support for new security features when sending a file by email:
8
+ * `confirmEmailBeforeDownload` can be set to `true` to require the user to enter their email address before accessing the file.
9
+ * `retentionPeriod` can be set to `<1-78> weeks` to set how long the file should be made available.
10
+
11
+ * The `isCsv` parameter to `prepareUpload` has now been replaced by an `options` parameter. The implementation has been done in a backwards-compatible way, so if you are just sending `true/false` values as the seecond parameter, that will continue to work. Though we still recommend updating to use the new options format.
12
+
13
+
1
14
  ## 5.1.2 - 2022-09-23
2
15
 
3
- Remove underscore.js dependency
16
+ Remove underscore.js dependencyr new send a file features)
4
17
 
5
18
  ## 5.1.1 - 2022-01-18
6
19
 
package/DOCUMENTATION.md CHANGED
@@ -289,10 +289,17 @@ If the request is not successful, the promise fails with an `err`.
289
289
 
290
290
  To send a file by email, add a placeholder to the template then upload a file. The placeholder will contain a secure link to download the file.
291
291
 
292
- The file will be available for the recipient to download for 18 months.
293
-
294
292
  The links are unique and unguessable. GOV.UK Notify cannot access or decrypt your file.
295
293
 
294
+ Your file will be available to download for a default period of 78 weeks (18 months). From 29 March 2023 we will reduce this to 26 weeks (6 months) for all new files. Files sent before 29 March will not be affected.
295
+
296
+ To help protect your files you can also:
297
+
298
+ * ask recipients to confirm their email address before downloading
299
+ * choose the length of time that a file is available to download
300
+
301
+ To turn these features on or off, you will need version 5.2.0 of the Node.js client library or a more recent version.
302
+
296
303
  #### Add contact details to the file download page
297
304
 
298
305
  1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
@@ -305,9 +312,9 @@ The links are unique and unguessable. GOV.UK Notify cannot access or decrypt you
305
312
  1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
306
313
  1. Go to the __Templates__ page and select the relevant email template.
307
314
  1. Select __Edit__.
308
- 1. Add a placeholder to the email template using double brackets. For example:
315
+ 1. Add a placeholder to the email template using double brackets. For example: "Download your file at: ((link_to_file))"
309
316
 
310
- "Download your file at: ((link_to_file))"
317
+ Your email should also tell recipients how long the file will be available to download.
311
318
 
312
319
  #### Upload your file
313
320
 
@@ -354,6 +361,91 @@ fs.readFile('path/to/document.csv', function (err, csvFile) {
354
361
  })
355
362
  ```
356
363
 
364
+ #### Ask recipients to confirm their email address before they can download the file
365
+
366
+ This new security feature is optional. You should use it if you send files that are sensitive - for example, because they contain personal information about your users.
367
+
368
+ When a recipient clicks the link in the email you’ve sent them, they have to enter their email address. Only someone who knows the recipient’s email address can download the file.
369
+
370
+ From 29 March 2023, we will turn this feature on by default for every file you send. Files sent before 29 March will not be affected.
371
+
372
+ ##### Turn on email address check
373
+
374
+ To use this feature before 29 March 2023 you will need version 5.2.0 of the Node.js client library, or a more recent version.
375
+
376
+ To make the recipient confirm their email address before downloading the file, set the `confirmEmailBeforeDownload` flag to `true`.
377
+
378
+ You will not need to do this after 29 March.
379
+
380
+ ```javascript
381
+ var fs = require('fs')
382
+
383
+ fs.readFile('path/to/document.pdf', function (err, pdfFile) {
384
+ console.log(err)
385
+ notifyClient.sendEmail(templateId, emailAddress, {
386
+ personalisation: {
387
+ first_name: 'Amala',
388
+ application_date: '2018-01-01',
389
+ link_to_file: notifyClient.prepareUpload(pdfFile, false, true, undefined)
390
+ }
391
+ }).then(response => console.log(response)).catch(err => console.error(err))
392
+ })
393
+ ```
394
+
395
+ ##### Turn off email address check (not recommended)
396
+
397
+ If you do not want to use this feature after 29 March 2023, you can turn it off on a file-by-file basis.
398
+
399
+ To do this you will need version 5.2.0 of the Node.js client library, or a more recent version.
400
+
401
+ You should not turn this feature off if you send files that contain:
402
+
403
+ * personally identifiable information
404
+ * commercially sensitive information
405
+ * information classified as ‘OFFICIAL’ or ‘OFFICIAL-SENSITIVE’ under the [Government Security Classifications](https://www.gov.uk/government/publications/government-security-classifications) policy
406
+
407
+ To let the recipient download the file without confirming their email address, set the `confirmEmailBeforeDownload` flag to `false`.
408
+
409
+ ```javascript
410
+ var fs = require('fs')
411
+
412
+ fs.readFile('path/to/document.pdf', function (err, pdfFile) {
413
+ console.log(err)
414
+ notifyClient.sendEmail(templateId, emailAddress, {
415
+ personalisation: {
416
+ first_name: 'Amala',
417
+ application_date: '2018-01-01',
418
+ link_to_file: notifyClient.prepareUpload(pdfFile, false, false, undefined)
419
+ }
420
+ }).then(response => console.log(response)).catch(err => console.error(err))
421
+ })
422
+ ```
423
+
424
+ #### Choose the length of time that a file is available to download
425
+
426
+ Set the number of weeks you want the file to be available using the `retentionPeriod` parameter.
427
+
428
+ You can choose any value between 1 week and 78 weeks.
429
+
430
+ To use this feature will need version 5.2.0 of the Node.js client library, or a more recent version.
431
+
432
+ If you do not choose a value, the file will be available for the default period of 78 weeks (18 months).
433
+
434
+ ```javascript
435
+ var fs = require('fs')
436
+
437
+ fs.readFile('path/to/document.pdf', function (err, pdfFile) {
438
+ console.log(err)
439
+ notifyClient.sendEmail(templateId, emailAddress, {
440
+ personalisation: {
441
+ first_name: 'Amala',
442
+ application_date: '2018-01-01',
443
+ link_to_file: notifyClient.prepareUpload(pdfFile, false, undefined, '52 weeks')
444
+ }
445
+ }).then(response => console.log(response)).catch(err => console.error(err))
446
+ })
447
+ ```
448
+
357
449
  #### Response
358
450
 
359
451
  If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
@@ -385,6 +477,8 @@ If the request is not successful, the promise fails with an `err`.
385
477
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient using a team-only API key"`<br>`}]`|Use the correct type of [API key](#api-keys)|
386
478
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode)|
387
479
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Unsupported file type '(FILE TYPE)'. Supported types are: '(ALLOWED TYPES)'"`<br>`}]`|Wrong file type. You can only upload .pdf, .csv, .txt, .doc, .docx, .xlsx, .rtf or .odt files|
480
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Unsupported value for retention_period '(PERIOD)'. Supported periods are from 1 to 78 weeks."`<br>`}]`|Choose a period between 1 and 78 weeks|
481
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Unsupported value for confirm_email_before_download: '(VALUE)'. Use a boolean true or false value."`<br>`}]`|Use either true or false|
388
482
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "File did not pass the virus scan"`<br>`}]`|The file contains a virus|
389
483
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Send files by email has not been set up - add contact details for your service at https://www.notifications.service.gov.uk/services/(SERVICE ID)/service-settings/send-files-by-email"`<br>`}]`|See how to [add contact details to the file download page](#add-contact-details-to-the-file-download-page)|
390
484
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can only send a file by email"`<br>`}]`|Make sure you are using an email template|
@@ -115,6 +115,9 @@ function _check_and_encode_file(file, size_limit) {
115
115
  if (file.length > size_limit * 1024 * 1024) {
116
116
  throw "File is larger than " + String(size_limit) + "MB.";
117
117
  }
118
+ if (typeof(file) === 'string') {
119
+ file = Buffer.from(file);
120
+ }
118
121
  return file.toString('base64')
119
122
  }
120
123
 
@@ -344,15 +347,33 @@ Object.assign(NotifyClient.prototype, {
344
347
  /**
345
348
  *
346
349
  * @param {Buffer} fileData
347
- * @param {Boolean} isCsv
350
+ * @param {object} options
348
351
  *
349
352
  * @returns {Dictionary}
350
353
  */
351
- prepareUpload: function(fileData, isCsv = false) {
352
- return {
353
- 'file': _check_and_encode_file(fileData, 2),
354
- 'is_csv': isCsv
354
+ prepareUpload: function(fileData, options) {
355
+ let data = {
356
+ file: _check_and_encode_file(fileData, 2),
357
+ is_csv: false,
358
+ confirm_email_before_download: null,
359
+ retention_period: null,
360
+ }
361
+
362
+ if (options !== undefined) {
363
+ if (typeof(options) === 'boolean') {
364
+ data.is_csv = options
365
+ }
366
+ else {
367
+ if (options.isCsv !== undefined) {
368
+ data.is_csv = options.isCsv;
369
+ }
370
+
371
+ data.confirm_email_before_download = options.confirmEmailBeforeDownload || null;
372
+ data.retention_period = options.retentionPeriod || null
373
+ }
355
374
  }
375
+
376
+ return data
356
377
  },
357
378
 
358
379
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notifications-node-client",
3
- "version": "5.1.2",
3
+ "version": "5.2.1",
4
4
  "homepage": "https://docs.notifications.service.gov.uk/node.html",
5
5
  "repository": {
6
6
  "type": "git",
@@ -188,6 +188,59 @@ describe('notification api', () => {
188
188
  notifyClient.prepareUpload(file);
189
189
  }).to.throw("File is larger than 2MB.")
190
190
  });
191
+ it('should accept files as buffers (from fs.readFile with no encoding)', () => {
192
+ let fs = require('fs');
193
+ let file = fs.readFileSync('./spec/test_files/simple.csv');
194
+ expect(typeof(file)).to.equal('object')
195
+ expect(Buffer.isBuffer(file)).to.equal(true);
196
+ expect(
197
+ notifyClient.prepareUpload(file, true)
198
+ ).contains({file: 'MSwyLDMKYSxiLGMK', is_csv: true})
199
+ });
200
+ it('should accept files as strings (from fs.readFile with an encoding)', () => {
201
+ let fs = require('fs');
202
+ let file = fs.readFileSync('./spec/test_files/simple.csv', 'binary');
203
+ expect(typeof(file)).to.equal('string')
204
+ expect(Buffer.isBuffer(file)).to.equal(false);
205
+ expect(
206
+ notifyClient.prepareUpload(file, true)
207
+ ).contains({file: 'MSwyLDMKYSxiLGMK', is_csv: true})
208
+ });
209
+
210
+ it('should allow isCsv to be set with the old method (directly into options)', () => {
211
+ let file = Buffer.alloc(2*1024*1024)
212
+ expect(
213
+ notifyClient.prepareUpload(file, true)
214
+ ).contains({is_csv: true, confirm_email_before_download: null, retention_period: null})
215
+ });
216
+
217
+ it('should allow isCsv to be set as part of the options object', () => {
218
+ let file = Buffer.alloc(2*1024*1024)
219
+ expect(
220
+ notifyClient.prepareUpload(file, {isCsv: true})
221
+ ).contains({is_csv: true, confirm_email_before_download: null, retention_period: null})
222
+ });
223
+
224
+ it('should imply isCsv=false from empty options', () => {
225
+ let file = Buffer.alloc(2*1024*1024)
226
+ expect(
227
+ notifyClient.prepareUpload(file, {})
228
+ ).contains({is_csv: false, confirm_email_before_download: null, retention_period: null})
229
+ });
230
+
231
+ it('should allow send a file email confirmation to be set', () => {
232
+ let file = Buffer.alloc(2*1024*1024)
233
+ expect(
234
+ notifyClient.prepareUpload(file, {confirmEmailBeforeDownload: true})
235
+ ).contains({is_csv: false, confirm_email_before_download: true, retention_period: null})
236
+ });
237
+
238
+ it('should allow custom retention periods to be set', () => {
239
+ let file = Buffer.alloc(2*1024*1024)
240
+ expect(
241
+ notifyClient.prepareUpload(file, {retentionPeriod: "52 weeks"})
242
+ ).contains({is_csv: false, confirm_email_before_download: null, retention_period: '52 weeks'})
243
+ });
191
244
  });
192
245
 
193
246
  describe('sendSms', () => {
@@ -0,0 +1,2 @@
1
+ 1,2,3
2
+ a,b,c