@bluedot-innovation/models 7.5.3 → 7.5.4
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 +3 -0
- package/__tests__/project/project.spec.js +92 -0
- package/package.json +1 -1
- package/schemas/Webhook.js +30 -2
package/CHANGELOG.md
CHANGED
|
@@ -384,6 +384,98 @@ describe('Project', () => {
|
|
|
384
384
|
done()
|
|
385
385
|
})
|
|
386
386
|
})
|
|
387
|
+
it(`should throw an error if structure is mobivity and taregt_id is not a body field`, (done) => {
|
|
388
|
+
project.webhooks[0].type = "register"
|
|
389
|
+
project.webhooks[0].structure = "mobivity"
|
|
390
|
+
project.webhooks[0].body = {"message_text": "test-value", "url": "test-value", "username": "test-value"}
|
|
391
|
+
let Project = new ProjectModel(project)
|
|
392
|
+
Project.validate((err, res) => {
|
|
393
|
+
assert.exists(err)
|
|
394
|
+
assert.match(err.message, /mobivity webhooks require "username", "target_id", "url" and "message_text" body fields to be present/)
|
|
395
|
+
done()
|
|
396
|
+
})
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
it(`should throw an error if structure is mobivity and message_text is not a body field`, (done) => {
|
|
400
|
+
project.webhooks[0].type = "register"
|
|
401
|
+
project.webhooks[0].structure = "mobivity"
|
|
402
|
+
project.webhooks[0].body = {"target_id": "test-value", "url": "test-value", "username": "test-value"}
|
|
403
|
+
let Project = new ProjectModel(project)
|
|
404
|
+
Project.validate((err, res) => {
|
|
405
|
+
assert.exists(err)
|
|
406
|
+
assert.match(err.message, /mobivity webhooks require "username", "target_id", "url" and "message_text" body fields to be present/)
|
|
407
|
+
done()
|
|
408
|
+
})
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
it(`should throw an error if structure is mobivity and url is not a body field`, (done) => {
|
|
412
|
+
project.webhooks[0].type = "register"
|
|
413
|
+
project.webhooks[0].structure = "mobivity"
|
|
414
|
+
project.webhooks[0].body = {"target_id": "test-value", "message_text": "test-value", "username": "test-value"}
|
|
415
|
+
let Project = new ProjectModel(project)
|
|
416
|
+
Project.validate((err, res) => {
|
|
417
|
+
assert.exists(err)
|
|
418
|
+
assert.match(err.message, /mobivity webhooks require "username", "target_id", "url" and "message_text" body fields to be present/)
|
|
419
|
+
done()
|
|
420
|
+
})
|
|
421
|
+
})
|
|
422
|
+
|
|
423
|
+
it(`should throw an error if structure is mobivity and username is not a body field`, (done) => {
|
|
424
|
+
project.webhooks[0].type = "register"
|
|
425
|
+
project.webhooks[0].structure = "mobivity"
|
|
426
|
+
project.webhooks[0].body = {"target_id": "test-value", "message_text": "url", "url": "test-value"}
|
|
427
|
+
let Project = new ProjectModel(project)
|
|
428
|
+
Project.validate((err, res) => {
|
|
429
|
+
assert.exists(err)
|
|
430
|
+
assert.match(err.message, /mobivity webhooks require "username", "target_id", "url" and "message_text" body fields to be present/)
|
|
431
|
+
done()
|
|
432
|
+
})
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
it(`should not validate if structure is mobivity and body is not present`, (done) => {
|
|
436
|
+
project.webhooks[0].type = "register"
|
|
437
|
+
project.webhooks[0].structure = "mobivity"
|
|
438
|
+
delete project.webhooks[0].body
|
|
439
|
+
let Project = new ProjectModel(project)
|
|
440
|
+
Project.validate((err, res) => {
|
|
441
|
+
assert.exists(err)
|
|
442
|
+
assert.match(err.message, /mobivity webhooks require "username", "target_id", "url" and "message_text" body fields to be present/)
|
|
443
|
+
done()
|
|
444
|
+
})
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
it(`should not validate if structure is mobivity and body is an empty object`, (done) => {
|
|
448
|
+
project.webhooks[0].type = "register"
|
|
449
|
+
project.webhooks[0].structure = "mobivity"
|
|
450
|
+
project.webhooks[0].body = {}
|
|
451
|
+
let Project = new ProjectModel(project)
|
|
452
|
+
Project.validate((err, res) => {
|
|
453
|
+
assert.exists(err)
|
|
454
|
+
assert.match(err.message, /mobivity webhooks require "username", "target_id", "url" and "message_text" body fields to be present/)
|
|
455
|
+
done()
|
|
456
|
+
})
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
it(`should not validate if structure is mobivity and the type is not register`, (done) => {
|
|
460
|
+
project.webhooks[0].type = "trigger"
|
|
461
|
+
project.webhooks[0].structure = "mobivity"
|
|
462
|
+
let Project = new ProjectModel(project)
|
|
463
|
+
Project.validate((err, res) => {
|
|
464
|
+
assert.exists(err)
|
|
465
|
+
assert.match(err.message, /type "register" can only have structure "mobivity"/)
|
|
466
|
+
done()
|
|
467
|
+
})
|
|
468
|
+
})
|
|
469
|
+
it(`should not validate if the type is register and the structure is not mobivity`, (done) => {
|
|
470
|
+
project.webhooks[0].type = "register"
|
|
471
|
+
project.webhooks[0].structure = "standard"
|
|
472
|
+
let Project = new ProjectModel(project)
|
|
473
|
+
Project.validate((err, res) => {
|
|
474
|
+
assert.exists(err)
|
|
475
|
+
assert.match(err.message, /type "register" can only have structure "mobivity"/)
|
|
476
|
+
done()
|
|
477
|
+
})
|
|
478
|
+
})
|
|
387
479
|
|
|
388
480
|
it("should not validate webhook if body contains keys with spaces", (done) => {
|
|
389
481
|
project.webhooks[0].body = { "key with spaces": "value" }
|
package/package.json
CHANGED
package/schemas/Webhook.js
CHANGED
|
@@ -14,12 +14,40 @@ function validateTealiumRequiredFields(structure){
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
function validateMobivityRequiredFields(structure){
|
|
18
|
+
if(structure === "mobivity"){
|
|
19
|
+
const webhookBodyFields = this.body;
|
|
20
|
+
if(!webhookBodyFields){
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const bodyKeys = Object.keys(webhookBodyFields)
|
|
24
|
+
if(!bodyKeys.includes("username") || !bodyKeys.includes("target_id") || !bodyKeys.includes("message_text") || !bodyKeys.includes("url")){
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function validateTypeAndStructure(type) {
|
|
31
|
+
if ((type === "register" && this.structure !== "mobivity")
|
|
32
|
+
|| (type !== "register" && this.structure === "mobivity")) {
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
17
37
|
let WebhookSchema = new mongoose.Schema({
|
|
18
|
-
type: {type: String, enum: ['tempo', 'wave', 'trigger'], required: [true, 'webhook type is required']},
|
|
38
|
+
type: {type: String, enum: ['tempo', 'wave', 'trigger', 'register'], required: [true, 'webhook type is required'], validate: {validator: validateTypeAndStructure, msg: 'type "register" can only have structure "mobivity"'}},
|
|
19
39
|
url: {type: String, required: [true, 'URL is required'], validate: {validator: validators.validateUrl, msg: 'Please enter a valid URL'}},
|
|
20
40
|
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
41
|
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: {
|
|
42
|
+
structure: {
|
|
43
|
+
type: String,
|
|
44
|
+
enum: ['standard', 'tealium', 'mobivity'],
|
|
45
|
+
default: 'standard',
|
|
46
|
+
validate: [
|
|
47
|
+
{validator: validateTealiumRequiredFields, msg: 'tealium webhooks require "tealium_account" and "tealium_profile" body fields to be present'},
|
|
48
|
+
{validator: validateMobivityRequiredFields, msg: 'mobivity webhooks require "username", "target_id", "url" and "message_text" body fields to be present'}
|
|
49
|
+
]
|
|
50
|
+
}
|
|
23
51
|
}, {
|
|
24
52
|
_id: false
|
|
25
53
|
})
|