test_webhooks.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from unittest import mock
  2. from django.test import TestCase
  3. from model_bakery import baker
  4. from events.models import LogLevel
  5. from glitchtip import test_utils # pylint: disable=unused-import
  6. from ..models import AlertRecipient, Notification
  7. from ..tasks import process_event_alerts
  8. from ..webhooks import send_issue_as_webhook, send_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,
  15. "from unit test",
  16. )
  17. mock_post.assert_called_once()
  18. @mock.patch("requests.post")
  19. def test_send_issue_as_webhook(self, mock_post):
  20. issue = baker.make("issues.Issue", level=LogLevel.WARNING, short_id=1)
  21. issue2 = baker.make("issues.Issue", level=LogLevel.ERROR, short_id=2)
  22. issue3 = baker.make("issues.Issue", level=LogLevel.NOTSET)
  23. send_issue_as_webhook(TEST_URL, [issue, issue2, issue3], 3)
  24. mock_post.assert_called_once()
  25. @mock.patch("requests.post")
  26. def test_trigger_webhook(self, mock_post):
  27. project = baker.make("projects.Project")
  28. alert = baker.make(
  29. "alerts.ProjectAlert",
  30. project=project,
  31. timespan_minutes=1,
  32. quantity=2,
  33. )
  34. baker.make(
  35. "alerts.AlertRecipient",
  36. alert=alert,
  37. recipient_type=AlertRecipient.RecipientType.WEBHOOK,
  38. url="example.com",
  39. )
  40. issue = baker.make("issues.Issue", project=project)
  41. baker.make("events.Event", issue=issue)
  42. process_event_alerts()
  43. self.assertEqual(Notification.objects.count(), 0)
  44. baker.make("events.Event", issue=issue)
  45. process_event_alerts()
  46. self.assertEqual(
  47. Notification.objects.filter(
  48. project_alert__alertrecipient__recipient_type=AlertRecipient.RecipientType.WEBHOOK
  49. ).count(),
  50. 1,
  51. )
  52. mock_post.assert_called_once()