@bluedot-innovation/models 7.1.0 → 7.2.0

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,3 +1,8 @@
1
+ ## 7.2.0
2
+ - Added webhook structure field with values 'standard' and 'tealium' (defaults to standard)
3
+ - Allowed multiple webhooks of the same type as long as they have different urls
4
+ - Added body fields to webhooks and allow multiple webhook headers
5
+
1
6
  ## 7.1.0
2
7
  - Added `hello.registrationTtl` to Project
3
8
 
@@ -20,11 +20,24 @@
20
20
  "securityToken" : "value"
21
21
  }
22
22
  },
23
+ {
24
+ "type": "tempo",
25
+ "url": "https://webhooklistener2.bluedotinnovation.com",
26
+ "headers" : {
27
+ "securityToken" : "value"
28
+ }
29
+ },
23
30
  {
24
31
  "type": "wave",
25
32
  "url": "https://webhooklistener.bluedotinnovation.com",
26
33
  "headers" : {
27
- "securityToken" : "value"
34
+ "securityToken" : "value",
35
+ "second-header": "second-value"
36
+ },
37
+ "structure": "standard",
38
+ "body": {
39
+ "key1": "value1",
40
+ "key2": "value2"
28
41
  }
29
42
  }
30
43
  ],
@@ -20,8 +20,8 @@ beforeEach(() => {
20
20
  project = JSON.parse(JSON.stringify(require('./project.data.json')))
21
21
  })
22
22
 
