|
6 | 6 |
|
7 | 7 | import jwt |
8 | 8 | import responses |
| 9 | +from jwt import DecodeError, ExpiredSignatureError, InvalidSignatureError |
9 | 10 | from rest_framework import status |
10 | 11 |
|
11 | 12 | from sentry.constants import ObjectStatus |
|
16 | 17 | AtlassianConnectValidationError, |
17 | 18 | get_query_hash, |
18 | 19 | ) |
19 | | -from sentry.testutils.asserts import assert_count_of_metric, assert_halt_metric |
| 20 | +from sentry.testutils.asserts import ( |
| 21 | + assert_count_of_metric, |
| 22 | + assert_failure_metric, |
| 23 | + assert_halt_metric, |
| 24 | +) |
20 | 25 | from sentry.testutils.cases import APITestCase |
21 | 26 | from sentry.testutils.silo import control_silo_test |
22 | 27 | from sentry.utils.http import absolute_uri |
@@ -104,7 +109,85 @@ def test_no_claims(self, mock_authenticate_asymmetric_jwt: MagicMock) -> None: |
104 | 109 | self.get_error_response( |
105 | 110 | **self.body(), |
106 | 111 | extra_headers=dict(HTTP_AUTHORIZATION="JWT " + self.jwt_token_cdn()), |
107 | | - status_code=status.HTTP_409_CONFLICT, |
| 112 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 113 | + ) |
| 114 | + |
| 115 | + @patch( |
| 116 | + "sentry.integrations.jira.webhooks.installed.authenticate_asymmetric_jwt", |
| 117 | + side_effect=ExpiredSignatureError(), |
| 118 | + ) |
| 119 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 120 | + @responses.activate |
| 121 | + def test_expired_signature( |
| 122 | + self, mock_record_event: MagicMock, mock_authenticate_asymmetric_jwt: MagicMock |
| 123 | + ) -> None: |
| 124 | + self.add_response() |
| 125 | + |
| 126 | + self.get_error_response( |
| 127 | + **self.body(), |
| 128 | + extra_headers=dict(HTTP_AUTHORIZATION="JWT " + self.jwt_token_cdn()), |
| 129 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 130 | + ) |
| 131 | + # SLO metric asserts |
| 132 | + # ENSURE_CONTROL_SILO (success) -> VERIFY_INSTALLATION (failure) -> GET_CONTROL_RESPONSE (success) |
| 133 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.STARTED, 3) |
| 134 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.FAILURE, 1) |
| 135 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.SUCCESS, 2) |
| 136 | + assert_failure_metric( |
| 137 | + mock_record_event, |
| 138 | + ExpiredSignatureError(), |
| 139 | + ) |
| 140 | + |
| 141 | + @patch( |
| 142 | + "sentry.integrations.jira.webhooks.installed.authenticate_asymmetric_jwt", |
| 143 | + side_effect=InvalidSignatureError(), |
| 144 | + ) |
| 145 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 146 | + @responses.activate |
| 147 | + def test_invalid_signature( |
| 148 | + self, mock_record_event: MagicMock, mock_authenticate_asymmetric_jwt: MagicMock |
| 149 | + ) -> None: |
| 150 | + self.add_response() |
| 151 | + |
| 152 | + self.get_error_response( |
| 153 | + **self.body(), |
| 154 | + extra_headers=dict(HTTP_AUTHORIZATION="JWT " + self.jwt_token_cdn()), |
| 155 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 156 | + ) |
| 157 | + # SLO metric asserts |
| 158 | + # ENSURE_CONTROL_SILO (success) -> VERIFY_INSTALLATION (halt) -> GET_CONTROL_RESPONSE (success) |
| 159 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.STARTED, 3) |
| 160 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.HALTED, 1) |
| 161 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.SUCCESS, 2) |
| 162 | + assert_halt_metric( |
| 163 | + mock_record_event, |
| 164 | + "JWT contained invalid signature", |
| 165 | + ) |
| 166 | + |
| 167 | + @patch( |
| 168 | + "sentry.integrations.jira.webhooks.installed.authenticate_asymmetric_jwt", |
| 169 | + side_effect=DecodeError(), |
| 170 | + ) |
| 171 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 172 | + @responses.activate |
| 173 | + def test_decode_error( |
| 174 | + self, mock_record_event: MagicMock, mock_authenticate_asymmetric_jwt: MagicMock |
| 175 | + ) -> None: |
| 176 | + self.add_response() |
| 177 | + |
| 178 | + self.get_error_response( |
| 179 | + **self.body(), |
| 180 | + extra_headers=dict(HTTP_AUTHORIZATION="JWT " + self.jwt_token_cdn()), |
| 181 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 182 | + ) |
| 183 | + # SLO metric asserts |
| 184 | + # ENSURE_CONTROL_SILO (success) -> VERIFY_INSTALLATION (halt) -> GET_CONTROL_RESPONSE (success) |
| 185 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.STARTED, 3) |
| 186 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.HALTED, 1) |
| 187 | + assert_count_of_metric(mock_record_event, EventLifecycleOutcome.SUCCESS, 2) |
| 188 | + assert_halt_metric( |
| 189 | + mock_record_event, |
| 190 | + "Could not decode JWT token", |
108 | 191 | ) |
109 | 192 |
|
110 | 193 | @patch("sentry_sdk.set_tag") |
|
0 commit comments