2021-03-08 16:49:10 +00:00
|
|
|
""" test for app action functionality """
|
2021-04-08 22:16:49 +00:00
|
|
|
import json
|
2021-01-13 19:45:08 +00:00
|
|
|
from unittest.mock import patch
|
2021-09-28 23:18:32 +00:00
|
|
|
from tidylib import tidy_document
|
|
|
|
|
2021-09-29 00:02:18 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2021-01-13 19:45:08 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
2021-09-29 00:02:18 +00:00
|
|
|
from bookwyrm import forms, models, views
|
2021-01-13 19:45:08 +00:00
|
|
|
from bookwyrm.activitypub import ActivitypubResponse
|
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
2021-08-03 20:27:32 +00:00
|
|
|
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
|
2021-09-06 21:50:33 +00:00
|
|
|
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
|
2021-09-07 01:39:14 +00:00
|
|
|
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
|
|
|
|
@patch("bookwyrm.activitystreams.remove_book_statuses_task.delay")
|
2021-01-13 19:45:08 +00:00
|
|
|
class ShelfViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""tag views"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-13 19:45:08 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-01-13 19:45:08 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
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-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,
|
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-07 19:29:13 +00:00
|
|
|
self.shelf = models.Shelf.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
name="Test Shelf", identifier="test-shelf", user=self.local_user
|
2021-02-07 19:29:13 +00:00
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-08-03 20:27:32 +00:00
|
|
|
def test_shelf_page(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-13 19:45:08 +00:00
|
|
|
view = views.Shelf.as_view()
|
|
|
|
shelf = self.local_user.shelf_set.first()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-13 19:45:08 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.shelf.is_api_request") as is_api:
|
2021-01-13 19:45:08 +00:00
|
|
|
is_api.return_value = False
|
|
|
|
result = view(request, self.local_user.username, shelf.identifier)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-09-28 23:18:32 +00:00
|
|
|
html = result.render()
|
|
|
|
_, errors = tidy_document(
|
|
|
|
html.content,
|
|
|
|
options={
|
|
|
|
"drop-empty-elements": False,
|
|
|
|
"warn-proprietary-attributes": False,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if errors:
|
|
|
|
raise Exception(errors)
|
2021-01-13 19:45:08 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.shelf.is_api_request") as is_api:
|
2021-01-13 19:45:08 +00:00
|
|
|
is_api.return_value = True
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, self.local_user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("/?page=1")
|
2021-01-13 19:45:08 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.shelf.is_api_request") as is_api:
|
2021-01-13 19:45:08 +00:00
|
|
|
is_api.return_value = True
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, self.local_user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-08-03 20:27:32 +00:00
|
|
|
def test_edit_shelf_privacy(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""set name or privacy on shelf"""
|
2021-01-13 19:45:08 +00:00
|
|
|
view = views.Shelf.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf = self.local_user.shelf_set.get(identifier="to-read")
|
|
|
|
self.assertEqual(shelf.privacy, "public")
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
request = self.factory.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"",
|
|
|
|
{
|
|
|
|
"privacy": "unlisted",
|
|
|
|
"user": self.local_user.id,
|
|
|
|
"name": "To Read",
|
|
|
|
},
|
|
|
|
)
|
2021-01-13 19:45:08 +00:00
|
|
|
request.user = self.local_user
|
2021-01-27 17:27:29 +00:00
|
|
|
view(request, self.local_user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
shelf.refresh_from_db()
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(shelf.privacy, "unlisted")
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-08-03 20:27:32 +00:00
|
|
|
def test_edit_shelf_name(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""change the name of an editable shelf"""
|
2021-01-13 19:45:08 +00:00
|
|
|
view = views.Shelf.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf = models.Shelf.objects.create(name="Test Shelf", user=self.local_user)
|
|
|
|
self.assertEqual(shelf.privacy, "public")
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
request = self.factory.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"", {"privacy": "public", "user": self.local_user.id, "name": "cool name"}
|
|
|
|
)
|
2021-01-13 19:45:08 +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-02-07 19:29:13 +00:00
|
|
|
view(request, request.user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
shelf.refresh_from_db()
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(shelf.name, "cool name")
|
2021-09-28 00:58:21 +00:00
|
|
|
self.assertEqual(shelf.identifier, f"testshelf-{shelf.id}")
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-08-03 20:27:32 +00:00
|
|
|
def test_edit_shelf_name_not_editable(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""can't change the name of an non-editable shelf"""
|
2021-01-13 19:45:08 +00:00
|
|
|
view = views.Shelf.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf = self.local_user.shelf_set.get(identifier="to-read")
|
|
|
|
self.assertEqual(shelf.privacy, "public")
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
request = self.factory.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"", {"privacy": "public", "user": self.local_user.id, "name": "cool name"}
|
|
|
|
)
|
2021-01-13 19:45:08 +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-02-07 19:29:13 +00:00
|
|
|
view(request, request.user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(shelf.name, "To Read")
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-09-29 00:02:18 +00:00
|
|
|
def test_shelve(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelve a book"""
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post(
|
|
|
|
"", {"book": self.book.id, "shelf": self.shelf.identifier}
|
|
|
|
)
|
2021-01-13 19:45:08 +00:00
|
|
|
request.user = self.local_user
|
2021-04-08 22:16:49 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
|
2021-02-07 19:29:13 +00:00
|
|
|
views.shelve(request)
|
2021-04-08 22:16:49 +00:00
|
|
|
|
|
|
|
self.assertEqual(mock.call_count, 1)
|
|
|
|
activity = json.loads(mock.call_args[0][1])
|
|
|
|
self.assertEqual(activity["type"], "Add")
|
|
|
|
|
|
|
|
item = models.ShelfBook.objects.get()
|
|
|
|
self.assertEqual(activity["object"]["id"], item.remote_id)
|
2021-01-13 19:45:08 +00:00
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(self.shelf.books.get(), self.book)
|
|
|
|
|
2021-09-29 00:02:18 +00:00
|
|
|
def test_shelve_to_read(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""special behavior for the to-read shelf"""
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf = models.Shelf.objects.get(identifier="to-read")
|
|
|
|
request = self.factory.post(
|
|
|
|
"", {"book": self.book.id, "shelf": shelf.identifier}
|
|
|
|
)
|
2021-01-13 19:45:08 +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-02-07 19:29:13 +00:00
|
|
|
views.shelve(request)
|
2021-01-13 19:45:08 +00:00
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
2021-09-29 00:02:18 +00:00
|
|
|
def test_shelve_reading(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""special behavior for the reading shelf"""
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf = models.Shelf.objects.get(identifier="reading")
|
|
|
|
request = self.factory.post(
|
|
|
|
"", {"book": self.book.id, "shelf": shelf.identifier}
|
|
|
|
)
|
2021-01-13 19:45:08 +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-02-07 19:29:13 +00:00
|
|
|
views.shelve(request)
|
2021-01-13 19:45:08 +00:00
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
2021-09-29 00:02:18 +00:00
|
|
|
def test_shelve_read(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""special behavior for the read shelf"""
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf = models.Shelf.objects.get(identifier="read")
|
|
|
|
request = self.factory.post(
|
|
|
|
"", {"book": self.book.id, "shelf": shelf.identifier}
|
|
|
|
)
|
2021-01-13 19:45:08 +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 19:45:08 +00:00
|
|
|
views.shelve(request)
|
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
2021-09-29 00:02:18 +00:00
|
|
|
def test_unshelve(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove a book from a shelf"""
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-07 19:29:13 +00:00
|
|
|
models.ShelfBook.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
book=self.book, user=self.local_user, shelf=self.shelf
|
2021-02-07 19:29:13 +00:00
|
|
|
)
|
2021-04-08 22:16:49 +00:00
|
|
|
item = models.ShelfBook.objects.get()
|
|
|
|
|
2021-01-13 19:45:08 +00:00
|
|
|
self.shelf.save()
|
|
|
|
self.assertEqual(self.shelf.books.count(), 1)
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("", {"book": self.book.id, "shelf": self.shelf.id})
|
2021-01-13 19:45:08 +00:00
|
|
|
request.user = self.local_user
|
2021-04-08 22:16:49 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
|
2021-01-13 19:45:08 +00:00
|
|
|
views.unshelve(request)
|
2021-04-08 22:16:49 +00:00
|
|
|
activity = json.loads(mock.call_args[0][1])
|
|
|
|
self.assertEqual(activity["type"], "Remove")
|
|
|
|
self.assertEqual(activity["object"]["id"], item.remote_id)
|
2021-01-13 19:45:08 +00:00
|
|
|
self.assertEqual(self.shelf.books.count(), 0)
|
2021-09-29 00:02:18 +00:00
|
|
|
|
|
|
|
def test_create_shelf(self, *_):
|
|
|
|
"""a brand new custom shelf"""
|
|
|
|
form = forms.ShelfForm()
|
|
|
|
form.data["user"] = self.local_user.id
|
|
|
|
form.data["name"] = "new shelf name"
|
|
|
|
form.data["description"] = "desc"
|
|
|
|
form.data["privacy"] = "unlisted"
|
|
|
|
request = self.factory.post("", form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
views.create_shelf(request)
|
|
|
|
|
|
|
|
shelf = models.Shelf.objects.get(name="new shelf name")
|
|
|
|
self.assertEqual(shelf.privacy, "unlisted")
|
|
|
|
self.assertEqual(shelf.description, "desc")
|
|
|
|
self.assertEqual(shelf.user, self.local_user)
|
|
|
|
|
|
|
|
def test_delete_shelf(self, *_):
|
|
|
|
"""delete a brand new custom shelf"""
|
|
|
|
request = self.factory.post("")
|
|
|
|
request.user = self.local_user
|
|
|
|
shelf_id = self.shelf.id
|
|
|
|
|
|
|
|
views.delete_shelf(request, shelf_id)
|
|
|
|
|
|
|
|
self.assertFalse(models.Shelf.objects.filter(id=shelf_id).exists())
|
|
|
|
|
|
|
|
def test_delete_shelf_unauthorized(self, *_):
|
|
|
|
"""delete a brand new custom shelf"""
|
2021-09-29 00:40:55 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
|
|
|
rat = models.User.objects.create_user(
|
|
|
|
"rat@local.com",
|
|
|
|
"rat@mouse.mouse",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="rat",
|
|
|
|
)
|
2021-09-29 00:02:18 +00:00
|
|
|
request = self.factory.post("")
|
2021-09-29 00:40:55 +00:00
|
|
|
request.user = rat
|
2021-09-29 00:02:18 +00:00
|
|
|
|
|
|
|
with self.assertRaises(PermissionDenied):
|
|
|
|
views.delete_shelf(request, self.shelf.id)
|
|
|
|
|
|
|
|
self.assertTrue(models.Shelf.objects.filter(id=self.shelf.id).exists())
|
|
|
|
|
|
|
|
def test_delete_shelf_has_book(self, *_):
|
|
|
|
"""delete a brand new custom shelf"""
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=self.book, user=self.local_user, shelf=self.shelf
|
|
|
|
)
|
|
|
|
request = self.factory.post("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with self.assertRaises(PermissionDenied):
|
|
|
|
views.delete_shelf(request, self.shelf.id)
|
|
|
|
|
|
|
|
self.assertTrue(models.Shelf.objects.filter(id=self.shelf.id).exists())
|
|
|
|
|
|
|
|
def test_delete_shelf_not_editable(self, *_):
|
|
|
|
"""delete a brand new custom shelf"""
|
2021-09-29 00:17:01 +00:00
|
|
|
shelf = self.local_user.shelf_set.first()
|
|
|
|
self.assertFalse(shelf.editable)
|
2021-09-29 00:02:18 +00:00
|
|
|
request = self.factory.post("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with self.assertRaises(PermissionDenied):
|
|
|
|
views.delete_shelf(request, shelf.id)
|
|
|
|
|
|
|
|
self.assertTrue(models.Shelf.objects.filter(id=shelf.id).exists())
|