moviewyrm/bookwyrm/tests/views/test_author.py

130 lines
5 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-13 18:04:44 +00:00
from unittest.mock import patch
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
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 AuthorViews(TestCase):
2021-04-26 16:15:42 +00:00
"""author views"""
2021-03-08 16:49:10 +00:00
2021-01-13 18:04:44 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-13 18:04:44 +00:00
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-08-03 17:25:53 +00:00
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
)
2021-03-08 16:49:10 +00:00
self.group = Group.objects.create(name="editor")
2021-01-13 18:04:44 +00:00
self.group.permissions.add(
Permission.objects.create(
2021-03-08 16:49:10 +00:00
name="edit_book",
codename="edit_book",
content_type=ContentType.objects.get_for_model(models.User),
).id
2021-01-13 18:04:44 +00:00
)
2021-08-02 23:05:40 +00:00
self.work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=self.work,
)
models.SiteSettings.objects.create()
2021-01-13 18:04:44 +00:00
def test_author_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-13 18:04:44 +00:00
view = views.Author.as_view()
2021-03-08 16:49:10 +00:00
author = models.Author.objects.create(name="Jessica")
request = self.factory.get("")
with patch("bookwyrm.views.author.is_api_request") as is_api:
2021-01-13 18:04:44 +00:00
is_api.return_value = False
result = view(request, author.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
2021-01-13 18:04:44 +00:00
self.assertEqual(result.status_code, 200)
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
with patch("bookwyrm.views.author.is_api_request") as is_api:
2021-01-13 18:04:44 +00:00
is_api.return_value = True
result = view(request, author.id)
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
def test_edit_author_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-13 18:04:44 +00:00
view = views.EditAuthor.as_view()
2021-03-08 16:49:10 +00:00
author = models.Author.objects.create(name="Test Author")
request = self.factory.get("")
2021-01-13 18:04:44 +00:00
request.user = self.local_user
request.user.is_superuser = True
result = view(request, author.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
2021-01-13 18:04:44 +00:00
self.assertEqual(result.status_code, 200)
def test_edit_author(self):
2021-04-26 16:15:42 +00:00
"""edit an author"""
2021-01-13 18:04:44 +00:00
view = views.EditAuthor.as_view()
2021-03-08 16:49:10 +00:00
author = models.Author.objects.create(name="Test Author")
2021-01-13 18:04:44 +00:00
self.local_user.groups.add(self.group)
form = forms.AuthorForm(instance=author)
2021-03-08 16:49:10 +00:00
form.data["name"] = "New Name"
form.data["last_edited_by"] = self.local_user.id
request = self.factory.post("", form.data)
2021-01-13 18:04:44 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 18:04:44 +00:00
view(request, author.id)
author.refresh_from_db()
2021-03-08 16:49:10 +00:00
self.assertEqual(author.name, "New Name")
2021-01-13 18:04:44 +00:00
self.assertEqual(author.last_edited_by, self.local_user)
def test_edit_author_non_editor(self):
2021-04-26 16:15:42 +00:00
"""edit an author with invalid post data"""
2021-01-13 18:04:44 +00:00
view = views.EditAuthor.as_view()
2021-03-08 16:49:10 +00:00
author = models.Author.objects.create(name="Test Author")
2021-01-13 18:04:44 +00:00
form = forms.AuthorForm(instance=author)
2021-03-08 16:49:10 +00:00
form.data["name"] = "New Name"
form.data["last_edited_by"] = self.local_user.id
request = self.factory.post("", form.data)
2021-01-13 18:04:44 +00:00
request.user = self.local_user
with self.assertRaises(PermissionDenied):
view(request, author.id)
author.refresh_from_db()
2021-03-08 16:49:10 +00:00
self.assertEqual(author.name, "Test Author")
2021-01-13 18:04:44 +00:00
def test_edit_author_invalid_form(self):
2021-04-26 16:15:42 +00:00
"""edit an author with invalid post data"""
2021-01-13 18:04:44 +00:00
view = views.EditAuthor.as_view()
2021-03-08 16:49:10 +00:00
author = models.Author.objects.create(name="Test Author")
2021-01-13 18:04:44 +00:00
self.local_user.groups.add(self.group)
form = forms.AuthorForm(instance=author)
2021-03-08 16:49:10 +00:00
form.data["name"] = ""
form.data["last_edited_by"] = self.local_user.id
request = self.factory.post("", form.data)
2021-01-13 18:04:44 +00:00
request.user = self.local_user
resp = view(request, author.id)
author.refresh_from_db()
2021-03-08 16:49:10 +00:00
self.assertEqual(author.name, "Test Author")
resp.render()
self.assertEqual(resp.status_code, 200)