test_proxy.py 2.5 KB

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