Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Accept JSON only, except where a file is uploaded

Roland Geider863dfea4d376
diff --git a/settings/settings_global.py b/settings/settings_global.py
index fa13d3dd6..b5ca8ec95 100644
--- a/settings/settings_global.py
+++ b/settings/settings_global.py
@@ -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',
diff --git a/wger/core/tests/test_oidc_provider.py b/wger/core/tests/test_oidc_provider.py
index a07bfe8ef..3f094629b 100644
--- a/wger/core/tests/test_oidc_provider.py
+++ b/wger/core/tests/test_oidc_provider.py
@@ -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)
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 56eeda3e0..0a3ce205d 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -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',
diff --git a/wger/exercises/tests/api_mixins.py b/wger/exercises/tests/api_mixins.py
index cacf7944f..9b6c53ae3 100644
--- a/wger/exercises/tests/api_mixins.py
+++ b/wger/exercises/tests/api_mixins.py
@@ -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)
diff --git a/wger/exercises/tests/test_exercise_images.py b/wger/exercises/tests/test_exercise_images.py
index 9f88e2156..dd052e850 100644
--- a/wger/exercises/tests/test_exercise_images.py
+++ b/wger/exercises/tests/test_exercise_images.py
@@ -251,3 +251,4 @@ class ExerciseImagesApiTestCase(
resource = ExerciseImage
overview_cached = True
data = {'is_main': True}
+ patch_format = 'multipart'
diff --git a/wger/exercises/tests/test_exercise_videos.py b/wger/exercises/tests/test_exercise_videos.py
index 57d8fbf5c..a93291094 100644
--- a/wger/exercises/tests/test_exercise_videos.py
+++ b/wger/exercises/tests/test_exercise_videos.py
@@ -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``.
diff --git a/wger/gallery/api/views.py b/wger/gallery/api/views.py
index 861e292e6..21fc9b66f 100644
--- a/wger/gallery/api/views.py
+++ b/wger/gallery/api/views.py
@@ -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
diff --git a/wger/manager/tests/test_api_ownership_checks.py b/wger/manager/tests/test_api_ownership_checks.py
index da3a40ae5..191d7fc34 100644
--- a/wger/manager/tests/test_api_ownership_checks.py
+++ b/wger/manager/tests/test_api_ownership_checks.py
@@ -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)
diff --git a/wger/manager/tests/test_api_rir.py b/wger/manager/tests/test_api_rir.py
index 4f8f5c710..b183ddc6f 100644
--- a/wger/manager/tests/test_api_rir.py
+++ b/wger/manager/tests/test_api_rir.py
@@ -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)
diff --git a/wger/manager/tests/test_log_session_api.py b/wger/manager/tests/test_log_session_api.py
index 546ca3a2e..f400ebd4b 100644
--- a/wger/manager/tests/test_log_session_api.py
+++ b/wger/manager/tests/test_log_session_api.py
@@ -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)