|
@ -10,23 +10,24 @@ from hc.api.models import Channel, Check, Notification |
|
|
from hc.test import BaseTestCase |
|
|
from hc.test import BaseTestCase |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NotifyTestCase(BaseTestCase): |
|
|
|
|
|
def _setup_data(self, value): |
|
|
|
|
|
|
|
|
class NotifySmsTestCase(BaseTestCase): |
|
|
|
|
|
def setUp(self): |
|
|
|
|
|
super().setUp() |
|
|
|
|
|
|
|
|
self.check = Check(project=self.project) |
|
|
self.check = Check(project=self.project) |
|
|
self.check.status = "down" |
|
|
self.check.status = "down" |
|
|
self.check.last_ping = now() - td(minutes=61) |
|
|
self.check.last_ping = now() - td(minutes=61) |
|
|
self.check.save() |
|
|
self.check.save() |
|
|
|
|
|
|
|
|
|
|
|
spec = {"value": "+1234567890", "up": False, "down": True} |
|
|
self.channel = Channel(project=self.project, kind="sms") |
|
|
self.channel = Channel(project=self.project, kind="sms") |
|
|
self.channel.value = value |
|
|
|
|
|
|
|
|
self.channel.value = json.dumps(spec) |
|
|
self.channel.save() |
|
|
self.channel.save() |
|
|
self.channel.checks.add(self.check) |
|
|
self.channel.checks.add(self.check) |
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
@patch("hc.api.transports.requests.request") |
|
|
def test_it_works(self, mock_post): |
|
|
def test_it_works(self, mock_post): |
|
|
self._setup_data("+1234567890") |
|
|
|
|
|
self.check.last_ping = now() - td(hours=2) |
|
|
self.check.last_ping = now() - td(hours=2) |
|
|
|
|
|
|
|
|
mock_post.return_value.status_code = 200 |
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
self.channel.notify(self.check) |
|
@ -34,7 +35,8 @@ class NotifyTestCase(BaseTestCase): |
|
|
args, kwargs = mock_post.call_args |
|
|
args, kwargs = mock_post.call_args |
|
|
payload = kwargs["data"] |
|
|
payload = kwargs["data"] |
|
|
self.assertEqual(payload["To"], "+1234567890") |
|
|
self.assertEqual(payload["To"], "+1234567890") |
|
|
self.assertFalse("\xa0" in payload["Body"]) |
|
|
|
|
|
|
|
|
self.assertNotIn("\xa0", payload["Body"]) |
|
|
|
|
|
self.assertIn("is DOWN", payload["Body"]) |
|
|
|
|
|
|
|
|
n = Notification.objects.get() |
|
|
n = Notification.objects.get() |
|
|
callback_path = f"/api/v1/notifications/{n.code}/status" |
|
|
callback_path = f"/api/v1/notifications/{n.code}/status" |
|
@ -44,21 +46,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
self.profile.refresh_from_db() |
|
|
self.profile.refresh_from_db() |
|
|
self.assertEqual(self.profile.sms_sent, 1) |
|
|
self.assertEqual(self.profile.sms_sent, 1) |
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
|
|
def test_it_handles_json_value(self, mock_post): |
|
|
|
|
|
value = {"label": "foo", "value": "+1234567890"} |
|
|
|
|
|
self._setup_data(json.dumps(value)) |
|
|
|
|
|
self.check.last_ping = now() - td(hours=2) |
|
|
|
|
|
|
|
|
|
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
assert Notification.objects.count() == 1 |
|
|
|
|
|
|
|
|
|
|
|
args, kwargs = mock_post.call_args |
|
|
|
|
|
payload = kwargs["data"] |
|
|
|
|
|
self.assertEqual(payload["To"], "+1234567890") |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
@patch("hc.api.transports.requests.request") |
|
|
def test_it_enforces_limit(self, mock_post): |
|
|
def test_it_enforces_limit(self, mock_post): |
|
|
# At limit already: |
|
|
# At limit already: |
|
@ -66,8 +53,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
self.profile.sms_sent = 50 |
|
|
self.profile.sms_sent = 50 |
|
|
self.profile.save() |
|
|
self.profile.save() |
|
|
|
|
|
|
|
|
self._setup_data("+1234567890") |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
self.channel.notify(self.check) |
|
|
self.assertFalse(mock_post.called) |
|
|
self.assertFalse(mock_post.called) |
|
|
|
|
|
|
|
@ -88,7 +73,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
self.profile.last_sms_date = now() - td(days=100) |
|
|
self.profile.last_sms_date = now() - td(days=100) |
|
|
self.profile.save() |
|
|
self.profile.save() |
|
|
|
|
|
|
|
|
self._setup_data("+1234567890") |
|
|
|
|
|
mock_post.return_value.status_code = 200 |
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
self.channel.notify(self.check) |
|
@ -96,7 +80,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
@patch("hc.api.transports.requests.request") |
|
|
def test_it_does_not_escape_special_characters(self, mock_post): |
|
|
def test_it_does_not_escape_special_characters(self, mock_post): |
|
|
self._setup_data("+1234567890") |
|
|
|
|
|
self.check.name = "Foo > Bar & Co" |
|
|
self.check.name = "Foo > Bar & Co" |
|
|
self.check.last_ping = now() - td(hours=2) |
|
|
self.check.last_ping = now() - td(hours=2) |
|
|
|
|
|
|
|
@ -107,3 +90,26 @@ class NotifyTestCase(BaseTestCase): |
|
|
args, kwargs = mock_post.call_args |
|
|
args, kwargs = mock_post.call_args |
|
|
payload = kwargs["data"] |
|
|
payload = kwargs["data"] |
|
|
self.assertIn("Foo > Bar & Co", payload["Body"]) |
|
|
self.assertIn("Foo > Bar & Co", payload["Body"]) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
|
|
def test_it_handles_disabled_down_notification(self, mock_post): |
|
|
|
|
|
payload = {"value": "+123123123", "up": True, "down": False} |
|
|
|
|
|
self.channel.value = json.dumps(payload) |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
self.assertFalse(mock_post.called) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
|
|
def test_it_sends_up_notification(self, mock_post): |
|
|
|
|
|
payload = {"value": "+123123123", "up": True, "down": False} |
|
|
|
|
|
self.channel.value = json.dumps(payload) |
|
|
|
|
|
|
|
|
|
|
|
self.check.last_ping = now() |
|
|
|
|
|
self.check.status = "up" |
|
|
|
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
|
|
|
|
args, kwargs = mock_post.call_args |
|
|
|
|
|
payload = kwargs["data"] |
|
|
|
|
|
self.assertIn("is UP", payload["Body"]) |