mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-25 16:38:09 +00:00
Adds view tests for shelf filters (#3162)
* Adds test file * Adds success assertion * Updates tests * Updates shelf books creation * Updates assertion to use isbn for Edition model * Updates query * trigger workflow test * Updates validate_html * Updates comment and test * Fixes none test * Adds management command to clear all deleted user data * Adds success message --------- Co-authored-by: Mouse Reeve <mousereeve@riseup.net> Co-authored-by: Mouse Reeve <mouse.reeve@gmail.com>
This commit is contained in:
parent
7469f1f4ca
commit
dd1999eb8e
2 changed files with 64 additions and 9 deletions
|
@ -13,16 +13,26 @@ def validate_html(html):
|
||||||
"warn-proprietary-attributes": False,
|
"warn-proprietary-attributes": False,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
# idk how else to filter out these unescape amp errs
|
# Tidy's parser is strict when validating unescaped/encoded ampersands found within
|
||||||
|
# the html document that are notpart of a character or entity reference
|
||||||
|
# (eg: `&` or `&`). Despite the fact the HTML5 spec no longer recommends
|
||||||
|
# escaping ampersands in URLs, Tidy will still complain if they are used as query
|
||||||
|
# param keys. Unfortunately, there is no way currently to configure tidy to ignore
|
||||||
|
# this so we must explictly redlist related strings that will appear in Tidy's
|
||||||
|
# errors output.
|
||||||
|
#
|
||||||
|
# See further discussion: https://github.com/htacg/tidy-html5/issues/1017
|
||||||
|
excluded = [
|
||||||
|
"&book",
|
||||||
|
"&type",
|
||||||
|
"&resolved",
|
||||||
|
"id and name attribute",
|
||||||
|
"illegal characters found in URI",
|
||||||
|
"escaping malformed URI reference",
|
||||||
|
"&filter",
|
||||||
|
]
|
||||||
errors = "\n".join(
|
errors = "\n".join(
|
||||||
e
|
e for e in errors.split("\n") if not any(exclude in e for exclude in excluded)
|
||||||
for e in errors.split("\n")
|
|
||||||
if "&book" not in e
|
|
||||||
and "&type" not in e
|
|
||||||
and "&resolved" not in e
|
|
||||||
and "id and name attribute" not in e
|
|
||||||
and "illegal characters found in URI" not in e
|
|
||||||
and "escaping malformed URI reference" not in e
|
|
||||||
)
|
)
|
||||||
if errors:
|
if errors:
|
||||||
raise Exception(errors)
|
raise Exception(errors)
|
||||||
|
|
|
@ -219,3 +219,48 @@ class ShelfViews(TestCase):
|
||||||
view(request, request.user.username, shelf.identifier)
|
view(request, request.user.username, shelf.identifier)
|
||||||
|
|
||||||
self.assertEqual(shelf.name, "To Read")
|
self.assertEqual(shelf.name, "To Read")
|
||||||
|
|
||||||
|
def test_filter_shelf_found(self, *_):
|
||||||
|
"""display books that match a filter keyword"""
|
||||||
|
models.ShelfBook.objects.create(
|
||||||
|
book=self.book,
|
||||||
|
shelf=self.shelf,
|
||||||
|
user=self.local_user,
|
||||||
|
)
|
||||||
|
shelf_book = models.ShelfBook.objects.create(
|
||||||
|
book=self.book,
|
||||||
|
shelf=self.local_user.shelf_set.first(),
|
||||||
|
user=self.local_user,
|
||||||
|
)
|
||||||
|
view = views.Shelf.as_view()
|
||||||
|
request = self.factory.get("", {"filter": shelf_book.book.title})
|
||||||
|
request.user = self.local_user
|
||||||
|
with patch("bookwyrm.views.shelf.shelf.is_api_request") as is_api:
|
||||||
|
is_api.return_value = False
|
||||||
|
result = view(request, self.local_user.username)
|
||||||
|
self.assertIsInstance(result, TemplateResponse)
|
||||||
|
validate_html(result.render())
|
||||||
|
self.assertEqual(result.status_code, 200)
|
||||||
|
self.assertEqual(len(result.context_data["books"].object_list), 1)
|
||||||
|
self.assertEqual(
|
||||||
|
result.context_data["books"].object_list[0].title,
|
||||||
|
shelf_book.book.title,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_filter_shelf_none(self, *_):
|
||||||
|
"""display a message when no books match a filter keyword"""
|
||||||
|
models.ShelfBook.objects.create(
|
||||||
|
book=self.book,
|
||||||
|
shelf=self.shelf,
|
||||||
|
user=self.local_user,
|
||||||
|
)
|
||||||
|
view = views.Shelf.as_view()
|
||||||
|
request = self.factory.get("", {"filter": "NOPE"})
|
||||||
|
request.user = self.local_user
|
||||||
|
with patch("bookwyrm.views.shelf.shelf.is_api_request") as is_api:
|
||||||
|
is_api.return_value = False
|
||||||
|
result = view(request, self.local_user.username)
|
||||||
|
self.assertIsInstance(result, TemplateResponse)
|
||||||
|
validate_html(result.render())
|
||||||
|
self.assertEqual(result.status_code, 200)
|
||||||
|
self.assertEqual(len(result.context_data["books"].object_list), 0)
|
||||||
|
|
Loading…
Reference in a new issue