From a50b64c03a4bffdfd49e8bc0d5a51554ca8dbb72 Mon Sep 17 00:00:00 2001
From: Budhi <964106+sapiderman@users.noreply.github.com>
Date: Mon, 18 Jul 2022 17:32:55 +0700
Subject: [PATCH] Add test to validate example configs (#773)

* add monika-configuration.json

* add checking for notifications

* add explicit requirement for data:url

* add more offline test cases

* ran prettier

* update schema filename

* add body and headers

* ran formatter

* cleanup test file

* remove array body and headers

* update filename

* free form body

* add url validation

* add test, fix body validation

* oops..removed *.only test debug

* update variable name from PR review

* use of const instead of enum

* test sample configs

* use latest schema, remove test.only

Co-authored-by: Gregory Sukanto <gregory.sukanto@gmail.com>
Co-authored-by: Nico Prananta <nicnocquee@users.noreply.github.com>
---
 config_sample/config.desktop.example.json     |  1 +
 config_sample/config.desktop.example.yml      |  3 +-
 config_sample/config.mailgun.example.json     |  3 +-
 config_sample/config.mailgun.example.yml      |  5 +-
 .../config.monika-whatsapp.example.json       |  3 +-
 .../config.monika-whatsapp.example.yml        |  7 +-
 config_sample/config.sendgrid.example.json    |  1 +
 config_sample/config.sendgrid.example.yml     |  5 +-
 config_sample/config.slack.example.json       |  1 +
 config_sample/config.slack.example.yml        |  3 +-
 config_sample/config.smtp-gmail.example.json  |  1 +
 config_sample/config.smtp-gmail.example.yml   |  3 +-
 config_sample/config.teams.example.json       |  1 +
 config_sample/config.teams.example.yml        |  5 +-
 config_sample/config.telegram.example.json    |  1 +
 config_sample/config.telegram.example.yml     |  3 +-
 config_sample/config.webhook.example.json     |  1 +
 config_sample/config.webhook.example.yml      |  3 +-
 config_sample/config.whatsapp.example.json    |  1 +
 config_sample/config.whatsapp.example.yml     |  7 +-
 config_sample/config.workplace.example.json   |  1 +
 config_sample/config.workplace.example.yml    |  9 +-
 .../validate-sample-configs.test.ts           | 85 +++++++++++++++++++
 23 files changed, 130 insertions(+), 23 deletions(-)
 create mode 100644 test/json-schema/validate-sample-configs.test.ts

diff --git a/config_sample/config.desktop.example.json b/config_sample/config.desktop.example.json
index c8e446fea..02aa508b0 100644
--- a/config_sample/config.desktop.example.json
+++ b/config_sample/config.desktop.example.json
@@ -7,6 +7,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.desktop.example.yml b/config_sample/config.desktop.example.yml
index 948dce520..5f237a50d 100644
--- a/config_sample/config.desktop.example.yml
+++ b/config_sample/config.desktop.example.yml
@@ -1,5 +1,6 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
diff --git a/config_sample/config.mailgun.example.json b/config_sample/config.mailgun.example.json
index 48c46dbfc..488df553b 100644
--- a/config_sample/config.mailgun.example.json
+++ b/config_sample/config.mailgun.example.json
@@ -4,7 +4,7 @@
       "id": "unique-id-mailgun",
       "type": "mailgun",
       "data": {
-        "recipients": ["RECIPIENT_EMAIL_ADDRESS"],
+        "recipients": ["RECIPIENT_EMAIL_ADDRESS", "RECIPIENT_EMAIL_ADDRESS2"],
         "apiKey": "YOUR_API_KEY",
         "domain": "YOUR_DOMAIN"
       }
@@ -12,6 +12,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.mailgun.example.yml b/config_sample/config.mailgun.example.yml
index 62863c79b..9c1ec2280 100644
--- a/config_sample/config.mailgun.example.yml
+++ b/config_sample/config.mailgun.example.yml
@@ -1,11 +1,12 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
   - id: unique-id-mailgun
     type: mailgun
     data:
-      recipients: [RECIPIENT_EMAIL_ADDRESS]
+      recipients: [RECIPIENT_EMAIL_ADDRESS, RECIPIENT_EMAIL_ADDRESS2]
       apiKey: YOUR_API_KEY
       domain: YOUR_DOMAIN
diff --git a/config_sample/config.monika-whatsapp.example.json b/config_sample/config.monika-whatsapp.example.json
index 791becbdb..234f3e197 100644
--- a/config_sample/config.monika-whatsapp.example.json
+++ b/config_sample/config.monika-whatsapp.example.json
@@ -4,12 +4,13 @@
       "id": "monika-webhook",
       "type": "monika-notif",
       "data": {
-        "url": "YOUR_MONIKA_WHATSAPP_WEBHOOK"
+        "url": "https://YOUR_MONIKA_WHATSAPP_WEBHOOK"
       }
     }
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.monika-whatsapp.example.yml b/config_sample/config.monika-whatsapp.example.yml
index bcc33f8dc..2bbfd6bf3 100644
--- a/config_sample/config.monika-whatsapp.example.yml
+++ b/config_sample/config.monika-whatsapp.example.yml
@@ -1,9 +1,10 @@
 probes:
