@bluedot-innovation/models 7.1.0 → 7.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,3 +1,12 @@
1
+ ## 7.2.1
2
+ - Added validation for required fields for tealium webhooks
3
+ - Added validation that webhook headers and body keys cannot contain spaces
4
+
5
+ ## 7.2.0
6
+ - Added webhook structure field with values 'standard' and 'tealium' (defaults to standard)
7
+ - Allowed multiple webhooks of the same type as long as they have different urls
8
+ - Added body fields to webhooks and allow multiple webhook headers
9
+
1
10
  ## 7.1.0
2
11
  - Added `hello.registrationTtl` to Project
3
12
 
@@ -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,63 +49,47 @@ 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: "standard", 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')
59
59
  done()
60
60
  })
61
61
  })
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
62
 
63
+ it('should validate a valid project with a tealium webhook with required tealium body fields', (done) => {
64
+ project.webhooks = [{
65
+ url: "https://mirror.com",
66
+ type: "tempo",
67
+ structure: "tealium",
68
+ body: {
69
+ "tealium_account": "test-value",
70
+ "tealium_profile": "test-value-2"
71
+ },
72
+ headers: {}
73
+ }]
68
74
  let Project = new ProjectModel(project)
69
75
  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/)
76
+ assert.notExists(res)
77
+ assert(err === null, 'err equals null')
90
78
  done()
91
79
  })
92
80
  })
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)
81
+ })
82
+ testCase('Valid Project ', () => {
83
+ assertions('should validate a valid project ', (done) => {
84
+ let Project = new ProjectModelBuiltInMongoose(project)
103
85
  Project.validate((err, res) => {
104
- assert.exists(err)
105
- assert.match(err.message, /`sandwich` is not a valid enum value for path `type`/)
86
+ assert.notExists(res)
87
+ assert(err === null, 'err equals null')
106
88
  done()
107
89
  })
108
90
  })
91
+ })
92
+ describe('Invalid Project ', () => {
109
93
  testCase('mparticle integration check', () => {
110
94
  assertions('should throw an error if the mparticle integration has empty key ', (done) => {
111
95
 
@@ -245,23 +229,6 @@ testCase('Project', () => {
245
229
  })
246
230
  })
247
231
 
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
232
  it(`should throw an error when firebaseApiKey is null`, (done) => {
266
233
  project = JSON.parse(JSON.stringify(project))
267
234
  project.firebaseApiKey = null
@@ -288,40 +255,191 @@ testCase('Project', () => {
288
255
  })
289
256
  })
290
257
 
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
- })
258
+ describe("webhook validation", () => {
259
+ assertions(`should not validate if webhook protocol is not valid `, (done) => {
260
+ project.webhooks[0].url = 'htt://test.com'
261
+ let Project = new ProjectModel(project)
262
+ Project.validate((err, res) => {
263
+ assert.exists(err)
264
+ assert.match(err.message, /Please enter a valid URL/)
265
+ done()
266
+ })
267
+ })
268
+ assertions(`should not validate if webhook domain is not valid `, (done) => {
269
+ project.webhooks[0].url = 'https://@'
270
+ let Project = new ProjectModel(project)
271
+ Project.validate((err, res) => {
272
+ assert.exists(err)
273
+ assert.match(err.message, /Please enter a valid URL/)
274
+ done()
275
+ })
276
+ })
303
277
 
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
- })
278
+ assertions(`should not validate if one of webhooks have a type other than 'tempo', 'wave' or 'trigger'`, (done) => {
279
+ project.webhooks.push({
280
+ "type": "sandwich",
281
+ "url": "https://webhooklistener.bluedotinnovation.com",
282
+ "headers" : {
283
+ "securityToken" : "value"
284
+ }
285
+ })
286
+ let Project = new ProjectModel(project)
287
+ Project.validate((err, res) => {
288
+ assert.exists(err)
289
+ assert.match(err.message, /`sandwich` is not a valid enum value for path `type`/)
290
+ done()
310
291
  })
311
292
  })
312
- })
313
293
 
294
+ it(`should throw an error when webhooks is null`, (done) => {
295
+ project = JSON.parse(JSON.stringify(project))
296
+ project.webhooks = null
297
+ helpers.testNullValue(project, ProjectModel, done)
298
+ })
314
299
 
