Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix tests that never exercised the logic they name

Roland Geider64684398dff3
diff --git a/wger/core/tests/test_auth_proxy_middleware.py b/wger/core/tests/test_auth_proxy_middleware.py
index ca0a424c2..b9ff06f6e 100644
--- a/wger/core/tests/test_auth_proxy_middleware.py
+++ b/wger/core/tests/test_auth_proxy_middleware.py
@@ -23,7 +23,6 @@ from django.urls import reverse
# Third Party
from allauth.account.models import EmailAddress
-from allauth.socialaccount.models import SocialApp
User = get_user_model()
@@ -300,3 +299,68 @@ class AuthProxyMiddlewareTests(TestCase):
)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], safe_next)
+
+ @override_settings(
+ AUTH_PROXY_TRUSTED_IPS=[TRUSTED_IP],
+ AUTH_PROXY_HEADER=PROXY_HEADER_KEY,
+ WGER_SETTINGS={'ALLOW_GUEST_USERS': False},
+ )
+ def test_session_user_is_replaced_on_mismatch(self):
+ """
+ The proxy is the authority: hitting the login page with a session that
+ belongs to a different user replaces it with the header user
+ """
+ other_user = User.objects.create_user(username='someone_else', password='password123')
+ self.client.force_login(other_user)
+
+ response = self.client.get(
+ self.login_url,
+ REMOTE_ADDR=TRUSTED_IP,
+ **{PROXY_HEADER_KEY: USERNAME},
+ )
+
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(int(self.client.session['_auth_user_id']), self.existing_user.pk)
+
+ @override_settings(
+ AUTH_PROXY_TRUSTED_IPS=[TRUSTED_IP],
+ AUTH_PROXY_HEADER=PROXY_HEADER_KEY,
+ AUTH_PROXY_CREATE_UNKNOWN_USER=True,
+ WGER_SETTINGS={'ALLOW_GUEST_USERS': False},
+ )
+ def test_existing_account_is_adopted_not_duplicated(self):
+ """
+ A header naming an existing user logs into that account, it does not
+ create a second one. Whoever controls the header controls every
+ account, including staff ones, which is why the header may only be
+ trusted from the proxy.
+ """
+ self.existing_user.is_staff = True
+ self.existing_user.save()
+ count_before = User.objects.count()
+
+ response = self.make_request(TRUSTED_IP, USERNAME)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(User.objects.count(), count_before)
+ self.assertEqual(int(self.client.session['_auth_user_id']), self.existing_user.pk)
+
+ @override_settings(
+ AUTH_PROXY_TRUSTED_IPS=[TRUSTED_IP],
+ AUTH_PROXY_HEADER=PROXY_HEADER_KEY,
+ WGER_SETTINGS={'ALLOW_GUEST_USERS': False},
+ )
+ def test_forwarded_for_does_not_grant_trust(self):
+ """
+ Only the peer address decides whether the header is trusted, a
+ client-supplied X-Forwarded-For must not be enough
+ """
+ response = self.client.get(
+ self.protected_url,
+ REMOTE_ADDR=UNTRUSTED_IP,
+ follow=True,
+ **{PROXY_HEADER_KEY: USERNAME, 'HTTP_X_FORWARDED_FOR': TRUSTED_IP},
+ )
+
+ self.assertNotIn('_auth_user_id', self.client.session)
+ self.assertContains(response, 'login', status_code=200)
diff --git a/wger/exercises/tests/test_management_commands.py b/wger/exercises/tests/test_management_commands.py
index fda79da3e..15150a455 100644
--- a/wger/exercises/tests/test_management_commands.py
+++ b/wger/exercises/tests/test_management_commands.py
@@ -96,8 +96,10 @@ class TestHealthCheckManagementCommands(WgerTestCase):
)
self.assertNotIn('-> deleted', self.out.getvalue())
- def atest_fix_untranslated(self):
+ def test_fix_untranslated(self):
+ # Exercise 1 has translations in two languages, both have to go
Translation.objects.get(pk=1).delete()
+ Translation.objects.get(pk=5).delete()
call_command('exercises-health-check', '--delete-untranslated', stdout=self.out)
self.assertIn('-> deleted', self.out.getvalue())
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index 5b702c278..a3446aba2 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -255,7 +255,7 @@ class IngredientDetailTestCase(WgerTestCase):
self.ingredient_detail(editor=False)
@patch('wger.nutrition.models.Ingredient.sync_serving_unit_from_off_if_missing')
- def test_ingredient_detail_does_not_triggers_lazy_serving_sync(self, mock_sync: MagicMock):
+ def test_ingredient_detail_does_not_trigger_lazy_serving_sync(self, mock_sync: MagicMock):
response = self.client.get(reverse('nutrition:ingredient:view', kwargs={'pk': 6}))
self.assertEqual(response.status_code, 200)
@@ -691,8 +691,15 @@ class IngredientModelTestCase(WgerTestCase):
class IngredientInfoLazyServingUnitSyncApiTestCase(WgerTestCase):
+ """
+ Reading ingredients must not sync serving units from OFF
+
+ The sync does an HTTP request per ingredient, so wiring it into a read
+ endpoint would put an external call in the hot path of every request.
+ """
+
@patch('wger.nutrition.models.Ingredient.sync_serving_unit_from_off_if_missing')
- def test_detail_triggers_lazy_sync(self, mock_sync: MagicMock):
+ def test_detail_does_not_trigger_lazy_sync(self, mock_sync: MagicMock):
self.client.get(reverse('api-ingredientinfo-detail', kwargs={'pk': 1}))
mock_sync.assert_not_called()
diff --git a/wger/utils/tests/test_username.py b/wger/utils/tests/test_username.py
index bffffd4f2..f592738ce 100644
--- a/wger/utils/tests/test_username.py
+++ b/wger/utils/tests/test_username.py
@@ -12,6 +12,9 @@
#
# You should have received a copy of the GNU Affero General Public License
+# Standard Library
+import random
+
# Django
from django.contrib.auth.models import User
from django.test import TestCase
@@ -21,6 +24,9 @@ from wger.utils.username import generate_username_suggestions
class TestGenerateUsernameSuggestions(TestCase):
+ # Creating a user creates a profile, which needs a notification language
+ fixtures = ('test-languages',)
+
def test_returns_correct_count(self):
"""
Should return the number of suggestions
@@ -28,16 +34,25 @@ class TestGenerateUsernameSuggestions(TestCase):
result = generate_username_suggestions('john', count=3)
self.assertEqual(len(result), 3)
- def test_suggestions_are_unique_in_db(self):
+ def test_taken_suggestions_are_filtered_out(self):
"""
- Suggestions should not exist in the database
+ Names that are already in use are not suggested
+
+ The candidates are random, so the generator is seeded to get the same
+ list twice: once to find out what it would suggest, once to check that
+ the name taken in between is gone.
"""
- suggestions = generate_username_suggestions('john', 100)
- taken = set(
- User.objects.filter(username__in=suggestions).values_list('username', flat=True)
- )
- for suggestion in suggestions:
- self.assertNotIn(suggestion, taken)
+ random.seed(1)
+ candidates = generate_username_suggestions('john', count=100)
+ self.assertGreater(len(candidates), 1)
+
+ User.objects.create(username=candidates[0])
+
+ random.seed(1)
+ suggestions = generate_username_suggestions('john', count=100)
+
+ self.assertNotIn(candidates[0], suggestions)
+ self.assertEqual(len(suggestions), len(candidates) - 1)
def test_suggestions_start_with_base(self):
"""