@bluedot-innovation/models 7.2.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 +4 -0
- package/__tests__/project/project.spec.js +84 -1
- package/package.json +1 -1
- package/schemas/Webhook.js +16 -3
- package/utils/validators.js +14 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
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
|
+
|
|
1
5
|
## 7.2.0
|
|
2
6
|
- Added webhook structure field with values 'standard' and 'tealium' (defaults to standard)
|
|
3
7
|
- Allowed multiple webhooks of the same type as long as they have different urls
|
|
@@ -51,7 +51,26 @@ describe('Project', () => {
|
|
|
51
51
|
})
|
|
52
52
|
|
|
53
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: "
|
|
54
|
+
project.webhooks = [{url: "https://mirror.com", type: "tempo", structure: "standard", body: {}, headers: {}}]
|
|
55
|
+
let Project = new ProjectModel(project)
|
|
56
|
+
Project.validate((err, res) => {
|
|
57
|
+
assert.notExists(res)
|
|
58
|
+
assert(err === null, 'err equals null')
|
|
59
|
+
done()
|
|
60
|
+
})
|
|
61
|
+
})
|
|
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
|
+
}]
|
|
55
74
|
let Project = new ProjectModel(project)
|
|
56
75
|
Project.validate((err, res) => {
|
|
57
76
|
assert.notExists(res)
|
|
@@ -322,6 +341,70 @@ describe('Project', () => {
|
|
|
322
341
|
})
|
|
323
342
|
})
|
|
324
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
|
+
|
|
325
408
|
it('should not validate if webhooks contains a webhook of the same type and url but different structure', () => {
|
|
326
409
|
const webhookTypes = ['tempo', 'wave', 'trigger']
|
|
327
410
|
|
package/package.json
CHANGED
package/schemas/Webhook.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
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: validators.
|
|
8
|
-
body: {type: mongoose.Schema.Types.Mixed, validate: { validator: validators.
|
|
9
|
-
structure: {type: String, enum: ['standard', 'tealium'], default: 'standard'}
|
|
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'}}
|
|
10
23
|
}, {
|
|
11
24
|
_id: false
|
|
12
25
|
})
|
package/utils/validators.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.validateUrl = function(url) {
|
|
|
4
4
|
return URL_REGEX.test(url)
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
exports.
|
|
7
|
+
exports.validateNonEmptyStringKeyValuePairsOnly = (object) => {
|
|
8
8
|
if(typeof object !== "object"){
|
|
9
9
|
return false;
|
|
10
10
|
}
|
|
@@ -12,6 +12,19 @@ exports.validateStringKeyValuePairsOnly = (object) => {
|
|
|
12
12
|
if(typeof key !== "string" || typeof object[key] !== "string"){
|
|
13
13
|
return false
|
|
14
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
|
+
}
|
|
15
28
|
}
|
|
16
29
|
|
|
17
30
|
return true
|