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
package/CHANGELOG.md DELETED
@@ -1,292 +0,0 @@
1
- ## 8.2.1 - 2024-07-03
2
-
3
- * Fix a bug where an internal API client function isn't being exposed in the library.
4
-
5
- ## 8.2.0 - 2024-05-17
6
-
7
- * Add support for providing a custom underlying Axios client via `setClient`.
8
-
9
- ## 8.1.0 - 2024-05-09
10
-
11
- * The `sendEmail` function can now be passed `oneClickUnsubscribeURL` as an optional argument.
12
-
13
- ## 8.0.0 - 2023-12-27
14
-
15
- * Remove the default `is_csv` boolean parameter from `prepareUpload`. This method now accepts a file and an options map with the following options. For more specific information please read the documentation.
16
- * `filename` (string) - specify the document's filename upon download
17
- * `confirm_email_before_download` (boolean) - require the user to enter their email address before the file can be downloaded.
18
- * `retention_period` (string) - how long Notify should make the file available to the user for.
19
-
20
- ## 7.0.6 - 2023-11-13
21
-
22
- * Bump axios from 1.2.6 to 1.6.1
23
-
24
- ## 7.0.5 - 2023-11-13
25
-
26
- * Fix a few cases of assignment to undeclared (global) variables
27
-
28
- ## 7.0.4 - 2023-11-10
29
-
30
- * Bump axios to the 1.x branch to address CVE-2023-45857. Due to underlying changes in Axios you may have to explicitly set the `protocol` property when constructing your `proxyConfig` object, if using a proxy.
31
-
32
- ## 7.0.3 - 2023-07-21
33
-
34
- * Bump word-wrap from 1.2.3 to 1.2.4 to address CVE-2023-26115.
35
-
36
- ## 7.0.2 - 2023-07-13
37
-
38
- * Bump semver from 5.7.1 to 5.7.2
39
-
40
- ## 7.0.1 - 2023-07-13
41
-
42
- * Fix a bug with default behaviour for `confirmEmailBeforeDownload`, which coalesced false to null.
43
-
44
- ## 7.0.0 - 2023-01-12
45
-
46
- * Remove support for node versions below v14.17.3.
47
-
48
- ## 6.0.0 - 2022-12-22
49
-
50
- * Bump jsonwebtokens from 8.5.1 to 9.0.0 to mitigate CVE-2022-23529. We don't believe this CVE affects any use-cases of notifications-node-client; this update is out of best-practice rather than any direct concern.
51
- * Remove support for node versions below v12.
52
-
53
- ## 5.2.3 - 2022-11-22
54
-
55
- * Bump follow-redirects from 1.14.7 to 1.15.2
56
-
57
- ## 5.2.2 - 2022-11-16
58
-
59
- * Upgrade ansi-regex dependencies to mitigate CVE-2021-3807.
60
-
61
- ## 5.2.1 - 2022-10-19
62
-
63
- * Support strings in calls to `prepareUpload`. `fs.readFile` can return strings if an encoding is provided, and the client didn't handle these correctly.
64
-
65
- ## 5.2.0 - 2022-09-27
66
-
67
- * Add support for new security features when sending a file by email:
68
- * `confirmEmailBeforeDownload` can be set to `true` to require the user to enter their email address before accessing the file.
69
- * `retentionPeriod` can be set to `<1-78> weeks` to set how long the file should be made available.
70
-
71
- * 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.
72
-
73
-
74
- ## 5.1.2 - 2022-09-23
75
-
76
- Remove underscore.js dependencyr new send a file features)
77
-
78
- ## 5.1.1 - 2022-01-18
79
-
80
- Upgrade axios version from ^0.21.1 to ^0.25.0
81
-
82
- ## 5.1.0 - 2020-12-30
83
-
84
- ### Changed
85
-
86
- * Upgrade axios version from 0.19.2 to 0.21.1
87
- * Allow any compatible version of axios to be used (0.21.1 to <1.0.0)
88
- * Allow any compatible version of jsonwebtoken to be used (8.2.1 to <9.0.0)
89
-
90
- ## 5.0.2 - 2020-11-20
91
-
92
- ### Changed
93
-
94
- Correct incorrect description of parameter to be used by `NotifyClient.setProxy`
95
-
96
- ## 5.0.1 - 2020-11-18
97
-
98
- ### Changed
99
-
100
- Remove unintentional global nature of variable `version`
101
-
102
- ## 5.0.0 - 2020-09-02
103
-
104
- ### Changed
105
-
106
- We have replaced the use of the npm [request-promise](https://www.npmjs.com/package/request-promise) package with [axios](https://www.npmjs.com/package/axios) as the npm [request](https://www.npmjs.com/package/request) package has been deprecated. This makes the following breaking changes:
107
-
108
- 1. The `response` `object` returned by a successful API call is now in the form of an [axios response](https://www.npmjs.com/package/axios#response-schema). This has a different interface to a [request response](https://nodejs.org/api/http.html#http_class_http_incomingmessage). For example:
109
-
110
- * `response.body` becomes `response.data`
111
- * `response.statusCode` becomes `response.status`
112
-
113
- 2. The `err` `object` returned by an unsuccessful API call has a different interface. For example, `err.error` becomes `err.response.data`. See the axios documentation for further details on [error handling](https://www.npmjs.com/package/axios#handling-errors).
114
-
115
- 3. To configure the use of a proxy you should pass the proxy configuration as an `object` rather than a URL. For details, see the [axios client](https://github.com/axios/axios).
116
-
117
- 4. We now return native [promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) rather than [bluebird promises](http://bluebirdjs.com). You will not need to make any changes unless you are using some of the additional methods found on bluebird promises that do not exist on native promises.
118
-
119
-
120
- ## 4.9.0 - 2020-08-19
121
-
122
- ### Changed
123
-
124
- * Added `letter_contact_block` to the responses for `getTemplateById`, `getTemplateByIdAndVersion` and `getAllTemplates`.
125
-
126
- ## 4.8.0 - 2020-06-18
127
-
128
- ### Changed
129
-
130
- * Add support for an optional `isCsv` parameter in the `prepareUpload` function. This fixes a bug when sending a CSV file by email. This ensures that the file is downloaded as a CSV rather than a TXT file.
131
-
132
- ## [4.7.3] - 2020-04-03
133
-
134
- ### Changed
135
-
136
- * Remove `__dirname` global call from api client
137
-
138
- ## [4.7.2] - 2020-01-31
139
-
140
- ### Changed
141
-
142
- * Add homepage to package.json
143
-
144
- ## [4.7.1] - 2020-01-27
145
-
146
- ### Changed
147
-
148
- * Refer to files, not documents, in error messages
149
-
150
- ## [4.7.0] - 2019-11-01
151
-
152
- ### Changed
153
-
154
- * Add `notifyClient.getPdfForLetterNotification(notificationId)`
155
- * Returns a Buffer with pdf data
156
- * Will raise a BadRequestError if the PDF is not available
157
-
158
- ## [4.6.0] - 2019-02-01
159
-
160
- ### Changed
161
-
162
- * Added an optional postage argument to `sendPrecompiledLetter`
163
-
164
- ## [4.5.2] - 2018-11-05
165
-
166
- ### Changed
167
-
168
- * Moved documentation to https://docs.notifications.service.gov.uk/node.html (generated from DOCUMENTATION.md)
169
-
170
- ## [4.5.1] - 2018-09-14
171
-
172
- ### Changed
173
-
174
- * Made formatting consistent across documentation.
175
-
176
- ## [4.5.0] - 2018-09-13
177
-
178
- ### Changed
179
-
180
- * Added a function to send precompiled letter through the client.
181
- * the new function uses a helper function to check if the file size is within the 5MB limit and to encode the file using base64. Then a POST request to our API is made with the file data and user reference.
182
- * instructions for the new functionality added to the documentation.
183
-
184
- ## [4.4.0] - 2018-09-05
185
-
186
- ### Changed
187
-
188
- * Added instructions for uploading a document to be linked to from an email notification.
189
- * Created a helper function `prepareUpload` as a part of this development. This function encodes the document that is to be uploaded with base64 and returns a dictionary with prepared document as a value (the way our API is prepared to receive it). It also checks if the provided file is not larger than 2MB. The function throws an error if the file is too big.
190
-
191
- ## [4.3.0] - 2018-09-04
192
-
193
- * Added `name` to the response for `getTemplateById()` and `getTemplateByIdAndVersion()`
194
- * These functions now return the name of the template as set in Notify
195
-
196
- ## [4.2.0] - 2018-07-24
197
-
198
- ### Changed
199
-
200
- * Added `created_by_name` to the response for `.getNotificationById()` and `.getNotifications()`:
201
- * If the notification was sent manually this will be the name of the sender. If the notification was sent through the API this will be `null`.
202
-
203
- ## [4.1.0] - 2017-11-23
204
-
205
- ### Changed
206
-
207
- * Added new method:
208
- * `getReceivedTexts` - get one page of text messages (250) per call
209
-
210
- ## [4.0.0] - 2017-11-07
211
-
212
- ### Changed
213
-
214
- * Updated `sendSms`, `sendEmail` and `sendLetter` to take an `options` object as a parameter.
215
- * `personalisation`, `reference`, `smsSenderId` and `emailReplyToId` now need to be passed to these functions inside `options`.
216
- * Removed the unused `Crypto` dependency
217
-
218
- ## [3.5.0] - 2017-11-01
219
-
220
- ### Changed
221
-
222
- * Updated `sendSms` method with optional argument:
223
- * `smsSenderId` - specify the identifier of the sms sender (optional)
224
-
225
- ## [3.4.0] - 2017-10-19
226
-
227
- ### Changed
228
-
229
- * Added new method:
230
- * `sendLetter` - send a letter
231
-
232
- ## [3.3.0] - 2017-10-13
233
-
234
- ### Changed
235
-
236
- * Updated `sendEmail` method with optional argument:
237
- * `emailReplyToId` - specify the identifier of the email reply-to address (optional)
238
-
239
- ## [3.2.0] - 2017-09-22
240
-
241
- ### Changed
242
-
243
- * Added new method:
244
- * `setProxy(proxyUrl)` - specify the URL of a proxy for the client to use (optional)
245
-
246
- ## [3.1.0] - 2017-05-10
247
-
248
- ### Changed
249
-
250
- * Added new methods for managing templates:
251
- * `getTemplateById` - retrieve a single template
252
- * `getTemplateByIdAndVersion` - retrieve a specific version for a desired template
253
- * `getAllTemplates` - retrieve all templates (can filter by type)
254
- * `previewTemplateById` - preview a template with personalisation applied
255
-
256
- * Update README to describe how to catch errors
257
-
258
- ## [3.0.0] - 2016-12-16
259
-
260
- ### Changed
261
-
262
- * Using v2 of the notification-api.
263
-
264
- * Update to `notifyClient.sendSms()`:
265
- * Added `reference`: an optional identifier you generate if you don’t want to use Notify’s `id`. It can be used to identify a single notification or a batch of notifications.
266
- * Updated method signature:
267
-
268
- ```javascript
269
- notifyClient.sendSms(templateId, phoneNumber, personalisation, reference);
270
- ```
271
- * Where `personalisation` and `reference` can be `undefined`.
272
-
273
- * Update to `notifyClient.sendEmail()`:
274
- * Added `reference`: an optional identifier you generate if you don’t want to use Notify’s `id`. It can be used to identify a single notification or a batch of notifications.
275
- * Updated method signature:
276
-
277
- ```javascript
278
- notifyClient.sendEmail(templateId, emailAddress, personalisation, reference);
279
- ```
280
- * Where `personalisation` and `reference` can be `undefined`.
281
- * `NotificationClient.getAllNotifications()`
282
- * Notifications can now be filtered by `reference` and `olderThanId`, see the README for details.
283
- * Updated method signature:
284
-
285
- ```javascript
286
- notifyClient.getNotifications(templateType, status, reference, olderThanId);
287
- ```
288
- * Each one of these parameters can be `undefined`
289
-
290
- # Prior versions
291
-
292
- Changelog not recorded - please see pull requests on github.
package/CONTRIBUTING.md DELETED
@@ -1,59 +0,0 @@
1
- # Contributing
2
-
3
- Pull requests welcome.
4
-
5
- ## Setting Up
6
-
7
- ### Docker container
8
-
9
- This app uses dependencies that are difficult to install locally. In order to make local development easy, we run app commands through a Docker container. Run the following to set this up:
10
-
11
- ```shell
12
- make bootstrap-with-docker
13
- ```
14
-
15
- ### `environment.sh`
16
-
17
- In the root directory of the repo, run:
18
-
19
- ```
20
- notify-pass credentials/client-integration-tests > environment.sh
21
- ```
22
-
23
- Unless you're part of the GOV.UK Notify team, you won't be able to run this command or the Integration Tests. However, the file still needs to exist - run `touch environment.sh` instead.
24
-
25
- ## Tests
26
-
27
- There are unit and integration tests that can be run to test functionality of the client.
28
-
29
- ### Unit tests
30
-
31
- To run the unit tests:
32
-
33
- ```
34
- make test-with-docker
35
- ```
36
-
37
- ### Integration Tests
38
-
39
- To run the integration tests:
40
-
41
- ```
42
- make integration-test-with-docker
43
- ```
44
-
45
- ## Working on the client locally
46
-
47
- ```
48
- npm install --save notifications-node-client
49
- ```
50
-
51
- ## Testing JavaScript examples in Markdown
52
-
53
- We automatically test that the JavaScript in the documentation examples matches [our linting standards](https://gds-way.cloudapps.digital/manuals/programming-languages/nodejs/#source-formatting-and-linting). This also catches some issues that could stop an example from running when copied.
54
-
55
- You can fix issues automatically by running:
56
-
57
- ```
58
- npm run test:markdown:standard -- --fix
59
- ```
package/Dockerfile DELETED
@@ -1,14 +0,0 @@
1
- FROM ghcr.io/alphagov/notify/node:18-slim
2
-
3
- ENV DEBIAN_FRONTEND=noninteractive
4
-
5
- RUN \
6
- echo "Install base packages" \
7
- && apt-get update \
8
- && apt-get install -y --no-install-recommends \
9
- make \
10
- gnupg \
11
- && echo "Clean up" \
12
- && rm -rf /var/lib/apt/lists/* /tmp/*
13
-
14
- WORKDIR /var/project
package/Makefile DELETED
@@ -1,41 +0,0 @@
1
- .DEFAULT_GOAL := help
2
- SHELL := /bin/bash
3
-
4
- .PHONY: help
5
- help:
6
- @cat $(MAKEFILE_LIST) | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
7
-
8
- .PHONY: bootstrap
9
- bootstrap: ## Install build dependencies
10
- npm ci
11
-
12
- .PHONY: build
13
- build: bootstrap ## Build project (dummy task for CI)
14
-
15
- .PHONY: test
16
- test: ## Run tests
17
- npm test
18
-
19
- .PHONY: integration-test
20
- integration-test: ## Run integration tests
21
- npm test --integration
22
-
23
- .PHONY: bootstrap-with-docker
24
- bootstrap-with-docker: ## Prepare the Docker builder image
25
- docker build -t notifications-node-client .
26
- ./scripts/run_with_docker.sh make bootstrap
27
-
28
- .PHONY: test-with-docker
29
- test-with-docker: ## Run tests inside a Docker container
30
- ./scripts/run_with_docker.sh make test
31
-
32
- .PHONY: integration-test-with-docker
33
- integration-test-with-docker: ## Run integration tests inside a Docker container
34
- ./scripts/run_with_docker.sh make integration-test
35
-
36
- .PHONY: get-client-version
37
- get-client-version: ## Retrieve client version number from source code
38
- @node -p "require('./package.json').version"
39
-
40
- clean:
41
- rm -rf .cache venv
@@ -1,21 +0,0 @@
1
- DOCKER_IMAGE_NAME=notifications-node-client
2
-
3
- source environment.sh
4
-
5
- docker run \
6
- --rm \
7
- -v "`pwd`:/var/project" \
8
- -e NOTIFY_API_URL=${NOTIFY_API_URL} \
9
- -e API_KEY=${API_KEY} \
10
- -e FUNCTIONAL_TEST_NUMBER=${FUNCTIONAL_TEST_NUMBER} \
11
- -e FUNCTIONAL_TEST_EMAIL=${FUNCTIONAL_TEST_EMAIL} \
12
- -e EMAIL_TEMPLATE_ID=${EMAIL_TEMPLATE_ID} \
13
- -e SMS_TEMPLATE_ID=${SMS_TEMPLATE_ID} \
14
- -e LETTER_TEMPLATE_ID=${LETTER_TEMPLATE_ID} \
15
- -e EMAIL_REPLY_TO_ID=${EMAIL_REPLY_TO_ID} \
16
- -e SMS_SENDER_ID=${SMS_SENDER_ID} \
17
- -e API_SENDING_KEY=${API_SENDING_KEY} \
18
- -e INBOUND_SMS_QUERY_KEY=${INBOUND_SMS_QUERY_KEY} \
19
- -it \
20
- ${DOCKER_IMAGE_NAME} \
21
- ${@}
@@ -1,57 +0,0 @@
1
- var argv = require('optimist')
2
- .usage('Usage: $0' +
3
- ' -b [baseUrl]' +
4
- ' -s [notify secret]' +
5
- ' -t [templateId]' +
6
- ' -d [destination (email address or phone number, not needed for letters]' +
7
- ' -p [personalisation (required for letter {"address_line_1": "mrs test", "address_line_2": "1 test street", "postcode": "SW1 1AA"})]' +
8
- ' -m [type (email, sms or letter, default email)]')
9
- .demand(['s', 't'])
10
- .argv,
11
- NotifyClient = require('../client/notification').NotifyClient,
12
- notifyClient,
13
-
14
- baseUrl = argv.b || 'https://api.notifications.service.gov.uk',
15
- secret = argv.s,
16
- templateId = argv.t,
17
- destination = argv.d,
18
- personalisation = argv.p ? JSON.parse(argv.p) : null,
19
- type = argv.m || 'email';
20
-
21
- notifyClient = new NotifyClient(baseUrl, secret);
22
-
23
- switch(type) {
24
-
25
- case 'email':
26
- notifyClient.sendEmail(templateId, destination, personalisation)
27
- .then(function(response) {
28
- console.log('Notify response: ' + JSON.stringify(response));
29
- })
30
- .catch(function(error) {
31
- console.log('Error ' + error);
32
- });
33
- break;
34
-
35
- case 'sms':
36
- notifyClient.sendSms(templateId, String(destination), personalisation)
37
- .then(function(response) {
38
- console.log('Notify response: ' + JSON.stringify(response));
39
- })
40
- .catch(function(error) {
41
- console.log('Error ' + error);
42
- });
43
- break;
44
-
45
- case 'letter':
46
- notifyClient.sendLetter(templateId, personalisation)
47
- .then(function(response) {
48
- console.log('Notify response: ' + JSON.stringify(response));
49
- })
50
- .catch(function(error) {
51
- console.log('Error ' + error);
52
- });
53
- break;
54
-
55
- default:
56
- console.log('Unrecognised notification type');
57
- }
@@ -1,156 +0,0 @@
1
- var expect = require('chai').expect,
2
- MockDate = require('mockdate'),
3
- ApiClient = require('../client/api_client.js'),
4
- nock = require('nock'),
5
- createGovukNotifyToken = require('../client/authentication.js'),
6
- version = require('../package.json').version,
7
- axios = require('axios'),
8
- sinon = require('sinon');
9
-
10
- describe('api client', function () {
11
-
12
- beforeEach(function() {
13
- MockDate.set(1234567890000);
14
- });
15
-
16
- afterEach(function() {
17
- MockDate.reset();
18
- });
19
-
20
- it('should make a get request with correct headers', function (done) {
21
-
22
- var urlBase = 'https://api.notifications.service.gov.uk',
23
- path = '/email',
24
- body = {
25
- 'body': 'body text'
26
- },
27
- serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1',
28
- apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3';
29
-
30
- [
31
- new ApiClient(serviceId, apiKeyId),
32
- new ApiClient(urlBase, serviceId, apiKeyId),
33
- new ApiClient(urlBase, 'key_name' + '-' + serviceId + '-' + apiKeyId),
34
- new ApiClient('key_name' + ':' + serviceId + ':' + apiKeyId),
35
- ].forEach(function(client, index, clients) {
36
-
37
- nock(urlBase, {
38
- reqheaders: {
39
- 'Authorization': 'Bearer ' + createGovukNotifyToken('GET', path, apiKeyId, serviceId),
40
- 'User-Agent': 'NOTIFY-API-NODE-CLIENT/' + version
41
- }
42
- })
43
- .get(path)
44
- .reply(200, body);
45
-
46
- client.get(path)
47
- .then(function (response) {
48
- expect(response.data).to.deep.equal(body);
49
- if (index == clients.length - 1) done();
50
- });
51
-
52
- });
53
-
54
- });
55
-
56
- it('should make a post request with correct headers', function (done) {
57
-
58
- var urlBase = 'http://localhost',
59
- path = '/email',
60
- data = {
61
- 'data': 'qwjjs'
62
- },
63
- serviceId = 123,
64
- apiKeyId = 'SECRET',
65
- apiClient = new ApiClient(urlBase, serviceId, apiKeyId);
66
-
67
- nock(urlBase, {
68
- reqheaders: {
69
- 'Authorization': 'Bearer ' + createGovukNotifyToken('POST', path, apiKeyId, serviceId),
70
- 'User-Agent': 'NOTIFY-API-NODE-CLIENT/' + version
71
- }
72
- })
73
- .post(path, data)
74
- .reply(200, {"hooray": "bkbbk"});
75
-
76
- apiClient = new ApiClient(urlBase, serviceId, apiKeyId);
77
- apiClient.post(path, data)
78
- .then(function (response) {
79
- expect(response.status).to.equal(200);
80
- done();
81
- });
82
- });
83
-
84
- it('should direct get requests through the proxy when set', function (done) {
85
- var urlBase = 'https://api.notifications.service.gov.uk',
86
- proxyConfig = { host: 'addressofmyproxy.test', protocol: 'http'},
87
- path = '/email',
88
- apiClient = new ApiClient(urlBase, 'apiKey');
89
-
90
- nock("http://" + proxyConfig.host)
91
- .get(urlBase + path)
92
- .reply(200, 'test');
93
-
94
- apiClient.setProxy(proxyConfig);
95
- apiClient.get(path)
96
- .then(function (response) {
97
- expect(response.status).to.equal(200);
98
- expect(response.config.proxy).to.eql(proxyConfig);
99
- done();
100
- });
101
- });
102
-
103
- it('should direct post requests through the proxy when set', function (done) {
104
- var urlBase = 'https://api.notifications.service.gov.uk',
105
- proxyConfig = { host: 'addressofmyproxy.test', protocol: 'http'},
106
- path = '/email',
107
- apiClient = new ApiClient(urlBase, 'apiKey');
108
-
109
- nock("http://" + proxyConfig.host)
110
- .post(urlBase + path)
111
- .reply(200, 'test');
112
-
113
- apiClient.setProxy(proxyConfig);
114
- apiClient.post(path)
115
- .then(function (response) {
116
- expect(response.status).to.equal(200);
117
- expect(response.config.proxy).to.eql(proxyConfig);
118
- done();
119
- });
120
- });
121
-
122
- it('should use the custom Axios client when set', function (done) {
123
- var urlBase = 'https://api.notifications.service.gov.uk',
124
- path = '/email',
125
- body = {
126
- 'body': 'body text'
127
- },
128
- serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1',
129
- apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3';
130
-
131
- var customClientStub = sinon.stub().resolves({ data: body });
132
-
133
- var apiClient = new ApiClient(serviceId, apiKeyId);
134
- apiClient.setClient(customClientStub);
135
-
136
- nock(urlBase, {
137
- reqheaders: {
138
- 'Authorization': 'Bearer ' + createGovukNotifyToken('GET', path, apiKeyId, serviceId),
139
- 'User-Agent': 'NOTIFY-API-NODE-CLIENT/' + version
140
- }
141
- })
142
- .get(path)
143
- .reply(200, body);
144
-
145
- apiClient.get(path)
146
- .then(function (response) {
147
- expect(response.data).to.deep.equal(body);
148
- expect(customClientStub.calledOnce).to.be.true;
149
- expect(customClientStub.args[0][0].url).to.equal(urlBase + path);
150
- expect(customClientStub.args[0][0].headers['Authorization']).to.equal('Bearer ' + createGovukNotifyToken('GET', path, apiKeyId, serviceId));
151
- expect(customClientStub.args[0][0].headers['User-Agent']).to.equal('NOTIFY-API-NODE-CLIENT/' + version);
152
- done();
153
- })
154
- .catch(done);
155
- });
156
- });
@@ -1,32 +0,0 @@
1
- var expect = require('chai').expect,
2
- MockDate = require('mockdate'),
3
- jwt = require('jsonwebtoken'),
4
- createGovukNotifyToken = require('../client/authentication.js');
5
-
6
-
7
- describe('Authentication', function() {
8
-
9
- beforeEach(function() {
10
- MockDate.set(1234567890000);
11
- });
12
-
13
- afterEach(function() {
14
- MockDate.reset();
15
- });
16
-
17
- describe('tokens', function() {
18
-
19
- it('can be generated and decoded', function() {
20
-
21
- var token = createGovukNotifyToken("POST", "/v2/notifications/sms", "SECRET", 123),
22
- decoded = jwt.verify(token, 'SECRET');
23
-
24
- expect(token).to.equal('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOjEyMywiaWF0IjoxMjM0NTY3ODkwfQ.18aBKSLffjbX_TLmosB_qYgW9EkWIQpBgWy7GpiKg6o');
25
- expect(decoded.iss).to.equal(123);
26
- expect(decoded.iat).to.equal(1234567890);
27
-
28
- });
29
-
30
- });
31
-
32
- });