forked from mirrors/bookwyrm
Adds tests for upload cover view
bonus test for user view
This commit is contained in:
parent
9470b2831f
commit
a8545e2701
3 changed files with 83 additions and 22 deletions
|
@ -1,7 +1,14 @@
|
|||
""" test for app action functionality """
|
||||
from io import BytesIO
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
|
||||
from PIL import Image
|
||||
import responses
|
||||
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
@ -229,3 +236,53 @@ class BookViews(TestCase):
|
|||
result = view(request, self.work.id)
|
||||
self.assertIsInstance(result, ActivitypubResponse)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_upload_cover_file(self):
|
||||
""" add a cover via file upload """
|
||||
self.assertFalse(self.book.cover)
|
||||
image_file = pathlib.Path(__file__).parent.joinpath(
|
||||
"../../static/images/default_avi.jpg"
|
||||
)
|
||||
|
||||
form = forms.CoverForm(instance=self.book)
|
||||
form.data["cover"] = SimpleUploadedFile(
|
||||
image_file, open(image_file, "rb").read(), content_type="image/jpeg"
|
||||
)
|
||||
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
views.upload_cover(request, self.book.id)
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertTrue(self.book.cover)
|
||||
|
||||
@responses.activate
|
||||
def test_upload_cover_url(self):
|
||||
""" add a cover via url """
|
||||
self.assertFalse(self.book.cover)
|
||||
image_file = pathlib.Path(__file__).parent.joinpath(
|
||||
"../../static/images/default_avi.jpg"
|
||||
)
|
||||
image = Image.open(image_file)
|
||||
output = BytesIO()
|
||||
image.save(output, format=image.format)
|
||||
responses.add(
|
||||
responses.GET,
|
||||
"http://example.com",
|
||||
body=output.getvalue(),
|
||||
status=200,
|
||||
)
|
||||
|
||||
form = forms.CoverForm(instance=self.book)
|
||||
form.data["cover-url"] = "http://example.com"
|
||||
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
views.upload_cover(request, self.book.id)
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertTrue(self.book.cover)
|
||||
|
|
|
@ -5,6 +5,7 @@ from PIL import Image
|
|||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
@ -157,28 +158,31 @@ class UserViews(TestCase):
|
|||
self.assertEqual(self.local_user.email, "wow@email.com")
|
||||
|
||||
# idk how to mock the upload form, got tired of triyng to make it work
|
||||
# def test_edit_user_avatar(self):
|
||||
# ''' use a form to update a user '''
|
||||
# view = views.EditUser.as_view()
|
||||
# form = forms.EditUserForm(instance=self.local_user)
|
||||
# form.data['name'] = 'New Name'
|
||||
# form.data['email'] = 'wow@email.com'
|
||||
# image_file = pathlib.Path(__file__).parent.joinpath(
|
||||
# '../../static/images/no_cover.jpg')
|
||||
# image = Image.open(image_file)
|
||||
# form.files['avatar'] = SimpleUploadedFile(
|
||||
# image_file, open(image_file), content_type='image/jpeg')
|
||||
# request = self.factory.post('', form.data, form.files)
|
||||
# request.user = self.local_user
|
||||
def test_edit_user_avatar(self):
|
||||
""" use a form to update a user """
|
||||
view = views.EditUser.as_view()
|
||||
form = forms.EditUserForm(instance=self.local_user)
|
||||
form.data["name"] = "New Name"
|
||||
form.data["email"] = "wow@email.com"
|
||||
image_file = pathlib.Path(__file__).parent.joinpath(
|
||||
"../../static/images/no_cover.jpg"
|
||||
)
|
||||
form.data["avatar"] = SimpleUploadedFile(
|
||||
image_file, open(image_file, "rb").read(), content_type="image/jpeg"
|
||||
)
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
|
||||
# with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay') \
|
||||
# as delay_mock:
|
||||
# view(request)
|
||||
# self.assertEqual(delay_mock.call_count, 1)
|
||||
# self.assertEqual(self.local_user.name, 'New Name')
|
||||
# self.assertEqual(self.local_user.email, 'wow@email.com')
|
||||
# self.assertIsNotNone(self.local_user.avatar)
|
||||
# self.assertEqual(self.local_user.avatar.size, (120, 120))
|
||||
with patch(
|
||||
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
|
||||
) as delay_mock:
|
||||
view(request)
|
||||
self.assertEqual(delay_mock.call_count, 1)
|
||||
self.assertEqual(self.local_user.name, "New Name")
|
||||
self.assertEqual(self.local_user.email, "wow@email.com")
|
||||
self.assertIsNotNone(self.local_user.avatar)
|
||||
self.assertEqual(self.local_user.avatar.width, 120)
|
||||
self.assertEqual(self.local_user.avatar.height, 120)
|
||||
|
||||
def test_crop_avatar(self):
|
||||
""" reduce that image size """
|
||||
|
|
|
@ -173,7 +173,7 @@ class EditUser(View):
|
|||
# set the name to a hash
|
||||
extension = form.files["avatar"].name.split(".")[-1]
|
||||
filename = "%s.%s" % (uuid4(), extension)
|
||||
user.avatar.save(filename, image)
|
||||
user.avatar.save(filename, image, save=False)
|
||||
user.save()
|
||||
|
||||
return redirect(user.local_path)
|
||||
|
|
Loading…
Reference in a new issue