moviewyrm/bookwyrm/tests/views/preferences/test_edit_user.py

124 lines
4.9 KiB
Python
Raw Normal View History

2021-06-14 16:57:51 +00:00
""" test for app action functionality """
import pathlib
from unittest.mock import patch
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
from bookwyrm import forms, models, views
2021-10-01 05:22:20 +00:00
from bookwyrm.tests.validate_html import validate_html
2021-06-14 16:57:51 +00:00
2021-08-03 20:03:24 +00:00
@patch("bookwyrm.suggested_users.remove_user_task.delay")
2021-06-14 16:57:51 +00:00
class EditUserViews(TestCase):
"""view user and edit profile"""
def setUp(self):
"""we need basic test data and mocks"""
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-10 03:14:10 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-08-03 17:25:53 +00:00
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
self.rat = models.User.objects.create_user(
"rat@local.com", "rat@rat.rat", "password", local=True, localname="rat"
)
2021-06-18 16:28:24 +00:00
2021-08-06 00:47:28 +00:00
self.book = models.Edition.objects.create(
title="test", parent_work=models.Work.objects.create(title="test work")
)
2021-11-12 17:17:00 +00:00
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.activitystreams.add_book_statuses_task.delay"):
2021-08-03 20:03:24 +00:00
models.ShelfBook.objects.create(
book=self.book,
user=self.local_user,
shelf=self.local_user.shelf_set.first(),
)
2021-06-14 16:57:51 +00:00
2021-08-02 23:05:40 +00:00
models.SiteSettings.objects.create()
2021-06-14 16:57:51 +00:00
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
2021-08-03 20:03:24 +00:00
def test_edit_user_page(self, _):
2021-06-14 16:57:51 +00:00
"""there are so many views, this just makes sure it LOADS"""
view = views.EditUser.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
2021-10-01 05:22:20 +00:00
validate_html(result.render())
2021-06-14 16:57:51 +00:00
self.assertEqual(result.status_code, 200)
2021-08-03 20:03:24 +00:00
def test_edit_user(self, _):
2021-06-14 16:57:51 +00:00
"""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"
2021-08-04 18:30:23 +00:00
form.data["default_post_privacy"] = "public"
2021-06-14 16:57:51 +00:00
form.data["preferred_timezone"] = "UTC"
request = self.factory.post("", form.data)
request.user = self.local_user
self.assertIsNone(self.local_user.name)
2021-08-02 23:05:40 +00:00
with patch(
2021-11-12 17:17:00 +00:00
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
2021-08-02 23:05:40 +00:00
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
2021-06-14 16:57:51 +00:00
self.assertEqual(self.local_user.name, "New Name")
self.assertEqual(self.local_user.email, "wow@email.com")
2021-08-03 20:03:24 +00:00
def test_edit_user_avatar(self, _):
2021-06-14 16:57:51 +00:00
"""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"
2021-08-04 18:30:23 +00:00
form.data["default_post_privacy"] = "public"
2021-06-14 16:57:51 +00:00
form.data["preferred_timezone"] = "UTC"
image_file = pathlib.Path(__file__).parent.joinpath(
2021-09-28 19:02:09 +00:00
"../../../static/images/no_cover.jpg"
2021-06-14 16:57:51 +00:00
)
2021-09-28 17:21:12 +00:00
# pylint: disable=consider-using-with
2021-06-14 16:57:51 +00:00
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
2021-08-02 23:05:40 +00:00
with patch(
2021-11-12 17:17:00 +00:00
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
2021-08-02 23:05:40 +00:00
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
2021-06-14 16:57:51 +00:00
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)
2021-08-03 20:03:24 +00:00
def test_crop_avatar(self, _):
2021-06-14 16:57:51 +00:00
"""reduce that image size"""
image_file = pathlib.Path(__file__).parent.joinpath(
2021-09-28 19:02:09 +00:00
"../../../static/images/no_cover.jpg"
2021-06-14 16:57:51 +00:00
)
image = Image.open(image_file)
result = views.preferences.edit_user.crop_avatar(image)
2021-06-14 16:57:51 +00:00
self.assertIsInstance(result, ContentFile)
image_result = Image.open(result)
self.assertEqual(image_result.size, (120, 120))