Self hosted FLOSS fitness/workout, nutrition and weight tracker
Start writing tests for the exercise crowdsourcing endpoints
Roland Geider0a7c18b9cd89
diff --git a/wger/core/tests/api_base_test.py b/wger/core/tests/api_base_test.pyindex 5d0d2f12a..6236ad7d1 100644--- a/wger/core/tests/api_base_test.py+++ b/wger/core/tests/api_base_test.py@@ -701,3 +701,134 @@ class ApiBaseResourceTestCase(All logic happens in the Api*TestCase classes"""pass+++class ExerciseCrudApiTestCase(BaseTestCase, ApiBaseTestCase):+ """+ Testcase for the exercise API endpoints+ """++ def test_get_overview_anonymous_user(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ response = self.client.get(self.url)+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def test_get_detail_anonymous_user(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ response = self.client.get(self.url_detail)+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def test_get_overview_user(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('test')+ response = self.client.get(self.url)+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def test_get_detail_user(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('test')+ response = self.client.get(self.url_detail)+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def test_patch_anonymous(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ for key in self.data:+ response = self.client.patch(self.url_detail, data={key: self.data[key]})+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_patch_user_unverified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('test')+ for key in self.data:+ response = self.client.patch(self.url_detail, data={key: self.data[key]})+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_patch_user_verified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('trainer1')+ for key in self.data:+ response = self.client.patch(self.url_detail, data={key: self.data[key]})+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def test_post_user_anonymous(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ response = self.client.post(self.url, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_post_user_unverified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('test')+ response = self.client.post(self.url, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_post_user_verified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('trainer1')+ response = self.client.post(self.url, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)++ def test_put_user_anonymous(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ response = self.client.put(self.url_detail, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_put_user_unverified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('test')+ response = self.client.put(self.url_detail, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_put_user_verified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('trainer1')+ response = self.client.put(self.url_detail, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def test_delete_anonymous(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ response = self.client.delete(self.url_detail)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_delete_user_unverified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('test')+ response = self.client.delete(self.url_detail)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_delete_user_verified_email(self):+ if self.__class__.__name__ == 'ExerciseCrudApiTestCase':+ return++ self.get_credentials('trainer1')+ response = self.client.delete(self.url_detail)+ self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.pyindex 203c73bd2..33dc355d7 100644--- a/wger/core/tests/base_testcase.py+++ b/wger/core/tests/base_testcase.py@@ -116,6 +116,8 @@ class BaseTestCase(object):'test-apikeys','test-weight-data','test-equipment',+ 'test-categories',+ 'test-muscles','test-exercises','test-exercise-images','test-weight-units',diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 73ab13635..e96b635e6 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -162,10 +162,13 @@ class ExerciseTranslationViewSet(CreateUpdateModelViewSet):Save entry to activity stream"""- # Don't allow to change the exercise base over the API+ # Don't allow to change the base or the language over the APIif serializer.validated_data.get('exercise_base'):del serializer.validated_data['exercise_base']+ if serializer.validated_data.get('language'):+ del serializer.validated_data['language']+# Clean the description HTMLif serializer.validated_data.get('description'):serializer.validated_data['description'] = bleach.clean(diff --git a/wger/exercises/fixtures/test-categories.json b/wger/exercises/fixtures/test-categories.jsonnew file mode 100644index 000000000..c8bebf8c3--- /dev/null+++ b/wger/exercises/fixtures/test-categories.json@@ -0,0 +1,30 @@+[+ {+ "pk": 1,+ "model": "exercises.exercisecategory",+ "fields": {+ "name": "Category"+ }+ },+ {+ "pk": 2,+ "model": "exercises.exercisecategory",+ "fields": {+ "name": "Another category"+ }+ },+ {+ "pk": 3,+ "model": "exercises.exercisecategory",+ "fields": {+ "name": "Yet another category"+ }+ },+ {+ "pk": 4,+ "model": "exercises.exercisecategory",+ "fields": {+ "name": "I will be deleted"+ }+ }+]diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.jsonindex 3390da90c..297b0ae9e 100644--- a/wger/exercises/fixtures/test-exercises.json+++ b/wger/exercises/fixtures/test-exercises.json@@ -1,92 +1,25 @@[+{"pk": 1,- "model": "exercises.muscle",- "fields": {- "is_front": true,- "name": "Anterior testoid",- "name_en": "Testoulders"- }- },- {- "pk": 2,- "model": "exercises.muscle",- "fields": {- "is_front": false,- "name": "Biceps testii",- "name_en": "Testceps"- }- },- {- "pk": 3,- "model": "exercises.muscle",- "fields": {- "is_front": false,- "name": "Biceps notusensis"- }- },- {- "pk": 4,- "model": "exercises.muscle",- "fields": {- "is_front": true,- "name": "Bigus Pectoralis",- "name_en": "Testch"- }- },- {- "pk": 5,- "model": "exercises.muscle",- "fields": {- "is_front": true,- "name": "Latissimus wingus"- }- },- {- "pk": 6,- "model": "exercises.muscle",- "fields": {- "is_front": true,- "name": "Gluteus jucicus",- "name_en": "Test jucicus"- }- },- {- "pk": 1,- "model": "exercises.exercisecategory",+ "model": "exercises.variation","fields": {- "name": "Category"}},{"pk": 2,- "model": "exercises.exercisecategory",+ "model": "exercises.variation","fields": {- "name": "Another category"}},{"pk": 3,- "model": "exercises.exercisecategory",- "fields": {- "name": "Yet another category"- }- },- {- "pk": 4,- "model": "exercises.exercisecategory",- "fields": {- "name": "I will be deleted"- }- },- {- "pk": 1,"model": "exercises.variation","fields": {}},{- "pk": 2,+ "pk": 4,"model": "exercises.variation","fields": {}@@ -342,5 +275,21 @@"comment": "Foobar","exercise": 2}+ },+ {+ "pk": 1,+ "model": "exercises.alias",+ "fields": {+ "alias": "a different name",+ "exercise": 1+ }+ },+ {+ "pk": 2,+ "model": "exercises.alias",+ "fields": {+ "alias": "yet another name",+ "exercise": 2+ }}]diff --git a/wger/exercises/fixtures/test-muscles.json b/wger/exercises/fixtures/test-muscles.jsonnew file mode 100644index 000000000..9b4bd0c6a--- /dev/null+++ b/wger/exercises/fixtures/test-muscles.json@@ -0,0 +1,54 @@+[+ {+ "pk": 1,+ "model": "exercises.muscle",+ "fields": {+ "is_front": true,+ "name": "Anterior testoid",+ "name_en": "Testoulders"+ }+ },+ {+ "pk": 2,+ "model": "exercises.muscle",+ "fields": {+ "is_front": false,+ "name": "Biceps testii",+ "name_en": "Testceps"+ }+ },+ {+ "pk": 3,+ "model": "exercises.muscle",+ "fields": {+ "is_front": false,+ "name": "Biceps notusensis"+ }+ },+ {+ "pk": 4,+ "model": "exercises.muscle",+ "fields": {+ "is_front": true,+ "name": "Bigus Pectoralis",+ "name_en": "Testch"+ }+ },+ {+ "pk": 5,+ "model": "exercises.muscle",+ "fields": {+ "is_front": true,+ "name": "Latissimus wingus"+ }+ },+ {+ "pk": 6,+ "model": "exercises.muscle",+ "fields": {+ "is_front": true,+ "name": "Gluteus jucicus",+ "name_en": "Test jucicus"+ }+ }+]diff --git a/wger/exercises/tests/test_alias.py b/wger/exercises/tests/test_alias.pynew file mode 100644index 000000000..7ef55f890--- /dev/null+++ b/wger/exercises/tests/test_alias.py@@ -0,0 +1,62 @@+# 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++# Standard Library+import json++# Django+from django.core import mail+from django.core.cache import cache+from django.core.cache.utils import make_template_fragment_key+from django.template import (+ Context,+ Template,+)+from django.urls import reverse++# Third Party+from rest_framework import status++# wger+from wger.core.tests import api_base_test+from wger.core.tests.api_base_test import (+ ApiBaseTestCase,+ ExerciseCrudApiTestCase,+)+from wger.core.tests.base_testcase import (+ STATUS_CODES_FAIL,+ WgerDeleteTestCase,+ WgerTestCase,+)+from wger.exercises.models import (+ Alias,+ Exercise,+ ExerciseCategory,+ Muscle,+)+from wger.utils.cache import cache_mapper+from wger.utils.constants import WORKOUT_TAB+from wger.utils.helpers import random_string+++class AliasCustomApiTestCase(ExerciseCrudApiTestCase):+ pk = 1++ data = {+ 'exercise': 1,+ 'alias': 'Alias 123',+ }++ def get_resource_name(self):+ return 'exercisealias'diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.pyindex 812490a80..11b169b7e 100644--- a/wger/exercises/tests/test_exercise.py+++ b/wger/exercises/tests/test_exercise.py@@ -30,7 +30,10 @@ from rest_framework import status# wgerfrom wger.core.tests import api_base_test-from wger.core.tests.api_base_test import ApiBaseTestCase+from wger.core.tests.api_base_test import (+ ApiBaseTestCase,+ ExerciseCrudApiTestCase,+)from wger.core.tests.base_testcase import (STATUS_CODES_FAIL,WgerDeleteTestCase,@@ -564,40 +567,23 @@ class ExerciseBaseInfoApiTestCase(return 'exercisebaseinfo'-class ExerciseCustomApiTestCase(api_base_test.BaseTestCase, ApiBaseTestCase):-+class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):pk = 1+ data = {+ 'name': 'A new name',+ 'description': 'The wild boar is a suid native to much of Eurasia and North Africa',+ 'language': 3,+ 'exercise_base': 2+ }+def get_resource_name(self):return 'exercise-translation'- def test_change_fields(self):- """- Test that it is possible to edit the fields of an exercise translation- """- exercise = Exercise.objects.get(pk=self.pk)- self.assertEqual(exercise.name, 'An exercise')- self.assertEqual(exercise.description, 'Lorem ipsum dolor sit amet')-- data = {- 'name': 'A new name',- 'description': 'The wild boar is a suid native to much of Eurasia and North Africa'- }- self.get_credentials('trainer1')- response = self.client.patch(self.url_detail, data=data)- self.assertEqual(response.status_code, status.HTTP_200_OK)-- exercise = Exercise.objects.get(pk=self.pk)- self.assertEqual(exercise.name, 'A new name')- self.assertEqual(- exercise.description,- 'The wild boar is a suid native to much of Eurasia and North Africa'- )-def test_cant_change_base_id(self):"""Test that it is not possible to change the base id of an existing- exercise resource.+ exercise translation."""exercise = Exercise.objects.get(pk=self.pk)self.assertEqual(exercise.exercise_base_id, 1)@@ -609,24 +595,25 @@ class ExerciseCustomApiTestCase(api_base_test.BaseTestCase, ApiBaseTestCase):exercise = Exercise.objects.get(pk=self.pk)self.assertEqual(exercise.exercise_base_id, 1)- def test_patch_clean_html(self):+ def test_cant_change_language(self):"""- Test that the description field has its HTML stripped before saving+ Test that it is not possible to change the language id of an existing+ exercise translation."""+ exercise = Exercise.objects.get(pk=self.pk)+ self.assertEqual(exercise.language_id, 1)- description = '<script>alert();</script> The wild boar is a suid native...'self.get_credentials('trainer1')- response = self.client.patch(self.url_detail, data={'description': description})+ response = self.client.patch(self.url_detail, data={'language': 2})self.assertEqual(response.status_code, status.HTTP_200_OK)exercise = Exercise.objects.get(pk=self.pk)- self.assertEqual(exercise.description, 'alert(); The wild boar is a suid native...')+ self.assertEqual(exercise.exercise_base_id, 1)- def test_post_clean_html(self):+ def test_patch_clean_html(self):"""- Test that the description field has its HTML stripped before creating an exercise+ Test that the description field has its HTML stripped before saving"""-description = '<script>alert();</script> The wild boar is a suid native...'self.get_credentials('trainer1')response = self.client.patch(self.url_detail, data={'description': description})diff --git a/wger/exercises/tests/test_exercise_base.py b/wger/exercises/tests/test_exercise_base.pyindex 3cedf674f..743d4f262 100644--- a/wger/exercises/tests/test_exercise_base.py+++ b/wger/exercises/tests/test_exercise_base.py@@ -13,6 +13,7 @@# You should have received a copy of the GNU Affero General Public License# wger+from wger.core.tests.api_base_test import ExerciseCrudApiTestCasefrom wger.core.tests.base_testcase import WgerTestCasefrom wger.exercises.models import (Exercise,@@ -84,3 +85,18 @@ class ExerciseBaseTestCase(WgerTestCase):exercise = Exercise.objects.get(pk=1)base = exercise.exercise_baseself.assertListEqual(self.get_ids(exercise.images), self.get_ids(base.exerciseimage_set))+++class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):+ pk = 1++ data = {+ "category": 3,+ "muscles": [1, 3],+ "muscles_secondary": [2],+ "equipment": [3],+ "variations": 4+ }++ def get_resource_name(self):+ return 'exercise-base'