test_plugin.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from unittest.mock import patch
  2. import pytest
  3. from botocore.client import ClientError
  4. from exam import fixture
  5. from sentry.testutils import PluginTestCase
  6. from sentry.utils import json
  7. from sentry_plugins.amazon_sqs.plugin import AmazonSQSPlugin
  8. class AmazonSQSPluginTest(PluginTestCase):
  9. @fixture
  10. def plugin(self):
  11. return AmazonSQSPlugin()
  12. def test_conf_key(self):
  13. assert self.plugin.conf_key == "amazon-sqs"
  14. def test_entry_point(self):
  15. self.assertPluginInstalled("amazon_sqs", self.plugin)
  16. def run_test(self):
  17. self.plugin.set_option("access_key", "access-key", self.project)
  18. self.plugin.set_option("secret_key", "secret-key", self.project)
  19. self.plugin.set_option("region", "us-east-1", self.project)
  20. self.plugin.set_option(
  21. "queue_url", "https://sqs-us-east-1.amazonaws.com/12345678/myqueue", self.project
  22. )
  23. event = self.store_event(
  24. data={
  25. "sentry.interfaces.Exception": {"type": "ValueError", "value": "foo bar"},
  26. "sentry.interfaces.User": {"id": "1", "email": "foo@example.com"},
  27. "type": "error",
  28. "metadata": {"type": "ValueError", "value": "foo bar"},
  29. },
  30. project_id=self.project.id,
  31. )
  32. with self.options({"system.url-prefix": "http://example.com"}):
  33. self.plugin.post_process(event)
  34. return event
  35. @patch("boto3.client")
  36. def test_simple_notification(self, mock_client):
  37. event = self.run_test()
  38. mock_client.assert_called_once_with(
  39. service_name="sqs",
  40. region_name="us-east-1",
  41. aws_access_key_id="access-key",
  42. aws_secret_access_key="secret-key",
  43. )
  44. mock_client.return_value.send_message.assert_called_once_with(
  45. QueueUrl="https://sqs-us-east-1.amazonaws.com/12345678/myqueue",
  46. MessageBody=json.dumps(self.plugin.get_event_payload(event)),
  47. )
  48. @patch("sentry_plugins.amazon_sqs.plugin.logger")
  49. @patch("boto3.client")
  50. def test_token_error(self, mock_client, logger):
  51. mock_client.return_value.send_message.side_effect = ClientError(
  52. {"Error": {"Code": "Hello", "Message": "hello"}}, "SendMessage"
  53. )
  54. with pytest.raises(ClientError):
  55. self.run_test()
  56. assert len(logger.info.call_args_list) == 0
  57. mock_client.return_value.send_message.side_effect = ClientError(
  58. {"Error": {"Code": "AccessDenied", "Message": "Hello"}}, "SendMessage"
  59. )
  60. self.run_test()
  61. assert len(logger.info.call_args_list) == 1
  62. assert (
  63. logger.info.call_args_list[0][0][0] == "sentry_plugins.amazon_sqs.access_token_invalid"
  64. )
  65. @patch("sentry_plugins.amazon_sqs.plugin.logger")
  66. @patch("boto3.client")
  67. def test_message_group_error(self, mock_client, logger):
  68. mock_client.return_value.send_message.side_effect = ClientError(
  69. {
  70. "Error": {
  71. "Code": "MissingParameter",
  72. "Message": "The request must contain the parameter MessageGroupId.",
  73. }
  74. },
  75. "SendMessage",
  76. )
  77. self.run_test()
  78. assert len(logger.info.call_args_list) == 1
  79. assert (
  80. logger.info.call_args_list[0][0][0]
  81. == "sentry_plugins.amazon_sqs.missing_message_group_id"
  82. )
  83. @patch("uuid.uuid4")
  84. @patch("boto3.client")
  85. def test_pass_message_group_id(self, mock_client, mock_uuid):
  86. mock_uuid.return_value = self.get_mock_uuid()
  87. self.plugin.set_option("message_group_id", "my_group", self.project)
  88. event = self.run_test()
  89. mock_client.return_value.send_message.assert_called_once_with(
  90. QueueUrl="https://sqs-us-east-1.amazonaws.com/12345678/myqueue",
  91. MessageBody=json.dumps(self.plugin.get_event_payload(event)),
  92. MessageGroupId="my_group",
  93. MessageDeduplicationId="abc123",
  94. )
  95. @patch("boto3.client")
  96. def test_use_s3_bucket(self, mock_client):
  97. self.plugin.set_option("s3_bucket", "my_bucket", self.project)
  98. event = self.run_test()
  99. date = event.datetime.strftime("%Y-%m-%d")
  100. key = f"{event.project.slug}/{date}/{event.event_id}"
  101. mock_client.return_value.send_message.assert_called_once_with(
  102. QueueUrl="https://sqs-us-east-1.amazonaws.com/12345678/myqueue",
  103. MessageBody=json.dumps(
  104. {
  105. "s3Url": f"https://my_bucket.s3-us-east-1.amazonaws.com/{key}",
  106. "eventID": event.event_id,
  107. }
  108. ),
  109. )
  110. mock_client.return_value.put_object.assert_called_once_with(
  111. Bucket="my_bucket", Body=json.dumps(self.plugin.get_event_payload(event)), Key=key
  112. )
  113. @patch("sentry_plugins.amazon_sqs.plugin.logger")
  114. @patch("boto3.client")
  115. def test_invalid_s3_bucket(self, mock_client, logger):
  116. self.plugin.set_option("s3_bucket", "bad_bucket", self.project)
  117. mock_client.return_value.put_object.side_effect = ClientError(
  118. {"Error": {"Code": "NoSuchBucket"}},
  119. "PutObject",
  120. )
  121. self.run_test()
  122. assert len(logger.info.call_args_list) == 2
  123. assert logger.info.call_args_list[1][0][0] == "sentry_plugins.amazon_sqs.s3_bucket_invalid"