moviewyrm/bookwyrm/tests/views/test_feed.py

162 lines
6.6 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
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models
from bookwyrm import views
from bookwyrm.activitypub import ActivitypubResponse
2021-03-23 16:09:30 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.get_activity_stream")
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
2021-08-03 23:21:29 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@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()
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.mouse",
"password",
local=True,
localname="mouse",
)
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)
result.render()
self.assertEqual(result.status_code, 200)
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-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
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)
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
result = view(request, "mouse", 12345)
self.assertEqual(result.status_code, 404)
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)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
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)
result.render()
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-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
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)
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)
result.render()
self.assertEqual(result.status_code, 200)
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-08-02 23:05:40 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
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)