moviewyrm/bookwyrm/tests/views/books/test_links.py
2022-01-12 09:16:24 -08:00

89 lines
3.2 KiB
Python

""" 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_add_link_page(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.AddFileLink.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_add_link_post(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.AddFileLink.as_view()
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
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, "www.example.com")
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)