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.

62 lines
2.0 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from django.contrib.auth.models import User
  2. from django.core import mail
  3. from django.test import TestCase
  4. from mock import patch
  5. from requests.exceptions import ReadTimeout
  6. from hc.api.models import Channel, Check, Notification
  7. class NotifyTestCase(TestCase):
  8. def _setup_data(self, channel_kind, channel_value, email_verified=True):
  9. self.alice = User(username="alice")
  10. self.alice.save()
  11. self.check = Check()
  12. self.check.status = "down"
  13. self.check.save()
  14. self.channel = Channel(user=self.alice)
  15. self.channel.kind = channel_kind
  16. self.channel.value = channel_value
  17. self.channel.email_verified = email_verified
  18. self.channel.save()
  19. self.channel.checks.add(self.check)
  20. @patch("hc.api.models.requests.get")
  21. def test_webhook(self, mock_get):
  22. self._setup_data("webhook", "http://example")
  23. self.channel.notify(self.check)
  24. mock_get.assert_called_with(u"http://example", timeout=5)
  25. @patch("hc.api.models.requests.get", side_effect=ReadTimeout)
  26. def test_webhooks_handle_timeouts(self, mock_get):
  27. self._setup_data("webhook", "http://example")
  28. self.channel.notify(self.check)
  29. assert Notification.objects.count() == 1
  30. def test_email(self):
  31. self._setup_data("email", "[email protected]")
  32. self.channel.notify(self.check)
  33. assert Notification.objects.count() == 1
  34. # And email should have been sent
  35. self.assertEqual(len(mail.outbox), 1)
  36. def test_it_skips_unverified_email(self):
  37. self._setup_data("email", "[email protected]", email_verified=False)
  38. self.channel.notify(self.check)
  39. assert Notification.objects.count() == 0
  40. self.assertEqual(len(mail.outbox), 0)
  41. @patch("hc.api.models.requests.post")
  42. def test_pd(self, mock_post):
  43. self._setup_data("pd", "123")
  44. self.channel.notify(self.check)
  45. assert Notification.objects.count() == 1
  46. args, kwargs = mock_post.call_args
  47. assert "trigger" in kwargs["data"]