Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add more tests
Roland Geider51cca14b9a19
diff --git a/wger/manager/tests/__init__.py b/wger/manager/tests/__init__.pyindex f98e8c428..81eab5637 100644--- a/wger/manager/tests/__init__.py+++ b/wger/manager/tests/__init__.py@@ -1,5 +1,8 @@from wger.manager.tests.index import *from wger.manager.tests.workout import *+from wger.manager.tests.day import *from wger.manager.tests.weight_log import *from wger.manager.tests.copy_workout import *from wger.manager.tests.demo_user import *+from wger.manager.tests.registration import *+from wger.manager.tests.preferences import *diff --git a/wger/manager/tests/day.py b/wger/manager/tests/day.pynew file mode 100644index 000000000..77db72552--- /dev/null+++ b/wger/manager/tests/day.py@@ -0,0 +1,240 @@+# This file is part of Workout Manager.+#+# 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.+#+# 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.models import Day++from wger.manager.tests.testcase import WorkoutManagerTestCase+++class AddWorkoutDayTestCase(WorkoutManagerTestCase):+ '''+ Tests adding a day to a workout+ '''++ def add_day(self, fail=False):+ '''+ Helper function to test adding a day to a workout+ '''++ # Fetch the day edit page+ response = self.client.get(reverse('day-add', kwargs={'workout_pk': 3}))++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ self.assertEqual(response.status_code, 200)++ # Edit the day+ response = self.client.post(reverse('day-add', kwargs={'workout_pk': 3}),+ {'description': 'a new day',+ 'day': [1, 4]})+ if fail:+ self.assertRaises(Day.DoesNotExist, Day.objects.get, pk=6)+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ day = Day.objects.get(pk=6)+ self.assertEqual(response.status_code, 302)+ self.assertEqual(day.description, 'a new day')+ self.assertEqual(len(day.day.all()), 2)+ self.assertEqual(day.day.all()[0].day_of_week, 'Monday')+ self.assertEqual(day.day.all()[1].day_of_week, 'Thursday')++ def test_add_day_anonymous(self):+ '''+ Test adding a day to a workout as an anonymous user+ '''++ self.add_day(fail=True)++ def test_add_workout_owner(self):+ '''+ Test adding a day to a workout as the owner user+ '''++ self.user_login('test')+ self.add_day(fail=False)++ def test_add_workout_other(self):+ '''+ Test adding a day to a workout a different logged in user+ '''++ self.user_login('admin')+ self.add_day(fail=True)+++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.delete_day', 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(WorkoutManagerTestCase):+ '''+ Tests editing the day of a Workout+ '''++ def edit_day(self, fail=False):+ '''+ Helper function to test editing the day+ '''++ # Fetch the day edit page+ response = self.client.get(reverse('day-edit', kwargs={'pk': 5}))++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ self.assertEqual(response.status_code, 200)++ # Edit the day+ response = self.client.post(reverse('day-edit', kwargs={'pk': 5}),+ {'description': 'a different description',+ 'day': [1, 4]})++ day = Day.objects.get(pk=5)++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')+ self.assertEqual(day.description, 'A cool day')+ self.assertEqual(len(day.day.all()), 1)+ self.assertEqual(day.day.all()[0].day_of_week, 'Friday')++ else:+ self.assertEqual(response.status_code, 302)+ self.assertEqual(day.description, 'a different description')+ self.assertEqual(len(day.day.all()), 2)+ self.assertEqual(day.day.all()[0].day_of_week, 'Monday')+ self.assertEqual(day.day.all()[1].day_of_week, 'Thursday')++ def test_edit_day_anonymous(self):+ '''+ Test editing the day of a workout as an anonymous user+ '''++ self.edit_day(fail=True)++ def test_create_workout_owner(self):+ '''+ Test editing the day of a workout as the owner user+ '''++ self.user_login('test')+ self.edit_day(fail=False)++ def test_create_workout_other(self):+ '''+ Test editing the day of a workout a logged in user+ '''++ self.user_login('admin')+ self.edit_day(fail=True)+++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.view_day', 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)diff --git a/wger/manager/tests/preferences.py b/wger/manager/tests/preferences.pynew file mode 100644index 000000000..8859ec42a--- /dev/null+++ b/wger/manager/tests/preferences.py@@ -0,0 +1,148 @@+# This file is part of Workout Manager.+#+# 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.+#+# 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.tests.testcase import WorkoutManagerTestCase++logger = logging.getLogger('workout_manager.custom')+++class PreferencesTestCase(WorkoutManagerTestCase):+ '''+ Tests the preferences page+ '''++ def preferences(self, fail=True):+ '''+ Helper function to test the preferences page+ '''++ # Fetch the preferences page+ response = self.client.get(reverse('preferences'))++ if fail:+ self.assertEqual(response.status_code, 302)+ self.assertTemplateUsed('index.html')+ else:+ self.assertEqual(response.status_code, 200)+ self.assertTemplateUsed('preferences.html')++ # Change some preferences+ response = self.client.post(reverse('preferences'),+ {'show_comments': 'on',+ 'show_english_ingredients': 'on',+ 'email': '[email protected]'})++ self.assertEqual(response.status_code, 302)+ response = self.client.get(reverse('preferences'))+ if not fail:+ profile = response.context['user'].get_profile()+ self.assertTrue(profile.show_comments)+ self.assertTrue(profile.show_english_ingredients)+ self.assertEqual(response.context['user'].email, '[email protected]')++ # Change some preferences+ response = self.client.post(reverse('preferences'),+ {'show_english_ingredients': 'on',+ 'email': ''})++ self.assertEqual(response.status_code, 302)+ response = self.client.get(reverse('preferences'))+ if not fail:+ profile = response.context['user'].get_profile()+ self.assertFalse(profile.show_comments)+ self.assertTrue(profile.show_english_ingredients)+ self.assertEqual(response.context['user'].email, '')++ def test_preferences_anonymous(self):+ '''+ Tests the preferences page as an anonymous user+ '''++ self.preferences(fail=True)++ def test_preferences_logged_in(self):+ '''+ Tests the preferences page as a logged in user+ '''++ self.user_login('test')+ self.preferences(fail=False)+++class AjaxPreferencesTestCase(WorkoutManagerTestCase):+ '''+ Tests editing user preferences via AJAX+ '''++ def preferences(self, fail=True):+ '''+ Helper function to test the preferences page+ '''++ # Set the 'show comments' option+ response = self.client.get(reverse('wger.manager.views.api_user_preferences'),+ {'do': 'set_show-comments',+ 'show': '1'},+ HTTP_X_REQUESTED_WITH='XMLHttpRequest')++ if fail:+ self.assertEqual(response.status_code, 302)+ self.assertTemplateUsed('index.html')+ else:+ self.assertEqual('Success', response.content)+ self.assertEqual(response.status_code, 200)++ response = self.client.get(reverse('preferences'))+ profile = response.context['user'].get_profile()+ self.assertTrue(profile.show_comments)+ self.assertFalse(profile.show_english_ingredients)+ self.assertEqual(response.context['user'].email, '')++ # Set the 'english ingredients' option+ response = self.client.get(reverse('wger.manager.views.api_user_preferences'),+ {'do': 'set_english-ingredients',+ 'show': '1'},+ HTTP_X_REQUESTED_WITH='XMLHttpRequest')++ if fail:+ self.assertEqual(response.status_code, 302)+ self.assertTemplateUsed('index.html')+ else:+ self.assertEqual('Success', response.content)+ self.assertEqual(response.status_code, 200)++ response = self.client.get(reverse('preferences'))+ profile = response.context['user'].get_profile()+ self.assertTrue(profile.show_comments)+ self.assertTrue(profile.show_english_ingredients)+ self.assertEqual(response.context['user'].email, '')++ def test_preferences_anonymous(self):+ '''+ Tests the preferences page as an anonymous user+ '''++ self.preferences(fail=True)++ def test_preferences_logged_in(self):+ '''+ Tests the preferences page as a logged in user+ '''++ self.user_login('test')+ self.preferences(fail=False)diff --git a/wger/manager/tests/registration.py b/wger/manager/tests/registration.pynew file mode 100644index 000000000..6ddd66b7e--- /dev/null+++ b/wger/manager/tests/registration.py@@ -0,0 +1,84 @@+# This file is part of Workout Manager.+#+# 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.+#+# 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('workout_manager.custom')+++class RegistrationTestCase(WorkoutManagerTestCase):+ '''+ Tests registering a new user+ '''++ def test_register(self):++ # Fetch the registration page+ response = self.client.get(reverse('registration'))+ self.assertEqual(response.status_code, 200)++ # Fill in the registration form+ registration_data = {'username': 'myusername',+ 'password1': 'secret',+ 'password2': 'secret',+ 'email': 'not an email',+ 'recaptcha_response_field': 'PASSED', }+ count_before = User.objects.count()++ # Wrong email+ response = self.client.post(reverse('registration'), registration_data)+ self.assertFalse(response.context['form'].is_valid())+ self.user_logout()++ # Correct email+ registration_data['email'] = '[email protected]'+ response = self.client.post(reverse('registration'), registration_data)+ count_after = User.objects.count()+ self.assertEqual(response.status_code, 302)+ self.assertEqual(count_before + 1, count_after)+ self.user_logout()++ # Username already exists+ response = self.client.post(reverse('registration'), registration_data)+ count_after = User.objects.count()+ self.assertFalse(response.context['form'].is_valid())+ self.assertEqual(response.status_code, 200)+ self.assertEqual(count_before + 1, count_after)++ # Email already exists+ registration_data['username'] = 'my.other.username'+ response = self.client.post(reverse('registration'), registration_data)+ count_after = User.objects.count()+ self.assertFalse(response.context['form'].is_valid())+ self.assertEqual(response.status_code, 200)+ self.assertEqual(count_before + 1, count_after)++ # No email+ registration_data['email'] = ''+ response = self.client.post(reverse('registration'), registration_data)+ count_after = User.objects.count()+ self.assertEqual(response.status_code, 302)+ self.assertEqual(count_before + 2, count_after)++ # Already logged in+ response = self.client.post(reverse('registration'), registration_data)+ count_after = User.objects.count()+ self.assertEqual(response.status_code, 302)+ self.assertEqual(count_before + 2, count_after)+ self.assertIn('dashboard', response['Location'])diff --git a/wger/manager/tests/testcase.py b/wger/manager/tests/testcase.pyindex ac2c216dc..5e8ffb54c 100644--- a/wger/manager/tests/testcase.py+++ b/wger/manager/tests/testcase.py@@ -104,7 +104,7 @@ class WorkoutManagerLiveServerTestCase(LiveServerTestCase):Logout the user"""- self.browser.get(self.live_server_url + reverse('manager.views.logout'))+ self.browser.get(self.live_server_url + reverse('wger.manager.views.logout'))def check_modal_dialog(self):"""diff --git a/wger/manager/tests/workout.py b/wger/manager/tests/workout.pyindex 8ebe896a4..9c2eacb52 100644--- a/wger/manager/tests/workout.py+++ b/wger/manager/tests/workout.py@@ -60,13 +60,17 @@ class AddWorkoutTestCase(WorkoutManagerTestCase):#workout = TrainingSchedule.objects.get(pk = 1)def test_create_workout_anonymous(self):- '''Test creating a workout as anonymous user'''+ '''+ Test creating a workout as anonymous user+ '''self.user_logout()self.create_workout()def test_create_workout_logged_in(self):- '''Test creating a workout a logged in user'''+ '''+ Test creating a workout a logged in user+ '''self.user_login()self.create_workout(logged_in=True)diff --git a/wger/manager/urls.py b/wger/manager/urls.pyindex 84b843e7d..c7a79ef97 100644--- a/wger/manager/urls.py+++ b/wger/manager/urls.py@@ -24,12 +24,12 @@ urlpatterns = patterns('wger.manager.views',# Userurl(r'^logout$', 'logout', name='logout'),- url(r'^user/registration$', 'registration'),+ url(r'^user/registration$', 'registration', name='registration'),url(r'^user/demo-account$','create_demo_user',name='demo-account'),- url(r'^user/preferences$', 'preferences'),+ url(r'^user/preferences$', 'preferences', name='preferences'),# Workouturl(r'^workout/overview$', 'overview'),diff --git a/wger/manager/views.py b/wger/manager/views.pyindex dc654927d..6ebf5b132 100644--- a/wger/manager/views.py+++ b/wger/manager/views.py@@ -178,6 +178,11 @@ def registration(request):template_data.update(csrf(request))if request.method == 'POST':++ # If the user is already logged in, redirect+ if request.user.is_authenticated():+ return HttpResponseRedirect(reverse('dashboard'))+form = RegistrationForm(data=request.POST)# If the data is valid, log in and redirect@@ -364,12 +369,14 @@ def create_demo_user(request):context_instance=RequestContext(request))+@login_requireddef preferences(request):'''An overview of all user preferences'''template_data = {}template_data.update(csrf(request))+ redirect = False# Process the preferences formif request.method == 'POST':@@ -380,6 +387,7 @@ def preferences(request):# Save the data if it validatesif form.is_valid():form.save()+ redirect = Trueelse:form = UserPreferencesForm(instance=request.user.get_profile())@@ -395,15 +403,19 @@ def preferences(request):if email_form.is_valid():request.user.email = email_form.cleaned_data['email']request.user.save()+ redirect = Trueelse:email_form = UserEmailForm(instance=request.user)template_data['form'] = formtemplate_data['email_form'] = email_form- return render_to_response('user/preferences.html',- template_data,- context_instance=RequestContext(request))+ if redirect:+ return HttpResponseRedirect(reverse('preferences'))+ else:+ return render_to_response('user/preferences.html',+ template_data,+ context_instance=RequestContext(request))@login_required