moviewyrm/bookwyrm/tests/views/books/test_links.py

156 lines
5.6 KiB
Python
Raw Normal View History

2021-12-15 21:57:48 +00:00
""" 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()
2022-01-12 17:16:24 +00:00
def test_add_link_page(self):
2021-12-15 21:57:48 +00:00
"""there are so many views, this just makes sure it LOADS"""
2022-01-12 17:16:24 +00:00
view = views.AddFileLink.as_view()
2021-12-15 21:57:48 +00:00
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)
2022-01-12 17:16:24 +00:00
def test_add_link_post(self, *_):
2021-12-15 21:57:48 +00:00
"""there are so many views, this just makes sure it LOADS"""
2022-01-12 17:16:24 +00:00
view = views.AddFileLink.as_view()
2021-12-15 21:57:48 +00:00
form = forms.FileLinkForm()
form.data["url"] = "https://www.example.com"
form.data["filetype"] = "HTML"
2021-12-16 01:25:20 +00:00
form.data["book"] = self.book.id
2022-01-11 16:06:52 +00:00
form.data["added_by"] = self.local_user.id
2022-01-17 18:29:48 +00:00
form.data["availability"] = "loan"
2021-12-15 21:57:48 +00:00
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")
self.assertIsInstance(activity["object"]["fileLinks"], list)
self.assertEqual(
activity["object"]["fileLinks"][0]["href"], "https://www.example.com"
)
2022-02-09 17:23:01 +00:00
self.assertEqual(activity["object"]["fileLinks"][0]["mediaType"], "HTML")
self.assertEqual(
activity["object"]["fileLinks"][0]["attributedTo"],
2022-02-09 17:23:01 +00:00
self.local_user.remote_id,
)
2021-12-15 21:57:48 +00:00
link = models.FileLink.objects.get()
2022-01-10 04:30:23 +00:00
self.assertEqual(link.name, "www.example.com")
2021-12-15 21:57:48 +00:00
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)
2022-01-17 18:29:48 +00:00
def test_book_links(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.BookFileLinks.as_view()
models.FileLink.objects.create(
book=self.book,
added_by=self.local_user,
url="https://www.hello.com",
)
request = self.factory.get("")
request.user = self.local_user
result = view(request, self.book.id)
self.assertEqual(result.status_code, 200)
validate_html(result.render())
def test_book_links_post(self):
"""there are so many views, this just makes sure it LOADS"""
link = models.FileLink.objects.create(
book=self.book,
added_by=self.local_user,
url="https://www.hello.com",
)
view = views.BookFileLinks.as_view()
form = forms.FileLinkForm()
form.data["url"] = link.url
form.data["filetype"] = "HTML"
form.data["book"] = self.book.id
form.data["added_by"] = self.local_user.id
form.data["availability"] = "loan"
request = self.factory.post("", form.data)
request.user = self.local_user
view(request, self.book.id, link.id)
link.refresh_from_db()
self.assertEqual(link.filetype, "HTML")
self.assertEqual(link.availability, "loan")
def test_delete_link(self):
"""remove a link"""
link = models.FileLink.objects.create(
book=self.book,
added_by=self.local_user,
url="https://www.hello.com",
)
form = forms.FileLinkForm()
form.data["url"] = "https://www.example.com"
form.data["filetype"] = "HTML"
form.data["book"] = self.book.id
form.data["added_by"] = self.local_user.id
form.data["availability"] = "loan"
request = self.factory.post("", form.data)
request.user = self.local_user
views.delete_link(request, self.book.id, link.id)
self.assertFalse(models.FileLink.objects.exists())