You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.4 KiB

  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. import json
  4. from unittest.mock import patch
  5. from django.test.utils import override_settings
  6. from django.utils.timezone import now
  7. from hc.api.models import Channel, Check, Notification
  8. from hc.test import BaseTestCase
  9. class NotifyTestCase(BaseTestCase):
  10. def _setup_data(self, value, status="down", email_verified=True):
  11. self.check = Check(project=self.project)
  12. self.check.name = "Foobar"
  13. self.check.status = status
  14. self.check.last_ping = now() - td(minutes=61)
  15. self.check.save()
  16. self.channel = Channel(project=self.project)
  17. self.channel.kind = "zulip"
  18. self.channel.value = value
  19. self.channel.email_verified = email_verified
  20. self.channel.save()
  21. self.channel.checks.add(self.check)
  22. @patch("hc.api.transports.requests.request")
  23. def test_zulip(self, mock_post):
  24. definition = {
  25. "bot_email": "[email protected]",
  26. "api_key": "fake-key",
  27. "mtype": "stream",
  28. "to": "general",
  29. }
  30. self._setup_data(json.dumps(definition))
  31. mock_post.return_value.status_code = 200
  32. self.channel.notify(self.check)
  33. assert Notification.objects.count() == 1
  34. args, kwargs = mock_post.call_args
  35. method, url = args
  36. self.assertEqual(url, "https://example.org/api/v1/messages")
  37. payload = kwargs["data"]
  38. self.assertIn("DOWN", payload["topic"])
  39. # payload should not contain check's code
  40. serialized = json.dumps(payload)
  41. self.assertNotIn(str(self.check.code), serialized)
  42. @patch("hc.api.transports.requests.request")
  43. def test_zulip_returns_error(self, mock_post):
  44. definition = {
  45. "bot_email": "[email protected]",
  46. "api_key": "fake-key",
  47. "mtype": "stream",
  48. "to": "general",
  49. }
  50. self._setup_data(json.dumps(definition))
  51. mock_post.return_value.status_code = 403
  52. mock_post.return_value.json.return_value = {"msg": "Nice try"}
  53. self.channel.notify(self.check)
  54. n = Notification.objects.first()
  55. self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"')
  56. @patch("hc.api.transports.requests.request")
  57. def test_zulip_uses_site_parameter(self, mock_post):
  58. definition = {
  59. "bot_email": "[email protected]",
  60. "site": "https://custom.example.org",
  61. "api_key": "fake-key",
  62. "mtype": "stream",
  63. "to": "general",
  64. }
  65. self._setup_data(json.dumps(definition))
  66. mock_post.return_value.status_code = 200
  67. self.channel.notify(self.check)
  68. assert Notification.objects.count() == 1
  69. args, kwargs = mock_post.call_args
  70. method, url = args
  71. self.assertEqual(url, "https://custom.example.org/api/v1/messages")
  72. payload = kwargs["data"]
  73. self.assertIn("DOWN", payload["topic"])
  74. @override_settings(ZULIP_ENABLED=False)
  75. def test_it_requires_zulip_enabled(self):
  76. definition = {
  77. "bot_email": "[email protected]",
  78. "api_key": "fake-key",
  79. "mtype": "stream",
  80. "to": "general",
  81. }
  82. self._setup_data(json.dumps(definition))
  83. self.channel.notify(self.check)
  84. n = Notification.objects.get()
  85. self.assertEqual(n.error, "Zulip notifications are not enabled.")