test_webhooks.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import json
  2. from unittest import mock
  3. from django.test import TestCase
  4. from model_bakery import baker
  5. from apps.issue_events.constants import LogLevel
  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. def setUp(self):
  20. self.environment_name = "test-environment"
  21. self.release_name = "test-release"
  22. def generate_issue_with_tags(self):
  23. key_environment = baker.make("issue_events.TagKey", key="environment")
  24. environment_value = baker.make(
  25. "issue_events.TagValue", value=self.environment_name
  26. )
  27. key_release = baker.make("issue_events.TagKey", key="release")
  28. release_value = baker.make("issue_events.TagValue", value=self.release_name)
  29. issue = baker.make("issue_events.Issue", level=LogLevel.ERROR)
  30. baker.make(
  31. "issue_events.IssueTag",
  32. issue=issue,
  33. tag_key=key_environment,
  34. tag_value=environment_value,
  35. )
  36. baker.make(
  37. "issue_events.IssueTag",
  38. issue=issue,
  39. tag_key=key_release,
  40. tag_value=release_value,
  41. )
  42. return issue
  43. @mock.patch("requests.post")
  44. def test_send_webhook(self, mock_post):
  45. send_webhook(
  46. TEST_URL,
  47. "from unit test",
  48. )
  49. mock_post.assert_called_once()
  50. @mock.patch("requests.post")
  51. def test_send_issue_as_webhook(self, mock_post):
  52. issue = self.generate_issue_with_tags()
  53. issue2 = baker.make("issue_events.Issue", level=LogLevel.ERROR, short_id=2)
  54. issue3 = baker.make("issue_events.Issue", level=LogLevel.NOTSET)
  55. send_issue_as_webhook(TEST_URL, [issue, issue2, issue3], 3)
  56. mock_post.assert_called_once()
  57. first_issue_json_data = json.dumps(
  58. mock_post.call_args.kwargs["json"]["attachments"][0]
  59. )
  60. self.assertIn(
  61. f'"title": "Environment", "value": "{self.environment_name}"',
  62. first_issue_json_data,
  63. )
  64. self.assertIn(
  65. f'"title": "Release", "value": "{self.release_name}"', first_issue_json_data
  66. )
  67. @mock.patch("requests.post")
  68. def test_trigger_webhook(self, mock_post):
  69. project = baker.make("projects.Project")
  70. alert = baker.make(
  71. "alerts.ProjectAlert",
  72. project=project,
  73. timespan_minutes=1,
  74. quantity=2,
  75. )
  76. baker.make(
  77. "alerts.AlertRecipient",
  78. alert=alert,
  79. recipient_type=RecipientType.GENERAL_WEBHOOK,
  80. url="example.com",
  81. )
  82. issue = baker.make("issue_events.Issue", project=project)
  83. baker.make("issue_events.IssueEvent", issue=issue)
  84. process_event_alerts()
  85. self.assertEqual(Notification.objects.count(), 0)
  86. baker.make("issue_events.IssueEvent", issue=issue)
  87. process_event_alerts()
  88. self.assertEqual(
  89. Notification.objects.filter(
  90. project_alert__alertrecipient__recipient_type=RecipientType.GENERAL_WEBHOOK
  91. ).count(),
  92. 1,
  93. )
  94. mock_post.assert_called_once()
  95. self.assertIn(
  96. issue.title, mock_post.call_args[1]["json"]["sections"][0]["activityTitle"]
  97. )
  98. @mock.patch("requests.post")
  99. def test_send_issue_with_tags_as_discord_webhook(self, mock_post):
  100. issue = self.generate_issue_with_tags()
  101. send_issue_as_discord_webhook(DISCORD_TEST_URL, [issue])
  102. mock_post.assert_called_once()
  103. json_data = json.dumps(mock_post.call_args.kwargs["json"])
  104. self.assertIn(
  105. f'"name": "Environment", "value": "{self.environment_name}"', json_data
  106. )
  107. self.assertIn(f'"name": "Release", "value": "{self.release_name}"', json_data)
  108. @mock.patch("requests.post")
  109. def test_send_issue_with_tags_as_googlechat_webhook(self, mock_post):
  110. issue = self.generate_issue_with_tags()
  111. send_issue_as_googlechat_webhook(GOOGLE_CHAT_TEST_URL, [issue])
  112. mock_post.assert_called_once()
  113. json_data = json.dumps(mock_post.call_args.kwargs["json"])
  114. self.assertIn(
  115. f'"topLabel": "Release", "text": "{self.release_name}"', json_data
  116. )
  117. self.assertIn(
  118. f'"topLabel": "Environment", "text": "{self.environment_name}"',
  119. json_data,
  120. )