Adds more tests of shelf views

This commit is contained in:
Mouse Reeve 2021-10-20 14:15:05 -07:00
parent b64a616ff9
commit 1bb23a8edf

View file

@ -2,6 +2,7 @@
import json import json
from unittest.mock import patch from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.test import TestCase from django.test import TestCase
@ -46,6 +47,46 @@ class ShelfViews(TestCase):
) )
models.SiteSettings.objects.create() models.SiteSettings.objects.create()
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
def test_shelf_page_all_books(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.Shelf.as_view()
request = self.factory.get("")
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)
def test_shelf_page_all_books_anonymous(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.Shelf.as_view()
request = self.factory.get("")
request.user = self.anonymous_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)
def test_shelf_page_sorted(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.Shelf.as_view()
shelf = self.local_user.shelf_set.first()
request = self.factory.get("", {"sort": "author"})
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, shelf.identifier)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_shelf_page(self, *_): def test_shelf_page(self, *_):
"""there are so many views, this just makes sure it LOADS""" """there are so many views, this just makes sure it LOADS"""
view = views.Shelf.as_view() view = views.Shelf.as_view()
@ -59,7 +100,7 @@ class ShelfViews(TestCase):
validate_html(result.render()) validate_html(result.render())
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.shelf.is_api_request") as is_api: with patch("bookwyrm.views.shelf.shelf.is_api_request") as is_api:
is_api.return_value = True is_api.return_value = True
result = view(request, self.local_user.username, shelf.identifier) result = view(request, self.local_user.username, shelf.identifier)
self.assertIsInstance(result, ActivitypubResponse) self.assertIsInstance(result, ActivitypubResponse)
@ -67,7 +108,7 @@ class ShelfViews(TestCase):
request = self.factory.get("/?page=1") request = self.factory.get("/?page=1")
request.user = self.local_user request.user = self.local_user
with patch("bookwyrm.views.shelf.is_api_request") as is_api: with patch("bookwyrm.views.shelf.shelf.is_api_request") as is_api:
is_api.return_value = True is_api.return_value = True
result = view(request, self.local_user.username, shelf.identifier) result = view(request, self.local_user.username, shelf.identifier)
self.assertIsInstance(result, ActivitypubResponse) self.assertIsInstance(result, ActivitypubResponse)