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.

58 lines
1.8 KiB

  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. from unittest import skipIf
  4. from unittest.mock import patch, Mock
  5. from django.utils.timezone import now
  6. from hc.api.models import Channel, Check, Notification
  7. from hc.test import BaseTestCase
  8. from django.test.utils import override_settings
  9. try:
  10. import apprise
  11. except ImportError:
  12. apprise = None
  13. @skipIf(apprise is None, "apprise not installed")
  14. class NotifyTestCase(BaseTestCase):
  15. def _setup_data(self, value, status="down", email_verified=True):
  16. self.check = Check(project=self.project)
  17. self.check.status = status
  18. self.check.last_ping = now() - td(minutes=61)
  19. self.check.save()
  20. self.channel = Channel(project=self.project)
  21. self.channel.kind = "apprise"
  22. self.channel.value = value
  23. self.channel.email_verified = email_verified
  24. self.channel.save()
  25. self.channel.checks.add(self.check)
  26. @patch("apprise.Apprise")
  27. @override_settings(APPRISE_ENABLED=True)
  28. def test_apprise_enabled(self, mock_apprise):
  29. self._setup_data("123")
  30. mock_aobj = Mock()
  31. mock_aobj.add.return_value = True
  32. mock_aobj.notify.return_value = True
  33. mock_apprise.return_value = mock_aobj
  34. self.channel.notify(self.check)
  35. self.assertEqual(Notification.objects.count(), 1)
  36. self.check.status = "up"
  37. self.assertEqual(Notification.objects.count(), 1)
  38. @patch("apprise.Apprise")
  39. @override_settings(APPRISE_ENABLED=False)
  40. def test_apprise_disabled(self, mock_apprise):
  41. self._setup_data("123")
  42. mock_aobj = Mock()
  43. mock_aobj.add.return_value = True
  44. mock_aobj.notify.return_value = True
  45. mock_apprise.return_value = mock_aobj
  46. self.channel.notify(self.check)
  47. self.assertEqual(Notification.objects.count(), 1)