Merge pull request #1469 from bookwyrm-social/user-shelf
Reorganize user/shelf/goal views code
This commit is contained in:
@ -1,11 +1,14 @@
|
||||
""" test for app action functionality """
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
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 models, views
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
|
||||
|
||||
@ -53,7 +56,16 @@ class ShelfViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, self.local_user.username, shelf.identifier)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.shelf.is_api_request") as is_api:
|
||||
@ -122,7 +134,7 @@ class ShelfViews(TestCase):
|
||||
|
||||
self.assertEqual(shelf.name, "To Read")
|
||||
|
||||
def test_handle_shelve(self, *_):
|
||||
def test_shelve(self, *_):
|
||||
"""shelve a book"""
|
||||
request = self.factory.post(
|
||||
"", {"book": self.book.id, "shelf": self.shelf.identifier}
|
||||
@ -140,7 +152,7 @@ class ShelfViews(TestCase):
|
||||
# make sure the book is on the shelf
|
||||
self.assertEqual(self.shelf.books.get(), self.book)
|
||||
|
||||
def test_handle_shelve_to_read(self, *_):
|
||||
def test_shelve_to_read(self, *_):
|
||||
"""special behavior for the to-read shelf"""
|
||||
shelf = models.Shelf.objects.get(identifier="to-read")
|
||||
request = self.factory.post(
|
||||
@ -153,7 +165,7 @@ class ShelfViews(TestCase):
|
||||
# make sure the book is on the shelf
|
||||
self.assertEqual(shelf.books.get(), self.book)
|
||||
|
||||
def test_handle_shelve_reading(self, *_):
|
||||
def test_shelve_reading(self, *_):
|
||||
"""special behavior for the reading shelf"""
|
||||
shelf = models.Shelf.objects.get(identifier="reading")
|
||||
request = self.factory.post(
|
||||
@ -166,7 +178,7 @@ class ShelfViews(TestCase):
|
||||
# make sure the book is on the shelf
|
||||
self.assertEqual(shelf.books.get(), self.book)
|
||||
|
||||
def test_handle_shelve_read(self, *_):
|
||||
def test_shelve_read(self, *_):
|
||||
"""special behavior for the read shelf"""
|
||||
shelf = models.Shelf.objects.get(identifier="read")
|
||||
request = self.factory.post(
|
||||
@ -179,7 +191,7 @@ class ShelfViews(TestCase):
|
||||
# make sure the book is on the shelf
|
||||
self.assertEqual(shelf.books.get(), self.book)
|
||||
|
||||
def test_handle_unshelve(self, *_):
|
||||
def test_unshelve(self, *_):
|
||||
"""remove a book from a shelf"""
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
models.ShelfBook.objects.create(
|
||||
@ -197,3 +209,76 @@ class ShelfViews(TestCase):
|
||||
self.assertEqual(activity["type"], "Remove")
|
||||
self.assertEqual(activity["object"]["id"], item.remote_id)
|
||||
self.assertEqual(self.shelf.books.count(), 0)
|
||||
|
||||
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"""
|
||||
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",
|
||||
)
|
||||
request = self.factory.post("")
|
||||
request.user = rat
|
||||
|
||||
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"""
|
||||
shelf = self.local_user.shelf_set.first()
|
||||
self.assertFalse(shelf.editable)
|
||||
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())
|
||||
|
@ -1,5 +1,6 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.http.response import Http404
|
||||
@ -55,7 +56,16 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
request.user = self.anonymous_user
|
||||
@ -63,7 +73,16 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
||||
@ -92,7 +111,16 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
||||
@ -123,7 +151,16 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
||||
|
Reference in New Issue
Block a user