|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +from sentry.constants import DataCategory |
| 4 | +from sentry.quotas.base import SeatAssignmentResult |
1 | 5 | from sentry.testutils.cases import TestCase, UptimeTestCase |
2 | 6 | from sentry.uptime.endpoints.validators import ( |
| 7 | + UptimeDomainCheckFailureValidator, |
3 | 8 | UptimeMonitorDataSourceValidator, |
4 | 9 | compute_http_request_size, |
5 | 10 | ) |
6 | | -from sentry.uptime.models import UptimeSubscription |
| 11 | +from sentry.uptime.grouptype import UptimeDomainCheckFailure |
| 12 | +from sentry.uptime.models import UptimeSubscription, get_uptime_subscription |
| 13 | +from sentry.uptime.types import ( |
| 14 | + DEFAULT_DOWNTIME_THRESHOLD, |
| 15 | + DEFAULT_RECOVERY_THRESHOLD, |
| 16 | + UptimeMonitorMode, |
| 17 | +) |
| 18 | +from sentry.utils.outcomes import Outcome |
7 | 19 |
|
8 | 20 |
|
9 | 21 | class ComputeHttpRequestSizeTest(UptimeTestCase): |
@@ -97,3 +109,188 @@ def test_too_big_request(self): |
97 | 109 | data = self.get_valid_data(body="0" * 1000) |
98 | 110 | validator = UptimeMonitorDataSourceValidator(data=data, context=self.context) |
99 | 111 | assert not validator.is_valid() |
| 112 | + |
| 113 | + |
| 114 | +class UptimeDomainCheckFailureValidatorTest(UptimeTestCase): |
| 115 | + def setUp(self): |
| 116 | + super().setUp() |
| 117 | + self.context = { |
| 118 | + "organization": self.organization, |
| 119 | + "project": self.project, |
| 120 | + "request": self.make_request(user=self.user), |
| 121 | + } |
| 122 | + |
| 123 | + def get_valid_data(self, **kwargs): |
| 124 | + return { |
| 125 | + "name": kwargs.get("name", "Test Uptime Monitor"), |
| 126 | + "type": UptimeDomainCheckFailure.slug, |
| 127 | + "enabled": kwargs.get("enabled", True), |
| 128 | + "config": kwargs.get( |
| 129 | + "config", |
| 130 | + { |
| 131 | + "mode": UptimeMonitorMode.MANUAL.value, |
| 132 | + "environment": None, |
| 133 | + "recovery_threshold": DEFAULT_RECOVERY_THRESHOLD, |
| 134 | + "downtime_threshold": DEFAULT_DOWNTIME_THRESHOLD, |
| 135 | + }, |
| 136 | + ), |
| 137 | + "dataSources": kwargs.get( |
| 138 | + "data_sources", |
| 139 | + [ |
| 140 | + { |
| 141 | + "url": "https://sentry.io", |
| 142 | + "intervalSeconds": 60, |
| 143 | + "timeoutMs": 1000, |
| 144 | + } |
| 145 | + ], |
| 146 | + ), |
| 147 | + } |
| 148 | + |
| 149 | + @mock.patch( |
| 150 | + "sentry.quotas.backend.assign_seat", |
| 151 | + return_value=Outcome.ACCEPTED, |
| 152 | + ) |
| 153 | + def test_create_enabled_assigns_seat(self, mock_assign_seat: mock.MagicMock) -> None: |
| 154 | + """Test that creating an enabled detector assigns a billing seat.""" |
| 155 | + validator = UptimeDomainCheckFailureValidator( |
| 156 | + data=self.get_valid_data(enabled=True), context=self.context |
| 157 | + ) |
| 158 | + assert validator.is_valid(), validator.errors |
| 159 | + detector = validator.save() |
| 160 | + |
| 161 | + detector.refresh_from_db() |
| 162 | + assert detector.enabled is True |
| 163 | + |
| 164 | + # Verify seat was assigned |
| 165 | + mock_assign_seat.assert_called_with(DataCategory.UPTIME, detector) |
| 166 | + |
| 167 | + @mock.patch( |
| 168 | + "sentry.quotas.backend.assign_seat", |
| 169 | + return_value=Outcome.RATE_LIMITED, |
| 170 | + ) |
| 171 | + def test_create_enabled_no_seat_available(self, mock_assign_seat: mock.MagicMock) -> None: |
| 172 | + """Test that creating a detector with no seats available creates it but leaves it disabled.""" |
| 173 | + validator = UptimeDomainCheckFailureValidator( |
| 174 | + data=self.get_valid_data(enabled=True), context=self.context |
| 175 | + ) |
| 176 | + assert validator.is_valid(), validator.errors |
| 177 | + detector = validator.save() |
| 178 | + |
| 179 | + detector.refresh_from_db() |
| 180 | + # Detector created but not enabled due to no seat assignment |
| 181 | + assert detector.enabled is False |
| 182 | + |
| 183 | + # Verify seat assignment was attempted |
| 184 | + mock_assign_seat.assert_called_with(DataCategory.UPTIME, detector) |
| 185 | + |
| 186 | + uptime_subscription = get_uptime_subscription(detector) |
| 187 | + assert uptime_subscription.status == UptimeSubscription.Status.DISABLED.value |
| 188 | + |
| 189 | + @mock.patch( |
| 190 | + "sentry.quotas.backend.assign_seat", |
| 191 | + return_value=Outcome.ACCEPTED, |
| 192 | + ) |
| 193 | + def test_update_enable_assigns_seat(self, mock_assign_seat: mock.MagicMock) -> None: |
| 194 | + """Test that enabling a previously disabled detector assigns a seat.""" |
| 195 | + # Create a disabled detector |
| 196 | + detector = self.create_uptime_detector(enabled=False) |
| 197 | + |
| 198 | + validator = UptimeDomainCheckFailureValidator( |
| 199 | + instance=detector, data={"enabled": True}, context=self.context, partial=True |
| 200 | + ) |
| 201 | + assert validator.is_valid(), validator.errors |
| 202 | + validator.save() |
| 203 | + |
| 204 | + detector.refresh_from_db() |
| 205 | + assert detector.enabled is True |
| 206 | + |
| 207 | + # Verify seat was assigned |
| 208 | + mock_assign_seat.assert_called_with(DataCategory.UPTIME, detector) |
| 209 | + |
| 210 | + uptime_subscription = get_uptime_subscription(detector) |
| 211 | + assert uptime_subscription.status == UptimeSubscription.Status.ACTIVE.value |
| 212 | + |
| 213 | + @mock.patch( |
| 214 | + "sentry.quotas.backend.check_assign_seat", |
| 215 | + return_value=SeatAssignmentResult(assignable=False, reason="No seats available"), |
| 216 | + ) |
| 217 | + def test_update_enable_no_seat_available(self, mock_check_assign_seat: mock.MagicMock) -> None: |
| 218 | + """Test that enabling fails with validation error when no seats are available.""" |
| 219 | + # Create a disabled detector |
| 220 | + detector = self.create_uptime_detector(enabled=False) |
| 221 | + |
| 222 | + validator = UptimeDomainCheckFailureValidator( |
| 223 | + instance=detector, data={"enabled": True}, context=self.context, partial=True |
| 224 | + ) |
| 225 | + |
| 226 | + # Validation should fail due to no seats available |
| 227 | + assert not validator.is_valid() |
| 228 | + assert "enabled" in validator.errors |
| 229 | + assert validator.errors["enabled"] == ["No seats available"] |
| 230 | + |
| 231 | + detector.refresh_from_db() |
| 232 | + # Detector should still be disabled |
| 233 | + assert detector.enabled is False |
| 234 | + |
| 235 | + # Verify seat availability check was performed |
| 236 | + mock_check_assign_seat.assert_called_with(DataCategory.UPTIME, detector) |
| 237 | + |
| 238 | + @mock.patch("sentry.quotas.backend.disable_seat") |
| 239 | + def test_update_disable_removes_seat(self, mock_disable_seat: mock.MagicMock) -> None: |
| 240 | + """Test that disabling a previously enabled detector removes the seat.""" |
| 241 | + # Create an enabled detector |
| 242 | + detector = self.create_uptime_detector(enabled=True) |
| 243 | + |
| 244 | + validator = UptimeDomainCheckFailureValidator( |
| 245 | + instance=detector, data={"enabled": False}, context=self.context, partial=True |
| 246 | + ) |
| 247 | + assert validator.is_valid(), validator.errors |
| 248 | + validator.save() |
| 249 | + |
| 250 | + detector.refresh_from_db() |
| 251 | + assert detector.enabled is False |
| 252 | + |
| 253 | + # Verify disable_seat was called |
| 254 | + mock_disable_seat.assert_called_with(DataCategory.UPTIME, detector) |
| 255 | + |
| 256 | + uptime_subscription = get_uptime_subscription(detector) |
| 257 | + assert uptime_subscription.status == UptimeSubscription.Status.DISABLED.value |
| 258 | + |
| 259 | + @mock.patch("sentry.quotas.backend.remove_seat") |
| 260 | + def test_delete_removes_seat(self, mock_remove_seat: mock.MagicMock) -> None: |
| 261 | + """Test that deleting a detector removes its billing seat immediately.""" |
| 262 | + detector = self.create_uptime_detector(enabled=True) |
| 263 | + |
| 264 | + validator = UptimeDomainCheckFailureValidator( |
| 265 | + instance=detector, data={}, context=self.context |
| 266 | + ) |
| 267 | + |
| 268 | + validator.delete() |
| 269 | + |
| 270 | + # Verify remove_seat was called immediately |
| 271 | + mock_remove_seat.assert_called_with(DataCategory.UPTIME, detector) |
| 272 | + |
| 273 | + @mock.patch( |
| 274 | + "sentry.quotas.backend.assign_seat", |
| 275 | + return_value=Outcome.ACCEPTED, |
| 276 | + ) |
| 277 | + def test_update_no_enable_change_no_seat_call(self, mock_assign_seat: mock.MagicMock) -> None: |
| 278 | + """Test that updating without changing enabled status doesn't trigger seat operations.""" |
| 279 | + # Create an enabled detector |
| 280 | + detector = self.create_uptime_detector(enabled=True) |
| 281 | + |
| 282 | + # Clear any previous mock calls from creation |
| 283 | + mock_assign_seat.reset_mock() |
| 284 | + |
| 285 | + validator = UptimeDomainCheckFailureValidator( |
| 286 | + instance=detector, data={"name": "Updated Name"}, context=self.context, partial=True |
| 287 | + ) |
| 288 | + assert validator.is_valid(), validator.errors |
| 289 | + validator.save() |
| 290 | + |
| 291 | + detector.refresh_from_db() |
| 292 | + assert detector.name == "Updated Name" |
| 293 | + assert detector.enabled is True |
| 294 | + |
| 295 | + # Verify no seat operations were called |
| 296 | + mock_assign_seat.assert_not_called() |
0 commit comments