|
@ -4,20 +4,21 @@ from datetime import timedelta as td |
|
|
import json |
|
|
import json |
|
|
from unittest.mock import patch |
|
|
from unittest.mock import patch |
|
|
|
|
|
|
|
|
|
|
|
from django.test.utils import override_settings |
|
|
from django.utils.timezone import now |
|
|
from django.utils.timezone import now |
|
|
from hc.api.models import Channel, Check, Notification |
|
|
from hc.api.models import Channel, Check, Notification |
|
|
from hc.test import BaseTestCase |
|
|
from hc.test import BaseTestCase |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NotifyTestCase(BaseTestCase): |
|
|
class NotifyTestCase(BaseTestCase): |
|
|
def _setup_data(self, kind, value, status="down", email_verified=True): |
|
|
|
|
|
|
|
|
def _setup_data(self, value, status="down", email_verified=True): |
|
|
self.check = Check(project=self.project) |
|
|
self.check = Check(project=self.project) |
|
|
self.check.status = status |
|
|
self.check.status = status |
|
|
self.check.last_ping = now() - td(minutes=61) |
|
|
self.check.last_ping = now() - td(minutes=61) |
|
|
self.check.save() |
|
|
self.check.save() |
|
|
|
|
|
|
|
|
self.channel = Channel(project=self.project) |
|
|
self.channel = Channel(project=self.project) |
|
|
self.channel.kind = kind |
|
|
|
|
|
|
|
|
self.channel.kind = "zulip" |
|
|
self.channel.value = value |
|
|
self.channel.value = value |
|
|
self.channel.email_verified = email_verified |
|
|
self.channel.email_verified = email_verified |
|
|
self.channel.save() |
|
|
self.channel.save() |
|
@ -31,7 +32,7 @@ class NotifyTestCase(BaseTestCase): |
|
|
"mtype": "stream", |
|
|
"mtype": "stream", |
|
|
"to": "general", |
|
|
"to": "general", |
|
|
} |
|
|
} |
|
|
self._setup_data("zulip", json.dumps(definition)) |
|
|
|
|
|
|
|
|
self._setup_data(json.dumps(definition)) |
|
|
mock_post.return_value.status_code = 200 |
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
self.channel.notify(self.check) |
|
@ -53,7 +54,7 @@ class NotifyTestCase(BaseTestCase): |
|
|
"mtype": "stream", |
|
|
"mtype": "stream", |
|
|
"to": "general", |
|
|
"to": "general", |
|
|
} |
|
|
} |
|
|
self._setup_data("zulip", json.dumps(definition)) |
|
|
|
|
|
|
|
|
self._setup_data(json.dumps(definition)) |
|
|
mock_post.return_value.status_code = 403 |
|
|
mock_post.return_value.status_code = 403 |
|
|
mock_post.return_value.json.return_value = {"msg": "Nice try"} |
|
|
mock_post.return_value.json.return_value = {"msg": "Nice try"} |
|
|
|
|
|
|
|
@ -71,7 +72,7 @@ class NotifyTestCase(BaseTestCase): |
|
|
"mtype": "stream", |
|
|
"mtype": "stream", |
|
|
"to": "general", |
|
|
"to": "general", |
|
|
} |
|
|
} |
|
|
self._setup_data("zulip", json.dumps(definition)) |
|
|
|
|
|
|
|
|
self._setup_data(json.dumps(definition)) |
|
|
mock_post.return_value.status_code = 200 |
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
self.channel.notify(self.check) |
|
@ -84,3 +85,18 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
|
|
|
|
|
payload = kwargs["data"] |
|
|
payload = kwargs["data"] |
|
|
self.assertIn("DOWN", payload["topic"]) |
|
|
self.assertIn("DOWN", payload["topic"]) |
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ZULIP_ENABLED=False) |
|
|
|
|
|
def test_it_requires_zulip_enabled(self): |
|
|
|
|
|
definition = { |
|
|
|
|
|
"bot_email": "[email protected]", |
|
|
|
|
|
"api_key": "fake-key", |
|
|
|
|
|
"mtype": "stream", |
|
|
|
|
|
"to": "general", |
|
|
|
|
|
} |
|
|
|
|
|
self._setup_data(json.dumps(definition)) |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
|
|
|
|
n = Notification.objects.get() |
|
|
|
|
|
self.assertEqual(n.error, "Zulip notifications are not enabled.") |