moviewyrm/bookwyrm/tests/views/test_list.py

343 lines
13 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-04-08 21:20:16 +00:00
import json
2021-01-31 15:09:03 +00:00
from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
from bookwyrm.activitypub import ActivitypubResponse
2021-03-08 16:49:10 +00:00
# pylint: disable=unused-argument
2021-01-31 15:09:03 +00:00
class ListViews(TestCase):
2021-04-26 16:15:42 +00:00
"""tag views"""
2021-03-08 16:49:10 +00:00
2021-01-31 15:09:03 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-31 15:09:03 +00:00
self.factory = RequestFactory()
2021-08-03 17:25:53 +00:00
with patch("bookwyrm.suggested_users.rerank_suggestions_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.rat = models.User.objects.create_user(
"rat@local.com",
"rat@rat.com",
"ratword",
local=True,
localname="rat",
remote_id="https://example.com/users/rat",
)
2021-08-02 23:05:40 +00:00
work = models.Work.objects.create(title="Work")
self.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=work,
)
work_two = models.Work.objects.create(title="Labori")
self.book_two = models.Edition.objects.create(
title="Example Edition 2",
remote_id="https://example.com/book/2",
parent_work=work_two,
)
work_three = models.Work.objects.create(title="Trabajar")
self.book_three = models.Edition.objects.create(
title="Example Edition 3",
remote_id="https://example.com/book/3",
parent_work=work_three,
)
work_four = models.Work.objects.create(title="Travailler")
self.book_four = models.Edition.objects.create(
title="Example Edition 4",
remote_id="https://example.com/book/4",
parent_work=work_four,
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-02-07 06:37:19 +00:00
self.list = models.List.objects.create(
2021-03-08 16:49:10 +00:00
name="Test List", user=self.local_user
)
2021-01-31 15:09:03 +00:00
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
2021-08-02 23:05:40 +00:00
models.SiteSettings.objects.create()
2021-01-31 15:09:03 +00:00
def test_lists_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-31 15:09:03 +00:00
view = views.Lists.as_view()
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.List.objects.create(name="Public list", user=self.local_user)
2021-02-07 06:37:19 +00:00
models.List.objects.create(
2021-03-08 16:49:10 +00:00
name="Private list", privacy="direct", user=self.local_user
)
request = self.factory.get("")
2021-01-31 15:09:03 +00:00
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
request.user = self.anonymous_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
def test_lists_create(self):
2021-04-26 16:15:42 +00:00
"""create list view"""
2021-01-31 21:12:56 +00:00
view = views.Lists.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post(
"",
{
"name": "A list",
"description": "wow",
"privacy": "unlisted",
"curation": "open",
"user": self.local_user.id,
},
)
2021-01-31 21:12:56 +00:00
request.user = self.local_user
2021-04-08 21:20:16 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
result = view(request)
2021-04-08 21:51:52 +00:00
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["type"], "Create")
self.assertEqual(activity["actor"], self.local_user.remote_id)
2021-04-08 21:20:16 +00:00
2021-01-31 21:12:56 +00:00
self.assertEqual(result.status_code, 302)
2021-03-08 16:49:10 +00:00
new_list = models.List.objects.filter(name="A list").get()
self.assertEqual(new_list.description, "wow")
self.assertEqual(new_list.privacy, "unlisted")
self.assertEqual(new_list.curation, "open")
2021-01-31 21:12:56 +00:00
def test_list_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-31 15:09:03 +00:00
view = views.List.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-31 15:09:03 +00:00
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.ListItem.objects.create(
book_list=self.list,
user=self.local_user,
book=self.book,
approved=True,
order=1,
)
with patch("bookwyrm.views.list.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
2021-05-11 01:28:31 +00:00
self.assertEqual(result.status_code, 200)
def test_list_page_sorted(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.List.as_view()
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
for (i, book) in enumerate([self.book, self.book_two, self.book_three]):
models.ListItem.objects.create(
book_list=self.list,
user=self.local_user,
book=book,
approved=True,
order=i + 1,
)
request = self.factory.get("/?sort_by=order")
request.user = self.local_user
with patch("bookwyrm.views.list.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
request = self.factory.get("/?sort_by=title")
request.user = self.local_user
with patch("bookwyrm.views.list.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
request = self.factory.get("/?sort_by=rating")
request.user = self.local_user
with patch("bookwyrm.views.list.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
request = self.factory.get("/?sort_by=sdkfh")
request.user = self.local_user
with patch("bookwyrm.views.list.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
def test_list_page_empty(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.List.as_view()
request = self.factory.get("")
request.user = self.local_user
2021-01-31 15:09:03 +00:00
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.list.is_api_request") as is_api:
2021-01-31 15:09:03 +00:00
is_api.return_value = False
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
def test_list_page_logged_out(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.List.as_view()
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.ListItem.objects.create(
book_list=self.list,
user=self.local_user,
book=self.book,
approved=True,
order=1,
)
request = self.factory.get("")
2021-01-31 21:38:26 +00:00
request.user = self.anonymous_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.list.is_api_request") as is_api:
2021-01-31 21:38:26 +00:00
is_api.return_value = False
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
def test_list_page_json_view(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.List.as_view()
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.ListItem.objects.create(
book_list=self.list,
user=self.local_user,
book=self.book,
approved=True,
order=1,
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.list.is_api_request") as is_api:
2021-01-31 15:09:03 +00:00
is_api.return_value = True
result = view(request, self.list.id)
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
def test_list_page_json_view_page(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.List.as_view()
request = self.factory.get("")
request.user = self.local_user
2021-03-08 16:49:10 +00:00
request = self.factory.get("/?page=1")
2021-01-31 15:09:03 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.list.is_api_request") as is_api:
2021-01-31 15:09:03 +00:00
is_api.return_value = True
result = view(request, self.list.id)
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
2021-01-31 21:12:56 +00:00
def test_list_edit(self):
2021-04-26 16:15:42 +00:00
"""edit a list"""
2021-01-31 21:12:56 +00:00
view = views.List.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post(
"",
{
"name": "New Name",
"description": "wow",
"privacy": "direct",
"curation": "curated",
"user": self.local_user.id,
},
)
2021-01-31 21:12:56 +00:00
request.user = self.local_user
2021-04-08 21:20:16 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
result = view(request, self.list.id)
2021-04-08 21:51:52 +00:00
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["type"], "Update")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], self.list.remote_id)
2021-04-08 21:20:16 +00:00
2021-01-31 21:12:56 +00:00
self.assertEqual(result.status_code, 302)
self.list.refresh_from_db()
2021-03-08 16:49:10 +00:00
self.assertEqual(self.list.name, "New Name")
self.assertEqual(self.list.description, "wow")
self.assertEqual(self.list.privacy, "direct")
self.assertEqual(self.list.curation, "curated")
2021-01-31 21:12:56 +00:00
def test_curate_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-31 21:12:56 +00:00
view = views.Curate.as_view()
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.List.objects.create(name="Public list", user=self.local_user)
models.List.objects.create(
2021-03-08 16:49:10 +00:00
name="Private list", privacy="direct", user=self.local_user
)
request = self.factory.get("")
2021-01-31 21:12:56 +00:00
request.user = self.local_user
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
request.user = self.anonymous_user
result = view(request, self.list.id)
self.assertEqual(result.status_code, 302)
def test_user_lists_page(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.UserLists.as_view()
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.List.objects.create(name="Public list", user=self.local_user)
models.List.objects.create(
name="Private list", privacy="direct", user=self.local_user
)
request = self.factory.get("")
2021-01-31 21:12:56 +00:00
request.user = self.local_user
result = view(request, self.local_user.localname)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
2021-01-31 21:12:56 +00:00
def test_user_lists_page_logged_out(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.UserLists.as_view()
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.List.objects.create(name="Public list", user=self.local_user)
models.List.objects.create(
name="Private list", privacy="direct", user=self.local_user
)
request = self.factory.get("")
request.user = self.anonymous_user
2021-01-31 21:12:56 +00:00
result = view(request, self.local_user.username)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)