moviewyrm/bookwyrm/tests/views/test_interaction.py

173 lines
6.7 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-13 16:10:50 +00:00
from unittest.mock import patch
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
2021-03-23 18:27:00 +00:00
2021-03-23 15:27:24 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
2021-01-13 16:10:50 +00:00
class InteractionViews(TestCase):
2021-03-08 16:49:10 +00:00
""" viewing and creating statuses """
2021-01-13 16:10:50 +00:00
def setUp(self):
2021-03-08 16:49:10 +00:00
""" we need basic test data and mocks """
2021-01-13 16:10:50 +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.com",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
2021-01-13 16:10:50 +00:00
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.user.set_remote_server"):
2021-01-13 16:10:50 +00:00
self.remote_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat",
"rat@email.com",
"ratword",
2021-01-13 16:10:50 +00:00
local=False,
2021-03-08 16:49:10 +00:00
remote_id="https://example.com/users/rat",
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
2021-01-13 16:10:50 +00:00
)
2021-03-08 16:49:10 +00:00
work = models.Work.objects.create(title="Test Work")
2021-01-13 16:10:50 +00:00
self.book = models.Edition.objects.create(
2021-03-08 16:49:10 +00:00
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=work,
2021-01-13 16:10:50 +00:00
)
2021-03-23 15:27:24 +00:00
def test_handle_favorite(self, _):
2021-03-08 16:49:10 +00:00
""" create and broadcast faving a status """
2021-01-13 16:10:50 +00:00
view = views.Favorite.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-13 22:31:52 +00:00
request.user = self.remote_user
2021-03-23 15:27:24 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(user=self.local_user, content="hi")
2021-01-13 16:10:50 +00:00
2021-01-13 22:31:52 +00:00
view(request, status.id)
2021-01-13 16:10:50 +00:00
fav = models.Favorite.objects.get()
self.assertEqual(fav.status, status)
self.assertEqual(fav.user, self.remote_user)
notification = models.Notification.objects.get()
2021-03-08 16:49:10 +00:00
self.assertEqual(notification.notification_type, "FAVORITE")
2021-01-13 16:10:50 +00:00
self.assertEqual(notification.user, self.local_user)
self.assertEqual(notification.related_user, self.remote_user)
2021-03-23 15:27:24 +00:00
def test_handle_unfavorite(self, _):
2021-03-08 16:49:10 +00:00
""" unfav a status """
2021-01-13 16:10:50 +00:00
view = views.Unfavorite.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-13 22:31:52 +00:00
request.user = self.remote_user
2021-03-23 15:27:24 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(user=self.local_user, content="hi")
2021-01-13 22:31:52 +00:00
views.Favorite.as_view()(request, status.id)
2021-01-13 16:10:50 +00:00
self.assertEqual(models.Favorite.objects.count(), 1)
self.assertEqual(models.Notification.objects.count(), 1)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 22:31:52 +00:00
view(request, status.id)
2021-01-13 16:10:50 +00:00
self.assertEqual(models.Favorite.objects.count(), 0)
self.assertEqual(models.Notification.objects.count(), 0)
2021-03-23 15:27:24 +00:00
def test_handle_boost(self, _):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
view = views.Boost.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-13 22:31:52 +00:00
request.user = self.remote_user
2021-03-23 15:27:24 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(user=self.local_user, content="hi")
2021-01-13 16:10:50 +00:00
2021-01-13 22:31:52 +00:00
view(request, status.id)
2021-01-13 16:10:50 +00:00
boost = models.Boost.objects.get()
self.assertEqual(boost.boosted_status, status)
self.assertEqual(boost.user, self.remote_user)
2021-03-08 16:49:10 +00:00
self.assertEqual(boost.privacy, "public")
2021-01-13 16:10:50 +00:00
notification = models.Notification.objects.get()
2021-03-08 16:49:10 +00:00
self.assertEqual(notification.notification_type, "BOOST")
2021-01-13 16:10:50 +00:00
self.assertEqual(notification.user, self.local_user)
self.assertEqual(notification.related_user, self.remote_user)
self.assertEqual(notification.related_status, status)
def test_handle_self_boost(self, _):
""" boost your own status """
view = views.Boost.as_view()
request = self.factory.post("")
request.user = self.local_user
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
status = models.Status.objects.create(user=self.local_user, content="hi")
view(request, status.id)
boost = models.Boost.objects.get()
self.assertEqual(boost.boosted_status, status)
self.assertEqual(boost.user, self.local_user)
self.assertEqual(boost.privacy, "public")
self.assertFalse(models.Notification.objects.exists())
2021-03-23 15:27:24 +00:00
def test_handle_boost_unlisted(self, _):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
view = views.Boost.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-13 16:10:50 +00:00
request.user = self.local_user
2021-03-23 15:27:24 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
2021-02-07 06:37:19 +00:00
status = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
user=self.local_user, content="hi", privacy="unlisted"
)
2021-01-13 16:10:50 +00:00
2021-01-13 22:31:52 +00:00
view(request, status.id)
2021-01-13 16:10:50 +00:00
boost = models.Boost.objects.get()
2021-03-08 16:49:10 +00:00
self.assertEqual(boost.privacy, "unlisted")
2021-01-13 16:10:50 +00:00
2021-03-23 15:27:24 +00:00
def test_handle_boost_private(self, _):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
view = views.Boost.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-13 16:10:50 +00:00
request.user = self.local_user
2021-03-23 15:27:24 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
2021-02-07 06:37:19 +00:00
status = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
user=self.local_user, content="hi", privacy="followers"
)
2021-01-13 16:10:50 +00:00
2021-01-13 22:31:52 +00:00
view(request, status.id)
2021-01-13 16:10:50 +00:00
self.assertFalse(models.Boost.objects.exists())
2021-03-23 15:27:24 +00:00
def test_handle_boost_twice(self, _):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
view = views.Boost.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-13 16:10:50 +00:00
request.user = self.local_user
2021-03-23 15:27:24 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(user=self.local_user, content="hi")
2021-01-13 16:10:50 +00:00
2021-01-13 22:31:52 +00:00
view(request, status.id)
view(request, status.id)
2021-01-13 16:10:50 +00:00
self.assertEqual(models.Boost.objects.count(), 1)
def test_handle_unboost(self, _):
2021-03-08 16:49:10 +00:00
""" undo a boost """
2021-01-13 16:10:50 +00:00
view = views.Unboost.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
request.user = self.remote_user
2021-03-23 15:27:24 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(user=self.local_user, content="hi")
2021-01-13 22:31:52 +00:00
views.Boost.as_view()(request, status.id)
2021-01-13 16:10:50 +00:00
self.assertEqual(models.Boost.objects.count(), 1)
self.assertEqual(models.Notification.objects.count(), 1)
2021-03-23 18:27:00 +00:00
with patch(
"bookwyrm.activitystreams.ActivityStream.remove_status"
) as redis_mock:
2021-01-13 22:31:52 +00:00
view(request, status.id)
2021-03-23 18:27:00 +00:00
self.assertTrue(redis_mock.called)
2021-01-13 16:10:50 +00:00
self.assertEqual(models.Boost.objects.count(), 0)
self.assertEqual(models.Notification.objects.count(), 0)