@bluedot-innovation/models 7.0.6 → 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/.gitlab-ci.yml CHANGED
@@ -62,16 +62,38 @@ create_dev_indexes:
62
62
  - ENVIRONMENT=dev MONGO_DB_URI=$DEV_MONGO_DB_URI make deploy-db-changes
63
63
 
64
64
  # Create any newly required indexes in the production dbs
65
- create_production_indexes:
65
+ create_au_production_indexes:
66
66
  stage: release
67
67
  tags:
68
68
  - infrastructure
69
69
  when: manual
70
- allow_failure: false
70
+ allow_failure: true
71
71
  only:
72
72
  - master
73
73
 
74
74
  script:
75
75
  - ENVIRONMENT=prod_au MONGO_DB_URI="$AU_MONGO_DB_URI" make deploy-db-changes
76
+
77
+ create_eu_production_indexes:
78
+ stage: release
79
+ tags:
80
+ - infrastructure
81
+ when: manual
82
+ allow_failure: true
83
+ only:
84
+ - master
85
+
86
+ script:
76
87
  - ENVIRONMENT=prod_eu MONGO_DB_URI="$EU_MONGO_DB_URI" make deploy-db-changes
88
+
89
+ create_us_production_indexes:
90
+ stage: release
91
+ tags:
92
+ - infrastructure
93
+ when: manual
94
+ allow_failure: true
95
+ only:
96
+ - master
97
+
98
+ script:
77
99
  - ENVIRONMENT=prod_us MONGO_DB_URI="$US_MONGO_DB_URI" make deploy-db-changes
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
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
+
6
+ ## 7.1.0
7
+ - Added `hello.registrationTtl` to Project
8
+
1
9
  ## 7.0.6
2
10
  - Removed tempo webhooks array for post wave migration deployment
3
11
  - Removed tempo destinationId for post wave migration deployment
@@ -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
  ],
@@ -41,5 +54,8 @@
41
54
  },
42
55
  "tempo": {
43
56
  "isochroneBracketInterval": [60, 120, 300]
57
+ },
58
+ "hello": {
59
+ "registrationTtl": 86400
44
60
  }
45
61
  }
@@ -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
@@ -278,40 +226,137 @@ testCase('Project', () => {
278
226
  })
279
227
  })
280
228
 
281
- testCase('should not validate if webhooks contains two webhooks of the same type', () => {
282
- const webhookTypes = ['tempo', 'wave', 'trigger']
283
-
284
- webhookTypes.map(webhookType => {
285
- assertions(`should not validate if there are duplicate ${webhookType} webhooks`, (done) => {
286
- project.webhooks.push({
287
- "type": webhookType,
288
- "url": "https://webhooklistener.bluedotinnovation.com",
289
- "headers" : {
290
- "securityToken" : "value"
291
- }
292
- })
229
+ it('should not validate if hello.registrationTtl is not an integer', (done) => {
230
+ project.hello.registrationTtl = 1.5
231
+ let Project = new ProjectModel(project)
232
+ Project.validate((err, res) => {
233
+ assert.exists(err)
234
+ assert.match(err.message, /hello.registrationTtl should be an integer number/)
235
+ done()
236
+ })
237
+ })
293
238
 
294
- let Project = new ProjectModel(project)
295
- Project.validate((err, res) => {
296
- assert.exists(err)
297
- assert.match(err.message, /Duplicate webhooks found. Only have one webhook of each type is allowed/)
298
- done()
299
- })
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()
300
256
  })
301
257
  })
302
- })
303
258
 
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()
272
+ })
273
+ })
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
+ })
304
280
 
305
- describe('should throw an error when any of the values of the array contains a falsy value', () => {
306
- let arrayTypeFields = rules.arrayTypeFields
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
+ })
307
291
 
308
- arrayTypeFields.map(arrayField => {
309
- FALSY_VALUES.map(falsyValue => {
310
- it(`should throw an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
311
- helpers.testFalsyValue(falsyValue, arrayField, project, ProjectModel, done)
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
+ })
302
+
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
+ })
312
345
  })
313
346
  })
314
347
  })
315
348
  })
316
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
+ })
317
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
@@ -16,10 +16,11 @@ let WebhookSchema = require('../schemas/Webhook')
16
16
  let iosConfig = require('../schemas/IosConfig')
17
17
  let androidConfig = require('../schemas/AndroidConfig')
18
18
  let projectTempoSchema = require('../schemas/ProjectTempo')
19
+ let projectHelloSchema = require('../schemas/ProjectHello')
19
20
 
20
21
  let webhooksValidators = [
21
22
  {validator: helpers.validateNotNull},
22
- {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"}
23
24
  ];
24
25
 
25
26
  let ProjectSchema = new mongoose.Schema({
@@ -48,7 +49,8 @@ let ProjectSchema = new mongoose.Schema({
48
49
  firebaseApiKey: {type: String, validate: helpers.validateNotNull},
49
50
  apiKey: {type: String},
50
51
  integrations: IntegrationSchema,
51
- tempo: projectTempoSchema
52
+ tempo: projectTempoSchema,
53
+ hello: projectHelloSchema
52
54
  },
53
55
  {collection: 'project'})
54
56
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "7.0.6",
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",
@@ -0,0 +1,9 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ let ProjectHelloSchema = new mongoose.Schema({
4
+ registrationTtl: {type: Number, validate: { validator: Number.isInteger, msg: 'hello.registrationTtl should be an integer number' }},
5
+ }, {
6
+ _id: false
7
+ })
8
+
9
+ module.exports = ProjectHelloSchema
@@ -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
  }