moviewyrm/bookwyrm/tests/views/test_user.py

208 lines
8.2 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
import pathlib
2021-01-12 20:05:30 +00:00
from unittest.mock import patch
from PIL import Image
2021-02-11 02:01:11 +00:00
from django.contrib.auth.models import AnonymousUser
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile
2021-01-12 20:05:30 +00:00
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.activitypub import ActivitypubResponse
class UserViews(TestCase):
2021-04-26 16:15:42 +00:00
"""view user and edit profile"""
2021-03-08 16:49:10 +00:00
2021-01-12 20:05:30 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-12 20:05:30 +00:00
self.factory = RequestFactory()
self.local_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
2021-01-26 16:31:55 +00:00
self.rat = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat@local.com", "rat@rat.rat", "password", local=True, localname="rat"
)
2021-04-20 19:31:45 +00:00
self.book = models.Edition.objects.create(title="test")
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.ShelfBook.objects.create(
book=self.book,
user=self.local_user,
shelf=self.local_user.shelf_set.first(),
)
models.SiteSettings.objects.create()
2021-02-11 02:01:11 +00:00
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
2021-01-12 20:05:30 +00:00
def test_user_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-12 20:05:30 +00:00
view = views.User.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-12 20:05:30 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-12 20:05:30 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "mouse")
2021-01-12 20:05:30 +00:00
self.assertIsInstance(result, TemplateResponse)
result.render()
2021-01-12 20:05:30 +00:00
self.assertEqual(result.status_code, 200)
2021-02-11 02:01:11 +00:00
request.user = self.anonymous_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-02-11 02:01:11 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "mouse")
2021-02-11 02:01:11 +00:00
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-12 20:05:30 +00:00
is_api.return_value = True
2021-03-08 16:49:10 +00:00
result = view(request, "mouse")
2021-01-12 20:05:30 +00:00
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
2021-01-26 16:31:55 +00:00
def test_user_page_blocked(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-26 16:31:55 +00:00
view = views.User.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-26 16:31:55 +00:00
request.user = self.local_user
self.rat.blocks.add(self.local_user)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-26 16:31:55 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "rat")
2021-01-26 16:31:55 +00:00
self.assertEqual(result.status_code, 404)
2021-01-12 20:05:30 +00:00
def test_followers_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-12 20:05:30 +00:00
view = views.Followers.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-12 20:05:30 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-12 20:05:30 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "mouse")
2021-01-12 20:05:30 +00:00
self.assertIsInstance(result, TemplateResponse)
result.render()
2021-01-12 20:05:30 +00:00
self.assertEqual(result.status_code, 200)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-12 20:05:30 +00:00
is_api.return_value = True
2021-03-08 16:49:10 +00:00
result = view(request, "mouse")
2021-01-12 20:05:30 +00:00
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
2021-01-26 16:31:55 +00:00
def test_followers_page_blocked(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-26 16:31:55 +00:00
view = views.Followers.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-26 16:31:55 +00:00
request.user = self.local_user
self.rat.blocks.add(self.local_user)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-26 16:31:55 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "rat")
2021-01-26 16:31:55 +00:00
self.assertEqual(result.status_code, 404)
2021-01-12 20:05:30 +00:00
def test_following_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-12 20:05:30 +00:00
view = views.Following.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-12 20:05:30 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-12 20:05:30 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "mouse")
2021-01-12 20:05:30 +00:00
self.assertIsInstance(result, TemplateResponse)
result.render()
2021-01-12 20:05:30 +00:00
self.assertEqual(result.status_code, 200)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-12 20:05:30 +00:00
is_api.return_value = True
2021-03-08 16:49:10 +00:00
result = view(request, "mouse")
2021-01-12 20:05:30 +00:00
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
2021-01-26 16:31:55 +00:00
def test_following_page_blocked(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-26 16:31:55 +00:00
view = views.Following.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-26 16:31:55 +00:00
request.user = self.local_user
self.rat.blocks.add(self.local_user)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.user.is_api_request") as is_api:
2021-01-26 16:31:55 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "rat")
2021-01-26 16:31:55 +00:00
self.assertEqual(result.status_code, 404)
2021-02-11 16:38:45 +00:00
def test_edit_user_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-12 20:05:30 +00:00
view = views.EditUser.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-12 20:05:30 +00:00
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
result.render()
2021-01-12 20:05:30 +00:00
self.assertEqual(result.status_code, 200)
def test_edit_user(self):
2021-04-26 16:15:42 +00:00
"""use a form to update a user"""
2021-01-12 20:05:30 +00:00
view = views.EditUser.as_view()
form = forms.EditUserForm(instance=self.local_user)
2021-03-08 16:49:10 +00:00
form.data["name"] = "New Name"
form.data["email"] = "wow@email.com"
form.data["preferred_timezone"] = "UTC"
2021-03-08 16:49:10 +00:00
request = self.factory.post("", form.data)
2021-01-12 20:05:30 +00:00
request.user = self.local_user
2021-02-11 20:42:54 +00:00
self.assertIsNone(self.local_user.name)
2021-03-08 16:49:10 +00:00
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
2021-01-12 20:05:30 +00:00
view(request)
2021-02-11 16:38:45 +00:00
self.assertEqual(delay_mock.call_count, 1)
2021-03-08 16:49:10 +00:00
self.assertEqual(self.local_user.name, "New Name")
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):
2021-04-26 16:15:42 +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"
form.data["preferred_timezone"] = "UTC"
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.width, 120)
self.assertEqual(self.local_user.avatar.height, 120)
2021-02-11 16:38:45 +00:00
def test_crop_avatar(self):
2021-04-26 16:15:42 +00:00
"""reduce that image size"""
image_file = pathlib.Path(__file__).parent.joinpath(
2021-03-08 16:49:10 +00:00
"../../static/images/no_cover.jpg"
)
image = Image.open(image_file)
result = views.user.crop_avatar(image)
self.assertIsInstance(result, ContentFile)
image_result = Image.open(result)
self.assertEqual(image_result.size, (120, 120))