-  - requests:
-      - url: http://github.com
+  - id: probe-01
+    requests:
+      - url: https://github.com
 
 notifications:
   - id: monika-webhook
     type: monika-notif
     data:
-      url: YOUR_MONIKA_WHATSAPP_WEBHOOK
+      url: https://YOUR_MONIKA_WHATSAPP_WEBHOOK
diff --git a/config_sample/config.sendgrid.example.json b/config_sample/config.sendgrid.example.json
index 10175bf85..7ed62e02c 100644
--- a/config_sample/config.sendgrid.example.json
+++ b/config_sample/config.sendgrid.example.json
@@ -12,6 +12,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.sendgrid.example.yml b/config_sample/config.sendgrid.example.yml
index edc0f0b81..03da524fe 100644
--- a/config_sample/config.sendgrid.example.yml
+++ b/config_sample/config.sendgrid.example.yml
@@ -1,10 +1,11 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
   - id: unique-id-mailgun
-    type: mailgun
+    type: sendgrid
     data:
       recipients: [RECIPIENT_EMAIL_ADDRESS]
       apiKey: YOUR_API_KEY
diff --git a/config_sample/config.slack.example.json b/config_sample/config.slack.example.json
index e4c198e61..46df6b63f 100644
--- a/config_sample/config.slack.example.json
+++ b/config_sample/config.slack.example.json
@@ -10,6 +10,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.slack.example.yml b/config_sample/config.slack.example.yml
index 0f4c4a242..c5c4c7e25 100644
--- a/config_sample/config.slack.example.yml
+++ b/config_sample/config.slack.example.yml
@@ -1,5 +1,6 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
diff --git a/config_sample/config.smtp-gmail.example.json b/config_sample/config.smtp-gmail.example.json
index d6589407a..15a5844c5 100644
--- a/config_sample/config.smtp-gmail.example.json
+++ b/config_sample/config.smtp-gmail.example.json
@@ -14,6 +14,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.smtp-gmail.example.yml b/config_sample/config.smtp-gmail.example.yml
index ca26b2523..d131417f8 100644
--- a/config_sample/config.smtp-gmail.example.yml
+++ b/config_sample/config.smtp-gmail.example.yml
@@ -1,5 +1,6 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
diff --git a/config_sample/config.teams.example.json b/config_sample/config.teams.example.json
index 537389791..2a188c02b 100644
--- a/config_sample/config.teams.example.json
+++ b/config_sample/config.teams.example.json
@@ -10,6 +10,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.teams.example.yml b/config_sample/config.teams.example.yml
index 60f01a8e0..f4b4a62c5 100644
--- a/config_sample/config.teams.example.yml
+++ b/config_sample/config.teams.example.yml
@@ -1,9 +1,10 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
   - id: unique-id-teams
-    type: type
+    type: teams
     data:
       url: https://YOUR_TEAMS_WEBHOOK_URL
diff --git a/config_sample/config.telegram.example.json b/config_sample/config.telegram.example.json
index 6c8f4157a..e3491279b 100644
--- a/config_sample/config.telegram.example.json
+++ b/config_sample/config.telegram.example.json
@@ -11,6 +11,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.telegram.example.yml b/config_sample/config.telegram.example.yml
index 8c25bef46..fc42ae5e4 100644
--- a/config_sample/config.telegram.example.yml
+++ b/config_sample/config.telegram.example.yml
@@ -1,5 +1,6 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
diff --git a/config_sample/config.webhook.example.json b/config_sample/config.webhook.example.json
index af08466d2..7f23f12dc 100644
--- a/config_sample/config.webhook.example.json
+++ b/config_sample/config.webhook.example.json
@@ -10,6 +10,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.webhook.example.yml b/config_sample/config.webhook.example.yml
index 33d4cf402..5ecb5c544 100644
--- a/config_sample/config.webhook.example.yml
+++ b/config_sample/config.webhook.example.yml
@@ -1,5 +1,6 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
diff --git a/config_sample/config.whatsapp.example.json b/config_sample/config.whatsapp.example.json
index 4ceb9f5ef..996e11dfd 100644
--- a/config_sample/config.whatsapp.example.json
+++ b/config_sample/config.whatsapp.example.json
@@ -13,6 +13,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.whatsapp.example.yml b/config_sample/config.whatsapp.example.yml
index 0d30cd79b..dcb0c6d0d 100644
--- a/config_sample/config.whatsapp.example.yml
+++ b/config_sample/config.whatsapp.example.yml
@@ -1,12 +1,13 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
   - id: unique-id-webhook
