moviewyrm/bookwyrm/tests/lists_stream/test_signals.py

112 lines
4.6 KiB
Python
Raw Normal View History

2021-11-17 17:38:02 +00:00
""" testing lists_stream """
2021-11-17 17:10:28 +00:00
from unittest.mock import patch
from django.test import TestCase
2021-11-17 17:38:02 +00:00
from bookwyrm import lists_stream, models
2021-11-17 17:10:28 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
2021-11-17 17:38:02 +00:00
class ListsStreamSignals(TestCase):
2021-11-17 17:10:28 +00:00
"""using redis to build activity streams"""
def setUp(self):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-10 03:14:10 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-11-17 17:10:28 +00:00
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "password", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
"fish", "fish@fish.fish", "password", local=True, localname="fish"
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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-11-17 17:38:02 +00:00
def test_add_list_on_create(self, _):
"""a new lists has entered"""
book_list = models.List.objects.create(
user=self.remote_user, name="hi", privacy="public"
)
with patch("bookwyrm.lists_stream.add_list_task.delay") as mock:
lists_stream.add_list_on_create_command(book_list)
2021-11-17 17:10:28 +00:00
self.assertEqual(mock.call_count, 1)
args = mock.call_args[0]
2021-11-17 17:38:02 +00:00
self.assertEqual(args[0], book_list.id)
2021-11-17 17:10:28 +00:00
2021-11-17 17:38:02 +00:00
def test_remove_list_on_delete(self, _):
"""delete a list"""
book_list = models.List.objects.create(
user=self.remote_user, name="hi", privacy="public"
2021-11-17 17:10:28 +00:00
)
2021-11-17 17:38:02 +00:00
with patch("bookwyrm.lists_stream.remove_list_task.delay") as mock:
lists_stream.remove_list_on_delete(models.List, book_list)
args = mock.call_args[0]
self.assertEqual(args[0], book_list.id)
2021-11-17 17:10:28 +00:00
2021-11-17 17:38:02 +00:00
def test_populate_lists_on_account_create(self, _):
2021-11-17 17:10:28 +00:00
"""create streams for a user"""
2021-11-17 17:38:02 +00:00
with patch("bookwyrm.lists_stream.populate_lists_task.delay") as mock:
lists_stream.populate_lists_on_account_create(
2021-11-17 17:10:28 +00:00
models.User, self.local_user, True
)
2021-11-17 17:38:02 +00:00
self.assertEqual(mock.call_count, 1)
2021-11-17 17:10:28 +00:00
args = mock.call_args[0]
2021-11-17 17:38:02 +00:00
self.assertEqual(args[0], self.local_user.id)
2021-11-17 17:10:28 +00:00
2021-12-10 17:19:32 +00:00
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
def test_remove_lists_on_block(self, *_):
2021-11-17 17:38:02 +00:00
"""don't show lists from blocked users"""
with patch("bookwyrm.lists_stream.remove_user_lists_task.delay") as mock:
2021-11-17 17:10:28 +00:00
models.UserBlocks.objects.create(
user_subject=self.local_user,
user_object=self.remote_user,
)
args = mock.call_args[0]
self.assertEqual(args[0], self.local_user.id)
self.assertEqual(args[1], self.remote_user.id)
2021-12-10 17:19:32 +00:00
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
@patch("bookwyrm.activitystreams.add_user_statuses_task.delay")
def test_add_lists_on_unblock(self, *_):
2021-11-17 17:38:02 +00:00
"""re-add lists on unblock"""
with patch("bookwyrm.lists_stream.remove_user_lists_task.delay"):
2021-11-17 17:10:28 +00:00
block = models.UserBlocks.objects.create(
user_subject=self.local_user,
user_object=self.remote_user,
)
2021-11-17 17:38:02 +00:00
with patch("bookwyrm.lists_stream.add_user_lists_task.delay") as mock:
2021-11-17 17:10:28 +00:00
block.delete()
args = mock.call_args[0]
self.assertEqual(args[0], self.local_user.id)
self.assertEqual(args[1], self.remote_user.id)
2021-12-10 17:19:32 +00:00
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
@patch("bookwyrm.activitystreams.add_user_statuses_task.delay")
def test_add_lists_on_unblock_reciprocal_block(self, *_):
2021-11-17 17:38:02 +00:00
"""dont' re-add lists on unblock if there's a block the other way"""
with patch("bookwyrm.lists_stream.remove_user_lists_task.delay"):
2021-11-17 17:10:28 +00:00
block = models.UserBlocks.objects.create(
user_subject=self.local_user,
user_object=self.remote_user,
)
block = models.UserBlocks.objects.create(
user_subject=self.remote_user,
user_object=self.local_user,
)
2021-11-17 17:38:02 +00:00
with patch("bookwyrm.lists_stream.add_user_lists_task.delay") as mock:
2021-11-17 17:10:28 +00:00
block.delete()
2021-11-17 17:38:02 +00:00
self.assertFalse(mock.called)