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.

90 lines
3.0 KiB

4 years ago
  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 django.test.utils import override_settings
  7. from hc.api.models import Channel, Check, Notification, TokenBucket
  8. from hc.test import BaseTestCase
  9. @override_settings(SIGNAL_CLI_ENABLED=True)
  10. class NotifySignalTestCase(BaseTestCase):
  11. def setUp(self):
  12. super().setUp()
  13. self.check = Check(project=self.project)
  14. self.check.name = "Daily Backup"
  15. self.check.status = "down"
  16. self.check.last_ping = now() - td(minutes=61)
  17. self.check.save()
  18. payload = {"value": "+123456789", "up": True, "down": True}
  19. self.channel = Channel(project=self.project)
  20. self.channel.kind = "signal"
  21. self.channel.value = json.dumps(payload)
  22. self.channel.save()
  23. self.channel.checks.add(self.check)
  24. @patch("hc.api.transports.dbus")
  25. def test_it_works(self, mock_bus):
  26. self.channel.notify(self.check)
  27. n = Notification.objects.get()
  28. self.assertEqual(n.error, "")
  29. args, kwargs = mock_bus.SystemBus.return_value.call_blocking.call_args
  30. message, attachments, recipients = args[-1]
  31. self.assertIn("is DOWN", message)
  32. self.assertEqual(recipients, ["+123456789"])
  33. @patch("hc.api.transports.dbus")
  34. def test_it_obeys_down_flag(self, mock_bus):
  35. payload = {"value": "+123456789", "up": True, "down": False}
  36. self.channel.value = json.dumps(payload)
  37. self.channel.save()
  38. self.channel.notify(self.check)
  39. # This channel should not notify on "down" events:
  40. self.assertEqual(Notification.objects.count(), 0)
  41. self.assertFalse(mock_bus.SystemBus.called)
  42. @patch("hc.api.transports.dbus")
  43. def test_it_requires_signal_cli_enabled(self, mock_bus):
  44. with override_settings(SIGNAL_CLI_ENABLED=False):
  45. self.channel.notify(self.check)
  46. n = Notification.objects.get()
  47. self.assertEqual(n.error, "Signal notifications are not enabled")
  48. self.assertFalse(mock_bus.SystemBus.called)
  49. @patch("hc.api.transports.dbus")
  50. def test_it_does_not_escape_special_characters(self, mock_bus):
  51. self.check.name = "Foo & Bar"
  52. self.check.save()
  53. self.channel.notify(self.check)
  54. args, kwargs = mock_bus.SystemBus.return_value.call_blocking.call_args
  55. message, attachments, recipients = args[-1]
  56. self.assertIn("Foo & Bar", message)
  57. @override_settings(SECRET_KEY="test-secret")
  58. @patch("hc.api.transports.dbus")
  59. def test_it_obeys_rate_limit(self, mock_bus):
  60. # "2862..." is sha1("+123456789test-secret")
  61. obj = TokenBucket(value="signal-2862991ccaa15c8856e7ee0abaf3448fb3c292e0")
  62. obj.tokens = 0
  63. obj.save()
  64. self.channel.notify(self.check)
  65. n = Notification.objects.first()
  66. self.assertEqual(n.error, "Rate limit exceeded")
  67. self.assertFalse(mock_bus.SysemBus.called)