apidocs_test_case.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.shortcuts import create_spec
  6. from openapi_core.validation.response import openapi_v30_response_validator
  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 create_spec(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. result = openapi_v30_response_validator.validate(
  27. self.cached_schema, DjangoOpenAPIRequest(request), DjangoOpenAPIResponse(response)
  28. )
  29. result.raise_for_errors()
  30. assert result.errors == []
  31. def create_event(self, name, **kwargs):
  32. # Somewhat sane default data.
  33. data = {
  34. "event_id": (name * 32)[:32],
  35. "fingerprint": ["1"],
  36. "sdk": {"version": "5.17.0", "name": "sentry.javascript.browser"},
  37. "timestamp": iso_format(before_now(seconds=1)),
  38. "user": {"id": self.user.id, "email": self.user.email},
  39. "release": name,
  40. }
  41. data.update(kwargs)
  42. return self.store_event(data=data, project_id=self.project.id)