Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add more tests (weight app, password reset), do some cleanup
Roland Geidere87f3093de69
diff --git a/wger/manager/tests/__init__.py b/wger/manager/tests/__init__.pyindex 98e8f139e..ab01aab1c 100644--- a/wger/manager/tests/__init__.py+++ b/wger/manager/tests/__init__.py@@ -2,6 +2,7 @@ from wger.manager.tests.index import *from wger.manager.tests.demo_user import *from wger.manager.tests.registration import *from wger.manager.tests.preferences import *+from wger.manager.tests.change_password import *from wger.manager.tests.workout import *from wger.manager.tests.day import *diff --git a/wger/manager/tests/change_password.py b/wger/manager/tests/change_password.pynew file mode 100644index 000000000..34a3f8178--- /dev/null+++ b/wger/manager/tests/change_password.py@@ -0,0 +1,68 @@+# 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 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/demo_user.py b/wger/manager/tests/demo_user.pyindex 6b6ad7b13..04498f01f 100644--- a/wger/manager/tests/demo_user.py+++ b/wger/manager/tests/demo_user.py@@ -26,7 +26,6 @@ class DemoUserTestCase(WorkoutManagerTestCase):def test_demo_user(self):- #os.environ['RECAPTCHA_TESTING'] = 'True'# Open the copy workout formresponse = self.client.get(reverse('demo-account'))self.assertEqual(response.status_code, 200)diff --git a/wger/manager/tests/testcase.py b/wger/manager/tests/testcase.pyindex 980ff7033..eaf37f774 100644--- a/wger/manager/tests/testcase.py+++ b/wger/manager/tests/testcase.py@@ -26,6 +26,7 @@ from selenium.webdriver.common.keys import Keysclass WorkoutManagerTestCase(TestCase):fixtures = ('tests-user-data',+ 'test-weight-data','test-exercises','tests-ingredients','days_of_week',diff --git a/wger/manager/tests/weight_log.py b/wger/manager/tests/weight_log.pyindex 5c3ea3461..5db4da599 100644--- a/wger/manager/tests/weight_log.py+++ b/wger/manager/tests/weight_log.py@@ -158,34 +158,3 @@ class WeightLogEntryTestCase(WorkoutManagerTestCase):self.user_login('test')self.edit_log_entry(fail=True)---#class WorkoutLogLiveServerTestCase(WorkoutManagerLiveServerTestCase):- #'''- #Selenium tests for workout log- #'''-- #def test_find_links(self):- #'''- #Tests that the correct links are in the right places on the workout- #view- #'''-- #self.user_login()- #self.browser.get(self.live_server_url + reverse('workout-view', kwargs = {'id': 1}))-- ## There is a link to view/edit the logs for the day- #table_row = self.browser.find_element_by_id('day-1')- #self.assertTrue(table_row)- #links = table_row.find_elements_by_tag_name('a')- #self.assertEqual(len(links), 3)- #log_link = links[2]- #self.assertIn('Add weight log to this day', log_link.get_attribute('title'))- #self.assertIn(reverse('day-log', kwargs={'pk': 1}),- #log_link.get_attribute('href'))-- ## There is a link on the sidebar to access the log for the current workout- #sidebar = self.browser.find_element_by_id('main-sidebar')- #workout_log = sidebar.find_elements_by_tag_name('a')[2]- #self.assertIn(reverse('workout-log', kwargs={'pk': 1}),- #workout_log.get_attribute('href'))diff --git a/wger/weight/fixtures/test-weight-data.json b/wger/weight/fixtures/test-weight-data.jsonnew file mode 100644index 000000000..e88311470--- /dev/null+++ b/wger/weight/fixtures/test-weight-data.json@@ -0,0 +1,65 @@+[+ {+ "pk": 1,+ "model": "weight.weightentry",+ "fields": {+ "user": 2,+ "weight": 77,+ "creation_date": "2012-10-01"+ }+ },+ {+ "pk": 2,+ "model": "weight.weightentry",+ "fields": {+ "user": 2,+ "weight": 77.2,+ "creation_date": "2012-10-10"+ }+ },+ {+ "pk": 3,+ "model": "weight.weightentry",+ "fields": {+ "user": 2,+ "weight": 80.6,+ "creation_date": "2012-11-01"+ }+ },+ {+ "pk": 4,+ "model": "weight.weightentry",+ "fields": {+ "user": 2,+ "weight": 80,+ "creation_date": "2013-01-01"+ }+ },+ {+ "pk": 5,+ "model": "weight.weightentry",+ "fields": {+ "user": 2,+ "weight": 81,+ "creation_date": "2013-01-10"+ }+ },+ {+ "pk": 6,+ "model": "weight.weightentry",+ "fields": {+ "user": 2,+ "weight": 82,+ "creation_date": "2013-01-20"+ }+ },+ {+ "pk": 7,+ "model": "weight.weightentry",+ "fields": {+ "user": 2,+ "weight": 83,+ "creation_date": "2013-01-30"+ }+ }+]diff --git a/wger/weight/tests/__init__.py b/wger/weight/tests/__init__.pyindex 0a60fdc93..2d15fc0de 100644--- a/wger/weight/tests/__init__.py+++ b/wger/weight/tests/__init__.py@@ -1 +1,3 @@from wger.weight.tests.csv_import import *+from wger.weight.tests.overview import *+from wger.weight.tests.entry import *diff --git a/wger/weight/tests/entry.py b/wger/weight/tests/entry.pynew file mode 100644index 000000000..708157649--- /dev/null+++ b/wger/weight/tests/entry.py@@ -0,0 +1,134 @@+# 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 datetime++from django.core.urlresolvers import reverse++from wger.weight.models import WeightEntry++from wger.manager.tests.testcase import WorkoutManagerTestCase+++class AddWeightEntryTestCase(WorkoutManagerTestCase):+ '''+ Tests adding a weight entry+ '''++ def add_entry(self, fail=False):+ '''+ Helper function to test adding a weight entry+ '''++ # Fetch the add entry page+ response = self.client.get(reverse('weight-add'))++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ self.assertEqual(response.status_code, 200)++ # Enter the data+ response = self.client.post(reverse('weight-add'),+ {'weight': '81',+ 'creation_date': '2013-02-01'})+ if fail:+ self.assertRaises(WeightEntry.DoesNotExist, WeightEntry.objects.get, pk=8)+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ entry = WeightEntry.objects.get(pk=8)+ self.assertEqual(response.status_code, 302)+ self.assertEqual(entry.weight, 81)+ self.assertEqual(entry.creation_date, datetime.date(2013, 2, 1))++ def test_add_entry_anonymous(self):+ '''+ Test adding a weight entry as an anonymous user+ '''++ self.add_entry(fail=True)++ def test_add_entry_logged_in(self):+ '''+ Test adding a weight entry as a logged in user+ '''++ self.user_login('test')+ self.add_entry(fail=False)+++class EditWeightEntryTestCase(WorkoutManagerTestCase):+ '''+ Tests editing a weight entry+ '''++ def edit_entry(self, fail=False):+ '''+ Helper function+ '''++ # Fetch the day edit page+ response = self.client.get(reverse('weight-edit', kwargs={'pk': 1}))++ 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('weight-edit', kwargs={'pk': 1}),+ {'weight': 100,+ 'creation_date': '2005-01-01'})++ entry = WeightEntry.objects.get(pk=1)++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')+ self.assertEqual(entry.weight, 77)+ self.assertEqual(entry.creation_date, datetime.date(2012, 10, 1))++ else:+ self.assertEqual(response.status_code, 302)+ self.assertEqual(entry.weight, 100)+ self.assertEqual(entry.creation_date, datetime.date(2005, 1, 1))++ def test_edit_entry_anonymous(self):+ '''+ Test editing a weight entry as an anonymous user+ '''++ self.edit_entry(fail=True)++ def test_edit_entry_owner(self):+ '''+ Test editing a weight entry as the owner user+ '''++ self.user_login('test')+ self.edit_entry(fail=False)++ def test_edit_entry_other(self):+ '''+ Test editing a weight entry as a different user+ '''++ self.user_login('admin')+ self.edit_entry(fail=True)diff --git a/wger/weight/tests/overview.py b/wger/weight/tests/overview.pynew file mode 100644index 000000000..be4d340bb--- /dev/null+++ b/wger/weight/tests/overview.py@@ -0,0 +1,87 @@+# 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 WeightOverviewTestCase(WorkoutManagerTestCase):+ '''+ Test case for the weight overview page+ '''++ def weight_overview(self, fail=False):+ '''+ Helper function to test the weight overview page+ '''+ response = self.client.get(reverse('weight-overview'))++ if fail:+ # There is a redirect+ self.assertEqual(response.status_code, 302)+ else:+ # Logged in users see a page+ self.assertEqual(response.status_code, 200)++ def test_weight_overview_anonymous(self):+ '''+ Test the weight overview page by an unauthorized user+ '''+ self.weight_overview(fail=True)++ def test_weight_overview_loged_in(self):+ '''+ Test the weight overview page by a logged in user+ '''+ self.user_login('test')+ self.weight_overview(fail=False)+++class WeightExportCsvTestCase(WorkoutManagerTestCase):+ '''+ Tests exporting the saved weight entries as a CSV file+ '''++ def csv_export(self, fail=False):+ '''+ Helper function to test exporting the saved weight entries as a CSV file+ '''+ response = self.client.get(reverse('wger.weight.views.export_csv'))++ if fail:+ # There is a redirect+ self.assertEqual(response.status_code, 302)+ else:+ # Logged in users get the file+ self.assertEqual(response.status_code, 200)+ self.assertEqual(response['Content-Type'], 'text/csv')+ self.assertEqual(len(response.content), 132)++ def test_csv_export_anonymous(self):+ '''+ Test exporting the saved weight entries as a CSV file an unauthorized user+ '''+ self.csv_export(fail=True)++ def test_csv_export_loged_in(self):+ '''+ Test exporting the saved weight entries as a CSV file by a logged in user+ '''+ self.user_login('test')+ self.csv_export(fail=False)