315
- describe('should throw an error when any of the values of the array contains a falsy value', () => {
316
- let arrayTypeFields = rules.arrayTypeFields
300
+ it(`should throw an error when webhooks has primitive value in headers`, (done) => {
301
+ project = JSON.parse(JSON.stringify(project))
302
+ project.webhooks[0].headers = 'primitive'
303
+ helpers.testNullValue(project, ProjectModel, done)
304
+ })
305
+ it(`should throw an error when webhooks has array in headers`, (done) => {
306
+ project = JSON.parse(JSON.stringify(project))
307
+ project.webhooks[0].headers = ['array']
308
+ helpers.testNullValue(project, ProjectModel, done)
309
+ })
317
310
 
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)
311
+ it(`should throw an error when webhooks has a number as a value in headers`, (done) => {
312
+ project = JSON.parse(JSON.stringify(project))
313
+ project.webhooks[0].headers["newValue"] = 420
314
+ let Project = new ProjectModel(project)
315
+ Project.validate((err, res) => {
316
+ assert.exists(err)
317
+ assert.match(err.message, /webhook header keys and values must be strings/)
318
+ done()
319
+ })
320
+ })
321
+
322
+ it(`should throw an error when webhooks has a number as a value in body`, (done) => {
323
+ project = JSON.parse(JSON.stringify(project))
324
+ project.webhooks[0].body = {"newValue": 420}
325
+ let Project = new ProjectModel(project)
326
+ Project.validate((err, res) => {
327
+ assert.exists(err)
328
+ assert.match(err.message, /webhook body keys and values must be strings/)
329
+ done()
330
+ })
331
+ })
332
+
333
+ it(`should throw an error if a structure is used that is not tealium or standard`, (done) => {
334
+ project = JSON.parse(JSON.stringify(project))
335
+ project.webhooks[0].structure = "different"
336
+ let Project = new ProjectModel(project)
337
+ Project.validate((err, res) => {
338
+ assert.exists(err)
339
+ assert.match(err.message, /is not a valid enum value for path `structure`/)
340
+ done()
341
+ })
342
+ })
343
+
344
+ it(`should throw an error if structure is tealium and tealium_account is not a body field`, (done) => {
345
+ project.webhooks[0].structure = "tealium"
346
+ project.webhooks[0].body = {"tealium_profile": "test-value"}
347
+ let Project = new ProjectModel(project)
348
+ Project.validate((err, res) => {
349
+ assert.exists(err)
350
+ assert.match(err.message, /tealium webhooks require "tealium_account" and "tealium_profile" body fields to be present/)
351
+ done()
352
+ })
353
+ })
354
+
355
+ it(`should throw an error if structure is tealium and tealium_profile is not a body field`, (done) => {
356
+ project.webhooks[0].structure = "tealium"
357
+ project.webhooks[0].body = {"tealium_account": "test-value"}
358
+ let Project = new ProjectModel(project)
359
+ Project.validate((err, res) => {
360
+ assert.exists(err)
361
+ assert.match(err.message, /tealium webhooks require "tealium_account" and "tealium_profile" body fields to be present/)
362
+ done()
363
+ })
364
+ })
365
+
366
+ it(`should not validate if structure is tealium and body is not present`, (done) => {
367
+ project.webhooks[0].structure = "tealium"
368
+ delete project.webhooks[0].body
369
+ let Project = new ProjectModel(project)
370
+ Project.validate((err, res) => {
371
+ assert.exists(err)
372
+ assert.match(err.message, /tealium webhooks require "tealium_account" and "tealium_profile" body fields to be present/)
373
+ done()
374
+ })
375
+ })
376
+
377
+ it(`should not validate if structure is tealium and body is an empty object`, (done) => {
378
+ project.webhooks[0].structure = "tealium"
379
+ project.webhooks[0].body = {}
380
+ let Project = new ProjectModel(project)
381
+ Project.validate((err, res) => {
382
+ assert.exists(err)
383
+ assert.match(err.message, /tealium webhooks require "tealium_account" and "tealium_profile" body fields to be present/)
384
+ done()
385
+ })
386
+ })
387
+
388
+ it("should not validate webhook if body contains keys with spaces", (done) => {
389
+ project.webhooks[0].body = { "key with spaces": "value" }
390
+ let Project = new ProjectModel(project)
391
+ Project.validate((err, res) => {
392
+ assert.exists(err)
393
+ assert.match(err.message, /webhook body keys cannot contain spaces/)
394
+ done()
395
+ })
396
+ })
397
+
398
+ it("should not validate webhook if headers contains keys with spaces", (done) => {
399
+ project.webhooks[0].headers = { "key with spaces": "value" }
400
+ let Project = new ProjectModel(project)
401
+ Project.validate((err, res) => {
402
+ assert.exists(err)
403
+ assert.match(err.message, /webhook header keys cannot contain spaces/)
404
+ done()
405
+ })
406
+ })
407
+
408
+ it('should not validate if webhooks contains a webhook of the same type and url but different structure', () => {
409
+ const webhookTypes = ['tempo', 'wave', 'trigger']
410
+
411
+ webhookTypes.map(webhookType => {
412
+ assertions(`should not validate if there are duplicate ${webhookType} webhooks`, (done) => {
413
+ project.webhooks.push({
414
+ "type": webhookType,
415
+ "url": "https://webhooklistener.bluedotinnovation.com",
416
+ "headers" : {
417
+ "securityToken" : "value"
418
+ },
419
+ "structure": "tealium"
420
+ })
421
+
422
+ let Project = new ProjectModel(project)
423
+ Project.validate((err, res) => {
424
+ assert.exists(err)
425
+ assert.match(err.message, /Duplicate webhooks found. Only have one webhook of each type is allowed/)
426
+ done()
427
+ })
322
428
  })
323
429
  })
324
430
  })
325
431
  })
