Self hosted FLOSS fitness/workout, nutrition and weight tracker
Some tweaks to trustworthiness checks, add some tests
Roland Geidera1830025bdbb
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.pyindex 78d31cffd..cdf3d8761 100644--- a/wger/core/models/profile.py+++ b/wger/core/models/profile.py@@ -192,21 +192,19 @@ by the US Department of Agriculture. It is extremely complete, with around- the email address has been verified"""- days_since_joined = datetime.date.today() - self.user.date_joined.date()+ # Superusers are always trustworthy+ if self.user.is_superuser:+ return True++ # Temporary users are never trustworthy+ if self.is_temporary:+ return False++ days_since_joined = datetime.date.today() - self.user.date_joinedminimum_account_age = 21return days_since_joined.days > minimum_account_age and self.email_verified- @property- def has_exercise_permission(self):- """Returns true if user has all the exercise permissions and not just one."""- # TODO: check if this can be removed / refactored- if self.user.groups.filter(name='admin'- ).exists() or self.user.groups.filter(name='exercises_editor'- ).exists():- return True- return False-## User statistics#diff --git a/wger/core/tests/api_base_test.py b/wger/core/tests/api_base_test.pyindex 4a2b7d212..13626d788 100644--- a/wger/core/tests/api_base_test.py+++ b/wger/core/tests/api_base_test.py@@ -819,4 +819,11 @@ class ExerciseCrudApiTestCase(BaseTestCase, ApiBaseTestCase):self.authenticate('trainer1')response = self.client.delete(self.url_detail)- self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_delete_user_with_permissions(self):+ self.skip_test()++ self.authenticate('admin')+ response = self.client.delete(self.url_detail)+ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)diff --git a/wger/core/tests/test_temporary_users.py b/wger/core/tests/test_temporary_users.pyindex 80cac567b..1fb425a69 100644--- a/wger/core/tests/test_temporary_users.py+++ b/wger/core/tests/test_temporary_users.py@@ -193,18 +193,3 @@ class DemoUserTestCase(WgerTestCase):self.assertEqual(self.count_temp_users(), 18)call_command('delete-temp-users')self.assertEqual(self.count_temp_users(), 2)-- def test_temporary_user_no_permissions(self):- """- Tests that temporary users do not pass the "is trustworthy" check and do not have- any permissions.- """-- # Get a temporary user- user = create_temporary_user()-- # User does not pass trustworthiness check- self.assertFalse(user.userprofile.is_trustworthy)-- # User does not pass admin/exercise editor permissions check- self.assertFalse(user.userprofile.has_exercise_permission)diff --git a/wger/core/tests/test_user.py b/wger/core/tests/test_user.pyindex c5048ee97..665074911 100644--- a/wger/core/tests/test_user.py+++ b/wger/core/tests/test_user.py@@ -11,6 +11,9 @@# GNU General Public License for more details.## You should have received a copy of the GNU Affero General Public License+# Standard Library+import datetime+from unittest import TestCase# Djangofrom django.contrib.auth.models import User@@ -20,6 +23,7 @@ from django.urls import ()# wger+from wger.core.demo import create_temporary_userfrom wger.core.tests.base_testcase import (WgerAccessTestCase,WgerEditTestCase,@@ -259,3 +263,75 @@ class UserDetailPageTestCase2(WgerAccessTestCase):'member1','member2',)+++class UserTrustworthynessTestCase(TestCase):++ def test_temporary_user_no_permissions(self):+ """+ Tests that temporary users do not pass the "is trustworthy" check and do not have+ any permissions.+ """++ # Get a temporary user+ user = create_temporary_user()++ # User does not pass trustworthiness check+ self.assertFalse(user.userprofile.is_trustworthy)++ def test_is_trustworthy_new(self):+ """+ Tests that new accounts are not considered trustworthy+ """++ # Get a temporary user+ user = create_temporary_user()+ user.userprofile.is_temporary = False+ user.userprofile.email_verified = True+ user.date_joined = datetime.date.today()++ # User does not pass trustworthiness check+ self.assertFalse(user.userprofile.is_trustworthy)++ def test_is_trustworthy_old_no_email(self):+ """+ Tests that users without verified email are not considered trustworthy+ """++ # Get a temporary user+ user = create_temporary_user()+ user.userprofile.is_temporary = False+ user.userprofile.email_verified = False+ user.date_joined = datetime.date.today() - datetime.timedelta(days=30)++ # User does not pass trustworthiness check+ self.assertFalse(user.userprofile.is_trustworthy)++ def test_is_trustworthy_old_email(self):+ """+ Tests that old accounts are not considered trustworthy+ """++ # Get a temporary user+ user = create_temporary_user()+ user.userprofile.is_temporary = False+ user.userprofile.email_verified = True+ user.date_joined = datetime.date.today() - datetime.timedelta(days=30)++ # User does not pass trustworthiness check+ self.assertTrue(user.userprofile.is_trustworthy)++ def test_is_trustworthy_admin(self):+ """+ Tests that superusers are always trustworthy+ """++ # Get a temporary user+ user = create_temporary_user()+ user.is_superuser = True+ user.userprofile.is_temporary = False+ user.userprofile.email_verified = False+ user.date_joined = datetime.date.today()++ # User does not pass trustworthiness check+ self.assertTrue(user.userprofile.is_trustworthy)diff --git a/wger/exercises/migrations/0018_delete_pending_exercises.py b/wger/exercises/migrations/0018_delete_pending_exercises.pyindex 3a7bd6d77..e46a1eccf 100644--- a/wger/exercises/migrations/0018_delete_pending_exercises.py+++ b/wger/exercises/migrations/0018_delete_pending_exercises.py@@ -23,6 +23,4 @@ class Migration(migrations.Migration):('exercises', '0017_muscle_name_en'),]- operations = [- migrations.RunPython(delete_pending_exercises)- ]+ operations = [migrations.RunPython(delete_pending_exercises)]