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.

91 lines
3.0 KiB

  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. import json
  4. from unittest.mock import patch
  5. from django.utils.timezone import now
  6. from hc.api.models import Channel, Check, Notification
  7. from hc.test import BaseTestCase
  8. from requests.exceptions import Timeout
  9. from django.test.utils import override_settings
  10. class NotifyTestCase(BaseTestCase):
  11. def _setup_data(self, value, status="down", email_verified=True):
  12. self.check = Check(project=self.project)
  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 = "slack"
  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_slack(self, mock_post):
  24. self._setup_data("123")
  25. mock_post.return_value.status_code = 200
  26. self.channel.notify(self.check)
  27. assert Notification.objects.count() == 1
  28. args, kwargs = mock_post.call_args
  29. payload = kwargs["json"]
  30. attachment = payload["attachments"][0]
  31. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  32. self.assertEqual(fields["Last Ping"], "an hour ago")
  33. @patch("hc.api.transports.requests.request")
  34. def test_slack_with_complex_value(self, mock_post):
  35. v = json.dumps({"incoming_webhook": {"url": "123"}})
  36. self._setup_data(v)
  37. mock_post.return_value.status_code = 200
  38. self.channel.notify(self.check)
  39. assert Notification.objects.count() == 1
  40. args, kwargs = mock_post.call_args
  41. self.assertEqual(args[1], "123")
  42. @patch("hc.api.transports.requests.request")
  43. def test_slack_handles_500(self, mock_post):
  44. self._setup_data("123")
  45. mock_post.return_value.status_code = 500
  46. self.channel.notify(self.check)
  47. n = Notification.objects.get()
  48. self.assertEqual(n.error, "Received status code 500")
  49. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  50. def test_slack_handles_timeout(self, mock_post):
  51. self._setup_data("123")
  52. self.channel.notify(self.check)
  53. n = Notification.objects.get()
  54. self.assertEqual(n.error, "Connection timed out")
  55. @patch("hc.api.transports.requests.request")
  56. def test_slack_with_tabs_in_schedule(self, mock_post):
  57. self._setup_data("123")
  58. self.check.kind = "cron"
  59. self.check.schedule = "*\t* * * *"
  60. self.check.save()
  61. mock_post.return_value.status_code = 200
  62. self.channel.notify(self.check)
  63. self.assertEqual(Notification.objects.count(), 1)
  64. self.assertTrue(mock_post.called)
  65. @override_settings(SLACK_ENABLED=False)
  66. def test_it_requires_slack_enabled(self):
  67. self._setup_data("123")
  68. self.channel.notify(self.check)
  69. n = Notification.objects.get()
  70. self.assertEqual(n.error, "Slack notifications are not enabled.")