Accept JSON only, except where a file is uploaded

This commit is contained in:
Roland Geider committed 2026-08-12 09:01:26 +02:00
1 parent 744ea6c006
commit 863dfea4d3
10 files changed
+30 -6

No files matched your search

+2
View File
@@ -487,6 +487,8 @@ REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'wger.utils.pagination.WgerLimitOffsetPagination',
'PAGE_SIZE': 20,
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
# JSON only, endpoints taking a file set MultiPartParser themselves.
'DEFAULT_PARSER_CLASSES': ('rest_framework.parsers.JSONParser',),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
+3
View File
@@ -388,6 +388,7 @@ class OidcProviderTestCase(WgerTestCase):
reverse('measurement-category-list'),
data={'name': 'Biceps', 'unit': 'cm'},
HTTP_AUTHORIZATION=f'Bearer {token["access_token"]}',
content_type='application/json',
)
self.assertEqual(response.status_code, 403)
@@ -579,6 +580,7 @@ class OidcProviderTestCase(WgerTestCase):
reverse('measurement-category-list'),
data={'name': 'Biceps', 'unit': 'cm'},
HTTP_AUTHORIZATION=f'Bearer {token}',
content_type='application/json',
)
self.assertEqual(response.status_code, 403)
@@ -591,6 +593,7 @@ class OidcProviderTestCase(WgerTestCase):
reverse('measurement-category-list'),
data={'name': 'Biceps', 'unit': 'cm'},
HTTP_AUTHORIZATION=f'Bearer {token}',
content_type='application/json',
)
self.assertEqual(response.status_code, 201)
+5
View File
@@ -30,6 +30,7 @@ from easy_thumbnails.files import get_thumbnailer
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.generics import CreateAPIView
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
@@ -346,6 +347,8 @@ class ExerciseImageViewSet(ModelViewSet):
queryset = ExerciseImage.objects.all()
serializer_class = ExerciseImageSerializer
permission_classes = (CanContributeExercises,)
# the image is uploaded as a file, which JSON cannot carry
parser_classes = (MultiPartParser,)
ordering_fields = '__all__'
filterset_fields = (
'is_main',
@@ -406,6 +409,8 @@ class ExerciseVideoViewSet(ModelViewSet):
queryset = ExerciseVideo.objects.all()
serializer_class = ExerciseVideoSerializer
permission_classes = (CanContributeExercises,)
# the video is uploaded as a file, which JSON cannot carry
parser_classes = (MultiPartParser,)
ordering_fields = '__all__'
filterset_fields = (
'is_main',
+4 -1
View File
@@ -59,10 +59,13 @@ class ActstreamCreateMixin(_ActstreamMixinBase):
class ActstreamUpdateMixin(_ActstreamMixinBase):
"""PATCHing ``self.data`` to ``self.url_detail`` emits an UPDATED event."""
patch_format = 'json'
"""Resources holding a file only parse multipart, those set 'multipart'."""
def test_actstream_event_on_update(self):
self.authenticate('admin')
before = self._count_actions(StreamVerbs.UPDATED.value)
response = self.client.patch(self.url_detail, data=self.data)
response = self.client.patch(self.url_detail, data=self.data, format=self.patch_format)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)
self.assertEqual(self._count_actions(StreamVerbs.UPDATED.value), before + 1)
@@ -251,3 +251,4 @@ class ExerciseImagesApiTestCase(
resource = ExerciseImage
overview_cached = True
data = {'is_main': True}
patch_format = 'multipart'
@@ -34,6 +34,7 @@ class ExerciseVideosApiTestCase(
resource = ExerciseVideo
overview_cached = False
data = {'is_main': True}
patch_format = 'multipart'
def get_resource_name(self):
# The video endpoint is registered as ``video``, not ``exercisevideo``.
+2 -5
View File
@@ -20,10 +20,7 @@ import logging
# Third Party
from rest_framework import viewsets
from rest_framework.parsers import (
FormParser,
MultiPartParser,
)
from rest_framework.parsers import MultiPartParser
from rest_framework.permissions import IsAuthenticated
# wger
@@ -39,7 +36,7 @@ class GalleryImageViewSet(viewsets.ModelViewSet):
API endpoint for gallery image
"""
parser_classes = [MultiPartParser, FormParser]
parser_classes = [MultiPartParser]
permission_classes = [IsAuthenticated]
serializer_class = ImageSerializer
is_private = True
@@ -61,6 +61,7 @@ class WorkoutLogSlotEntryOwnershipTestCase(WgerTestCase):
'date': '2024-01-01',
'iteration': 1,
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@@ -90,6 +91,7 @@ class WorkoutLogSlotEntryOwnershipTestCase(WgerTestCase):
'date': '2024-01-01',
'iteration': 1,
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
@@ -125,6 +127,7 @@ class WorkoutLogNextLogOwnershipTestCase(WgerTestCase):
'date': '2024-01-01',
'iteration': 1,
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@@ -154,6 +157,7 @@ class WorkoutSessionDayOwnershipTestCase(WgerTestCase):
'notes': '',
'impression': '2',
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@@ -178,6 +182,7 @@ class WorkoutSessionDayOwnershipTestCase(WgerTestCase):
'notes': '',
'impression': '2',
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+4
View File
@@ -41,6 +41,7 @@ class RiRApiTestCase(WgerTestCase):
'operation': 'r',
'step': 'abs',
},
content_type='application/json',
)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
@@ -59,6 +60,7 @@ class RiRApiTestCase(WgerTestCase):
'operation': 'r',
'step': 'abs',
},
content_type='application/json',
)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
@@ -77,6 +79,7 @@ class RiRApiTestCase(WgerTestCase):
'operation': 'r',
'step': 'abs',
},
content_type='application/json',
)
data = response.json()
@@ -96,6 +99,7 @@ class RiRApiTestCase(WgerTestCase):
'rir': 1.3,
'rir_target': 1.6,
},
content_type='application/json',
)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
@@ -75,6 +75,7 @@ class WorkoutLogSessionPinningRESTTestCase(WgerTestCase):
'weight': 30,
'repetitions': 8,
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)
@@ -96,6 +97,7 @@ class WorkoutLogSessionPinningRESTTestCase(WgerTestCase):
'weight': 30,
'repetitions': 8,
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(WorkoutLog.objects.count(), before)
@@ -116,6 +118,7 @@ class WorkoutLogSessionPinningRESTTestCase(WgerTestCase):
'weight': 30,
'repetitions': 8,
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)