|
10 | 10 | get_autofix_repos_from_project_code_mappings,
|
11 | 11 | get_autofix_state,
|
12 | 12 | get_autofix_state_from_pr_id,
|
| 13 | + is_seer_autotriggered_autofix_rate_limited, |
| 14 | + is_seer_scanner_rate_limited, |
13 | 15 | )
|
14 | 16 | from sentry.testutils.cases import TestCase
|
| 17 | +from sentry.testutils.helpers import with_feature |
15 | 18 | from sentry.utils import json
|
16 | 19 |
|
17 | 20 |
|
@@ -183,3 +186,63 @@ def test_get_autofix_state_http_error(self, mock_post):
|
183 | 186 |
|
184 | 187 | # Assertions
|
185 | 188 | assert "HTTP Error" in str(context.value)
|
| 189 | + |
| 190 | + |
| 191 | +class TestAutomationRateLimiting(TestCase): |
| 192 | + @with_feature("organizations:unlimited-auto-triggered-autofix-runs") |
| 193 | + def test_scanner_rate_limited_with_unlimited_flag(self): |
| 194 | + """Test scanner rate limiting bypassed with unlimited feature flag""" |
| 195 | + project = self.create_project() |
| 196 | + organization = project.organization |
| 197 | + |
| 198 | + is_rate_limited, current, limit = is_seer_scanner_rate_limited(project, organization) |
| 199 | + |
| 200 | + assert is_rate_limited is False |
| 201 | + assert current == 0 |
| 202 | + assert limit == 0 |
| 203 | + |
| 204 | + @patch("sentry.autofix.utils.ratelimits.backend.is_limited_with_value") |
| 205 | + def test_scanner_rate_limited_logic(self, mock_is_limited): |
| 206 | + """Test scanner rate limiting logic""" |
| 207 | + project = self.create_project() |
| 208 | + organization = project.organization |
| 209 | + |
| 210 | + mock_is_limited.return_value = (True, 950, None) |
| 211 | + |
| 212 | + with self.options({"seer.max_num_scanner_autotriggered_per_hour": 1000}): |
| 213 | + is_rate_limited, current, limit = is_seer_scanner_rate_limited(project, organization) |
| 214 | + |
| 215 | + assert is_rate_limited is True |
| 216 | + assert current == 950 |
| 217 | + assert limit == 1000 |
| 218 | + |
| 219 | + @with_feature("organizations:unlimited-auto-triggered-autofix-runs") |
| 220 | + def test_autofix_rate_limited_with_unlimited_flag(self): |
| 221 | + """Test autofix rate limiting bypassed with unlimited feature flag""" |
| 222 | + project = self.create_project() |
| 223 | + organization = project.organization |
| 224 | + |
| 225 | + is_rate_limited, current, limit = is_seer_autotriggered_autofix_rate_limited( |
| 226 | + project, organization |
| 227 | + ) |
| 228 | + |
| 229 | + assert is_rate_limited is False |
| 230 | + assert current == 0 |
| 231 | + assert limit == 0 |
| 232 | + |
| 233 | + @patch("sentry.autofix.utils.ratelimits.backend.is_limited_with_value") |
| 234 | + def test_autofix_rate_limited_logic(self, mock_is_limited): |
| 235 | + """Test autofix rate limiting logic""" |
| 236 | + project = self.create_project() |
| 237 | + organization = project.organization |
| 238 | + |
| 239 | + mock_is_limited.return_value = (True, 19, None) |
| 240 | + |
| 241 | + with self.options({"seer.max_num_autofix_autotriggered_per_hour": 20}): |
| 242 | + is_rate_limited, current, limit = is_seer_autotriggered_autofix_rate_limited( |
| 243 | + project, organization |
| 244 | + ) |
| 245 | + |
| 246 | + assert is_rate_limited is True |
| 247 | + assert current == 19 |
| 248 | + assert limit == 20 |
0 commit comments