-    type: webhook
+    type: whatsapp
     data:
-      recipients: [628123456789]
+      recipients: ['628123456789']
       url: https://yourwhatsappapiserver.com
       username: whatsappusername
       password: whatsapppassword
diff --git a/config_sample/config.workplace.example.json b/config_sample/config.workplace.example.json
index 204af0f60..aba7cefd0 100644
--- a/config_sample/config.workplace.example.json
+++ b/config_sample/config.workplace.example.json
@@ -11,6 +11,7 @@
   ],
   "probes": [
     {
+      "id": "probe-01",
       "requests": [
         {
           "url": "https://github.com"
diff --git a/config_sample/config.workplace.example.yml b/config_sample/config.workplace.example.yml
index 7318bbe0c..810238b18 100644
--- a/config_sample/config.workplace.example.yml
+++ b/config_sample/config.workplace.example.yml
@@ -1,10 +1,11 @@
 probes:
-  - requests:
+  - id: probe-01
+    requests:
       - url: http://github.com
 
 notifications:
-  - id: unique-id-webhook
-    type: webhook
+  - id: unique-id-workplace
+    type: workplace
     data:
-      thread_id: 12345678910
+      thread_id: 'abcdef12345678910'
       access_token: your_custom_integration_access_token
diff --git a/test/json-schema/validate-sample-configs.test.ts b/test/json-schema/validate-sample-configs.test.ts
new file mode 100644
index 000000000..7e2046366
--- /dev/null
+++ b/test/json-schema/validate-sample-configs.test.ts
@@ -0,0 +1,85 @@
+/**********************************************************************************
+ * MIT License                                                                    *
+ *                                                                                *
+ * Copyright (c) 2021 Hyperjump Technology                                        *
+ *                                                                                *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy   *
+ * of this software and associated documentation files (the "Software"), to deal  *
+ * in the Software without restriction, including without limitation the rights   *
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      *
+ * copies of the Software, and to permit persons to whom the Software is          *
+ * furnished to do so, subject to the following conditions:                       *
+ *                                                                                *
+ * The above copyright notice and this permission notice shall be included in all *
+ * copies or substantial portions of the Software.                                *
+ *                                                                                *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     *
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       *
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    *
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         *
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  *
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  *
+ * SOFTWARE.                                                                      *
+ **********************************************************************************/
+
+import yaml from 'js-yaml'
+import fs from 'fs'
+import path from 'path'
+import { expect } from 'chai'
+import Ajv from 'ajv'
+
+const ajv = new Ajv()
+
+import mySchema from '../../monika-config-schema.json'
+
+ajv.addVocabulary(['name', 'fileMatch', 'url']) // add custom Scheme Store keywords, reference: https://www.schemastore.org/json/
+
+const validate = ajv.compile(mySchema)
+
+const getAllFiles = function (dirPath: string, arrayOfFiles: string[]) {
+  const files = fs.readdirSync(dirPath)
+
+  arrayOfFiles = arrayOfFiles || []
+
+  // files.forEach(function(file) {
+  for (const file of files) {
+    if (fs.statSync(dirPath + '/' + file).isDirectory()) {
+      arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles)
+    } else {
+      arrayOfFiles.push(path.join(dirPath, '/', file))
+    }
+  }
+
+  return arrayOfFiles
+}
+
+describe('validate example configs', () => {
+  it('should detect examples that does not conform to the schema', () => {
+    const files = getAllFiles('./config_sample', [])
+
+    let sampleFile: any
+
+    for (const file of files) {
+      switch (path.extname(file)) {
+        case '.yml':
+        case '.yaml':
+          sampleFile = yaml.load(fs.readFileSync(file, 'utf8'))
+          break
+        case '.json':
+          sampleFile = JSON.parse(fs.readFileSync(file, 'utf8'))
+          break
+        default:
+          sampleFile = null
+          continue // skip for other file.extension
+      }
+
+      const isValid = validate(sampleFile)
+      if (isValid === false) {
+        console.error('file:', file, 'validity:', isValid)
+        console.error('error:', validate.errors)
+      }
+
+      expect(isValid).to.be.true
+    }
+  })
+})