test_proxy.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from __future__ import annotations
  2. from dataclasses import asdict
  3. import orjson
  4. import pytest
  5. from django.urls import reverse
  6. from pytest_django.live_server_helper import LiveServer
  7. from rest_framework.test import APIClient
  8. from sentry.models.apikey import ApiKey
  9. from sentry.models.organization import Organization
  10. from sentry.models.team import Team
  11. from sentry.silo.base import SiloMode, SingleProcessSiloModeState
  12. from sentry.testutils.asserts import assert_status_code
  13. from sentry.testutils.cases import TransactionTestCase
  14. from sentry.testutils.factories import Factories
  15. from sentry.testutils.region import override_regions
  16. from sentry.testutils.silo import region_silo_test
  17. from sentry.types.region import Region
  18. from tests.sentry.middleware.test_proxy import test_region
  19. @pytest.fixture(scope="function")
  20. def local_live_server(request, live_server):
  21. if hasattr(request, "cls"):
  22. request.cls.live_server = live_server
  23. request.node.live_server = live_server
  24. @region_silo_test(regions=[test_region])
  25. @pytest.mark.usefixtures("local_live_server")
  26. class EndToEndAPIProxyTest(TransactionTestCase):
  27. live_server: LiveServer
  28. endpoint = "sentry-api-0-organization-teams"
  29. method = "post"
  30. organization: Organization
  31. api_key: ApiKey
  32. def get_response(self, *args, **params):
  33. url = reverse(self.endpoint, args=args)
  34. headers = params.pop("extra_headers", {})
  35. return getattr(self.client, self.method)(url, format="json", data=params, **headers)
  36. def test_through_api_gateway(self):
  37. if SiloMode.get_current_mode() == SiloMode.MONOLITH:
  38. return
  39. self.client = APIClient()
  40. config = asdict(test_region)
  41. config["address"] = self.live_server.url
  42. with override_regions([Region(**config)]):
  43. self.organization = Factories.create_organization(owner=self.user, region="us")
  44. self.api_key = Factories.create_api_key(
  45. organization=self.organization, scope_list=["org:write", "org:admin", "team:write"]
  46. )
  47. with SingleProcessSiloModeState.enter(SiloMode.CONTROL):
  48. resp = self.get_response(
  49. self.organization.slug,
  50. name="hello world",
  51. idp_provisioned=True,
  52. extra_headers=dict(
  53. HTTP_AUTHORIZATION=self.create_basic_auth_header(self.api_key.key)
  54. ),
  55. )
  56. assert_status_code(resp, 201)
  57. result = orjson.loads(resp.getvalue())
  58. team = Team.objects.get(id=result["id"])
  59. assert team.idp_provisioned