test_webhooks.py 3.1 KB

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