Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add exercise submission API test and fix endpoint URL

Roland Geider1caf368e2ec1
diff --git a/pyproject.toml b/pyproject.toml
index 5cb654398..79d6f763c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -129,7 +129,6 @@ exclude = [
".ruff_cache",
".svn",
".tox",
- ".venv",
".vscode",
"__pypackages__",
"_build",
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 8755b31a2..1d4805d5f 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -40,6 +40,7 @@ from wger.exercises.models import (
Variation,
)
from wger.utils.cache import CacheKeyMapper
+from wger.utils.constants import CC_BY_SA_4_LICENSE_ID
class ExerciseSerializer(serializers.ModelSerializer):
@@ -567,6 +568,10 @@ class ExerciseInfoSerializer(serializers.ModelSerializer):
return representation
+def get_default_license() -> License:
+ return License.objects.get(pk=CC_BY_SA_4_LICENSE_ID)
+
+
class ExerciseSubmissionSerializer(serializers.ModelSerializer):
"""
Exercise submission serializer
@@ -581,7 +586,8 @@ class ExerciseSubmissionSerializer(serializers.ModelSerializer):
license = serializers.PrimaryKeyRelatedField(
queryset=License.objects.all(),
required=False,
- # default=License.objects.get(pk=CC_BY_SA_4_LICENSE_ID),
+ # Note, using a function here because of problems during tests
+ default=get_default_license,
)
translations = ExerciseTranslationSubmissionSerializer(many=True)
@@ -601,10 +607,6 @@ class ExerciseSubmissionSerializer(serializers.ModelSerializer):
@transaction.atomic
def create(self, validated_data):
- # Standard Library
- from pprint import pprint
-
- pprint(validated_data)
translations_data = validated_data.pop('translations')
# Create the Exercise object first
diff --git a/wger/exercises/tests/test_exercise_api_cache.py b/wger/exercises/tests/api/test_exercise_api_cache.py
similarity index 100%
rename from wger/exercises/tests/test_exercise_api_cache.py
rename to wger/exercises/tests/api/test_exercise_api_cache.py
diff --git a/wger/exercises/tests/api/test_exercise_submission_api.py b/wger/exercises/tests/api/test_exercise_submission_api.py
new file mode 100644
index 000000000..e8a65a7de
--- /dev/null
+++ b/wger/exercises/tests/api/test_exercise_submission_api.py
@@ -0,0 +1,112 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+
+# Third Party
+from rest_framework import status
+
+# wger
+from wger.core.tests.api_base_test import ApiBaseTestCase
+from wger.core.tests.base_testcase import BaseTestCase
+from wger.exercises.models import Exercise
+
+
+class SearchSubmissionApiTestCase(BaseTestCase, ApiBaseTestCase):
+ url = '/api/v2/exercise-submission/'
+
+ def test_successful_submission(self):
+ self.authenticate('admin')
+
+ payload = {
+ 'category': 3,
+ 'license': 5,
+ 'muscles': [3, 4],
+ 'muscles_secondary': [1],
+ 'equipment': [3],
+ 'license_author': 'test man',
+ 'variation': 1,
+ 'translations': [
+ {
+ 'name': '1-Arm Half-Kneeling Lat Pulldown',
+ 'description': 'Attach a D-Handle to a high pully. And use your lat muscles to pull the weight single handedly.',
+ 'language': 2,
+ 'license_author': 'tester',
+ 'aliases': [
+ {'alias': 'This is another name'},
+ {'alias': 'ashda dsads asa dssa'},
+ ],
+ 'comments': [
+ {'comment': 'This is a very important note'},
+ {'comment': 'Do the exercise correctly'},
+ ],
+ },
+ {
+ 'name': '2 Handed Kettlebell Swing',
+ 'description': '<p>das ist die Beschreibung für die Übung</p>',
+ 'language': 1,
+ 'license_author': 'tester',
+ },
+ ],
+ }
+
+ count_before = Exercise.objects.count()
+ response = self.client.post(self.url, data=payload)
+ count_after = Exercise.objects.count()
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+ self.assertEqual(response.json().get('id'), 9)
+ self.assertEqual(count_before + 1, count_after)
+
+ def test_unsuccessful_submission(self):
+ """
+ If any part of the exercise submission fails, no exercise is created.
+ """
+ self.authenticate('admin')
+
+ payload = {
+ 'category': 100,
+ 'license': 5,
+ 'muscles': [3, 4],
+ 'muscles_secondary': [1],
+ 'equipment': [3],
+ 'license_author': 'test man',
+ 'variation': 1,
+ 'translations': [
+ {
+ 'name': '1-Arm Half-Kneeling Lat Pulldown',
+ 'description': 'Attach a D-Handle to a high pully. And use your lat muscles to pull the weight single handedly.',
+ 'language': 2,
+ 'license_author': 'tester',
+ 'aliases': [
+ {'alias': 'This is another name'},
+ {'alias': 'ashda dsads asa dssa'},
+ ],
+ 'comments': [
+ {'comment': 'This is a very important note'},
+ {'comment': 'Do the exercise correctly'},
+ ],
+ },
+ {
+ 'name': '2 Handed Kettlebell Swing',
+ 'description': '<p>das ist die Beschreibung für die Übung</p>',
+ 'language': 1,
+ 'license_author': 'tester',
+ },
+ ],
+ }
+
+ count_before = Exercise.objects.count()
+ response = self.client.post(self.url, data=payload)
+ count_after = Exercise.objects.count()
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(count_before, count_after)
diff --git a/wger/exercises/tests/test_search_api.py b/wger/exercises/tests/api/test_search_api.py
similarity index 100%
rename from wger/exercises/tests/test_search_api.py
rename to wger/exercises/tests/api/test_search_api.py
diff --git a/wger/urls.py b/wger/urls.py
index 11e1ea90a..54017be90 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -290,7 +290,7 @@ urlpatterns += [
path('api/v2/', include(router.urls)),
path('api/v2/exercise/search/', exercises_api_views.search, name='exercise-search'),
path(
- 'api/v2/exercise/submission/',
+ 'api/v2/exercise-submission/',
exercises_api_views.ExerciseSubmissionViewSet.as_view(),
name='exercise-submission',
),