Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Expose the user's permission over the API

Roland Geider32754e170cb8
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index 160468db0..f9ca701bf 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -157,6 +157,24 @@ class ApplicationVersionView(viewsets.ViewSet):
return Response(get_version())
+class PermissionView(viewsets.ViewSet):
+ """
+ Returns the application's version
+ """
+ permission_classes = (AllowAny, )
+
+ @staticmethod
+ def get(request):
+ permission = request.query_params.get('permission')
+ if request.user.is_anonymous or permission is None:
+ return Response(
+ "Please pass a permission name in the 'permission' parameter",
+ status=status.HTTP_400_BAD_REQUEST,
+ )
+
+ return Response({'result': request.user.has_perm(permission)})
+
+
class RequiredApplicationVersionView(viewsets.ViewSet):
"""
Returns the minimum required version of flutter app to access this server
diff --git a/wger/core/tests/test_permission_api.py b/wger/core/tests/test_permission_api.py
new file mode 100644
index 000000000..273df0996
--- /dev/null
+++ b/wger/core/tests/test_permission_api.py
@@ -0,0 +1,84 @@
+# 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
+
+
+class ExerciseCustomApiTestCase(BaseTestCase, ApiBaseTestCase):
+
+ url = '/api/v2/check-permission/'
+ error_message = "Please pass a permission name in the 'permission' parameter"
+
+ def get_resource_name(self):
+ return 'check-permission'
+
+ def test_check_permission_anonymous(self):
+ """
+ Test that logged-out users get a error message
+ """
+ response = self.client.get(self.url)
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(response.data, self.error_message)
+
+ def test_check_permission_anonymous_with_parameter(self):
+ """
+ Test that logged-out users get en error message when passing a parameter
+ """
+ response = self.client.get(self.url + '?permission=exercises.change_muscle')
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(response.data, self.error_message)
+
+ def test_check_no_parameter_logged_in(self):
+ """
+ Test that authenticated users get an error when not passing a parameter
+ """
+ self.get_credentials('test')
+ response = self.client.get(self.url)
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(response.data, self.error_message)
+
+ def test_check_parameter_logged_in_no_permission(self):
+ """
+ Test that the api correctly returns the user's permission
+ """
+ self.get_credentials('test')
+ response = self.client.get(self.url + '?permission=exercises.change_muscle')
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.data['result'], False)
+
+ def test_check_parameter_logged_in_wrong_permission(self):
+ """
+ Test that the result if false if passing a non-existent permission
+
+ (this is the default behaviour of django)
+ """
+ self.get_credentials('test')
+ response = self.client.get(self.url + '?permission=foo.bar_baz')
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.data['result'], False)
+
+ def test_check_parameter_logged_in_admin(self):
+ """
+ Test that the api correctly returns the user's permission
+ """
+ self.get_credentials('admin')
+ response = self.client.get(self.url + '?permission=exercises.change_muscle')
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.data['result'], True)
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index fe418d8e5..94fdb6392 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -250,8 +250,7 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
# -> Check if the language already exists, excluding the current object
if self.instance:
if self.instance.exercise_base.exercises.filter(
- ~Q(id=self.instance.pk),
- language=value['language']
+ ~Q(id=self.instance.pk), language=value['language']
).exists():
raise serializers.ValidationError(
f"There is already a translation for this exercise in {value['language']}"
@@ -260,8 +259,7 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
# -> Check if the language already exists
else:
if Exercise.objects.filter(
- exercise_base=value['exercise_base'],
- language=value['language']
+ exercise_base=value['exercise_base'], language=value['language']
).exists():
raise serializers.ValidationError(
f"There is already a translation for this exercise in {value['language']}"
diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.py
index 73d1b9fe2..66f4bf4a6 100644
--- a/wger/exercises/tests/test_exercise_translation.py
+++ b/wger/exercises/tests/test_exercise_translation.py
@@ -114,7 +114,7 @@ class ExerciseIndexTestCase(WgerTestCase):
def test_exercise_index_editor(self):
"""
- Tests the exercise overview page as a logged in user with editor rights
+ Tests the exercise overview page as a logged-in user with editor rights
"""
self.user_login('admin')
@@ -122,7 +122,7 @@ class ExerciseIndexTestCase(WgerTestCase):
def test_exercise_index_non_editor(self):
"""
- Tests the exercise overview page as a logged in user without editor rights
+ Tests the exercise overview page as a logged-in user without editor rights
"""
self.user_login('test')
@@ -205,7 +205,7 @@ class ExerciseDetailTestCase(WgerTestCase):
def test_exercise_detail_non_editor(self):
"""
- Tests the exercise details page as a logged in user without editor rights
+ Tests the exercise details page as a logged-in user without editor rights
"""
self.user_login('test')
@@ -255,7 +255,7 @@ class ExercisesTestCase(WgerTestCase):
def test_search_exercise_logged_in(self):
"""
- Test deleting an exercise by a logged in user
+ Test deleting an exercise by a logged-in user
"""
self.user_login('test')
diff --git a/wger/urls.py b/wger/urls.py
index b7e9fcd24..8cbd71573 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -251,6 +251,11 @@ urlpatterns += [
core_api_views.ApplicationVersionView.as_view({'get': 'get'}),
name='app_version'
),
+ path(
+ 'api/v2/check-permission/',
+ core_api_views.PermissionView.as_view({'get': 'get'}),
+ name='permission'
+ ),
path(
'api/v2/min-app-version/',
core_api_views.RequiredApplicationVersionView.as_view({'get': 'get'}),