Self hosted FLOSS fitness/workout, nutrition and weight tracker
Also test for email erro handling
Roland Geider4d56033c122d
diff --git a/wger/core/templates/base_empty.html b/wger/core/templates/base_empty.htmlindex 31d37df75..10d223799 100644--- a/wger/core/templates/base_empty.html+++ b/wger/core/templates/base_empty.html@@ -5,7 +5,7 @@{% if messages and not no_messages %}{% for message in messages %}<div- class="alert {% if message.tags == 'warning' %}alert-error{% elif message.tags == 'info' %}alert-info{% elif message.tags == 'success' %}alert-success{% endif %}">+ class="alert {% if message.tags == 'error' %}alert-danger{% elif message.tags == 'warning' %}alert-warning{% elif message.tags == 'info' %}alert-info{% elif message.tags == 'success' %}alert-success{% endif %}"><a href="#" class="close extra-bold" data-bs-dismiss="alert">×</a>{{ message }}</div>diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.pyindex 63f3fbd31..3772def9f 100644--- a/wger/core/tests/test_preferences.py+++ b/wger/core/tests/test_preferences.py@@ -36,6 +36,24 @@ class PreferencesTestCase(WgerTestCase):Tests the preferences page"""+ def setUp(self):+ super().setUp()+ self.form_data = {+ 'show_comments': True,+ 'show_english_ingredients': True,+ 'email': '',+ 'first_name': '',+ 'last_name': '',+ 'workout_reminder_active': True,+ 'workout_reminder': 30,+ 'workout_duration': 12,+ 'notification_language': 2,+ 'num_days_weight_reminder': 10,+ 'weight_unit': 'kg',+ 'birthdate': '02/25/1987',+ 'height': 180,+ }+def test_preferences(self):"""Helper function to test the preferences page@@ -53,19 +71,7 @@ class PreferencesTestCase(WgerTestCase):# Change some preferencesresponse = self.client.post(reverse('core:user:preferences'),- {- 'show_comments': True,- 'show_english_ingredients': True,- 'email': '[email protected]',- 'workout_reminder_active': True,- 'workout_reminder': '30',- 'workout_duration': 12,- 'notification_language': 2,- 'num_days_weight_reminder': 10,- 'weight_unit': 'kg',- 'birthdate': '02/25/1987',- 'height': 180,- },+ {**self.form_data, 'email': '[email protected]'},)self.assertEqual(response.status_code, 302)@@ -81,16 +87,11 @@ class PreferencesTestCase(WgerTestCase):response = self.client.post(reverse('core:user:preferences'),{+ **self.form_data,'show_comments': False,- 'show_english_ingredients': True,- 'email': '',- 'workout_reminder_active': True,'workout_reminder': 22,'workout_duration': 10,- 'notification_language': 2,- 'num_days_weight_reminder': 10,'weight_unit': 'lb',- 'birthdate': '02/25/1987','height': 170,},)@@ -156,17 +157,10 @@ class PreferencesTestCase(WgerTestCase):response = self.client.post(reverse('core:user:preferences'),{- 'show_comments': True,- 'show_english_ingredients': True,+ **self.form_data,'email': '[email protected]','first_name': 'Brand','last_name': 'New',- 'workout_reminder_active': True,- 'workout_reminder': 30,- 'workout_duration': 12,- 'notification_language': 2,- 'num_days_weight_reminder': 10,- 'weight_unit': 'kg','birthdate': '01/01/2000','height': 175,},@@ -186,6 +180,48 @@ class PreferencesTestCase(WgerTestCase):self.assertEqual(user_after.userprofile.height, snapshot['height'])self.assertEqual(user_after.userprofile.birthdate, snapshot['birthdate'])+ def test_invalid_email_format_shows_error_and_saves_nothing(self):+ """+ Submitting the preferences form with a malformed email must show an inline+ field error AND a banner, and not persist any submitted fields+ """+ self.user_login('test')++ user_before = User.objects.get(username='test')+ snapshot = {+ 'email': user_before.email,+ 'first_name': user_before.first_name,+ 'last_name': user_before.last_name,+ 'height': user_before.userprofile.height,+ 'birthdate': user_before.userprofile.birthdate,+ }++ response = self.client.post(+ reverse('core:user:preferences'),+ {+ **self.form_data,+ 'email': 'not an email',+ 'first_name': 'Brand',+ 'last_name': 'New',+ 'birthdate': '01/01/2000',+ 'height': 175,+ },+ )++ self.assertEqual(response.status_code, 200)+ self.assertContains(response, 'Enter a valid email address.')+ self.assertContains(response, 'is-invalid')+ self.assertContains(response, 'invalid-feedback')+ self.assertContains(response, 'alert-danger')+ self.assertContains(response, 'Please correct the errors below.')++ user_after = User.objects.get(username='test')+ self.assertEqual(user_after.email, snapshot['email'])+ self.assertEqual(user_after.first_name, snapshot['first_name'])+ self.assertEqual(user_after.last_name, snapshot['last_name'])+ self.assertEqual(user_after.userprofile.height, snapshot['height'])+ self.assertEqual(user_after.userprofile.birthdate, snapshot['birthdate'])+class UserBodyweightTestCase(WgerTestCase):"""diff --git a/wger/manager/tests/test_generator.py b/wger/manager/tests/test_generator.pyindex 42b8da813..4417f5d05 100644--- a/wger/manager/tests/test_generator.py+++ b/wger/manager/tests/test_generator.py@@ -44,5 +44,4 @@ class RoutineGeneratorTestCase(WgerTestCase):# Assert# Things like nr of training days or exercises are random- self.assertGreaterEqual(WorkoutLog.objects.filter(routine__user_id=1).count(), 100)- self.assertLessEqual(WorkoutLog.objects.filter(routine__user_id=1).count(), 800)+ self.assertGreaterEqual(WorkoutLog.objects.filter(routine__user_id=1).count(), 60)