Browse Source

Better tests.

pull/230/head
Pēteris Caune 6 years ago
parent
commit
c7eca1c4a7
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 161 additions and 63 deletions
  1. +2
    -2
      CHANGELOG.md
  2. +151
    -0
      hc/api/tests/test_channel_model.py
  3. +8
    -61
      hc/api/tests/test_notify.py

+ 2
- 2
CHANGELOG.md View File

@ -6,8 +6,8 @@ All notable changes to this project will be documented in this file.
### Improvements ### Improvements
- Add the `prunetokenbucket` management command - Add the `prunetokenbucket` management command
- Show check counts in JSON "badges" (#251) - Show check counts in JSON "badges" (#251)
- Webhooks support PUT method (#249)
- Webhooks can have different request bodies and headers for "up" and "events" (#249)
- Webhooks support HTTP PUT (#249)
- Webhooks can use different req. bodies and headers for "up" and "down" events. (#249)
### Bug Fixes ### Bug Fixes
- Fix badges for tags containing special characters (#240, #237) - Fix badges for tags containing special characters (#240, #237)


+ 151
- 0
hc/api/tests/test_channel_model.py View File

@ -0,0 +1,151 @@
import json
from hc.api.models import Channel
from hc.test import BaseTestCase
class ChannelModelTestCase(BaseTestCase):
def test_webhook_spec_handles_plain_single_address(self):
c = Channel(kind="webhook")
c.value = "http://example.org"
self.assertEqual(
c.down_webhook_spec,
{"method": "GET", "url": "http://example.org", "body": "", "headers": {}},
)
self.assertEqual(
c.up_webhook_spec, {"method": "GET", "url": "", "body": "", "headers": {}}
)
def test_webhook_spec_handles_plain_pair(self):
c = Channel(kind="webhook")
c.value = "http://example.org\nhttp://example.com/up/"
self.assertEqual(
c.down_webhook_spec,
{"method": "GET", "url": "http://example.org", "body": "", "headers": {}},
)
self.assertEqual(
c.up_webhook_spec,
{
"method": "GET",
"url": "http://example.com/up/",
"body": "",
"headers": {},
},
)
def test_webhook_spec_handles_plain_post(self):
c = Channel(kind="webhook")
c.value = "http://example.org\n\nhello world"
self.assertEqual(
c.down_webhook_spec,
{
"method": "POST",
"url": "http://example.org",
"body": "hello world",
"headers": {},
},
)
self.assertEqual(
c.up_webhook_spec,
{"method": "POST", "url": "", "body": "hello world", "headers": {}},
)
def test_webhook_spec_handles_legacy_get(self):
c = Channel(kind="webhook")
c.value = json.dumps(
{
"url_down": "http://example.org",
"url_up": "http://example.org/up/",
"headers": {"X-Name": "value"},
"post_data": "",
}
)
self.assertEqual(
c.down_webhook_spec,
{
"method": "GET",
"url": "http://example.org",
"body": "",
"headers": {"X-Name": "value"},
},
)
self.assertEqual(
c.up_webhook_spec,
{
"method": "GET",
"url": "http://example.org/up/",
"body": "",
"headers": {"X-Name": "value"},
},
)
def test_webhook_spec_handles_legacy_post(self):
c = Channel(kind="webhook")
c.value = json.dumps(
{
"url_down": "http://example.org",
"url_up": "http://example.org/up/",
"headers": {"X-Name": "value"},
"post_data": "hello world",
}
)
self.assertEqual(
c.down_webhook_spec,
{
"method": "POST",
"url": "http://example.org",
"body": "hello world",
"headers": {"X-Name": "value"},
},
)
self.assertEqual(
c.up_webhook_spec,
{
"method": "POST",
"url": "http://example.org/up/",
"body": "hello world",
"headers": {"X-Name": "value"},
},
)
def test_webhook_spec_handles_mixed(self):
c = Channel(kind="webhook")
c.value = json.dumps(
{
"method_down": "GET",
"url_down": "http://example.org",
"body_down": "",
"headers_down": {"X-Status": "X"},
"method_up": "POST",
"url_up": "http://example.org/up/",
"body_up": "hello world",
"headers_up": {"X-Status": "OK"},
}
)
self.assertEqual(
c.down_webhook_spec,
{
"method": "GET",
"url": "http://example.org",
"body": "",
"headers": {"X-Status": "X"},
},
)
self.assertEqual(
c.up_webhook_spec,
{
"method": "POST",
"url": "http://example.org/up/",
"body": "hello world",
"headers": {"X-Status": "OK"},
},
)

+ 8
- 61
hc/api/tests/test_notify.py View File

@ -146,8 +146,13 @@ class NotifyTestCase(BaseTestCase):
self.assertIsInstance(kwargs["data"], bytes) self.assertIsInstance(kwargs["data"], bytes)
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_legacy_webhooks_handle_json_value(self, mock_request):
definition = {"url_down": "http://foo.com", "post_data": "", "headers": {}}
def test_webhooks_handle_json_value(self, mock_request):
definition = {
"method_down": "GET",
"url_down": "http://foo.com",
"body_down": "",
"headers_down": {},
}
self._setup_data("webhook", json.dumps(definition)) self._setup_data("webhook", json.dumps(definition))
self.channel.notify(self.check) self.channel.notify(self.check)
@ -156,23 +161,13 @@ class NotifyTestCase(BaseTestCase):
"get", "http://foo.com", headers=headers, timeout=5 "get", "http://foo.com", headers=headers, timeout=5
) )
@patch("hc.api.transports.requests.request")
def test_legacy_webhooks_handle_json_up_event(self, mock_request):
definition = {"url_up": "http://bar", "post_data": "", "headers": {}}
self._setup_data("webhook", json.dumps(definition), status="up")
self.channel.notify(self.check)
headers = {"User-Agent": "healthchecks.io"}
mock_request.assert_called_with("get", "http://bar", headers=headers, timeout=5)
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_webhooks_handle_json_up_event(self, mock_request): def test_webhooks_handle_json_up_event(self, mock_request):
definition = { definition = {
"method_up": "GET", "method_up": "GET",
"url_up": "http://bar", "url_up": "http://bar",
"body_up": "", "body_up": "",
"headers_up": {}
"headers_up": {},
} }
self._setup_data("webhook", json.dumps(definition), status="up") self._setup_data("webhook", json.dumps(definition), status="up")
@ -181,22 +176,6 @@ class NotifyTestCase(BaseTestCase):
headers = {"User-Agent": "healthchecks.io"} headers = {"User-Agent": "healthchecks.io"}
mock_request.assert_called_with("get", "http://bar", headers=headers, timeout=5) mock_request.assert_called_with("get", "http://bar", headers=headers, timeout=5)
@patch("hc.api.transports.requests.request")
def test_legacy_webhooks_handle_post_headers(self, mock_request):
definition = {
"url_down": "http://foo.com",
"post_data": "data",
"headers": {"Content-Type": "application/json"},
}
self._setup_data("webhook", json.dumps(definition))
self.channel.notify(self.check)
headers = {"User-Agent": "healthchecks.io", "Content-Type": "application/json"}
mock_request.assert_called_with(
"post", "http://foo.com", data=b"data", headers=headers, timeout=5
)
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_webhooks_handle_post_headers(self, mock_request): def test_webhooks_handle_post_headers(self, mock_request):
definition = { definition = {
@ -214,22 +193,6 @@ class NotifyTestCase(BaseTestCase):
"post", "http://foo.com", data=b"data", headers=headers, timeout=5 "post", "http://foo.com", data=b"data", headers=headers, timeout=5
) )
@patch("hc.api.transports.requests.request")
def test_legacy_webhooks_handle_get_headers(self, mock_request):
definition = {
"url_down": "http://foo.com",
"post_data": "",
"headers": {"Content-Type": "application/json"},
}
self._setup_data("webhook", json.dumps(definition))
self.channel.notify(self.check)
headers = {"User-Agent": "healthchecks.io", "Content-Type": "application/json"}
mock_request.assert_called_with(
"get", "http://foo.com", headers=headers, timeout=5
)
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_webhooks_handle_get_headers(self, mock_request): def test_webhooks_handle_get_headers(self, mock_request):
definition = { definition = {
@ -247,22 +210,6 @@ class NotifyTestCase(BaseTestCase):
"get", "http://foo.com", headers=headers, timeout=5 "get", "http://foo.com", headers=headers, timeout=5
) )
@patch("hc.api.transports.requests.request")
def test_legacy_webhooks_allow_user_agent_override(self, mock_request):
definition = {
"url_down": "http://foo.com",
"post_data": "",
"headers": {"User-Agent": "My-Agent"},
}
self._setup_data("webhook", json.dumps(definition))
self.channel.notify(self.check)
headers = {"User-Agent": "My-Agent"}
mock_request.assert_called_with(
"get", "http://foo.com", headers=headers, timeout=5
)
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_webhooks_allow_user_agent_override(self, mock_request): def test_webhooks_allow_user_agent_override(self, mock_request):
definition = { definition = {


Loading…
Cancel
Save