Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add data to webhook #474

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions GeoHealthCheck/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,19 @@ def load_data(file_path):
checks[check_name] = check
DB.session.add(check)

# add Recipient, keeping track of DB objects
recipients = {}
for recipient_name in objects.get('recipients', {}):
recipient = objects['recipients'][recipient_name]

recipient = Recipient(recipient['channel'], recipient['location'])
attached_resources = objects['recipients'][recipient_name]['resources']
for resource_name in attached_resources:
recipient.resources.append(resources[resource_name])

recipients[recipient_name] = recipient
DB.session.add(recipient)

db_commit()


Expand Down
2 changes: 2 additions & 0 deletions GeoHealthCheck/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ def do_webhook(config, resource, run, status_changed, result):
params['ghc.resource.title'] = resource.title
params['ghc.resource.type'] = resource.resource_type
params['ghc.resource.view'] = resource_view
params['ghc.run.message'] = run.message
params['ghc.run.report'] = json.dumps(run.report, sort_keys=True)

try:
r = requests.post(url, params)
Expand Down
2 changes: 1 addition & 1 deletion GeoHealthCheck/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def perform_request(self):
if self.response:
self.log('response: status=%d' % self.response.status_code)

if self.response.status_code / 100 in [4, 5]:
if self.response.status_code >= 400:
self.log('Error response: %s' % (str(self.response.text)))

def perform_get_request(self, url):
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
flake8==5.0.4
Paver==1.3.4
pylint==2.13.9
responses
19 changes: 14 additions & 5 deletions tests/data/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"resource_type": "OGC:WFS",
"active": true,
"title": "WOUDC Web Feature Service",
"url": "http://geo.woudc.org/ows",
"url": "https://geo.woudc.org/ows",
"tags": [
"ows"
]
Expand All @@ -52,7 +52,7 @@
"resource_type": "OGC:CSW",
"active": true,
"title": "WOUDC Catalogue Service",
"url": "http://geo.woudc.org/csw",
"url": "https://geo.woudc.org/csw",
"tags": [
"ows"
]
Expand Down Expand Up @@ -85,7 +85,7 @@
"resource_type": "WWW:LINK",
"active": true,
"title": "WOUDC Definitions Service",
"url": "http://geo.woudc.org/def",
"url": "https://geo.woudc.org/def",
"tags": []
},
"OPENGEOGROEP TMS": {
Expand Down Expand Up @@ -176,7 +176,7 @@
"parameters": {
"type_name": "bag:verblijfsobject",
"type_ns_prefix": "bag",
"type_ns_uri": "http://bag.geonovum.nl",
"type_ns_uri": "https://bag.geonovum.nl",
"srs": "EPSG:28992",
"bbox": ["180635", "455870", "180961", "456050"]
}
Expand All @@ -187,7 +187,7 @@
"parameters": {
"type_name": "all 5 featuretypes",
"type_ns_prefix": "bag",
"type_ns_uri": "http://bag.geonovum.nl",
"type_ns_uri": "https://bag.geonovum.nl",
"srs": "EPSG:28992",
"bbox": ["180635", "455870", "180961", "456050"]
}
Expand Down Expand Up @@ -427,5 +427,14 @@
"check_class": "GeoHealthCheck.plugins.check.checks.HttpHasImageContentType",
"parameters": {}
}
},
"recipients": {
"PDOK BAG WMS": {
"channel": "webhook",
"location": "https://localhost/webhook",
"resources": [
"PDOK BAG WMS"
]
}
}
}
86 changes: 80 additions & 6 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@
#
# =================================================================

import json
import unittest
import os

import responses
from responses.matchers import urlencoded_params_matcher

from init import App
from models import (DB, Resource, Run, load_data, Recipient)
from healthcheck import run_test_resource
from notifications import _parse_webhook_location
from notifications import _parse_webhook_location, do_webhook
from resourceauth import ResourceAuth
import result


TEST_DIR = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -90,26 +96,30 @@ def testRunResoures(self):
(resource.url, str(resource.runs[0])))

def testNotificationsApi(self):
Rcp = Recipient
test_emails = ['[email protected]', '[email protected]', '[email protected]']
invalid_emails = ['invalid', None, object()]

# invalid values should raise exception
for email in invalid_emails:
with self.assertRaises(ValueError):
Rcp.get_or_create('email', email)
Recipient.get_or_create('email', email)

for email in test_emails:
Rcp.get_or_create('email', email)
from_db = set(r[0] for r in DB.session.query(Rcp.location).all())
Recipient.get_or_create('email', email)
from_db = {
r[0]
for r in DB.session.query(Recipient.location).filter(
Recipient.channel == 'email'
)
}
self.assertEqual(from_db, set(test_emails))

r = Resource.query.first()
r.set_recipients('email', test_emails[:2])

# unused email should be removed
self.assertEqual(set(r.get_recipients('email')), set(test_emails[:2]))
q = Rcp.query.filter(Rcp.location == test_emails[-1])
q = Recipient.query.filter(Recipient.location == test_emails[-1])
self.assertEqual(q.count(), 0)

def testWebhookNotifications(self):
Expand All @@ -133,6 +143,70 @@ def testWebhookNotifications(self):
except Exception as err:
self.assertFalse(success, str(err))

@responses.activate
def testWebhookPayload(self):

# create a report from scratch as as local fixture for

recipient = Recipient.query.filter(
Recipient.channel == Recipient.TYPE_WEBHOOK
).first()

resource = recipient.resources[0]

resource_result = result.ResourceResult(resource)
resource_result.start()

probe = resource.probe_vars[0]
probe_result = result.ProbeResult(None, probe)
probe_result.start()

check = probe.check_vars[0]
check_result = result.CheckResult(
check=None,
check_vars=check,
success=False,
message='Failed'
)
check_result.start()
check_result.stop()

probe_result.add_result(check_result)
probe_result.stop()

resource_result.add_result(probe_result)
resource_result.stop()

run = Run(resource=resource, result=resource_result)

# Build up the expected payload being sent ot the webhook

expected_payload = {
'ghc.result': 'Broken',
'ghc.resource.url': resource.url,
'ghc.resource.title': resource.title,
'ghc.resource.type': resource.resource_type,
'ghc.resource.view': f'http://host/resource/{resource.identifier}',
'ghc.run.message': 'Failed',
'ghc.run.report': json.dumps(
resource_result.get_report(), sort_keys=True
),
}

responses.add(
method=responses.POST,
url='https://localhost/webhook',
match=[urlencoded_params_matcher(expected_payload)]
)

do_webhook(
config=App.get_config(),
resource=resource,
run=run,
status_changed=None, # unused in do_webhook
result='Broken'
)

def testSetGetResoureAuth(self):
# Test set/get auth for any Resource, tests en/decrypt
resource = Resource.query.first()
Expand Down
Loading