email-notifier 14.0.2

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 (51) hide show
  1. package/.circleci/config.yml +11 -0
  2. package/.dockerignore +17 -0
  3. package/.github/ISSUE_TEMPLATE/config.yml +1 -0
  4. package/.github/ISSUE_TEMPLATE/no-issue-template.md +18 -0
  5. package/.ncurc.yaml +2 -0
  6. package/.nvmrc +1 -0
  7. package/.nycrc.yml +19 -0
  8. package/.versionrc +15 -0
  9. package/CHANGELOG.md +68 -0
  10. package/CODEOWNERS +5 -0
  11. package/Dockerfile +44 -0
  12. package/LICENSE.md +10 -0
  13. package/README.md +135 -0
  14. package/app.js +34 -0
  15. package/audit-ci.jsonc +9 -0
  16. package/config/custom-environment-variables.json +17 -0
  17. package/config/default.json +96 -0
  18. package/istanbul.yml +14 -0
  19. package/onboarding.md +182 -0
  20. package/package.json +114 -0
  21. package/src/lib/config.js +3 -0
  22. package/src/lib/enum.js +149 -0
  23. package/src/lib/healthCheck/subServiceHealth.js +83 -0
  24. package/src/lib/kafka/consumer.js +200 -0
  25. package/src/lib/kafka/index.js +43 -0
  26. package/src/lib/kafka/producer.js- +136 -0
  27. package/src/lib/utility.js +251 -0
  28. package/src/nodeMailer/sendMail.js +101 -0
  29. package/src/observables/actions.js +91 -0
  30. package/src/observables/index.js +30 -0
  31. package/src/setup.js +81 -0
  32. package/templates/en/adjustment/dfspEmail.mustache +7 -0
  33. package/templates/en/adjustment/hubEmail.mustache +4 -0
  34. package/templates/en/breach/dfspEmail.mustache +11 -0
  35. package/templates/en/breach/hubEmail.mustache +10 -0
  36. package/templates/en/position/dfspEmail.mustache +6 -0
  37. package/templates/en/position/hubEmail.mustache +5 -0
  38. package/templates/index.js +36 -0
  39. package/test/messageSubmitter.js +21 -0
  40. package/test/unit/hanlder.test--.js +944 -0
  41. package/test/unit/lib/healthCheck/subServiceHealth.test.js +110 -0
  42. package/test/unit/lib/kafka/consumer.test.js +365 -0
  43. package/test/unit/lib/kafka/producer.test--.js +323 -0
  44. package/test/unit/lib/utility.test.js +152 -0
  45. package/test/unit/nodeMailer/sendMail.test.js +239 -0
  46. package/test/unit/observables/action.test.js +315 -0
  47. package/test/unit/setup.mmtest.js +151 -0
  48. package/test/unit/setup.test.js +123 -0
  49. package/test/unit/templates/empty.mustache +0 -0
  50. package/test/unit/templates/index.test.js +97 -0
  51. package/test/unit/templates/ok.template +1 -0