23
- testCase('Project', () => {
24
- testCase('Valid Project ', () => {
23
+ describe('Project', () => {
24
+ describe('Valid Project ', () => {
25
25
  assertions('should validate a valid project ', (done) => {
26
26
  let Project = new ProjectModel(project)
27
27
  Project.validate((err, res) => {
@@ -49,10 +49,10 @@ testCase('Project', () => {
49
49
  done()
50
50
  })
51
51
  })
52
- })
53
- testCase('Valid Project ', () => {
54
- assertions('should validate a valid project ', (done) => {
55
- let Project = new ProjectModelBuiltInMongoose(project)
52
+
53
+ it('should validate a valid project with a webhook with empty header and body object', (done) => {
54
+ project.webhooks = [{url: "https://mirror.com", type: "tempo", structure: "tealium", body: {}, headers: {}}]
55
+ let Project = new ProjectModel(project)
56
56
  Project.validate((err, res) => {
57
57
  assert.notExists(res)
58
58
  assert(err === null, 'err equals null')
@@ -60,52 +60,17 @@ testCase('Project', () => {
60
60
  })
61
61
  })
62
62
  })
63
- testCase('Invalid Project ', () => {
64
- assertions('should not validate if webhook headers have more than 1 key value pair ', (done) => {
65
-
66
- project.webhooks[0].headers.anotherKey = 'anotherValue'
67
-
68
- let Project = new ProjectModel(project)
69
- Project.validate((err, res) => {
70
- assert.exists(err)
71
- assert.match(err.message, /1 key <-> value pair/)
72
- done()
73
- })
74
- })
75
- assertions(`should not validate if webhook protocol is not valid `, (done) => {
76
- project.webhooks[0].url = 'htt://test.com'
77
- let Project = new ProjectModel(project)
78
- Project.validate((err, res) => {
79
- assert.exists(err)
80
- assert.match(err.message, /Please enter a valid URL/)
81
- done()
82
- })
83
- })
84
- assertions(`should not validate if webhook domain is not valid `, (done) => {
85
- project.webhooks[0].url = 'https://@'
86
- let Project = new ProjectModel(project)
87
- Project.validate((err, res) => {
88
- assert.exists(err)
89
- assert.match(err.message, /Please enter a valid URL/)
90
- done()
91
- })
92
- })
93
-
94
- assertions(`should not validate if one of webhooks have a type other than 'tempo', 'wave' or 'trigger'`, (done) => {
95
- project.webhooks.push({
96
- "type": "sandwich",
97
- "url": "https://webhooklistener.bluedotinnovation.com",
98
- "headers" : {
99
- "securityToken" : "value"
100
- }
101
- })
102
- let Project = new ProjectModel(project)
63
+ testCase('Valid Project ', () => {
64
+ assertions('should validate a valid project ', (done) => {
65
+ let Project = new ProjectModelBuiltInMongoose(project)
103
66
  Project.validate((err, res) => {
104
- assert.exists(err)
105
- assert.match(err.message, /`sandwich` is not a valid enum value for path `type`/)
67
+ assert.notExists(res)
68
+ assert(err === null, 'err equals null')
106
69
  done()
107
70
  })
108
71
  })
72
+ })
73
+ describe('Invalid Project ', () => {
109
74
  testCase('mparticle integration check', () => {
110
75
  assertions('should throw an error if the mparticle integration has empty key ', (done) => {
111
76
 
@@ -245,23 +210,6 @@ testCase('Project', () => {
245
210
  })
246
211
  })
247
212
 
248
- it(`should throw an error when webhooks is null`, (done) => {
249
- project = JSON.parse(JSON.stringify(project))
250
- project.webhooks = null
251
- helpers.testNullValue(project, ProjectModel, done)
252
- })
253
-
254
- it(`should throw an error when webhooks has primitive value in headers`, (done) => {
255
- project = JSON.parse(JSON.stringify(project))
256
- project.webhooks[0].headers = 'primitive'
257
- helpers.testNullValue(project, ProjectModel, done)
258
- })
259
- it(`should throw an error when webhooks has array in headers`, (done) => {
260
- project = JSON.parse(JSON.stringify(project))
261
- project.webhooks[0].headers = ['array']
262
- helpers.testNullValue(project, ProjectModel, done)
263
- })
264
-
265
213
  it(`should throw an error when firebaseApiKey is null`, (done) => {
266
214
  project = JSON.parse(JSON.stringify(project))
267
215
  project.firebaseApiKey = null
@@ -288,40 +236,127 @@ testCase('Project', () => {
288
236
  })
289
237
  })
290
238
 
291
- testCase('should not validate if webhooks contains two webhooks of the same type', () => {
292
- const webhookTypes = ['tempo', 'wave', 'trigger']
293
-
294
- webhookTypes.map(webhookType => {
295
- assertions(`should not validate if there are duplicate ${webhookType} webhooks`, (done) => {
296
- project.webhooks.push({
297
- "type": webhookType,
298
- "url": "https://webhooklistener.bluedotinnovation.com",
299
- "headers" : {
300
- "securityToken" : "value"
301
- }
302
- })
239
+ describe("webhook validation", () => {
240
+ assertions(`should not validate if webhook protocol is not valid `, (done) => {
241
+ project.webhooks[0].url = 'htt://test.com'
242
+ let Project = new ProjectModel(project)
243
+ Project.validate((err, res) => {
244
+ assert.exists(err)
245
+ assert.match(err.message, /Please enter a valid URL/)
246
+ done()
247
+ })
248
+ })
249
+ assertions(`should not validate if webhook domain is not valid `, (done) => {
250
+ project.webhooks[0].url = 'https://@'
251
+ let Project = new ProjectModel(project)
252
+ Project.validate((err, res) => {
253
+ assert.exists(err)
254
+ assert.match(err.message, /Please enter a valid URL/)
255
+ done()
256
+ })
257
+ })
303
258
 
304
- let Project = new ProjectModel(project)
305
- Project.validate((err, res) => {
306
- assert.exists(err)
307
- assert.match(err.message, /Duplicate webhooks found. Only have one webhook of each type is allowed/)
308
- done()
309
- })
259
+ assertions(`should not validate if one of webhooks have a type other than 'tempo', 'wave' or 'trigger'`, (done) => {
260
+ project.webhooks.push({
261
+ "type": "sandwich",
262
+ "url": "https://webhooklistener.bluedotinnovation.com",
263
+ "headers" : {
264
+ "securityToken" : "value"
265
+ }
266
+ })
267
+ let Project = new ProjectModel(project)
268
+ Project.validate((err, res) => {
269
+ assert.exists(err)
270
+ assert.match(err.message, /`sandwich` is not a valid enum value for path `type`/)
271
+ done()
310
272
  })
311
273
  })
312
- })
313
274
 
275
+ it(`should throw an error when webhooks is null`, (done) => {
276
+ project = JSON.parse(JSON.stringify(project))
277
+ project.webhooks = null
278
+ helpers.testNullValue(project, ProjectModel, done)
279
+ })
280
+
281
+ it(`should throw an error when webhooks has primitive value in headers`, (done) => {
282
+ project = JSON.parse(JSON.stringify(project))
283
+ project.webhooks[0].headers = 'primitive'
284
+ helpers.testNullValue(project, ProjectModel, done)
285
+ })
286
+ it(`should throw an error when webhooks has array in headers`, (done) => {
287
+ project = JSON.parse(JSON.stringify(project))
288
+ project.webhooks[0].headers = ['array']
289
+ helpers.testNullValue(project, ProjectModel, done)
290
+ })
314
291
 
315
- describe('should throw an error when any of the values of the array contains a falsy value', () => {
316
- let arrayTypeFields = rules.arrayTypeFields
292
+ it(`should throw an error when webhooks has a number as a value in headers`, (done) => {
293
+ project = JSON.parse(JSON.stringify(project))
294
+ project.webhooks[0].headers["newValue"] = 420
295
+ let Project = new ProjectModel(project)
296
+ Project.validate((err, res) => {
297
+ assert.exists(err)
298
+ assert.match(err.message, /webhook header keys and values must be strings/)
299
+ done()
300
+ })
301
+ })
317
302
 
318
- arrayTypeFields.map(arrayField => {
319
- FALSY_VALUES.map(falsyValue => {
320
- it(`should throw an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
321
- helpers.testFalsyValue(falsyValue, arrayField, project, ProjectModel, done)
303
+ it(`should throw an error when webhooks has a number as a value in body`, (done) => {
304
+ project = JSON.parse(JSON.stringify(project))
305
+ project.webhooks[0].body = {"newValue": 420}
306
+ let Project = new ProjectModel(project)
307
+ Project.validate((err, res) => {
308
+ assert.exists(err)
309
+ assert.match(err.message, /webhook body keys and values must be strings/)
310
+ done()
311
+ })
312
+ })
313
+
314
+ it(`should throw an error if a structure is used that is not tealium or standard`, (done) => {
315
+ project = JSON.parse(JSON.stringify(project))
316
+ project.webhooks[0].structure = "different"
317
+ let Project = new ProjectModel(project)
318
+ Project.validate((err, res) => {
319
+ assert.exists(err)
320
+ assert.match(err.message, /is not a valid enum value for path `structure`/)
321
+ done()
322
+ })
323
+ })
324
+
325
+ it('should not validate if webhooks contains a webhook of the same type and url but different structure', () => {
326
+ const webhookTypes = ['tempo', 'wave', 'trigger']
327
+
328
+ webhookTypes.map(webhookType => {
329
+ assertions(`should not validate if there are duplicate ${webhookType} webhooks`, (done) => {
330
+ project.webhooks.push({
331
+ "type": webhookType,
332
+ "url": "https://webhooklistener.bluedotinnovation.com",
333
+ "headers" : {
334
+ "securityToken" : "value"
335
+ },
336
+ "structure": "tealium"
337
+ })
338
+
339
+ let Project = new ProjectModel(project)
340
+ Project.validate((err, res) => {
341
+ assert.exists(err)
342
+ assert.match(err.message, /Duplicate webhooks found. Only have one webhook of each type is allowed/)
343
+ done()
344
+ })
322
345
  })
323
346
  })
324
347
  })
325
348
  })
326
349
  })
350
+
351
+ describe('should throw an error when any of the values of the array contains a falsy value', () => {
352
+ let arrayTypeFields = rules.arrayTypeFields
353
+
354
+ arrayTypeFields.map(arrayField => {
355
+ FALSY_VALUES.map(falsyValue => {
356
+ it(`should throw an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
357
+ helpers.testFalsyValue(falsyValue, arrayField, project, ProjectModel, done)
358
+ })
359
+ })
360
+ })
361
+ })
327
362
  })
package/helpers.js CHANGED
@@ -266,19 +266,27 @@ helpers.validateDestinationIdGeofeatureType = function(destinationId) {
266
266
  return true
267
267
  }
268
268
 
269
- helpers.validateNoDuplicateWebhookTypes = function(webhookArray) {
270
- const webhookCounts = {}
271
- let duplicateFound = false
269
+ helpers.validateNoDuplicateWebhooks = function(webhookArray) {
270
+ const webhooksByType = {}
272
271
  webhookArray.forEach(webhook => {
273
- if(webhookCounts[webhook.type] === undefined){
274
- webhookCounts[webhook.type] = 0
272
+ if(webhooksByType[webhook.type] === undefined){
273
+ webhooksByType[webhook.type] = [webhook]
274
+ }else{
275
+ webhooksByType[webhook.type].push(webhook)
275
276
  }
276
- if(webhookCounts[webhook.type] > 0){
277
- duplicateFound = true
278
- }
279
- webhookCounts[webhook.type]++
280
277
  })
281
- return !duplicateFound;
278
+
279
+ for(const sameTypeWebhookArray of Object.values(webhooksByType)){
280
+ const urlCounts = {}
281
+ for(const webhook of sameTypeWebhookArray){
282
+ if(urlCounts[webhook.url] === undefined){
283
+ urlCounts[webhook.url] = [webhook];
284
+ }else{
285
+ return false;
286
+ }
287
+ }
288
+ }
289
+ return true;
282
290
  }
283
291
 
284
292
 
package/models/Project.js CHANGED
@@ -20,7 +20,7 @@ let projectHelloSchema = require('../schemas/ProjectHello')
20
20
 
21
21
  let webhooksValidators = [
22
22
  {validator: helpers.validateNotNull},
23
- {validator: helpers.validateNoDuplicateWebhookTypes, msg: "Duplicate webhooks found. Only have one webhook of each type is allowed"}
23
+ {validator: helpers.validateNoDuplicateWebhooks, msg: "Duplicate webhooks found with the same type and url as another. Only one is allowed"}
24
24
  ];
25
25
 
26
26
  let ProjectSchema = new mongoose.Schema({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "main": "Models.js",
5
5
  "scripts": {
6
6
  "lint": "./node_modules/.bin/eslint -c .eslintrc.js ./models/*.js ./__tests__/**/*.js ./__tests__/**/**/*.js ./schemas/*.js",
@@ -4,25 +4,11 @@ let validators = require('../utils/validators')
4
4
  let WebhookSchema = new mongoose.Schema({
5
5
  type: {type: String, enum: ['tempo', 'wave', 'trigger'], required: [true, 'webhook type is required']},
6
6
  url: {type: String, required: [true, 'URL is required'], validate: {validator: validators.validateUrl, msg: 'Please enter a valid URL'}},
7
- headers: {type: mongoose.Schema.Types.Mixed, validate: { validator: validateHeaderlength, msg: 'Header object can only have 1 key <-> value pair' }}
7
+ headers: {type: mongoose.Schema.Types.Mixed, validate: { validator: validators.validateStringKeyValuePairsOnly, msg: 'webhook header keys and values must be strings' }},
8
+ body: {type: mongoose.Schema.Types.Mixed, validate: { validator: validators.validateStringKeyValuePairsOnly, msg: 'webhook body keys and values must be strings' }},
9
+ structure: {type: String, enum: ['standard', 'tealium'], default: 'standard'}
8
10
  }, {
9
11
  _id: false
10
12
  })
11
13
 
12
- // https://bluedotinnovation.atlassian.net/browse/BP-1500 -- Limiting headers to 1
13
- function validateHeaderlength (headers) {
14
- // handles the case of headers being a primitive value
15
- if(typeof headers !== 'object' || Array.isArray(headers)){
16
- return false
17
- }
18
- const headerKeys = Object.keys(headers)
19
- // only one key-value pair is allowed
20
- if(headerKeys.length > 1){
21
- return false
22
- }
23
- const key = headerKeys[0]
24
- // the value should be present
25
- return headers[key]
26
- }
27
-
28
14
  module.exports = WebhookSchema
@@ -1,4 +1,18 @@
1
+ const {val} = require("mongoose/lib/helpers/populate/SkipPopulateValue");
1
2
  const URL_REGEX = new RegExp("^(https?|ftp)://[^\\s/$.?#].[^\\s]*$", "i")
2
3
  exports.validateUrl = function(url) {
3
4
  return URL_REGEX.test(url)
5
+ }
6
+
7
+ exports.validateStringKeyValuePairsOnly = (object) => {
8
+ if(typeof object !== "object"){
9
+ return false;
10
+ }
11
+ for(const key of Object.getOwnPropertyNames(object)){
12
+ if(typeof key !== "string" || typeof object[key] !== "string"){
13
+ return false
14
+ }
15
+ }
16
+
17
+ return true
4
18
  }