test_webhooks.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from unittest import mock
  2. from django.test import TestCase
  3. from model_bakery import baker
  4. from glitchtip import test_utils # pylint: disable=unused-import
  5. from events.models import LogLevel
  6. from ..tasks import process_event_alerts
  7. from ..models import AlertRecipient, Notification
  8. from ..webhooks import send_webhook, send_issue_as_webhook
  9. TEST_URL = "https://burkesoftware.rocket.chat/hooks/Y8TttGY7RvN7Qm3gD/rqhHLiRSvYRZ8BhbhhhLYumdMksWnyj3Dqsqt8QKrmbNndXH"
  10. class WebhookTestCase(TestCase):
  11. @mock.patch("requests.post")
  12. def test_send_webhook(self, mock_post):
  13. send_webhook(
  14. TEST_URL, "from unit test",
  15. )
  16. mock_post.assert_called_once()
  17. @mock.patch("requests.post")
  18. def test_send_issue_as_webhook(self, mock_post):
  19. issue = baker.make("issues.Issue", level=LogLevel.WARNING, short_id=1)
  20. issue2 = baker.make("issues.Issue", level=LogLevel.ERROR, short_id=2)
  21. issue3 = baker.make("issues.Issue", level=LogLevel.NOTSET)
  22. send_issue_as_webhook(TEST_URL, [issue, issue2, issue3], 3)
  23. mock_post.assert_called_once()
  24. @mock.patch("requests.post")
  25. def test_trigger_webhook(self, mock_post):
  26. project = baker.make("projects.Project")
  27. alert = baker.make(
  28. "alerts.ProjectAlert", project=project, timespan_minutes=1, quantity=2,
  29. )
  30. baker.make(
  31. "alerts.AlertRecipient",
  32. alert=alert,
  33. recipient_type=AlertRecipient.RecipientType.WEBHOOK,
  34. url="example.com",
  35. )
  36. issue = baker.make("issues.Issue", project=project)
  37. baker.make("events.Event", issue=issue)
  38. process_event_alerts()
  39. self.assertEqual(Notification.objects.count(), 0)
  40. baker.make("events.Event", issue=issue)
  41. process_event_alerts()
  42. self.assertEqual(
  43. Notification.objects.filter(
  44. project_alert__alertrecipient__recipient_type=AlertRecipient.RecipientType.WEBHOOK
  45. ).count(),
  46. 1,
  47. )
  48. mock_post.assert_called_once()