moviewyrm/bookwyrm/tests/views/test_interaction.py

185 lines
7.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-04-23 01:16:00 +00:00
import json
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-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.remove_status_task.delay")
2021-01-13 16:10:50 +00:00
class InteractionViews(TestCase):
2021-04-26 16:15:42 +00:00
"""viewing and creating statuses"""
2021-03-08 16:49:10 +00:00
2021-01-13 16:10:50 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-13 16:10:50 +00:00
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-08-03 17:25:53 +00:00
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",
)
2021-08-02 23:05:40 +00:00
with patch("bookwyrm.models.user.set_remote_server"):
self.remote_user = models.User.objects.create_user(
"rat",
"rat@email.com",
"ratword",
local=False,
remote_id="https://example.com/users/rat",
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
2021-05-26 21:57:29 +00:00
)
2021-01-13 16:10:50 +00:00
2021-08-02 23:05:40 +00:00
work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=work,
)
2021-06-14 23:56:47 +00:00
def test_favorite(self, *_):
2021-04-26 16:15:42 +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-09-06 20:53:49 +00:00
with patch("bookwyrm.activitystreams.add_status_task.delay"):
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-06-14 23:56:47 +00:00
def test_unfavorite(self, *_):
2021-04-26 16:15:42 +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-09-06 20:53:49 +00:00
with patch("bookwyrm.activitystreams.add_status_task.delay"):
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-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
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-06-14 23:56:47 +00:00
def test_boost(self, *_):
2021-04-26 16:15:42 +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-09-06 20:53:49 +00:00
with patch("bookwyrm.activitystreams.add_status_task.delay"):
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()
2021-04-23 01:16:00 +00:00
2021-01-13 16:10:50 +00:00
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)
2021-06-14 23:56:47 +00:00
def test_self_boost(self, *_):
2021-04-26 16:15:42 +00:00
"""boost your own status"""
view = views.Boost.as_view()
request = self.factory.post("")
request.user = self.local_user
2021-09-06 20:53:49 +00:00
with patch("bookwyrm.activitystreams.add_status_task.delay"):
status = models.Status.objects.create(user=self.local_user, content="hi")
2021-04-23 01:16:00 +00:00
with patch(
2021-11-12 17:17:00 +00:00
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
2021-04-23 01:16:00 +00:00
) as broadcast_mock:
view(request, status.id)
self.assertEqual(broadcast_mock.call_count, 1)
2021-11-12 17:17:00 +00:00
activity = json.loads(broadcast_mock.call_args[1]["args"][1])
2021-04-23 01:16:00 +00:00
self.assertEqual(activity["type"], "Announce")
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-06-14 23:56:47 +00:00
def test_boost_unlisted(self, *_):
2021-04-26 16:15:42 +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-09-06 20:53:49 +00:00
with patch("bookwyrm.activitystreams.add_status_task.delay"):
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-06-14 23:56:47 +00:00
def test_boost_private(self, *_):
2021-04-26 16:15:42 +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-09-06 20:53:49 +00:00
with patch("bookwyrm.activitystreams.add_status_task.delay"):
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-06-14 23:56:47 +00:00
def test_boost_twice(self, *_):
2021-04-26 16:15:42 +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-09-06 20:53:49 +00:00
with patch("bookwyrm.activitystreams.add_status_task.delay"):
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)
2021-09-06 20:53:49 +00:00
@patch("bookwyrm.activitystreams.add_status_task.delay")
2021-06-14 23:56:47 +00:00
def test_unboost(self, *_):
2021-04-26 16:15:42 +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-06-17 20:10:01 +00:00
status = models.Status.objects.create(user=self.local_user, content="hi")
2021-09-06 23:59:58 +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-09-06 23:59:58 +00:00
view(request, status.id)
2021-01-13 16:10:50 +00:00
self.assertEqual(models.Boost.objects.count(), 0)
self.assertEqual(models.Notification.objects.count(), 0)