Merge pull request #1477 from bookwyrm-social/add-edit-book
Updates for adding and editing books
This commit is contained in:
21
bookwyrm/tests/validate_html.py
Normal file
21
bookwyrm/tests/validate_html.py
Normal file
@ -0,0 +1,21 @@
|
||||
""" html validation on rendered templates """
|
||||
from tidylib import tidy_document
|
||||
|
||||
|
||||
def validate_html(html):
|
||||
"""run tidy on html"""
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
# idk how else to filter out these unescape amp errs
|
||||
errors = "\n".join(
|
||||
e
|
||||
for e in errors.split("\n")
|
||||
if "&book" not in e and "id and name attribute" not in e
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
@ -1,11 +1,11 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class DashboardViews(TestCase):
|
||||
@ -35,8 +35,5 @@ class DashboardViews(TestCase):
|
||||
request.user.is_superuser = True
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
@ -1,12 +1,12 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class EmailBlocklistViews(TestCase):
|
||||
@ -38,10 +38,7 @@ class EmailBlocklistViews(TestCase):
|
||||
result = view(request)
|
||||
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_blocklist_page_post(self):
|
||||
@ -54,10 +51,7 @@ class EmailBlocklistViews(TestCase):
|
||||
result = view(request)
|
||||
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
self.assertTrue(
|
||||
|
@ -1,7 +1,6 @@
|
||||
""" test for app action functionality """
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.template.response import TemplateResponse
|
||||
@ -9,6 +8,7 @@ from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class FederationViews(TestCase):
|
||||
@ -48,16 +48,7 @@ class FederationViews(TestCase):
|
||||
request.user.is_superuser = True
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_instance_page(self):
|
||||
@ -70,10 +61,7 @@ class FederationViews(TestCase):
|
||||
|
||||
result = view(request, server.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_server_page_block(self):
|
||||
@ -162,10 +150,7 @@ class FederationViews(TestCase):
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_add_view_post_create(self):
|
||||
|
@ -1,11 +1,11 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class IPBlocklistViews(TestCase):
|
||||
@ -37,8 +37,5 @@ class IPBlocklistViews(TestCase):
|
||||
result = view(request)
|
||||
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
@ -1,13 +1,13 @@
|
||||
""" test for app action functionality """
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
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.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class ReportViews(TestCase):
|
||||
@ -44,16 +44,7 @@ class ReportViews(TestCase):
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_reports_page_with_data(self):
|
||||
@ -66,16 +57,7 @@ class ReportViews(TestCase):
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_report_page(self):
|
||||
@ -89,10 +71,7 @@ class ReportViews(TestCase):
|
||||
result = view(request, report.id)
|
||||
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_report_comment(self):
|
||||
|
@ -1,6 +1,5 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
@ -8,6 +7,7 @@ from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class UserAdminViews(TestCase):
|
||||
@ -36,10 +36,7 @@ class UserAdminViews(TestCase):
|
||||
request.user.is_superuser = True
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_user_admin_page(self):
|
||||
@ -52,10 +49,7 @@ class UserAdminViews(TestCase):
|
||||
result = view(request, self.local_user.id)
|
||||
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
|
||||
@ -77,10 +71,7 @@ class UserAdminViews(TestCase):
|
||||
result = view(request, self.local_user.id)
|
||||
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
|
||||
self.assertEqual(
|
||||
list(self.local_user.groups.values_list("name", flat=True)), ["editor"]
|
||||
|
1
bookwyrm/tests/views/books/__init__.py
Normal file
1
bookwyrm/tests/views/books/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from . import *
|
228
bookwyrm/tests/views/books/test_book.py
Normal file
228
bookwyrm/tests/views/books/test_book.py
Normal file
@ -0,0 +1,228 @@
|
||||
""" test for app action functionality """
|
||||
from io import BytesIO
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
from PIL import Image
|
||||
|
||||
import responses
|
||||
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.http import Http404
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
from django.utils import timezone
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class BookViews(TestCase):
|
||||
"""books books books"""
|
||||
|
||||
def setUp(self):
|
||||
"""we need basic test data and mocks"""
|
||||
self.factory = RequestFactory()
|
||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||
"bookwyrm.activitystreams.populate_stream_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",
|
||||
)
|
||||
self.group = Group.objects.create(name="editor")
|
||||
self.group.permissions.add(
|
||||
Permission.objects.create(
|
||||
name="edit_book",
|
||||
codename="edit_book",
|
||||
content_type=ContentType.objects.get_for_model(models.User),
|
||||
).id
|
||||
)
|
||||
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()
|
||||
|
||||
def test_book_page(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
models.ReadThrough.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
start_date=timezone.now(),
|
||||
)
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
|
||||
is_api.return_value = True
|
||||
result = view(request, self.book.id)
|
||||
self.assertIsInstance(result, ActivitypubResponse)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
def test_book_page_statuses(self, *_):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
|
||||
review = models.Review.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
content="hi",
|
||||
)
|
||||
|
||||
comment = models.Comment.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
content="hi",
|
||||
)
|
||||
|
||||
quote = models.Quotation.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
content="hi",
|
||||
quote="wow",
|
||||
)
|
||||
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id, user_statuses="review")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["statuses"].object_list[0], review)
|
||||
|
||||
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id, user_statuses="comment")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["statuses"].object_list[0], comment)
|
||||
|
||||
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id, user_statuses="quotation")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["statuses"].object_list[0], quote)
|
||||
|
||||
def test_book_page_invalid_id(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
with self.assertRaises(Http404):
|
||||
view(request, 0)
|
||||
|
||||
def test_book_page_work_id(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.work.id)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["book"], self.book)
|
||||
|
||||
def test_upload_cover_file(self):
|
||||
"""add a cover via file upload"""
|
||||
self.assertFalse(self.book.cover)
|
||||
image_file = pathlib.Path(__file__).parent.joinpath(
|
||||
"../../../static/images/default_avi.jpg"
|
||||
)
|
||||
|
||||
form = forms.CoverForm(instance=self.book)
|
||||
# pylint: disable=consider-using-with
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertTrue(self.book.cover)
|
||||
|
||||
@responses.activate
|
||||
def test_upload_cover_url(self):
|
||||
"""add a cover via url"""
|
||||
self.assertFalse(self.book.cover)
|
||||
form = forms.CoverForm(instance=self.book)
|
||||
form.data["cover-url"] = _setup_cover_url()
|
||||
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
|
||||
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)
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertTrue(self.book.cover)
|
||||
|
||||
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
|
||||
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
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)
|
||||
|
||||
|
||||
def _setup_cover_url():
|
||||
"""creates cover url mock"""
|
||||
cover_url = "http://example.com"
|
||||
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,
|
||||
cover_url,
|
||||
body=output.getvalue(),
|
||||
status=200,
|
||||
)
|
||||
return cover_url
|
@ -1,25 +1,19 @@
|
||||
""" test for app action functionality """
|
||||
from io import BytesIO
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
|
||||
from PIL import Image
|
||||
import responses
|
||||
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.http import Http404
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
from django.utils import timezone
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
from bookwyrm.tests.views.books.test_book import _setup_cover_url
|
||||
|
||||
|
||||
class BookViews(TestCase):
|
||||
class EditBookViews(TestCase):
|
||||
"""books books books"""
|
||||
|
||||
def setUp(self):
|
||||
@ -53,102 +47,6 @@ class BookViews(TestCase):
|
||||
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
def test_book_page(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
models.ReadThrough.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
start_date=timezone.now(),
|
||||
)
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
||||
is_api.return_value = True
|
||||
result = view(request, self.book.id)
|
||||
self.assertIsInstance(result, ActivitypubResponse)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
def test_book_page_statuses(self, *_):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
|
||||
review = models.Review.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
content="hi",
|
||||
)
|
||||
|
||||
comment = models.Comment.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
content="hi",
|
||||
)
|
||||
|
||||
quote = models.Quotation.objects.create(
|
||||
user=self.local_user,
|
||||
book=self.book,
|
||||
content="hi",
|
||||
quote="wow",
|
||||
)
|
||||
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id, user_statuses="review")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["statuses"].object_list[0], review)
|
||||
|
||||
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id, user_statuses="comment")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["statuses"].object_list[0], comment)
|
||||
|
||||
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.book.id, user_statuses="quotation")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["statuses"].object_list[0], quote)
|
||||
|
||||
def test_book_page_invalid_id(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
with self.assertRaises(Http404):
|
||||
view(request, 0)
|
||||
|
||||
def test_book_page_work_id(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Book.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
with patch("bookwyrm.views.books.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.work.id)
|
||||
result.render()
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.context_data["book"], self.book)
|
||||
|
||||
def test_edit_book_page(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.EditBook.as_view()
|
||||
@ -157,7 +55,7 @@ class BookViews(TestCase):
|
||||
request.user.is_superuser = True
|
||||
result = view(request, self.book.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_edit_book(self):
|
||||
@ -188,7 +86,7 @@ class BookViews(TestCase):
|
||||
request.user = self.local_user
|
||||
|
||||
result = view(request, self.book.id)
|
||||
result.render()
|
||||
validate_html(result.render())
|
||||
|
||||
# the changes haven't been saved yet
|
||||
self.book.refresh_from_db()
|
||||
@ -283,29 +181,12 @@ class BookViews(TestCase):
|
||||
self.assertEqual(book.authors.first().name, "Sappho")
|
||||
self.assertEqual(book.authors.first(), book.parent_work.authors.first())
|
||||
|
||||
def _setup_cover_url(self):
|
||||
cover_url = "http://example.com"
|
||||
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,
|
||||
cover_url,
|
||||
body=output.getvalue(),
|
||||
status=200,
|
||||
)
|
||||
return cover_url
|
||||
|
||||
@responses.activate
|
||||
def test_create_book_upload_cover_url(self):
|
||||
"""create an entirely new book and work with cover url"""
|
||||
self.assertFalse(self.book.cover)
|
||||
view = views.ConfirmEditBook.as_view()
|
||||
self.local_user.groups.add(self.group)
|
||||
cover_url = self._setup_cover_url()
|
||||
cover_url = _setup_cover_url()
|
||||
|
||||
form = forms.EditionForm()
|
||||
form.data["title"] = "New Title"
|
||||
@ -322,59 +203,3 @@ class BookViews(TestCase):
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertTrue(self.book.cover)
|
||||
|
||||
def test_upload_cover_file(self):
|
||||
"""add a cover via file upload"""
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertTrue(self.book.cover)
|
||||
|
||||
@responses.activate
|
||||
def test_upload_cover_url(self):
|
||||
"""add a cover via url"""
|
||||
self.assertFalse(self.book.cover)
|
||||
form = forms.CoverForm(instance=self.book)
|
||||
form.data["cover-url"] = self._setup_cover_url()
|
||||
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
|
||||
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)
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertTrue(self.book.cover)
|
||||
|
||||
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
|
||||
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
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)
|
@ -7,6 +7,7 @@ from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class BookViews(TestCase):
|
||||
@ -40,11 +41,11 @@ class BookViews(TestCase):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Editions.as_view()
|
||||
request = self.factory.get("")
|
||||
with patch("bookwyrm.views.editions.is_api_request") as is_api:
|
||||
with patch("bookwyrm.views.books.editions.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.work.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertTrue("paperback" in result.context_data["formats"])
|
||||
|
||||
@ -57,11 +58,11 @@ class BookViews(TestCase):
|
||||
)
|
||||
view = views.Editions.as_view()
|
||||
request = self.factory.get("")
|
||||
with patch("bookwyrm.views.editions.is_api_request") as is_api:
|
||||
with patch("bookwyrm.views.books.editions.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.work.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
result.render()
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(len(result.context_data["editions"].object_list), 2)
|
||||
self.assertEqual(len(result.context_data["formats"]), 2)
|
||||
@ -69,26 +70,26 @@ class BookViews(TestCase):
|
||||
self.assertTrue("okay" in result.context_data["formats"])
|
||||
|
||||
request = self.factory.get("", {"q": "fish"})
|
||||
with patch("bookwyrm.views.editions.is_api_request") as is_api:
|
||||
with patch("bookwyrm.views.books.editions.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.work.id)
|
||||
result.render()
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(len(result.context_data["editions"].object_list), 1)
|
||||
|
||||
request = self.factory.get("", {"q": "okay"})
|
||||
with patch("bookwyrm.views.editions.is_api_request") as is_api:
|
||||
with patch("bookwyrm.views.books.editions.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.work.id)
|
||||
result.render()
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(len(result.context_data["editions"].object_list), 1)
|
||||
|
||||
request = self.factory.get("", {"format": "okay"})
|
||||
with patch("bookwyrm.views.editions.is_api_request") as is_api:
|
||||
with patch("bookwyrm.views.books.editions.is_api_request") as is_api:
|
||||
is_api.return_value = False
|
||||
result = view(request, self.work.id)
|
||||
result.render()
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(len(result.context_data["editions"].object_list), 1)
|
||||
|
||||
@ -96,7 +97,7 @@ class BookViews(TestCase):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.Editions.as_view()
|
||||
request = self.factory.get("")
|
||||
with patch("bookwyrm.views.editions.is_api_request") as is_api:
|
||||
with patch("bookwyrm.views.books.editions.is_api_request") as is_api:
|
||||
is_api.return_value = True
|
||||
result = view(request, self.work.id)
|
||||
self.assertIsInstance(result, ActivitypubResponse)
|
@ -1,12 +1,12 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
||||
@ -46,10 +46,7 @@ class BlockViews(TestCase):
|
||||
request.user = self.local_user
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_block_post(self, _):
|
||||
|
@ -1,12 +1,12 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class ChangePasswordViews(TestCase):
|
||||
@ -35,10 +35,7 @@ class ChangePasswordViews(TestCase):
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_password_change(self):
|
||||
|
@ -1,7 +1,6 @@
|
||||
""" test for app action functionality """
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.contrib.sessions.middleware import SessionMiddleware
|
||||
from django.template.response import TemplateResponse
|
||||
@ -9,6 +8,7 @@ from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
@patch("bookwyrm.suggested_users.remove_user_task.delay")
|
||||
@ -53,10 +53,7 @@ class DeleteUserViews(TestCase):
|
||||
request.user = self.local_user
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
@patch("bookwyrm.suggested_users.rerank_suggestions_task")
|
||||
|
@ -2,7 +2,6 @@
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
from PIL import Image
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.core.files.base import ContentFile
|
||||
@ -12,6 +11,7 @@ from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
@patch("bookwyrm.suggested_users.remove_user_task.delay")
|
||||
@ -58,10 +58,7 @@ class EditUserViews(TestCase):
|
||||
request.user = self.local_user
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_edit_user(self, _):
|
||||
|
@ -1,6 +1,5 @@
|
||||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
from tidylib import tidy_document
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.template.response import TemplateResponse
|
||||
@ -8,6 +7,7 @@ from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
class DirectoryViews(TestCase):
|
||||
@ -52,16 +52,7 @@ class DirectoryViews(TestCase):
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_directory_page_empty(self):
|
||||
@ -72,10 +63,7 @@ class DirectoryViews(TestCase):
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_directory_page_logged_out(self):
|
||||
|
@ -1,6 +1,5 @@
|
||||
""" 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 import Http404
|
||||
@ -10,6 +9,7 @@ from django.test.client import RequestFactory
|
||||
from django.utils import timezone
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class GoalViews(TestCase):
|
||||
@ -62,16 +62,7 @@ class GoalViews(TestCase):
|
||||
request.user = self.local_user
|
||||
|
||||
result = view(request, self.local_user.localname, self.year)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
||||
def test_goal_page_anonymous(self):
|
||||
@ -102,16 +93,7 @@ class GoalViews(TestCase):
|
||||
request.user = self.rat
|
||||
|
||||
result = view(request, self.local_user.localname, timezone.now().year)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
||||
def test_goal_page_private(self):
|
||||
|
@ -1,7 +1,6 @@
|
||||
""" 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
|
||||
@ -10,6 +9,7 @@ from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
||||
@ -56,16 +56,7 @@ class ShelfViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, self.local_user.username, shelf.identifier)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.shelf.is_api_request") as is_api:
|
||||
|
@ -1,6 +1,5 @@
|
||||
""" 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
|
||||
@ -10,6 +9,7 @@ from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
class UserViews(TestCase):
|
||||
@ -56,16 +56,7 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
request.user = self.anonymous_user
|
||||
@ -73,16 +64,7 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
||||
@ -111,16 +93,7 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
||||
@ -151,16 +124,7 @@ class UserViews(TestCase):
|
||||
is_api.return_value = False
|
||||
result = view(request, "mouse")
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
html = result.render()
|
||||
_, errors = tidy_document(
|
||||
html.content,
|
||||
options={
|
||||
"drop-empty-elements": False,
|
||||
"warn-proprietary-attributes": False,
|
||||
},
|
||||
)
|
||||
if errors:
|
||||
raise Exception(errors)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
||||
|
Reference in New Issue
Block a user