Self hosted FLOSS fitness/workout, nutrition and weight tracker
Sanity check for token lifetimes
Roland Geider1c944b0ba48f
diff --git a/wger/core/checks.py b/wger/core/checks.pyindex 56c8fb71b..cc7978298 100644--- a/wger/core/checks.py+++ b/wger/core/checks.py@@ -1,3 +1,6 @@+# Standard Library+from datetime import timedelta+# Djangofrom django.conf import settingsfrom django.core.checks import (@@ -25,6 +28,22 @@ def settings_check(app_configs, **kwargs):))+ # A very short refresh token lifetime is usually a unit mix-up: the+ # REFRESH_TOKEN_LIFETIME environment variable is in hours, not days+ # or minutes. Apps can only stay logged in for this long while unused.+ refresh_lifetime = settings.SIMPLE_JWT['REFRESH_TOKEN_LIFETIME']+ if refresh_lifetime < timedelta(days=7):+ errors.append(+ Warning(+ f'Very short JWT refresh token lifetime: {refresh_lifetime}',+ hint='The REFRESH_TOKEN_LIFETIME environment variable is set in hours '+ '(default 2880, i.e. 120 days). Users of the mobile app have to log in '+ 'again after not opening it for longer than this value.',+ obj=settings,+ id='wger.W003',+ )+ )+# Only one setting should be setif settings.WGER_SETTINGS['DOWNLOAD_INGREDIENTS_FROM'] not in DOWNLOAD_INGREDIENT_OPTIONS:errors.append(diff --git a/wger/core/tests/test_checks.py b/wger/core/tests/test_checks.pynew file mode 100644index 000000000..0796defa8--- /dev/null+++ b/wger/core/tests/test_checks.py@@ -0,0 +1,46 @@+# 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+from datetime import timedelta++# Django+from django.test import SimpleTestCase+from django.test.utils import override_settings++# wger+from wger.core.checks import settings_check+++class RefreshTokenLifetimeCheckTestCase(SimpleTestCase):+ """Tests for the wger.W003 refresh token lifetime check"""++ @staticmethod+ def check_ids():+ return [entry.id for entry in settings_check(None)]++ @override_settings(SIMPLE_JWT={'REFRESH_TOKEN_LIFETIME': timedelta(hours=24)})+ def test_warns_on_short_lifetime(self):+ """A lifetime below one week triggers the warning"""+ self.assertIn('wger.W003', self.check_ids())++ @override_settings(SIMPLE_JWT={'REFRESH_TOKEN_LIFETIME': timedelta(hours=2880)})+ def test_no_warning_on_default_lifetime(self):+ """The default lifetime of 120 days passes the check"""+ self.assertNotIn('wger.W003', self.check_ids())++ @override_settings(SIMPLE_JWT={'REFRESH_TOKEN_LIFETIME': timedelta(days=7)})+ def test_no_warning_at_threshold(self):+ """Exactly one week is accepted"""+ self.assertNotIn('wger.W003', self.check_ids())