Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add some tests for the gallery
Roland Geider92c21708eadd
diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.pyindex 8d1451773..83b718636 100644--- a/wger/core/tests/base_testcase.py+++ b/wger/core/tests/base_testcase.py@@ -16,6 +16,7 @@import decimalimport loggingimport os+import pathlibimport shutilimport tempfile@@ -118,7 +119,8 @@ class BaseTestCase(object):'test-nutrition-diary','test-workout-data','test-workout-session',- 'test-schedules')+ 'test-schedules',+ 'test-gallery-images')current_user = 'anonymous'current_password = ''diff --git a/wger/exercises/tests/test_exercise_images.py b/wger/exercises/tests/test_exercise_images.pyindex 01279b41f..6f0358879 100644--- a/wger/exercises/tests/test_exercise_images.py+++ b/wger/exercises/tests/test_exercise_images.py@@ -35,7 +35,7 @@ class MainImageTestCase(WgerTestCase):Tests the methods to make sure there is always a main image per picture"""- def save_image(self, exercise, filename, db_filename=None):+ def save_image(self, exercise, filename, db_filename=None) -> int:"""Helper function to save an image to an exercise"""diff --git a/wger/gallery/fixtures/test-gallery-images.json b/wger/gallery/fixtures/test-gallery-images.jsonnew file mode 100644index 000000000..a5162aef3--- /dev/null+++ b/wger/gallery/fixtures/test-gallery-images.json@@ -0,0 +1,38 @@+[+ {+ "pk": 1,+ "model": "gallery.image",+ "fields": {+ "user": 1,+ "date": "2021-05-01",+ "description": "",+ "image": "gallery/1/protestschwein.jpg",+ "width": 500,+ "height": 332+ }+ },+ {+ "pk": 2,+ "model": "gallery.image",+ "fields": {+ "user": 1,+ "date": "2021-03-10",+ "description": "The Shiba Inu (柴犬) is a breed of hunting dog from Japan",+ "image": "gallery/1/wildschwein.jpg",+ "width": 482,+ "height": 320+ }+ },+ {+ "pk": 3,+ "model": "gallery.image",+ "fields": {+ "user": 2,+ "date": "2021-05-10",+ "description": "",+ "image": "gallery/2/wildschwein.jpg",+ "width": 482,+ "height": 320+ }+ }+]diff --git a/wger/gallery/migrations/0001_initial.py b/wger/gallery/migrations/0001_initial.pyindex 37541c1f5..2752dd91d 100644--- a/wger/gallery/migrations/0001_initial.py+++ b/wger/gallery/migrations/0001_initial.py@@ -19,13 +19,24 @@ class Migration(migrations.Migration):migrations.CreateModel(name='Image',fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),+ ('id', models.AutoField(auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name='ID')),('date', models.DateField(default=datetime.datetime.now, verbose_name='Date')),- ('image', models.ImageField(height_field='height', help_text='Only PNG and JPEG formats are supported', upload_to=wger.gallery.models.image.gallery_upload_dir, verbose_name='Image', width_field='width')),+ ('image', models.ImageField(height_field='height',+ help_text='Only PNG and JPEG formats are supported',+ upload_to=wger.gallery.models.image.gallery_upload_dir,+ verbose_name='Image',+ width_field='width')),('height', models.IntegerField(editable=False)),('width', models.IntegerField(editable=False)),- ('description', models.TextField(blank=True, max_length=1000, verbose_name='Description')),- ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='User')),+ ('description', models.TextField(blank=True,+ max_length=1000,+ verbose_name='Description')),+ ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,+ to=settings.AUTH_USER_MODEL,+ verbose_name='User')),],options={'ordering': ['-date'],diff --git a/wger/gallery/models/image.py b/wger/gallery/models/image.pyindex 6edb90888..c98a879bc 100644--- a/wger/gallery/models/image.py+++ b/wger/gallery/models/image.py@@ -31,8 +31,8 @@ def gallery_upload_dir(instance, filename):Returns the upload target for exercise images"""return "gallery/{0}/{1}{2}".format(instance.user.id,- uuid.uuid4(),- pathlib.Path(filename).suffix)+ uuid.uuid4(),+ pathlib.Path(filename).suffix)class Image(models.Model):@@ -63,7 +63,6 @@ class Image(models.Model):max_length=1000,blank=True)-def get_owner_object(self):"""Returns the object that has owner information@@ -74,6 +73,7 @@ class Image(models.Model):def is_landscape(self):return self.width > self.height+@receiver(models.signals.post_delete, sender=Image)def auto_delete_file_on_delete(sender, instance: Image, **kwargs):"""@@ -107,4 +107,3 @@ def auto_delete_file_on_change(sender, instance: Image, **kwargs):path = pathlib.Path(old_file.path)if path.is_file():path.unlink()-diff --git a/wger/gallery/tests/test_gallery_image.py b/wger/gallery/tests/test_gallery_image.pynew file mode 100644index 000000000..3055baba1--- /dev/null+++ b/wger/gallery/tests/test_gallery_image.py@@ -0,0 +1,65 @@+# 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+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# Standard Library+import datetime++# wger+from wger.core.tests.base_testcase import (+ WgerAddTestCase,+ WgerDeleteTestCase+)+from wger.gallery.models import Image+++class AddGalleryImageTestCase(WgerAddTestCase):+ """+ Tests adding an image to the gallery+ """++ object_class = Image+ url = 'gallery:images:add'+ user_fail = False+ data = {'date': datetime.date(2021, 5, 1),+ 'user': 1,+ 'description': 'Everything going well',+ 'image': open('wger/exercises/tests/protestschwein.jpg', 'rb')}+++class DeleteGalleryImageTestCase(WgerDeleteTestCase):+ """+ Tests deleting a gallery image+ """++ pk = 3+ object_class = Image+ url = 'gallery:images:delete'+ user_success = 'test'+ user_fail = 'admin'+++# class EditGalleryImageTestCase(WgerEditTestCase):+# """+# Tests editing a gallery image+# """+#+# pk = 3+# object_class = Image+# url = 'gallery:images:edit'+# user_success = 'test'+# user_fail = 'admin'+# data = {'date': datetime.date(2021, 5, 1),+# 'user': 2,+# 'description': 'Everything going well',+# 'image': open('wger/exercises/tests/protestschwein.jpg', 'rb')}diff --git a/wger/gallery/tests/test_gallery_overview.py b/wger/gallery/tests/test_gallery_overview.pynew file mode 100644index 000000000..2b5c27458--- /dev/null+++ b/wger/gallery/tests/test_gallery_overview.py@@ -0,0 +1,43 @@+# 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+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.++# Django+from django.urls import reverse++# wger+from wger.core.tests.base_testcase import WgerTestCase+++class GalleryAccessTestCase(WgerTestCase):+ """+ Gallery tests+ """++ def test_access_overview(self):+ """+ Test accessing the gallery overview+ """++ self.user_login('admin')+ response = self.client.get(reverse('gallery:images:overview'))+ self.assertEqual(response.status_code, 200)++ self.user_login('test')+ response = self.client.get(reverse('gallery:images:overview'))+ self.assertEqual(response.status_code, 200)++ self.user_logout()+ response = self.client.get(reverse('gallery:images:overview'))+ self.assertEqual(response.status_code, 302)diff --git a/wger/gallery/views/images.py b/wger/gallery/views/images.pyindex 34dff0b8d..97d5f0580 100644--- a/wger/gallery/views/images.py+++ b/wger/gallery/views/images.py@@ -13,6 +13,7 @@# GNU General Public License for more details.# You should have received a copy of the GNU Affero General Public License#+# Standard Libraryfrom datetime import datetime