Skip to content

Commit f8569fb

Browse files
authored
Fix broken schemathesis tests (#5276)
1 parent 1e2e7f6 commit f8569fb

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

api/api/views/media_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):
4646
view_is_async = True
4747

4848
lookup_field = "identifier"
49+
lookup_value_converter = "uuid"
4950

5051
pagination_class = StandardPagination
5152

api/conf/settings/sentry.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger
55

66
from conf.settings.base import ENVIRONMENT
7+
from conf.settings.security import DEBUG
78

89

910
SENTRY_DSN = config("SENTRY_DSN", default="")
@@ -13,9 +14,6 @@
1314
"SENTRY_PROFILES_SAMPLE_RATE", default=0, cast=float
1415
)
1516

16-
# SECURITY WARNING: don't run with debug turned on in production!
17-
DEBUG = config("DJANGO_DEBUG_ENABLED", default=False, cast=bool)
18-
1917
INTEGRATIONS = [
2018
DjangoIntegration(),
2119
# This prevents two errors from being sent to Sentry (one with the correct

api/conf/urls/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
path("", include(deprecations_urlpatterns)), # Deprecated, redirects to new URL
2525
]
2626

27-
router = SimpleRouter()
27+
router = SimpleRouter(use_regex_path=False)
2828
router.register("audio", AudioViewSet, basename="audio")
2929
router.register("images", ImageViewSet, basename="image")
3030
versioned_paths += router.urls

api/test/test_schema.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,32 @@
33
from django.conf import settings
44

55
import schemathesis
6+
from schemathesis.checks import content_type_conformance
67

78

89
schema = schemathesis.from_uri(f"{settings.CANONICAL_ORIGIN}/v1/schema/")
910

1011

12+
def status_code_aware_content_type_conformance(ctx, res, case):
13+
"""
14+
Skip Content-Type conformance check when status code is 404.
15+
16+
A status code of 404 can come from two sources:
17+
- Django sees the incoming URL and cannot map it to any endpoint.
18+
This returns an HTML response.
19+
- DRF gets the request but cannot map any object to the identifier.
20+
This returns a content-negotiated HTML/JSON response.
21+
22+
Since the end user will likely not be using the data in the 404
23+
response anyway, we don't need the Content-Type to be validated.
24+
"""
25+
26+
if res.status_code == 404:
27+
return
28+
29+
return content_type_conformance(ctx, res, case)
30+
31+
1132
# The null-bytes Bearer tokens are skipped.
1233
# The pattern identifies tests with headers that are acceptable,
1334
# by only allowing authorization headers that use characters valid for
@@ -30,4 +51,7 @@ def test_schema(case: schemathesis.Case):
3051
# from schemathesis's implementation of `parameterize`.
3152
return
3253

33-
case.call_and_validate()
54+
case.call_and_validate(
55+
excluded_checks=[content_type_conformance],
56+
additional_checks=[status_code_aware_content_type_conformance],
57+
)

0 commit comments

Comments
 (0)