|
28 | 28 | # |
29 | 29 | # ================================================================= |
30 | 30 |
|
| 31 | +import json |
31 | 32 | import unittest |
32 | 33 | import os |
33 | 34 |
|
| 35 | +import responses |
| 36 | +from responses.matchers import urlencoded_params_matcher |
| 37 | + |
34 | 38 | from init import App |
35 | 39 | from models import (DB, Resource, Run, load_data, Recipient) |
36 | 40 | from healthcheck import run_test_resource |
37 | | -from notifications import _parse_webhook_location |
| 41 | +from notifications import _parse_webhook_location, do_webhook |
38 | 42 | from resourceauth import ResourceAuth |
| 43 | +import result |
39 | 44 |
|
40 | 45 | TEST_DIR = os.path.dirname(os.path.abspath(__file__)) |
41 | 46 |
|
@@ -133,6 +138,66 @@ def testWebhookNotifications(self): |
133 | 138 | except Exception as err: |
134 | 139 | self.assertFalse(success, str(err)) |
135 | 140 |
|
| 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 | + |
136 | 201 | def testSetGetResoureAuth(self): |
137 | 202 | # Test set/get auth for any Resource, tests en/decrypt |
138 | 203 | resource = Resource.query.first() |
|
0 commit comments