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.

98 lines
3.3 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. @override_settings(SITE_ROOT="http://testserver")
  24. @patch("hc.api.transports.requests.request")
  25. def test_slack(self, mock_post):
  26. self._setup_data("123")
  27. mock_post.return_value.status_code = 200
  28. self.channel.notify(self.check)
  29. assert Notification.objects.count() == 1
  30. args, kwargs = mock_post.call_args
  31. payload = kwargs["json"]
  32. attachment = payload["attachments"][0]
  33. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  34. self.assertEqual(fields["Last Ping"], "an hour ago")
  35. # The payload should not contain check's code
  36. serialized = json.dumps(payload)
  37. self.assertNotIn(str(self.check.code), serialized)
  38. self.assertIn("http://testserver/static/img/logo.png", serialized)
  39. @patch("hc.api.transports.requests.request")
  40. def test_slack_with_complex_value(self, mock_post):
  41. v = json.dumps({"incoming_webhook": {"url": "123"}})
  42. self._setup_data(v)
  43. mock_post.return_value.status_code = 200
  44. self.channel.notify(self.check)
  45. assert Notification.objects.count() == 1
  46. args, kwargs = mock_post.call_args
  47. self.assertEqual(args[1], "123")
  48. @patch("hc.api.transports.requests.request")
  49. def test_slack_handles_500(self, mock_post):
  50. self._setup_data("123")
  51. mock_post.return_value.status_code = 500
  52. self.channel.notify(self.check)
  53. n = Notification.objects.get()
  54. self.assertEqual(n.error, "Received status code 500")
  55. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  56. def test_slack_handles_timeout(self, mock_post):
  57. self._setup_data("123")
  58. self.channel.notify(self.check)
  59. n = Notification.objects.get()
  60. self.assertEqual(n.error, "Connection timed out")
  61. @patch("hc.api.transports.requests.request")
  62. def test_slack_with_tabs_in_schedule(self, mock_post):
  63. self._setup_data("123")
  64. self.check.kind = "cron"
  65. self.check.schedule = "*\t* * * *"
  66. self.check.save()
  67. mock_post.return_value.status_code = 200
  68. self.channel.notify(self.check)
  69. self.assertEqual(Notification.objects.count(), 1)
  70. self.assertTrue(mock_post.called)
  71. @override_settings(SLACK_ENABLED=False)
  72. def test_it_requires_slack_enabled(self):
  73. self._setup_data("123")
  74. self.channel.notify(self.check)
  75. n = Notification.objects.get()
  76. self.assertEqual(n.error, "Slack notifications are not enabled.")