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.

96 lines
3.2 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 NotifySlackTestCase(BaseTestCase):
  11. def _setup_data(self, value, status="down", email_verified=True):
  12. self.check = Check(project=self.project)
  13. self.check.name = "Foobar"
  14. self.check.status = status
  15. self.check.last_ping = now() - td(minutes=61)
  16. self.check.save()
  17. self.channel = Channel(project=self.project)
  18. self.channel.kind = "slack"
  19. self.channel.value = value
  20. self.channel.email_verified = email_verified
  21. self.channel.save()
  22. self.channel.checks.add(self.check)
  23. @patch("hc.api.transports.requests.request")
  24. def test_slack(self, mock_post):
  25. self._setup_data("123")
  26. mock_post.return_value.status_code = 200
  27. self.channel.notify(self.check)
  28. assert Notification.objects.count() == 1
  29. args, kwargs = mock_post.call_args
  30. payload = kwargs["json"]
  31. attachment = payload["attachments"][0]
  32. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  33. self.assertEqual(fields["Last Ping"], "an hour ago")
  34. # The payload should not contain check's code
  35. serialized = json.dumps(payload)
  36. self.assertNotIn(str(self.check.code), serialized)
  37. @patch("hc.api.transports.requests.request")
  38. def test_slack_with_complex_value(self, mock_post):
  39. v = json.dumps({"incoming_webhook": {"url": "123"}})
  40. self._setup_data(v)
  41. mock_post.return_value.status_code = 200
  42. self.channel.notify(self.check)
  43. assert Notification.objects.count() == 1
  44. args, kwargs = mock_post.call_args
  45. self.assertEqual(args[1], "123")
  46. @patch("hc.api.transports.requests.request")
  47. def test_slack_handles_500(self, mock_post):
  48. self._setup_data("123")
  49. mock_post.return_value.status_code = 500
  50. self.channel.notify(self.check)
  51. n = Notification.objects.get()
  52. self.assertEqual(n.error, "Received status code 500")
  53. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  54. def test_slack_handles_timeout(self, mock_post):
  55. self._setup_data("123")
  56. self.channel.notify(self.check)
  57. n = Notification.objects.get()
  58. self.assertEqual(n.error, "Connection timed out")
  59. @patch("hc.api.transports.requests.request")
  60. def test_slack_with_tabs_in_schedule(self, mock_post):
  61. self._setup_data("123")
  62. self.check.kind = "cron"
  63. self.check.schedule = "*\t* * * *"
  64. self.check.save()
  65. mock_post.return_value.status_code = 200
  66. self.channel.notify(self.check)
  67. self.assertEqual(Notification.objects.count(), 1)
  68. self.assertTrue(mock_post.called)
  69. @override_settings(SLACK_ENABLED=False)
  70. def test_it_requires_slack_enabled(self):
  71. self._setup_data("123")
  72. self.channel.notify(self.check)
  73. n = Notification.objects.get()
  74. self.assertEqual(n.error, "Slack notifications are not enabled.")