test_webhooks.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 ..constants import RecipientType
  7. from ..models import Notification
  8. from ..tasks import process_event_alerts
  9. from ..webhooks import (
  10. send_issue_as_discord_webhook,
  11. send_issue_as_webhook,
  12. send_webhook,
  13. )
  14. TEST_URL = "https://burkesoftware.rocket.chat/hooks/Y8TttGY7RvN7Qm3gD/rqhHLiRSvYRZ8BhbhhhLYumdMksWnyj3Dqsqt8QKrmbNndXH"
  15. DISCORD_TEST_URL = "https://discord.com/api/webhooks/not_real_id/not_real_token"
  16. class WebhookTestCase(TestCase):
  17. @mock.patch("requests.post")
  18. def test_send_webhook(self, mock_post):
  19. send_webhook(
  20. TEST_URL,
  21. "from unit test",
  22. )
  23. mock_post.assert_called_once()
  24. @mock.patch("requests.post")
  25. def test_send_issue_as_webhook(self, mock_post):
  26. issue = baker.make("issues.Issue", level=LogLevel.WARNING, short_id=1)
  27. issue2 = baker.make("issues.Issue", level=LogLevel.ERROR, short_id=2)
  28. issue3 = baker.make("issues.Issue", level=LogLevel.NOTSET)
  29. send_issue_as_webhook(TEST_URL, [issue, issue2, issue3], 3)
  30. mock_post.assert_called_once()
  31. @mock.patch("requests.post")
  32. def test_trigger_webhook(self, mock_post):
  33. project = baker.make("projects.Project")
  34. alert = baker.make(
  35. "alerts.ProjectAlert",
  36. project=project,
  37. timespan_minutes=1,
  38. quantity=2,
  39. )
  40. baker.make(
  41. "alerts.AlertRecipient",
  42. alert=alert,
  43. recipient_type=RecipientType.GENERAL_WEBHOOK,
  44. url="example.com",
  45. )
  46. issue = baker.make("issues.Issue", project=project)
  47. baker.make("events.Event", issue=issue)
  48. process_event_alerts()
  49. self.assertEqual(Notification.objects.count(), 0)
  50. baker.make("events.Event", issue=issue)
  51. process_event_alerts()
  52. self.assertEqual(
  53. Notification.objects.filter(
  54. project_alert__alertrecipient__recipient_type=RecipientType.GENERAL_WEBHOOK
  55. ).count(),
  56. 1,
  57. )
  58. mock_post.assert_called_once()
  59. @mock.patch("requests.post")
  60. def test_send_issue_as_discord_webhook(self, mock_post):
  61. issue = baker.make("issues.Issue", level=LogLevel.WARNING, short_id=5)
  62. issue2 = baker.make("issues.Issue", level=LogLevel.ERROR, short_id=6)
  63. issue3 = baker.make("issues.Issue", level=LogLevel.NOTSET)
  64. send_issue_as_discord_webhook(DISCORD_TEST_URL, [issue, issue2, issue3], 3)
  65. mock_post.assert_called_once()