2021-03-08 16:49:10 +00:00
|
|
|
""" test for app action functionality """
|
2021-01-29 18:44:21 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class FeedMessageViews(TestCase):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" dms """
|
|
|
|
|
2021-01-29 18:44:21 +00:00
|
|
|
def setUp(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" we need basic test data and mocks """
|
2021-01-29 18:44:21 +00:00
|
|
|
self.factory = RequestFactory()
|
|
|
|
self.local_user = models.User.objects.create_user(
|
2021-03-08 16:49:10 +00:00
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.mouse",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
2021-01-29 18:44:21 +00:00
|
|
|
self.book = models.Edition.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
parent_work=models.Work.objects.create(title="hi"),
|
|
|
|
title="Example Edition",
|
|
|
|
remote_id="https://example.com/book/1",
|
2021-01-29 18:44:21 +00:00
|
|
|
)
|
2021-01-30 20:16:57 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-01-29 18:44:21 +00:00
|
|
|
|
|
|
|
def test_feed(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" there are so many views, this just makes sure it LOADS """
|
2021-01-29 18:44:21 +00:00
|
|
|
view = views.Feed.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-29 18:44:21 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "local")
|
2021-01-29 18:44:21 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-29 18:44:21 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_status_page(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" there are so many views, this just makes sure it LOADS """
|
2021-01-29 18:44:21 +00:00
|
|
|
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("")
|
2021-01-29 18:44:21 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.feed.is_api_request") as is_api:
|
2021-01-29 18:44:21 +00:00
|
|
|
is_api.return_value = False
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse", status.id)
|
2021-01-29 18:44:21 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-29 18:44:21 +00:00
|
|
|
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:
|
2021-01-29 18:44:21 +00:00
|
|
|
is_api.return_value = True
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse", status.id)
|
2021-01-29 18:44:21 +00:00
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_replies_page(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" there are so many views, this just makes sure it LOADS """
|
2021-01-29 18:44:21 +00:00
|
|
|
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("")
|
2021-01-29 18:44:21 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.feed.is_api_request") as is_api:
|
2021-01-29 18:44:21 +00:00
|
|
|
is_api.return_value = False
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse", status.id)
|
2021-01-29 18:44:21 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-29 18:44:21 +00:00
|
|
|
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:
|
2021-01-29 18:44:21 +00:00
|
|
|
is_api.return_value = True
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse", status.id)
|
2021-01-29 18:44:21 +00:00
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_direct_messages_page(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" there are so many views, this just makes sure it LOADS """
|
2021-01-29 18:44:21 +00:00
|
|
|
view = views.DirectMessage.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-29 18:44:21 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
result = view(request)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-29 18:44:21 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_suggested_book(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" gets books the ~*~ algorithm ~*~ thinks you want to post about """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-07 06:37:19 +00:00
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=self.book,
|
|
|
|
user=self.local_user,
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf=self.local_user.shelf_set.get(identifier="reading"),
|
2021-02-07 06:37:19 +00:00
|
|
|
)
|
2021-01-29 18:44:21 +00:00
|
|
|
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)
|