mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-11 10:46:37 +00:00
Adds view tests
This commit is contained in:
parent
ed7d8e5501
commit
af8cb51325
2 changed files with 90 additions and 4 deletions
88
bookwyrm/tests/views/books/test_links.py
Normal file
88
bookwyrm/tests/views/books/test_links.py
Normal file
|
@ -0,0 +1,88 @@
|
|||
""" test for app action functionality """
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
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 LinkViews(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",
|
||||
)
|
||||
group = Group.objects.create(name="editor")
|
||||
group.permissions.add(
|
||||
Permission.objects.create(
|
||||
name="edit_book",
|
||||
codename="edit_book",
|
||||
content_type=ContentType.objects.get_for_model(models.User),
|
||||
).id
|
||||
)
|
||||
self.local_user.groups.add(group)
|
||||
|
||||
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_edit_link_page(self):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.FileLink.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
result = view(request, self.book.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_edit_link_post(self, *_):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
view = views.FileLink.as_view()
|
||||
form = forms.FileLinkForm()
|
||||
form.data["name"] = "hi"
|
||||
form.data["url"] = "https://www.example.com"
|
||||
form.data["filetype"] = "HTML"
|
||||
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
with patch(
|
||||
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
|
||||
) as mock:
|
||||
view(request, self.book.id)
|
||||
self.assertEqual(mock.call_count, 1)
|
||||
|
||||
activity = json.loads(mock.call_args[1]["args"][1])
|
||||
self.assertEqual(activity["type"], "Update")
|
||||
self.assertEqual(activity["object"]["type"], "Edition")
|
||||
|
||||
link = models.FileLink.objects.get()
|
||||
self.assertEqual(link.name, "hi")
|
||||
self.assertEqual(link.url, "https://www.example.com")
|
||||
self.assertEqual(link.filetype, "HTML")
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertEqual(self.book.file_links.first(), link)
|
|
@ -432,13 +432,11 @@ urlpatterns = [
|
|||
views.add_description,
|
||||
name="add-description",
|
||||
),
|
||||
re_path(
|
||||
rf"{BOOK_PATH}/file-link/?$", views.FileLink.as_view(), name="file-link"
|
||||
),
|
||||
re_path(rf"{BOOK_PATH}/file-link/?$", views.FileLink.as_view(), name="file-link"),
|
||||
re_path(
|
||||
rf"{BOOK_PATH}/file-link/(?P<link_id>\d+)/?$",
|
||||
views.FileLink.as_view(),
|
||||
name="file-link"
|
||||
name="file-link",
|
||||
),
|
||||
re_path(r"^resolve-book/?$", views.resolve_book, name="resolve-book"),
|
||||
re_path(r"^switch-edition/?$", views.switch_edition, name="switch-edition"),
|
||||
|
|
Loading…
Reference in a new issue