@@ -0,0 +1,323 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2017 Bill & Melinda Gates Foundation
5
+ The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10
+
11
+ Contributors
12
+ --------------
13
+ This is the official list of the Mojaloop project contributors for this file.
14
+ Names of the original copyright holders (individuals or organizations)
15
+ should be listed with a '*' in the first column. People who have
16
+ contributed from an organization can be listed under the organization
17
+ that actually holds the copyright for their contributions (see the
18
+ Gates Foundation organization for an example). Those individuals should have
19
+ their names indented and be marked with a '-'. Email address can be added
20
+ optionally within square brackets <email>.
21
+
22
+ * Gates Foundation
23
+ - Name Surname <name.surname@gatesfoundation.com>
24
+
25
+ * Rajiv Mothilal <rajiv.mothilal@modusbox.com>
26
+ * Miguel de Barros <miguel.debarros@modusbox.com>
27
+
28
+ --------------
29
+ ******/
30
+ 'use strict'
31
+
32
+ const src = '../../../../src/'
33
+ const Sinon = require('sinon')
34
+ const rewire = require('rewire')
35
+ const Test = require('tapes')(require('tape'))
36
+ const KafkaProducer = require('@mojaloop/central-services-stream').Kafka.Producer
37
+ const Producer = require(`${src}/lib/kafka/producer`)
38
+ const P = require('bluebird')
39
+ const Uuid = require('uuid4')
40
+
41
+ const transfer = {
42
+ transferId: 'b51ec534-ee48-4575-b6a9-ead2955b8999',
43
+ payerFsp: 'dfsp1',
44
+ payeeFsp: 'dfsp2',
45
+ amount: {
46
+ currency: 'USD',
47
+ amount: '433.88'
48
+ },
49
+ ilpPacket: 'AYIBgQAAAAAAAASwNGxldmVsb25lLmRmc3AxLm1lci45T2RTOF81MDdqUUZERmZlakgyOVc4bXFmNEpLMHlGTFGCAUBQU0svMS4wCk5vbmNlOiB1SXlweUYzY3pYSXBFdzVVc05TYWh3CkVuY3J5cHRpb246IG5vbmUKUGF5bWVudC1JZDogMTMyMzZhM2ItOGZhOC00MTYzLTg0NDctNGMzZWQzZGE5OGE3CgpDb250ZW50LUxlbmd0aDogMTM1CkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24vanNvbgpTZW5kZXItSWRlbnRpZmllcjogOTI4MDYzOTEKCiJ7XCJmZWVcIjowLFwidHJhbnNmZXJDb2RlXCI6XCJpbnZvaWNlXCIsXCJkZWJpdE5hbWVcIjpcImFsaWNlIGNvb3BlclwiLFwiY3JlZGl0TmFtZVwiOlwibWVyIGNoYW50XCIsXCJkZWJpdElkZW50aWZpZXJcIjpcIjkyODA2MzkxXCJ9IgA',
50
+ condition: 'YlK5TZyhflbXaDRPtR5zhCu8FrbgvrQwwmzuH0iQ0AI',
51
+ expiration: '2016-05-24T08:38:08.699-04:00',
52
+ extensionList: {
53
+ extension: [
54
+ {
55
+ key: 'key1',
56
+ value: 'value1'
57
+ },
58
+ {
59
+ key: 'key2',
60
+ value: 'value2'
61
+ }
62
+ ]
63
+ }
64
+ }
65
+
66
+ const messageProtocol = {
67
+ id: transfer.transferId,
68
+ from: transfer.payerFsp,
69
+ to: transfer.payeeFsp,
70
+ type: 'application/json',
71
+ content: {
72
+ header: '',
73
+ payload: transfer
74
+ },
75
+ metadata: {
76
+ event: {
77
+ id: Uuid(),
78
+ type: 'prepare',
79
+ action: 'prepare',
80
+ createdAt: new Date(),
81
+ state: {
82
+ status: 'success',
83
+ code: 0
84
+ }
85
+ }
86
+ },
87
+ pp: ''
88
+ }
89
+
90
+ const topicConf = {
91
+ topicName: 'topic-dfsp1-transfer-prepare',
92
+ key: 'producerTest',
93
+ partition: 0,
94
+ opaqueKey: 0
95
+ }
96
+
97
+ Test('Producer', producerTest => {
98
+ let sandbox
99
+ const config = {}
100
+
101
+ producerTest.test('produceMessage should', produceMessageTest => {
102
+ produceMessageTest.beforeEach(t => {
103
+ sandbox = Sinon.createSandbox()
104
+ sandbox.stub(KafkaProducer.prototype, 'constructor').returns(P.resolve())
105
+ sandbox.stub(KafkaProducer.prototype, 'connect').returns(P.resolve())
106
+ sandbox.stub(KafkaProducer.prototype, 'sendMessage').returns(P.resolve())
107
+ sandbox.stub(KafkaProducer.prototype, 'disconnect').returns(P.resolve())
108
+ t.end()
109
+ })
110
+
111
+ produceMessageTest.afterEach(t => {
112
+ sandbox.restore()
113
+ t.end()
114
+ })
115
+
116
+ produceMessageTest.test('return true', async test => {
117
+ const result = await Producer.produceMessage(messageProtocol, topicConf, config)
118
+ test.equal(result, true)
119
+ await Producer.disconnect(topicConf.topicName)
120
+ test.end()
121
+ })
122
+
123
+ produceMessageTest.test('disconnect specific topic correctly', async test => {
124
+ try {
125
+ topicConf.topicName = 'someTopic'
126
+ await Producer.produceMessage(messageProtocol, topicConf, config)
127
+ await Producer.disconnect(topicConf.topicName)
128
+ test.pass('Disconnect specific topic successfully')
129
+ test.end()
130
+ } catch (e) {
131
+ test.fail('Error thrown')
132
+ test.end()
133
+ }
134
+ })
135
+
136
+ produceMessageTest.test('disconnect all topics correctly', async test => {
137
+ try {
138
+ topicConf.topicName = 'someTopic1'
139
+ await Producer.produceMessage(messageProtocol, topicConf, config)
140
+ topicConf.topicName = 'someTopic2'
141
+ await Producer.produceMessage(messageProtocol, topicConf, config)
142
+ await Producer.disconnect()
143
+ test.pass('Disconnected all topics successfully')
144
+ test.end()
145
+ } catch (e) {
146
+ test.fail('Error thrown')
147
+ test.end()
148
+ }
149
+ })
150
+
151
+ produceMessageTest.end()
152
+ })
153
+
154
+ producerTest.test('getProducer should', getProducerTest => {
155
+ getProducerTest.beforeEach(t => {
156
+ sandbox = Sinon.createSandbox()
157
+ sandbox.stub(KafkaProducer.prototype, 'constructor').returns(P.resolve())
158
+ sandbox.stub(KafkaProducer.prototype, 'connect').returns(P.resolve())
159
+ sandbox.stub(KafkaProducer.prototype, 'sendMessage').returns(P.resolve())
160
+ sandbox.stub(KafkaProducer.prototype, 'disconnect').returns(P.resolve())
161
+ t.end()
162
+ })
163
+
164
+ getProducerTest.afterEach(t => {
165
+ sandbox.restore()
166
+ t.end()
167
+ })
168
+
169
+ getProducerTest.test('fetch a specific Producers', async test => {
170
+ await Producer.produceMessage({}, { topicName: 'test' }, {})
171
+ test.ok(Producer.getProducer('test'))
172
+ test.end()
173
+ })
174
+
175
+ getProducerTest.test('throw an exception for a specific Producers not found', async test => {
176
+ try {
177
+ test.ok(Producer.getProducer('undefined'))
178
+ test.fail('Error not thrown!')
179
+ } catch (e) {
180
+ test.ok(e.toString() === 'Error: No producer found for topic undefined')
181
+ }
182
+ test.end()
183
+ })
184
+
185
+ getProducerTest.end()
186
+ })
187
+
188
+ producerTest.test('disconnect should', disconnectTest => {
189
+ disconnectTest.beforeEach(t => {
190
+ sandbox = Sinon.createSandbox()
191
+ sandbox.stub(KafkaProducer.prototype, 'constructor').returns(P.resolve())
192
+ sandbox.stub(KafkaProducer.prototype, 'connect').returns(P.resolve())
193
+ sandbox.stub(KafkaProducer.prototype, 'sendMessage').returns(P.resolve())
194
+ sandbox.stub(KafkaProducer.prototype, 'disconnect').returns(P.resolve())
195
+ t.end()
196
+ })
197
+
198
+ disconnectTest.afterEach(t => {
199
+ sandbox.restore()
200
+ t.end()
201
+ })
202
+ disconnectTest.test('disconnect from kafka', async test => {
203
+ await Producer.produceMessage({}, { topicName: 'test' }, {})
204
+ test.ok(Producer.disconnect('test'))
205
+ test.end()
206
+ })
207
+
208
+ disconnectTest.test('disconnect specific topic correctly', async test => {
209
+ try {
210
+ const topicName = 'someTopic'
211
+ test.ok(await Producer.produceMessage({}, { topicName }, {}))
212
+ await Producer.disconnect(topicName)
213
+ test.pass('Disconnect specific topic successfully')
214
+ test.end()
215
+ } catch (e) {
216
+ test.fail('Error thrown')
217
+ test.end()
218
+ }
219
+ })
220
+
221
+ disconnectTest.test('disconnect all topics correctly', async test => {
222
+ try {
223
+ let topicName = 'someTopic1'
224
+ test.ok(await Producer.produceMessage({}, { topicName }, {}))
225
+ await Producer.disconnect(topicName)
226
+ topicName = 'someTopic2'
227
+ test.ok(await Producer.produceMessage({}, { topicName }, {}))
228
+ await Producer.disconnect()
229
+ test.pass('Disconnected all topics successfully')
230
+ test.end()
231
+ } catch (e) {
232
+ test.fail('Error thrown')
233
+ test.end()
234
+ }
235
+ })
236
+
237
+ disconnectTest.test('throw error if failure to disconnect from kafka when disconnecting all Producers', async test => {
238
+ // setup stubs for getProducer method
239
+ const topicNameSuccess = 'topic1'
240
+ const topicNameFailure = 'topic2'
241
+ const getProducerStub = sandbox.stub()
242
+ getProducerStub.returns(new KafkaProducer({}))
243
+ getProducerStub.withArgs(topicNameFailure).throws(`No producer found for topic ${topicNameFailure}`)
244
+
245
+ // lets rewire the producer import
246
+ const KafkaProducerProxy = rewire(`${src}/lib/kafka/producer`)
247
+
248
+ // lets override the getProducer method within the import
249
+ KafkaProducerProxy.__set__('getProducer', getProducerStub)
250
+ try {
251
+ await KafkaProducerProxy.produceMessage({}, { topicName: topicNameSuccess }, {})
252
+ await KafkaProducerProxy.produceMessage({}, { topicName: topicNameFailure }, {})
253
+
254
+ await KafkaProducerProxy.disconnect()
255
+
256
+ test.fail()
257
+ test.end()
258
+ } catch (e) {
259
+ test.ok(e instanceof Error)
260
+ test.ok(e.toString() === `Error: The following Producers could not be disconnected: [{"topic":"${topicNameFailure}","error":"No producer found for topic ${topicNameFailure}"}]`)
261
+ test.end()
262
+ }
263
+ getProducerStub.restore()
264
+ })
265
+
266
+ disconnectTest.test('throw error if failure to disconnect from kafka if topic does not exist', async test => {
267
+ try {
268
+ const topicName = 'someTopic'
269
+ await Producer.produceMessage({}, { topicName }, {})
270
+ await Producer.disconnect('undefined')
271
+ } catch (e) {
272
+ test.ok(e instanceof Error)
273
+ test.end()
274
+ }
275
+ })
276
+
277
+ disconnectTest.test('throw error when a non-string value is passed into disconnect', async (test) => {
278
+ try {
279
+ const badTopicName = {}
280
+ await Producer.disconnect(badTopicName)
281
+ test.fail('Error not thrown')
282
+ test.end()
283
+ } catch (e) {
284
+ test.pass('Error Thrown')
285
+ test.end()
286
+ }
287
+ })
288
+
289
+ disconnectTest.end()
290
+ })
291
+
292
+ producerTest.test('produceMessage failure should', produceMessageTest => {
293
+ produceMessageTest.beforeEach(t => {
294
+ sandbox = Sinon.createSandbox()
295
+ sandbox.stub(KafkaProducer.prototype, 'constructor').returns(P.resolve())
296
+ sandbox.stub(KafkaProducer.prototype, 'connect').throws(new Error())
297
+ sandbox.stub(KafkaProducer.prototype, 'sendMessage').returns(P.resolve())
298
+ sandbox.stub(KafkaProducer.prototype, 'disconnect').throws(new Error())
299
+ t.end()
300
+ })
301
+
302
+ produceMessageTest.afterEach(t => {
303
+ sandbox.restore()
304
+ t.end()
305
+ })
306
+
307
+ produceMessageTest.test('throw error when connect throws error', async test => {
308
+ try {
309
+ topicConf.topicName = 'invalidTopic'
310
+ await Producer.produceMessage(messageProtocol, topicConf, config)
311
+ test.fail('Error not thrown')
312
+ test.end()
313
+ } catch (e) {
314
+ test.pass('Error thrown')
315
+ test.end()
316
+ }
317
+ })
318
+
319
+ produceMessageTest.end()
320
+ })
321
+
322
+ producerTest.end()
323
+ })
@@ -0,0 +1,152 @@
1
+ 'use strict'
2
+
3
+ const Sinon = require('sinon')
4
+ const Test = require('tapes')(require('tape'))
5
+ // const P = require('bluebird')
6
+ const Uuid = require('uuid4')
7
+ // const Logger = require('@mojaloop/central-services-logger')
8
+ // const KafkaProducer = require('@mojaloop/central-services-stream').Kafka.Producer
9
+ // const Proxyquire = require('proxyquire')
10
+ const Utility = require('../../../src/lib/utility')
11
+
12
+ // let participantName
13
+ const TRANSFER = 'transfer'
14
+ const PREPARE = 'prepare'
15
+ // const FULFIL = 'fulfil'
16
+ const NOTIFICATION = 'notification'
17
+ const EVENT = 'event'
18
+ // const COMMIT = 'commit'
19
+ const CONSUMER = 'CONSUMER'
20
+
21
+ // const participantTopic = 'topic-testParticipant-transfer-prepare'
22
+ // const generalTopic = 'topic-transfer-fulfil'
23
+
24
+ const transfer = {
25
+ transferId: 'b51ec534-ee48-4575-b6a9-ead2955b8999',
26
+ payerFsp: 'dfsp1',
27
+ payeeFsp: 'dfsp2',
28
+ amount: {
29
+ currency: 'USD',
30
+ amount: '433.88'
31
+ },
32
+ ilpPacket: 'AYIBgQAAAAAAAASwNGxldmVsb25lLmRmc3AxLm1lci45T2RTOF81MDdqUUZERmZlakgyOVc4bXFmNEpLMHlGTFGCAUBQU0svMS4wCk5vbmNlOiB1SXlweUYzY3pYSXBFdzVVc05TYWh3CkVuY3J5cHRpb246IG5vbmUKUGF5bWVudC1JZDogMTMyMzZhM2ItOGZhOC00MTYzLTg0NDctNGMzZWQzZGE5OGE3CgpDb250ZW50LUxlbmd0aDogMTM1CkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24vanNvbgpTZW5kZXItSWRlbnRpZmllcjogOTI4MDYzOTEKCiJ7XCJmZWVcIjowLFwidHJhbnNmZXJDb2RlXCI6XCJpbnZvaWNlXCIsXCJkZWJpdE5hbWVcIjpcImFsaWNlIGNvb3BlclwiLFwiY3JlZGl0TmFtZVwiOlwibWVyIGNoYW50XCIsXCJkZWJpdElkZW50aWZpZXJcIjpcIjkyODA2MzkxXCJ9IgA',
33
+ condition: 'YlK5TZyhflbXaDRPtR5zhCu8FrbgvrQwwmzuH0iQ0AI',
34
+ expiration: '2016-05-24T08:38:08.699-04:00',
35
+ extensionList: {
36
+ extension: [
37
+ {
38
+ key: 'key1',
39
+ value: 'value1'
40
+ },
41
+ {
42
+ key: 'key2',
43
+ value: 'value2'
44
+ }
45
+ ]
46
+ }
47
+ }
48
+
49
+ const messageProtocol = {
50
+ id: transfer.transferId,
51
+ from: transfer.payerFsp,
52
+ to: transfer.payeeFsp,
53
+ type: 'application/json',
54
+ content: {
55
+ header: {},
56
+ payload: transfer
57
+ },
58
+ metadata: {
59
+ event: {
60
+ id: Uuid(),
61
+ type: 'prepare',
62
+ action: 'commit',
63
+ createdAt: new Date(),
64
+ state: {
65
+ status: 'success',
66
+ code: 0,
67
+ description: 'action successful'
68
+ }
69
+ }
70
+ },
71
+ pp: ''
72
+ }
73
+
74
+ Test('Utility Test', utilityTest => {
75
+ let sandbox
76
+
77
+ utilityTest.beforeEach(test => {
78
+ sandbox = Sinon.createSandbox()
79
+ // sandbox.stub(KafkaProducer.prototype, 'constructor').returns(P.resolve())
80
+ // sandbox.stub(KafkaProducer.prototype, 'connect').returns(P.resolve())
81
+ // sandbox.stub(KafkaProducer.prototype, 'sendMessage').returns(P.resolve())
82
+ // sandbox.stub(KafkaProducer.prototype, 'disconnect').returns(P.resolve())
83
+ // participantName = 'testParticipant'
84
+ test.end()
85
+ })
86
+
87
+ utilityTest.afterEach(test => {
88
+ sandbox.restore()
89
+ test.end()
90
+ })
91
+
92
+ utilityTest.test('updateMessageProtocolMetadata should', updateMessageProtocolMetadataTest => {
93
+ updateMessageProtocolMetadataTest.test('return an updated metadata object in the message protocol', test => {
94
+ const previousEventId = messageProtocol.metadata.event.id
95
+ const newMessageProtocol = Utility.updateMessageProtocolMetadata(messageProtocol, TRANSFER, PREPARE, Utility.ENUMS.STATE.SUCCESS)
96
+ test.equal(newMessageProtocol.metadata.event.state, Utility.ENUMS.STATE.SUCCESS)
97
+ test.equal(newMessageProtocol.metadata.event.type, TRANSFER)
98
+ test.equal(newMessageProtocol.metadata.event.action, PREPARE)
99
+ test.equal(newMessageProtocol.metadata.event.responseTo, previousEventId)
100
+ test.end()
101
+ })
102
+
103
+ updateMessageProtocolMetadataTest.test('return an updated metadata object in the message protocol if metadata is not present', test => {
104
+ const newMessageProtocol = Utility.updateMessageProtocolMetadata({}, TRANSFER, PREPARE, Utility.ENUMS.STATE.SUCCESS)
105
+ test.equal(newMessageProtocol.metadata.event.state, Utility.ENUMS.STATE.SUCCESS)
106
+ test.equal(newMessageProtocol.metadata.event.type, TRANSFER)
107
+ test.equal(newMessageProtocol.metadata.event.action, PREPARE)
108
+ test.end()
109
+ })
110
+
111
+ updateMessageProtocolMetadataTest.end()
112
+ })
113
+
114
+ utilityTest.test('getKafkaConfig should', getKafkaConfigTest => {
115
+ getKafkaConfigTest.test('return the Kafka config from the default.json', test => {
116
+ const config = Utility.getKafkaConfig(CONSUMER, NOTIFICATION.toUpperCase(), EVENT.toUpperCase())
117
+ test.ok(config.rdkafkaConf !== undefined)
118
+ test.ok(config.options !== undefined)
119
+ test.end()
120
+ })
121
+
122
+ getKafkaConfigTest.test('throw and error if Kafka config not in default.json', test => {
123
+ try {
124
+ Utility.getKafkaConfig(CONSUMER, NOTIFICATION, EVENT)
125
+ test.fail('Error not thrown')
126
+ test.end()
127
+ } catch (e) {
128
+ test.pass('Error thrown')
129
+ test.end()
130
+ }
131
+ })
132
+
133
+ getKafkaConfigTest.end()
134
+ })
135
+
136
+ utilityTest.test('createState should', createStateTest => {
137
+ createStateTest.test('create a state', async (test) => {
138
+ const state = {
139
+ status: 'status',
140
+ code: 1,
141
+ description: 'description'
142
+ }
143
+ const result = await Utility.createState(state.status, state.code, state.description)
144
+ test.deepEqual(result, state)
145
+ test.end()
146
+ })
147
+
148
+ createStateTest.end()
149
+ })
150
+
151
+ utilityTest.end()
152
+ })