326
432
  })
433
+
434
+ describe('should throw an error when any of the values of the array contains a falsy value', () => {
435
+ let arrayTypeFields = rules.arrayTypeFields
436
+
437
+ arrayTypeFields.map(arrayField => {
438
+ FALSY_VALUES.map(falsyValue => {
439
+ it(`should throw an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
440
+ helpers.testFalsyValue(falsyValue, arrayField, project, ProjectModel, done)
441
+ })
442
+ })
443
+ })
444
+ })
327
445
  })
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.1",
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",
@@ -1,28 +1,27 @@
1
1
  let mongoose = require('mongoose')
2
2
  let validators = require('../utils/validators')
3
3
 
4
+ function validateTealiumRequiredFields(structure){
5
+ if(structure === "tealium"){
6
+ const webhookBodyFields = this.body;
7
+ if(!webhookBodyFields){
8
+ return false;
9
+ }
10
+ const bodyKeys = Object.keys(webhookBodyFields)
11
+ if(!bodyKeys.includes("tealium_account") || !bodyKeys.includes("tealium_profile")){
12
+ return false;
13
+ }
14
+ }
15
+ }
16
+
4
17
  let WebhookSchema = new mongoose.Schema({
5
18
  type: {type: String, enum: ['tempo', 'wave', 'trigger'], required: [true, 'webhook type is required']},
6
19
  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' }}
20
+ headers: {type: mongoose.Schema.Types.Mixed, validate: [{ validator: validators.validateNonEmptyStringKeyValuePairsOnly, msg: 'webhook header keys and values must be strings' }, { validator: validators.validateNoSpacesInObjectKeys, msg: 'webhook header keys cannot contain spaces' }]},
21
+ body: {type: mongoose.Schema.Types.Mixed, validate: [{ validator: validators.validateNonEmptyStringKeyValuePairsOnly, msg: 'webhook body keys and values must be strings' }, { validator: validators.validateNoSpacesInObjectKeys, msg: 'webhook body keys cannot contain spaces' }]},
22
+ structure: {type: String, enum: ['standard', 'tealium'], default: 'standard', validate: {validator: validateTealiumRequiredFields, msg: 'tealium webhooks require "tealium_account" and "tealium_profile" body fields to be present'}}
8
23
  }, {
9
24
  _id: false
10
25
  })
11
26
 
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
27
  module.exports = WebhookSchema
@@ -1,4 +1,31 @@
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.validateNonEmptyStringKeyValuePairsOnly = (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
+ if(!key || !object[key]){
16
+ return false;
17
+ }
18
+ }
19
+
20
+ return true
21
+ }
22
+
23
+ exports.validateNoSpacesInObjectKeys = (object) => {
24
+ for(const key of Object.keys(object)){
25
+ if(typeof key !== "string" || key.indexOf(" ") !== -1){
26
+ return false
27
+ }
28
+ }
29
+
30
+ return true
4
31
  }