moviewyrm/bookwyrm/tests/views/test_author.py

174 lines
7 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 AnonymousUser, Group, Permission
2021-01-13 18:04:44 +00:00
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
2021-10-15 21:26:07 +00:00
from bookwyrm.tests.validate_html import validate_html
2021-01-13 18:04:44 +00:00
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-12-09 23:03:01 +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.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,
)
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
2021-08-02 23:05:40 +00:00
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("")
request.user = self.local_user
with patch("bookwyrm.views.author.is_api_request") as is_api:
is_api.return_value = False
result = view(request, author.id)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_author_page_logged_out(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Author.as_view()
author = models.Author.objects.create(name="Jessica")
request = self.factory.get("")
request.user = self.anonymous_user
2021-03-08 16:49:10 +00:00
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)
2021-10-15 21:26:07 +00:00
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-01-13 18:04:44 +00:00
def test_author_page_api_response(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Author.as_view()
author = models.Author.objects.create(name="Jessica")
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
request.user = self.local_user
2021-03-08 16:49:10 +00:00
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)
2021-10-15 21:26:07 +00:00
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-01-13 18:04:44 +00:00
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-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
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")
validate_html(resp.render())
self.assertEqual(resp.status_code, 200)
2021-12-07 21:05:31 +00:00
def test_update_author_from_remote(self):
"""call out to sync with remote connector"""
author = models.Author.objects.create(name="Test Author")
models.Connector.objects.create(
identifier="openlibrary.org",
name="OpenLibrary",
connector_file="openlibrary",
base_url="https://openlibrary.org",
books_url="https://openlibrary.org",
covers_url="https://covers.openlibrary.org",
search_url="https://openlibrary.org/search?q=",
isbn_search_url="https://openlibrary.org/isbn",
)
self.local_user.groups.add(self.group)
request = self.factory.post("")
request.user = self.local_user
with patch(
"bookwyrm.connectors.openlibrary.Connector.update_author_from_remote"
) as mock:
views.update_author_from_remote(request, author.id, "openlibrary.org")
self.assertEqual(mock.call_count, 1)