Skip to content

Commit

Permalink
Add a test to make sure the webhook payload is correct
Browse files Browse the repository at this point in the history
This uses the responses library to intercept the request so we can
confirm that the body content is exactly as expected.
  • Loading branch information
Henry Walshaw committed Sep 10, 2024
1 parent a1b859e commit cfeba33
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@
#
# =================================================================

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 @@ -133,6 +138,66 @@ 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

0 comments on commit cfeba33

Please sign in to comment.