From fbefcbc0ed23f415e0f3a9a5a2fa690abe75071f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Tue, 19 Jan 2021 13:57:55 +0200 Subject: [PATCH] Update apprise tests to skip if apprise is not installed --- hc/api/tests/test_notify.py | 27 -------------- hc/api/tests/test_notify_apprise.py | 58 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 hc/api/tests/test_notify_apprise.py diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index ec7dca5b..5b079f23 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -664,33 +664,6 @@ class NotifyTestCase(BaseTestCase): self.channel.notify(self.check) self.assertTrue(mock_post.called) - @patch("apprise.Apprise") - @override_settings(APPRISE_ENABLED=True) - def test_apprise_enabled(self, mock_apprise): - self._setup_data("apprise", "123") - - mock_aobj = Mock() - mock_aobj.add.return_value = True - mock_aobj.notify.return_value = True - mock_apprise.return_value = mock_aobj - self.channel.notify(self.check) - self.assertEqual(Notification.objects.count(), 1) - - self.check.status = "up" - self.assertEqual(Notification.objects.count(), 1) - - @patch("apprise.Apprise") - @override_settings(APPRISE_ENABLED=False) - def test_apprise_disabled(self, mock_apprise): - self._setup_data("apprise", "123") - - mock_aobj = Mock() - mock_aobj.add.return_value = True - mock_aobj.notify.return_value = True - mock_apprise.return_value = mock_aobj - self.channel.notify(self.check) - self.assertEqual(Notification.objects.count(), 1) - def test_not_implimented(self): self._setup_data("webhook", "http://example") self.channel.kind = "invalid" diff --git a/hc/api/tests/test_notify_apprise.py b/hc/api/tests/test_notify_apprise.py new file mode 100644 index 00000000..8642de2f --- /dev/null +++ b/hc/api/tests/test_notify_apprise.py @@ -0,0 +1,58 @@ +# coding: utf-8 + +from datetime import timedelta as td +from unittest import skipIf +from unittest.mock import patch, Mock + +from django.utils.timezone import now +from hc.api.models import Channel, Check, Notification +from hc.test import BaseTestCase +from django.test.utils import override_settings + +try: + import apprise +except ImportError: + apprise = None + + +@skipIf(apprise is None, "apprise not installed") +class NotifyTestCase(BaseTestCase): + def _setup_data(self, value, status="down", email_verified=True): + self.check = Check(project=self.project) + self.check.status = status + self.check.last_ping = now() - td(minutes=61) + self.check.save() + + self.channel = Channel(project=self.project) + self.channel.kind = "apprise" + self.channel.value = value + self.channel.email_verified = email_verified + self.channel.save() + self.channel.checks.add(self.check) + + @patch("apprise.Apprise") + @override_settings(APPRISE_ENABLED=True) + def test_apprise_enabled(self, mock_apprise): + self._setup_data("123") + + mock_aobj = Mock() + mock_aobj.add.return_value = True + mock_aobj.notify.return_value = True + mock_apprise.return_value = mock_aobj + self.channel.notify(self.check) + self.assertEqual(Notification.objects.count(), 1) + + self.check.status = "up" + self.assertEqual(Notification.objects.count(), 1) + + @patch("apprise.Apprise") + @override_settings(APPRISE_ENABLED=False) + def test_apprise_disabled(self, mock_apprise): + self._setup_data("123") + + mock_aobj = Mock() + mock_aobj.add.return_value = True + mock_aobj.notify.return_value = True + mock_apprise.return_value = mock_aobj + self.channel.notify(self.check) + self.assertEqual(Notification.objects.count(), 1)