2021-03-08 16:49:10 +00:00
|
|
|
""" test for app action functionality """
|
2021-03-19 16:20:48 +00:00
|
|
|
from io import BytesIO
|
|
|
|
import pathlib
|
2021-01-13 17:42:54 +00:00
|
|
|
from unittest.mock import patch
|
2021-03-19 16:20:48 +00:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
import responses
|
|
|
|
|
2021-01-13 17:42:54 +00:00
|
|
|
from django.contrib.auth.models import Group, Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2021-03-19 16:20:48 +00:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2021-01-13 17:42:54 +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
|
|
|
|
|
|
|
|
|
2021-01-13 18:04:44 +00:00
|
|
|
class BookViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""books books books"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-13 17:42:54 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-01-13 17:42:54 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-08-03 17:25:53 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
|
|
|
|
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 17:42:54 +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 17:42:54 +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 17:42:54 +00:00
|
|
|
|
|
|
|
def test_book_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-13 17:42:54 +00:00
|
|
|
view = views.Book.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-13 17:42:54 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
2021-01-13 17:42:54 +00:00
|
|
|
is_api.return_value = False
|
|
|
|
result = view(request, self.book.id)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-13 17:42:54 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
2021-01-13 17:42:54 +00:00
|
|
|
is_api.return_value = True
|
|
|
|
result = view(request, self.book.id)
|
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_edit_book_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-13 17:42:54 +00:00
|
|
|
view = views.EditBook.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-13 17:42:54 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
2021-08-02 23:05:40 +00:00
|
|
|
result = view(request, self.book.id)
|
2021-01-13 17:42:54 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-13 17:42:54 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_edit_book(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""lets a user edit a book"""
|
2021-01-13 17:42:54 +00:00
|
|
|
view = views.EditBook.as_view()
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm(instance=self.book)
|
2021-03-08 16:49:10 +00:00
|
|
|
form.data["title"] = "New Title"
|
|
|
|
form.data["last_edited_by"] = self.local_user.id
|
|
|
|
request = self.factory.post("", form.data)
|
2021-01-13 17:42:54 +00:00
|
|
|
request.user = self.local_user
|
2021-05-26 21:57:29 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
view(request, self.book.id)
|
2021-05-26 21:57:29 +00:00
|
|
|
|
2021-01-13 17:42:54 +00:00
|
|
|
self.book.refresh_from_db()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(self.book.title, "New Title")
|
2021-01-13 17:42:54 +00:00
|
|
|
|
2021-03-04 21:48:50 +00:00
|
|
|
def test_edit_book_add_author(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""lets a user edit a book with new authors"""
|
2021-03-07 21:59:27 +00:00
|
|
|
view = views.EditBook.as_view()
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm(instance=self.book)
|
2021-03-08 18:10:30 +00:00
|
|
|
form.data["title"] = "New Title"
|
|
|
|
form.data["last_edited_by"] = self.local_user.id
|
|
|
|
form.data["add_author"] = "Sappho"
|
|
|
|
request = self.factory.post("", form.data)
|
2021-03-07 21:59:27 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
result = view(request, self.book.id)
|
|
|
|
result.render()
|
2021-03-07 21:59:27 +00:00
|
|
|
|
|
|
|
# the changes haven't been saved yet
|
|
|
|
self.book.refresh_from_db()
|
2021-03-08 18:10:30 +00:00
|
|
|
self.assertEqual(self.book.title, "Example Edition")
|
2021-03-07 21:59:27 +00:00
|
|
|
|
|
|
|
def test_edit_book_add_new_author_confirm(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""lets a user edit a book confirmed with new authors"""
|
2021-03-07 21:59:27 +00:00
|
|
|
view = views.ConfirmEditBook.as_view()
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm(instance=self.book)
|
2021-03-08 18:10:30 +00:00
|
|
|
form.data["title"] = "New Title"
|
|
|
|
form.data["last_edited_by"] = self.local_user.id
|
2021-03-14 02:09:09 +00:00
|
|
|
form.data["author-match-count"] = 1
|
|
|
|
form.data["author_match-0"] = "Sappho"
|
2021-03-08 18:10:30 +00:00
|
|
|
request = self.factory.post("", form.data)
|
2021-03-07 21:59:27 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
view(request, self.book.id)
|
2021-03-07 21:59:27 +00:00
|
|
|
|
|
|
|
self.book.refresh_from_db()
|
2021-03-08 18:10:30 +00:00
|
|
|
self.assertEqual(self.book.title, "New Title")
|
|
|
|
self.assertEqual(self.book.authors.first().name, "Sappho")
|
2021-03-04 21:48:50 +00:00
|
|
|
|
2021-03-07 23:14:57 +00:00
|
|
|
def test_edit_book_remove_author(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove an author from a book"""
|
2021-03-08 18:10:30 +00:00
|
|
|
author = models.Author.objects.create(name="Sappho")
|
2021-03-07 23:14:57 +00:00
|
|
|
self.book.authors.add(author)
|
|
|
|
form = forms.EditionForm(instance=self.book)
|
|
|
|
view = views.EditBook.as_view()
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm(instance=self.book)
|
2021-03-08 18:10:30 +00:00
|
|
|
form.data["title"] = "New Title"
|
|
|
|
form.data["last_edited_by"] = self.local_user.id
|
|
|
|
form.data["remove_authors"] = [author.id]
|
|
|
|
request = self.factory.post("", form.data)
|
2021-03-07 23:14:57 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
view(request, self.book.id)
|
2021-03-07 23:14:57 +00:00
|
|
|
self.book.refresh_from_db()
|
2021-03-08 18:10:30 +00:00
|
|
|
self.assertEqual(self.book.title, "New Title")
|
2021-03-07 23:14:57 +00:00
|
|
|
self.assertFalse(self.book.authors.exists())
|
|
|
|
|
2021-03-08 18:48:45 +00:00
|
|
|
def test_create_book(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""create an entirely new book and work"""
|
2021-03-08 18:48:45 +00:00
|
|
|
view = views.ConfirmEditBook.as_view()
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm()
|
|
|
|
form.data["title"] = "New Title"
|
|
|
|
form.data["last_edited_by"] = self.local_user.id
|
|
|
|
request = self.factory.post("", form.data)
|
|
|
|
request.user = self.local_user
|
2021-03-08 19:54:26 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
view(request)
|
|
|
|
|
2021-03-08 18:48:45 +00:00
|
|
|
book = models.Edition.objects.get(title="New Title")
|
|
|
|
self.assertEqual(book.parent_work.title, "New Title")
|
|
|
|
|
|
|
|
def test_create_book_existing_work(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""create an entirely new book and work"""
|
2021-03-08 18:48:45 +00:00
|
|
|
view = views.ConfirmEditBook.as_view()
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm()
|
|
|
|
form.data["title"] = "New Title"
|
|
|
|
form.data["parent_work"] = self.work.id
|
|
|
|
form.data["last_edited_by"] = self.local_user.id
|
|
|
|
request = self.factory.post("", form.data)
|
|
|
|
request.user = self.local_user
|
2021-03-08 19:54:26 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
view(request)
|
|
|
|
|
2021-03-08 18:48:45 +00:00
|
|
|
book = models.Edition.objects.get(title="New Title")
|
|
|
|
self.assertEqual(book.parent_work, self.work)
|
|
|
|
|
2021-03-08 19:11:05 +00:00
|
|
|
def test_create_book_with_author(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""create an entirely new book and work"""
|
2021-03-08 19:11:05 +00:00
|
|
|
view = views.ConfirmEditBook.as_view()
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm()
|
|
|
|
form.data["title"] = "New Title"
|
2021-03-14 02:09:09 +00:00
|
|
|
form.data["author-match-count"] = "1"
|
|
|
|
form.data["author_match-0"] = "Sappho"
|
2021-03-08 19:11:05 +00:00
|
|
|
form.data["last_edited_by"] = self.local_user.id
|
|
|
|
request = self.factory.post("", form.data)
|
|
|
|
request.user = self.local_user
|
2021-03-08 19:54:26 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
view(request)
|
|
|
|
|
2021-03-08 19:11:05 +00:00
|
|
|
book = models.Edition.objects.get(title="New Title")
|
|
|
|
self.assertEqual(book.parent_work.title, "New Title")
|
|
|
|
self.assertEqual(book.authors.first().name, "Sappho")
|
|
|
|
self.assertEqual(book.authors.first(), book.parent_work.authors.first())
|
|
|
|
|
2021-08-03 23:21:29 +00:00
|
|
|
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
|
|
|
|
def test_switch_edition(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""updates user's relationships to a book"""
|
2021-03-08 16:49:10 +00:00
|
|
|
work = models.Work.objects.create(title="test work")
|
2021-08-02 23:05:40 +00:00
|
|
|
edition1 = models.Edition.objects.create(title="first ed", parent_work=work)
|
2021-08-02 23:07:39 +00:00
|
|
|
edition2 = models.Edition.objects.create(title="second ed", parent_work=work)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
shelf = models.Shelf.objects.create(name="Test Shelf", user=self.local_user)
|
2021-02-08 17:38:28 +00:00
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=edition1,
|
|
|
|
user=self.local_user,
|
|
|
|
shelf=shelf,
|
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
models.ReadThrough.objects.create(user=self.local_user, book=edition1)
|
2021-01-13 17:42:54 +00:00
|
|
|
|
|
|
|
self.assertEqual(models.ShelfBook.objects.get().book, edition1)
|
|
|
|
self.assertEqual(models.ReadThrough.objects.get().book, edition1)
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("", {"edition": edition2.id})
|
2021-01-13 17:42:54 +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 17:42:54 +00:00
|
|
|
views.switch_edition(request)
|
|
|
|
|
|
|
|
self.assertEqual(models.ShelfBook.objects.get().book, edition2)
|
|
|
|
self.assertEqual(models.ReadThrough.objects.get().book, edition2)
|
|
|
|
|
|
|
|
def test_editions_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-13 17:42:54 +00:00
|
|
|
view = views.Editions.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
|
|
|
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
2021-01-13 17:42:54 +00:00
|
|
|
is_api.return_value = False
|
|
|
|
result = view(request, self.work.id)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-13 17:42:54 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
|
|
|
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
2021-01-13 17:42:54 +00:00
|
|
|
is_api.return_value = True
|
|
|
|
result = view(request, self.work.id)
|
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2021-03-19 16:20:48 +00:00
|
|
|
|
|
|
|
def test_upload_cover_file(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add a cover via file upload"""
|
2021-03-19 16:20:48 +00:00
|
|
|
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
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
|
|
|
|
) as delay_mock:
|
|
|
|
views.upload_cover(request, self.book.id)
|
|
|
|
self.assertEqual(delay_mock.call_count, 1)
|
2021-03-19 16:20:48 +00:00
|
|
|
|
|
|
|
self.book.refresh_from_db()
|
|
|
|
self.assertTrue(self.book.cover)
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_upload_cover_url(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add a cover via url"""
|
2021-03-19 16:20:48 +00:00
|
|
|
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
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
|
|
|
|
) as delay_mock:
|
|
|
|
views.upload_cover(request, self.book.id)
|
|
|
|
self.assertEqual(delay_mock.call_count, 1)
|
2021-03-19 16:20:48 +00:00
|
|
|
|
|
|
|
self.book.refresh_from_db()
|
|
|
|
self.assertTrue(self.book.cover)
|
2021-08-06 01:05:57 +00:00
|
|
|
|
|
|
|
def test_add_description(self):
|
|
|
|
"""add a book description"""
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
request = self.factory.post("", {"description": "new description hi"})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
views.add_description(request, self.book.id)
|
|
|
|
|
|
|
|
self.book.refresh_from_db()
|
|
|
|
self.assertEqual(self.book.description, "new description hi")
|
|
|
|
self.assertEqual(self.book.last_edited_by, self.local_user)
|