test_plugin.py 5.3 KB

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