apidocs_test_case.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import functools
  2. import os
  3. from django.conf import settings
  4. from openapi_core.contrib.django import DjangoOpenAPIRequest, DjangoOpenAPIResponse
  5. from openapi_core.spec import Spec
  6. from openapi_core.validation.response.validators import V30ResponseDataValidator
  7. from sentry.testutils.cases import APITestCase
  8. from sentry.testutils.helpers.datetime import before_now, iso_format
  9. from sentry.testutils.skips import requires_snuba
  10. from sentry.utils import json
  11. @requires_snuba
  12. class APIDocsTestCase(APITestCase):
  13. @functools.cached_property
  14. def cached_schema(self):
  15. path = os.path.join(os.path.dirname(__file__), "../tests/apidocs/openapi-derefed.json")
  16. with open(path) as json_file:
  17. data = json.load(json_file)
  18. data["servers"][0]["url"] = settings.SENTRY_OPTIONS["system.url-prefix"]
  19. del data["components"]
  20. return Spec.from_dict(data)
  21. def validate_schema(self, request, response):
  22. assert 200 <= response.status_code < 300, response.status_code
  23. if isinstance(response.data, list):
  24. assert len(response.data) > 0, "Cannot validate an empty list"
  25. response["Content-Type"] = "application/json"
  26. V30ResponseDataValidator(self.cached_schema).validate(
  27. DjangoOpenAPIRequest(request), DjangoOpenAPIResponse(response)
  28. )
  29. def create_event(self, name, **kwargs):
  30. # Somewhat sane default data.
  31. data = {
  32. "event_id": (name * 32)[:32],
  33. "fingerprint": ["1"],
  34. "sdk": {"version": "5.17.0", "name": "sentry.javascript.browser"},
  35. "timestamp": iso_format(before_now(seconds=1)),
  36. "user": {"id": self.user.id, "email": self.user.email},
  37. "release": name,
  38. }
  39. data.update(kwargs)
  40. return self.store_event(data=data, project_id=self.project.id)