|
| 1 | +import orjson |
1 | 2 | import pytest |
2 | 3 | import responses |
3 | 4 | from django.db import router, transaction |
|
17 | 18 | from sentry.testutils.helpers.options import override_options |
18 | 19 | from sentry.testutils.outbox import assert_no_webhook_payloads, assert_webhook_payloads_for_mailbox |
19 | 20 | from sentry.testutils.region import override_regions |
20 | | -from sentry.testutils.silo import control_silo_test |
| 21 | +from sentry.testutils.silo import control_silo_test, create_test_regions |
21 | 22 | from sentry.types.region import Region, RegionCategory |
22 | 23 |
|
23 | 24 | region = Region("us", 1, "https://us.testserver", RegionCategory.MULTI_TENANT) |
@@ -304,3 +305,109 @@ class GithubRequestParserTypeRoutingTest(GithubRequestParserTest): |
304 | 305 | def setup(self): |
305 | 306 | with override_options({"github.webhook-type-routing.enabled": True}): |
306 | 307 | yield |
| 308 | + |
| 309 | + |
| 310 | +@control_silo_test(regions=create_test_regions("us")) |
| 311 | +class GithubRequestParserOverwatchForwarderTest(TestCase): |
| 312 | + factory = RequestFactory() |
| 313 | + path = reverse("sentry-integration-github-webhook") |
| 314 | + |
| 315 | + @pytest.fixture(autouse=True) |
| 316 | + def setup(self): |
| 317 | + with override_options({"github.webhook-type-routing.enabled": True}): |
| 318 | + yield |
| 319 | + |
| 320 | + def setUp(self) -> None: |
| 321 | + super().setUp() |
| 322 | + self.organization = self.create_organization( |
| 323 | + name="Test Org", |
| 324 | + slug="test-org", |
| 325 | + region="us", |
| 326 | + owner=self.create_user(email="test@example.com"), |
| 327 | + ) |
| 328 | + self.integration = self.create_integration( |
| 329 | + provider="github", |
| 330 | + external_id="1", |
| 331 | + name="Test Integration", |
| 332 | + organization=self.organization, |
| 333 | + ) |
| 334 | + |
| 335 | + @responses.activate |
| 336 | + def test_overwatch_forwarder(self) -> None: |
| 337 | + with ( |
| 338 | + override_options({"overwatch.enabled-regions": ["us"]}), |
| 339 | + override_settings( |
| 340 | + OVERWATCH_REGION_URLS={"us": "https://us.example.com/api"}, |
| 341 | + OVERWATCH_WEBHOOK_SECRET="test-secret", |
| 342 | + ), |
| 343 | + ): |
| 344 | + responses.add( |
| 345 | + responses.POST, |
| 346 | + "https://us.example.com/api/webhooks/sentry", |
| 347 | + status=200, |
| 348 | + ) |
| 349 | + |
| 350 | + request = self.factory.post( |
| 351 | + self.path, |
| 352 | + data={"installation": {"id": "1"}, "action": "created"}, |
| 353 | + content_type="application/json", |
| 354 | + headers={"x-github-event": GithubWebhookType.PULL_REQUEST.value}, |
| 355 | + ) |
| 356 | + parser = GithubRequestParser( |
| 357 | + request=request, |
| 358 | + response_handler=lambda _: HttpResponse(status=200, content="passthrough"), |
| 359 | + ) |
| 360 | + |
| 361 | + response = parser.get_response() |
| 362 | + assert isinstance(response, HttpResponse) |
| 363 | + assert response.status_code == status.HTTP_202_ACCEPTED |
| 364 | + assert response.content == b"" |
| 365 | + |
| 366 | + assert len(responses.calls) == 1 |
| 367 | + assert responses.calls[0].request.url == "https://us.example.com/api/webhooks/sentry" |
| 368 | + assert responses.calls[0].request.method == "POST" |
| 369 | + json_body = orjson.loads(responses.calls[0].request.body) |
| 370 | + |
| 371 | + assert json_body["organizations"] == [ |
| 372 | + { |
| 373 | + "name": "Test Org", |
| 374 | + "slug": "test-org", |
| 375 | + "id": self.organization.id, |
| 376 | + "region": "us", |
| 377 | + "github_integration_id": self.integration.id, |
| 378 | + "organization_integration_id": self.organization_integration.id, |
| 379 | + } |
| 380 | + ] |
| 381 | + assert json_body["webhook_body"] == {"installation": {"id": "1"}, "action": "created"} |
| 382 | + |
| 383 | + assert json_body["webhook_headers"]["X-Github-Event"] == "pull_request" |
| 384 | + assert json_body["integration_provider"] == "github" |
| 385 | + assert json_body["region"] == "us" |
| 386 | + assert json_body["event_type"] == "github" |
| 387 | + |
| 388 | + @responses.activate |
| 389 | + def test_overwatch_forwarder_missing_region_config(self) -> None: |
| 390 | + with ( |
| 391 | + override_options({"overwatch.enabled-regions": ["us"]}), |
| 392 | + override_settings( |
| 393 | + OVERWATCH_REGION_URLS={"de": "https://de.example.com/api"}, |
| 394 | + OVERWATCH_WEBHOOK_SECRET="test-secret", |
| 395 | + ), |
| 396 | + ): |
| 397 | + request = self.factory.post( |
| 398 | + self.path, |
| 399 | + data={"installation": {"id": "1"}, "action": "created"}, |
| 400 | + content_type="application/json", |
| 401 | + headers={"x-github-event": GithubWebhookType.PULL_REQUEST.value}, |
| 402 | + ) |
| 403 | + parser = GithubRequestParser( |
| 404 | + request=request, |
| 405 | + response_handler=lambda _: HttpResponse(status=200, content="passthrough"), |
| 406 | + ) |
| 407 | + |
| 408 | + response = parser.get_response() |
| 409 | + assert isinstance(response, HttpResponse) |
| 410 | + assert response.status_code == status.HTTP_202_ACCEPTED |
| 411 | + assert response.content == b"" |
| 412 | + |
| 413 | + assert len(responses.calls) == 0 |
0 commit comments