Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add more test files that somehow didn't make it into the last changeset
Roland Geider8b72d1ea9507
diff --git a/wger/manager/tests/test_change_password.py b/wger/manager/tests/test_change_password.pynew file mode 100644index 000000000..0d194b8fb--- /dev/null+++ b/wger/manager/tests/test_change_password.py@@ -0,0 +1,68 @@+# 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++import logging++from django.core.urlresolvers import reverse+from django.contrib.auth.models import User++from wger.manager.tests.testcase import WorkoutManagerTestCase++logger = logging.getLogger('wger.custom')+++class ChangePasswordTestCase(WorkoutManagerTestCase):+ '''+ Tests changing the password of a registered user+ '''++ def change_password(self, fail=True):++ # Fetch the change passwort page+ response = self.client.get(reverse('change-password'))++ if fail:+ self.assertEqual(response.status_code, 302)+ else:+ self.assertEqual(response.status_code, 200)++ # Fill in the change password form+ form_data = {'old_password': 'testtest',+ 'new_password1': 'secret',+ 'new_password2': 'secret'}++ response = self.client.post(reverse('change-password'), form_data)+ self.assertEqual(response.status_code, 302)++ # Check the new password was accepted+ user = User.objects.get(username='test')+ if fail:+ self.assertTrue(user.check_password('testtest'))+ else:+ self.assertTrue(user.check_password('secret'))++ def test_change_password_anonymous(self):+ '''+ Test changing a password as an anonymous user+ '''++ self.change_password()++ def test_copy_workout_logged_in(self, fail=True):+ '''+ Test changing a password as a logged in user+ '''++ self.user_login('test')+ self.change_password(fail=False)diff --git a/wger/manager/tests/test_copy_workout.py b/wger/manager/tests/test_copy_workout.pynew file mode 100644index 000000000..26b826204--- /dev/null+++ b/wger/manager/tests/test_copy_workout.py@@ -0,0 +1,108 @@+# 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++import logging++from django.core.urlresolvers import reverse++from wger.manager.models import Workout+from wger.manager.tests.testcase import WorkoutManagerTestCase++logger = logging.getLogger('wger.custom')+++class CopyWorkoutTestCase(WorkoutManagerTestCase):+ '''+ Tests copying a workout+ '''++ def copy_workout(self, owner=False):+ '''+ Helper function to test copying workouts+ '''++ # Open the copy workout form+ response = self.client.get(reverse('workout-copy', kwargs={'pk': '3'}))+ if not owner:+ self.assertEqual(response.status_code, 404)+ else:+ self.assertEqual(response.status_code, 200)++ # Copy the workout+ count_before = Workout.objects.count()+ response = self.client.post(reverse('workout-copy', kwargs={'pk': '3'}),+ {'comment': 'A copied workout'})+ count_after = Workout.objects.count()++ if not owner:+ self.assertEqual(count_before, count_after)+ else:+ self.assertGreater(count_after, count_before)+ self.assertEqual(count_after, 4)++ self.assertTemplateUsed('workout/view.html')++ # Test accessing the copied workout+ response = self.client.get(reverse('wger.manager.views.workout.view',+ kwargs={'id': 4}))++ if not owner:+ self.assertEqual(response.status_code, 404)+ else:+ self.assertEqual(response.status_code, 200)++ original = Workout.objects.get(pk=3)+ copy = Workout.objects.get(pk=4)++ days_original = original.day_set.all()+ days_copy = copy.day_set.all()++ # Test that the different attributes and objects are correctly copied over+ for i in range(0, original.day_set.count()):+ self.assertEqual(days_original[i].description, days_copy[i].description)++ for j in range(0, days_original[i].day.count()):+ self.assertEqual(days_original[i].day.all()[j], days_copy[i].day.all()[j])++ sets_original = days_original[i].set_set.all()+ sets_copy = days_copy[i].set_set.all()++ for j in range(days_original[i].set_set.count()):+ sets_original_id = sets_original[j].id+ sets_copy_id = sets_copy[j].id++ self.assertEqual(sets_original[j].sets, sets_copy[j].sets)+ self.assertEqual(sets_original[j].order, sets_copy[j].order)++ exercises_original = sets_original[j].exercises.all()+ exercises_copy = sets_copy[j].exercises.all()++ for k in range(sets_original[j].exercises.count()):+ self.assertEqual(exercises_original[k], exercises_copy[k])++ def test_copy_workout_other(self):+ '''+ Test copying a workout as different user+ '''++ self.user_login('admin')+ self.copy_workout(owner=False)++ def test_copy_workout_owner(self):+ '''+ Test copying a workout as the owener user+ '''++ self.user_login('test')+ self.copy_workout(owner=True)diff --git a/wger/manager/tests/test_day.py b/wger/manager/tests/test_day.pynew file mode 100644index 000000000..5259e072f--- /dev/null+++ b/wger/manager/tests/test_day.py@@ -0,0 +1,181 @@+# 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++from django.core.urlresolvers import reverse+from django.core.cache import cache++from wger.manager.models import Day++from wger.manager.tests.testcase import WorkoutManagerTestCase+from wger.manager.tests.testcase import WorkoutManagerAddTestCase+from wger.manager.tests.testcase import WorkoutManagerEditTestCase+from wger.utils.cache import get_template_cache_name+++class AddWorkoutDayTestCase(WorkoutManagerAddTestCase):+ '''+ Tests adding a day to a workout+ '''++ object_class = Day+ url = reverse('day-add', kwargs={'workout_pk': 3})+ pk = 6+ user_success = 'test'+ user_fail = 'admin'+ data = {'description': 'a new day',+ 'day': [1, 4]}+++class DeleteWorkoutDayTestCase(WorkoutManagerTestCase):+ '''+ Tests deleting a day+ '''++ def delete_day(self, fail=False):+ '''+ Helper function to test deleting a day+ '''++ # Fetch the day edit page+ count_before = Day.objects.count()+ response = self.client.get(reverse('wger.manager.views.day.delete', kwargs={'id': 3,+ 'day_id': 5}))+ count_after = Day.objects.count()++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')+ self.assertEqual(count_before, count_after)++ else:+ self.assertRaises(Day.DoesNotExist, Day.objects.get, pk=5)+ self.assertEqual(response.status_code, 302)+ self.assertEqual(count_before - 1, count_after)++ def test_delete_day_anonymous(self):+ '''+ Test deleting a day as an anonymous user+ '''++ self. delete_day(fail=True)++ def test_delete_workout_owner(self):+ '''+ Test deleting a day as the owner user+ '''++ self.user_login('test')+ self.delete_day(fail=False)++ def test_delete_workout_other(self):+ '''+ Test deleting a day as a different logged in user+ '''++ self.user_login('admin')+ self.delete_day(fail=True)+++class EditWorkoutDayTestCase(WorkoutManagerEditTestCase):+ '''+ Tests editing the day of a Workout+ '''++ object_class = Day+ url = 'day-edit'+ pk = 5+ user_success = 'test'+ user_fail = 'admin'+ data = {'description': 'a different description',+ 'day': [1, 4]}+++class RenderWorkoutDayTestCase(WorkoutManagerTestCase):+ '''+ Tests rendering a single workout day+ '''++ def render_day(self, fail=False):+ '''+ Helper function to test rendering a single workout day+ '''++ # Fetch the day edit page+ response = self.client.get(reverse('wger.manager.views.day.view', kwargs={'id': 5}))++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ self.assertEqual(response.status_code, 200)+ self.assertTemplateUsed('day/view.html')++ def test_render_day_anonymous(self):+ '''+ Test rendering a single workout day as an anonymous user+ '''++ self.render_day(fail=True)++ def test_render_workout_owner(self):+ '''+ Test rendering a single workout day as the owner user+ '''++ self.user_login('test')+ self.render_day(fail=False)++ def test_render_workout_other(self):+ '''+ Test rendering a single workout day as a different logged in user+ '''++ self.user_login('admin')+ self.render_day(fail=True)+++class WorkoutCacheTestCase(WorkoutManagerTestCase):+ '''+ Workout cache test case+ '''++ def test_workout_view_day(self):+ '''+ Test the workout view cache is correctly generated on visit+ '''++ self.user_login('admin')+ self.client.get(reverse('wger.manager.views.workout.view', kwargs={'id': 1}))++ old_day1 = cache.get(get_template_cache_name('day-view', 1))+ old_day2 = cache.get(get_template_cache_name('day-view', 2))+ self.assertTrue(old_day1)+ self.assertTrue(old_day2)++ day1 = Day.objects.get(pk=1)+ day1.description = 'A new name'+ day1.save()+ self.assertFalse(cache.get(get_template_cache_name('day-view', 1)))+ self.assertTrue(cache.get(get_template_cache_name('day-view', 2)))++ self.client.get(reverse('wger.manager.views.workout.view', kwargs={'id': 1}))+ new_day1 = cache.get(get_template_cache_name('day-view', 1))+ new_day2 = cache.get(get_template_cache_name('day-view', 2))+ self.assertNotEqual(old_day1, new_day1)+ self.assertEqual(old_day2, new_day2)++ day1.delete()+ self.assertFalse(cache.get(get_template_cache_name('day-view', 1)))+ self.assertTrue(cache.get(get_template_cache_name('day-view', 2)))diff --git a/wger/manager/tests/test_feedback.py b/wger/manager/tests/test_feedback.pynew file mode 100644index 000000000..7bfcb2af4--- /dev/null+++ b/wger/manager/tests/test_feedback.py@@ -0,0 +1,83 @@+# 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++from django.core.urlresolvers import reverse+from django.core import mail++from wger.manager.models import Workout++from wger.manager.tests.testcase import WorkoutManagerTestCase+from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase+from wger.manager.tests.testcase import WorkoutManagerEditTestCase+++class FeedbackTestCase(WorkoutManagerTestCase):+ '''+ Tests the feedback form+ '''++ def send_feedback(self, logged_in=True):+ '''+ Helper function+ '''+ response = self.client.get(reverse('feedback'))+ self.assertEqual(response.status_code, 200)+ response = self.client.post(reverse('feedback'),+ {'comment': 'A very long and interesting comment'})+ if logged_in:+ self.assertEqual(response.status_code, 302)+ self.assertEqual(len(mail.outbox), 1)+ response = self.client.get(response['Location'])+ self.assertEqual(response.status_code, 200)++ # Short comment+ response = self.client.post(reverse('feedback'), {'comment': '12345'})+ self.assertEqual(response.status_code, 200)+ self.assertEqual(len(response.context['form'].errors), 1)+ else:+ # No recaptcha field+ self.assertEqual(response.status_code, 200)+ self.assertEqual(len(mail.outbox), 0)++ # Correctly filled in reCaptcha+ response = self.client.post(reverse('feedback'),+ {'comment': 'A very long and interesting comment',+ 'recaptcha_response_field': 'PASSED'})+ self.assertEqual(response.status_code, 302)+ self.assertEqual(len(mail.outbox), 1)+ response = self.client.get(response['Location'])+ self.assertEqual(response.status_code, 200)++ def test_send_feedback_admin(self):+ '''+ Tests the feedback form as an admin user+ '''++ self.user_login('admin')+ self.send_feedback()++ def test_send_feedback_user(self):+ '''+ Tests the feedback form as a regular user+ '''++ self.user_login('test')+ self.send_feedback()++ def test_send_feedback_logged_out(self):+ '''+ Tests the feedback form as a logged out user+ '''++ self.send_feedback(logged_in=False)diff --git a/wger/manager/tests/test_index.py b/wger/manager/tests/test_index.pynew file mode 100644index 000000000..94402e648--- /dev/null+++ b/wger/manager/tests/test_index.py@@ -0,0 +1,97 @@+# 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+++from django.core.urlresolvers import reverse++from wger.nutrition.models import NutritionPlan+from wger.manager.models import Workout+from wger.weight.models import WeightEntry++from wger.manager.tests.testcase import WorkoutManagerTestCase+++class DashboardTestCase(WorkoutManagerTestCase):+ '''+ Dashboard (landing page) test case+ '''++ def dashboard(self):+ '''+ Helper function to test the dashboard+ '''++ response = self.client.get(reverse('index'))++ # Everybody is redirected+ self.assertEqual(response.status_code, 302)++ # Delete the objects so we can test adding them later+ NutritionPlan.objects.all().delete()+ Workout.objects.all().delete()+ WeightEntry.objects.all().delete()++ response = self.client.get(reverse('dashboard'))+ # There is something to send to the template+ self.assertEqual(response.status_code, 200)+ self.assertFalse(response.context['weight'])+ self.assertFalse(response.context['current_workout'])+ self.assertFalse(response.context['plan'])+ self.assertRaises(KeyError, lambda: response.context['weekdays'])++ #+ # 1. Add a workout+ #+ self.client.get(reverse('wger.manager.views.workout.add'))+ response = self.client.get(reverse('dashboard'))++ self.assertEqual(response.status_code, 200)+ self.assertFalse(response.context['weight'])+ self.assertTrue(response.context['current_workout'])+ self.assertFalse(response.context['plan'])+ self.assertTrue(response.context['weekdays'])++ #+ # 2. Add a nutrition plan+ #+ self.client.get(reverse('wger.nutrition.views.plan.add'))+ response = self.client.get(reverse('dashboard'))++ self.assertEqual(response.status_code, 200)+ self.assertFalse(response.context['weight'])+ self.assertTrue(response.context['current_workout'])+ self.assertTrue(response.context['plan'])+ self.assertTrue(response.context['weekdays'])++ #+ # 3. Add a weight entry+ #+ self.client.post(reverse('weight-add'),+ {'weight': 100,+ 'creation_date': '2012-01-01'},)+ response = self.client.get(reverse('dashboard'))++ self.assertEqual(response.status_code, 200)+ self.assertTrue(response.context['weight'])+ self.assertTrue(response.context['current_workout'])+ self.assertTrue(response.context['plan'])+ self.assertTrue(response.context['weekdays'])++ def test_dashboard_logged_in(self):+ '''+ Test index page as a logged in user+ '''++ self.user_login('admin')+ self.dashboard()diff --git a/wger/manager/tests/test_pdf.py b/wger/manager/tests/test_pdf.pynew file mode 100644index 000000000..adc082411--- /dev/null+++ b/wger/manager/tests/test_pdf.py@@ -0,0 +1,64 @@+# 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++from django.core.urlresolvers import reverse++from wger.manager.tests.testcase import WorkoutManagerTestCase+++class WorkoutPdfExportTestCase(WorkoutManagerTestCase):+ '''+ Tests exporting a workout as a pdf+ '''++ def export_pdf(self, fail=False):+ '''+ Helper function to test exporting a workout as a pdf+ '''++ # Create a workout+ response = self.client.get(reverse('wger.manager.pdf.workout_log', kwargs={'id': 3}))++ if fail:+ self.assertIn(response.status_code, (404, 302))+ else:+ self.assertEqual(response.status_code, 200)+ self.assertEqual(response['Content-Type'], 'application/pdf')+ self.assertEqual(response['Content-Disposition'], 'attachment; filename=Workout.pdf')++ # Approximate size, there's a timestamp in the document+ self.assertGreater(len(response.content), 3200)++ def test_export_pdf_anonymous(self):+ '''+ Tests exporting a workout as a pdf as an anonymous user+ '''++ self.export_pdf(fail=True)++ def test_export_pdf_owner(self):+ '''+ Tests exporting a workout as a pdf as the owner user+ '''++ self.user_login('test')+ self.export_pdf(fail=False)++ def test_export_pdf_other(self):+ '''+ Tests exporting a workout as a pdf as a logged user not owning the data+ '''++ self.user_login('admin')+ self.export_pdf(fail=True)