Skip to content

Commit cfeba33

Browse files
author
Henry Walshaw
committed
Add a test to make sure the webhook payload is correct
This uses the responses library to intercept the request so we can confirm that the body content is exactly as expected.
1 parent a1b859e commit cfeba33

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

tests/test_resources.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@
2828
#
2929
# =================================================================
3030

31+
import json
3132
import unittest
3233
import os
3334

35+
import responses
36+
from responses.matchers import urlencoded_params_matcher
37+
3438
from init import App
3539
from models import (DB, Resource, Run, load_data, Recipient)
3640
from healthcheck import run_test_resource
37-
from notifications import _parse_webhook_location
41+
from notifications import _parse_webhook_location, do_webhook
3842
from resourceauth import ResourceAuth
43+
import result
3944

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

@@ -133,6 +138,66 @@ def testWebhookNotifications(self):
133138
except Exception as err:
134139
self.assertFalse(success, str(err))
135140

141+
@responses.activate
142+
def testWebhookPayload(self):
143+
144+
# create a report from scratch as as local fixture for
145+
146+
recipient = Recipient.query.filter(Recipient.channel==Recipient.TYPE_WEBHOOK).first()
147+
148+
resource = recipient.resources[0]
149+
150+
resource_result = result.ResourceResult(resource)
151+
resource_result.start()
152+
153+
probe = resource.probe_vars[0]
154+
probe_result = result.ProbeResult(None, probe)
155+
probe_result.start()
156+
157+
check = probe.check_vars[0]
158+
check_result = result.CheckResult(
159+
check=None,
160+
check_vars=check,
161+
success=False,
162+
message='Failed'
163+
)
164+
check_result.start()
165+
check_result.stop()
166+
167+
probe_result.add_result(check_result)
168+
probe_result.stop()
169+
170+
resource_result.add_result(probe_result)
171+
resource_result.stop()
172+
173+
run = Run(resource=resource, result=resource_result)
174+
175+
# Build up the expected payload being sent ot the webhook
176+
177+
expected_payload = {
178+
'ghc.result': 'Broken',
179+
'ghc.resource.url': resource.url,
180+
'ghc.resource.title': resource.title,
181+
'ghc.resource.type': resource.resource_type,
182+
'ghc.resource.view': f'http://host/resource/{resource.identifier}',
183+
'ghc.run.message': 'Failed',
184+
'ghc.run.report': json.dumps(resource_result.get_report(), sort_keys=True),
185+
}
186+
187+
responses.add(
188+
method=responses.POST,
189+
url='https://localhost/webhook',
190+
match=[urlencoded_params_matcher(expected_payload)]
191+
)
192+
193+
do_webhook(
194+
config=App.get_config(),
195+
resource=resource,
196+
run=run,
197+
status_changed=None, # unused in do_webhook
198+
result='Broken'
199+
)
200+
136201
def testSetGetResoureAuth(self):
137202
# Test set/get auth for any Resource, tests en/decrypt
138203
resource = Resource.query.first()

0 commit comments

Comments
 (0)