moviewyrm/bookwyrm/tests/views/test_feed.py

224 lines
9.3 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-03-15 20:55:48 +00:00
from io import BytesIO
from unittest.mock import patch
2021-03-15 20:55:48 +00:00
import pathlib
from PIL import Image
from django.core.files.base import ContentFile
2021-09-28 00:32:54 +00:00
from django.http import Http404
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
2021-12-09 23:03:01 +00:00
from bookwyrm import forms, models, views
from bookwyrm.activitypub import ActivitypubResponse
2021-12-29 18:20:54 +00:00
from bookwyrm.tests.validate_html import validate_html
2021-03-23 16:09:30 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.get_activity_stream")
2021-09-06 20:53:49 +00:00
@patch("bookwyrm.activitystreams.add_status_task.delay")
2021-08-03 23:21:29 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
2021-09-06 21:50:33 +00:00
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
2021-12-09 23:03:01 +00:00
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
2021-08-03 23:21:29 +00:00
@patch("bookwyrm.suggested_users.remove_user_task.delay")
2021-03-15 20:55:48 +00:00
class FeedViews(TestCase):
2021-04-26 16:15:42 +00:00
"""activity feed, statuses, dms"""
2021-03-08 16:49:10 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""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"
2021-12-09 23:03:01 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-08-03 17:25:53 +00:00
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
2021-12-28 23:44:47 +00:00
self.another_user = models.User.objects.create_user(
"nutria@local.com",
"nutria@nutria.nutria",
2021-12-28 23:44:47 +00:00
"password",
local=True,
localname="nutria",
2021-12-28 23:44:47 +00:00
)
2021-08-02 23:05:40 +00:00
self.book = models.Edition.objects.create(
parent_work=models.Work.objects.create(title="hi"),
title="Example Edition",
remote_id="https://example.com/book/1",
)
models.SiteSettings.objects.create()
2021-08-04 15:50:50 +00:00
@patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions")
2021-03-23 16:09:30 +00:00
def test_feed(self, *_):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
view = views.Feed.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
request.user = self.local_user
2021-08-05 01:22:06 +00:00
result = view(request, "home")
self.assertIsInstance(result, TemplateResponse)
2021-12-29 18:20:54 +00:00
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-12-09 23:03:01 +00:00
@patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions")
def test_save_feed_settings(self, *_):
"""update display preferences"""
self.assertEqual(
self.local_user.feed_status_types,
["review", "comment", "quotation", "everything"],
)
view = views.Feed.as_view()
form = forms.FeedStatusTypesForm(instance=self.local_user)
form.data["feed_status_types"] = "review"
request = self.factory.post("", form.data)
request.user = self.local_user
result = view(request, "home")
self.assertEqual(result.status_code, 200)
self.local_user.refresh_from_db()
self.assertEqual(self.local_user.feed_status_types, ["review"])
2021-03-23 16:09:30 +00:00
def test_status_page(self, *_):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
view = views.Status.as_view()
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(content="hi", user=self.local_user)
request = self.factory.get("")
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "mouse", status.id)
self.assertIsInstance(result, TemplateResponse)
2021-12-29 18:20:54 +00:00
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = True
2021-03-08 16:49:10 +00:00
result = view(request, "mouse", status.id)
self.assertIsInstance(result, ActivitypubResponse)
2021-03-15 20:55:48 +00:00
self.assertEqual(result.status_code, 200)
2021-03-30 17:46:02 +00:00
def test_status_page_not_found(self, *_):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-03-30 17:46:02 +00:00
view = views.Status.as_view()
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = False
2021-09-28 00:32:54 +00:00
with self.assertRaises(Http404):
view(request, "mouse", 12345)
2021-03-30 17:46:02 +00:00
2021-08-28 16:55:06 +00:00
def test_status_page_not_found_wrong_user(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.Status.as_view()
another_user = models.User.objects.create_user(
"rat@local.com",
"rat@rat.rat",
"password",
local=True,
localname="rat",
)
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-08-28 16:55:06 +00:00
status = models.Status.objects.create(content="hi", user=another_user)
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = False
2021-09-28 00:32:54 +00:00
with self.assertRaises(Http404):
view(request, "mouse", status.id)
2021-08-28 16:55:06 +00:00
2021-03-23 16:09:30 +00:00
def test_status_page_with_image(self, *_):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-03-15 20:55:48 +00:00
view = views.Status.as_view()
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)
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-08-02 23:05:40 +00:00
status = models.Review.objects.create(
content="hi",
user=self.local_user,
book=self.book,
)
2021-03-15 20:55:48 +00:00
attachment = models.Image.objects.create(
status=status, caption="alt text here"
)
attachment.image.save("test.jpg", ContentFile(output.getvalue()))
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = False
result = view(request, "mouse", status.id)
self.assertIsInstance(result, TemplateResponse)
2021-12-29 18:20:54 +00:00
validate_html(result.render())
2021-03-15 20:55:48 +00:00
self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = True
result = view(request, "mouse", status.id)
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
2021-03-23 16:09:30 +00:00
def test_replies_page(self, *_):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
view = views.Replies.as_view()
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(content="hi", user=self.local_user)
request = self.factory.get("")
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = False
2021-03-08 16:49:10 +00:00
result = view(request, "mouse", status.id)
self.assertIsInstance(result, TemplateResponse)
2021-12-29 18:20:54 +00:00
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = True
2021-03-08 16:49:10 +00:00
result = view(request, "mouse", status.id)
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
2021-03-23 16:09:30 +00:00
def test_direct_messages_page(self, *_):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
view = views.DirectMessage.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
2021-12-29 18:20:54 +00:00
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-12-28 23:44:47 +00:00
def test_direct_messages_page_user(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.DirectMessage.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request, "nutria")
2021-12-28 23:44:47 +00:00
self.assertIsInstance(result, TemplateResponse)
2021-12-29 18:20:54 +00:00
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-12-28 23:44:47 +00:00
self.assertEqual(result.context_data["partner"], self.another_user)
2021-11-12 17:17:00 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
2021-03-23 16:09:30 +00:00
def test_get_suggested_book(self, *_):
2021-04-26 16:15:42 +00:00
"""gets books the ~*~ algorithm ~*~ thinks you want to post about"""
2021-09-06 23:59:58 +00:00
models.ShelfBook.objects.create(
book=self.book,
user=self.local_user,
shelf=self.local_user.shelf_set.get(identifier="reading"),
)
suggestions = views.feed.get_suggested_books(self.local_user)
2021-03-08 16:49:10 +00:00
self.assertEqual(suggestions[0]["name"], "Currently Reading")
self.assertEqual(suggestions[0]["books"][0], self.book)