From 6f5876487c8d53dba7dc1c6485696ad3693a42c0 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Sat, 24 Feb 2018 11:14:00 +0100 Subject: [PATCH] Added tests for forms and views in images plugin. --- tests/plugins/images/test_forms.py | 15 ++++++++++ tests/plugins/images/test_views.py | 44 +++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/plugins/images/test_forms.py diff --git a/tests/plugins/images/test_forms.py b/tests/plugins/images/test_forms.py new file mode 100644 index 00000000..fdb0f0f5 --- /dev/null +++ b/tests/plugins/images/test_forms.py @@ -0,0 +1,15 @@ +from django.test import TestCase +from django.utils.translation import gettext + +from wiki.plugins.images.forms import PurgeForm + + +class PurgeFormTest(TestCase): + def test_not_sure(self): + form = PurgeForm(data={'confirm': False}) + self.assertIs(form.is_valid(), False) + self.assertEqual(form.errors['confirm'], [gettext('You are not sure enough!')]) + + def test_sure(self): + form = PurgeForm(data={'confirm': True}) + self.assertIs(form.is_valid(), True) diff --git a/tests/plugins/images/test_views.py b/tests/plugins/images/test_views.py index 22804b78..a89d410a 100644 --- a/tests/plugins/images/test_views.py +++ b/tests/plugins/images/test_views.py @@ -3,12 +3,16 @@ from io import BytesIO from django.core.files.uploadedfile import InMemoryUploadedFile from django.urls import reverse +from PIL import Image from wiki.core.plugins import registry as plugin_registry from wiki.models import URLPath from wiki.plugins.images import models from wiki.plugins.images.wiki_plugin import ImagePlugin -from ...base import ArticleWebTestUtils, DjangoClientTestBase, RequireRootArticleMixin +from ...base import ( + ArticleWebTestUtils, DjangoClientTestBase, RequireRootArticleMixin, + wiki_override_settings, +) class ImageTests(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTestBase): @@ -137,3 +141,41 @@ class ImageTests(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTestB '
' ) self.assertEqual(output, expected) + + # https://gist.github.com/guillaumepiot/817a70706587da3bd862835c59ef584e + def generate_photo_file(self): + file = BytesIO() + image = Image.new('RGBA', size=(100, 100), color=(155, 0, 0)) + image.save(file, 'gif') + file.name = 'test.gif' + file.seek(0) + return file + + def test_add_revision(self): + self._create_test_image(path='') + image = models.Image.objects.get() + before_edit_rev = image.current_revision.revision_number + + response = self.client.post( + reverse('wiki:images_add_revision', kwargs={ + 'article_id': self.root_article, 'image_id': image.pk, 'path': '', + }), + data={'image': self.generate_photo_file()} + ) + self.assertRedirects( + response, reverse('wiki:edit', kwargs={'path': ''}) + ) + image = models.Image.objects.get() + self.assertEqual(models.Image.objects.count(), 1) + self.assertEqual(image.current_revision.previous_revision.revision_number, before_edit_rev) + + @wiki_override_settings(ACCOUNT_HANDLING=True) + def test_login_on_revision_add(self): + self._create_test_image(path='') + self.client.logout() + image = models.Image.objects.get() + url = reverse('wiki:images_add_revision', kwargs={ + 'article_id': self.root_article, 'image_id': image.pk, 'path': '', + }) + response = self.client.post(url, data={'image': self.generate_photo_file()}) + self.assertRedirects(response, '{}?next={}'.format(reverse('wiki:login'), url)) -- 2.45.2