moviewyrm/bookwyrm/tests/views/inbox/test_inbox_block.py

105 lines
4.2 KiB
Python
Raw Normal View History

""" tests incoming activities"""
from unittest.mock import patch
from django.test import TestCase
from bookwyrm import models, views
# pylint: disable=too-many-public-methods
class InboxBlock(TestCase):
2021-04-26 16:15:42 +00:00
"""inbox tests"""
def setUp(self):
2021-04-26 16:15:42 +00:00
"""basic user and book data"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-09 23:02:53 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-08-03 20:27:32 +00:00
self.local_user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
2021-08-02 23:05:40 +00:00
self.local_user.remote_id = "https://example.com/user/mouse"
2021-08-03 20:27:32 +00:00
self.local_user.save(broadcast=False, update_fields=["remote_id"])
2021-08-02 23:05:40 +00:00
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-05-26 21:57:29 +00:00
)
2021-08-02 23:05:40 +00:00
models.SiteSettings.objects.create()
def test_handle_blocks(self):
2021-04-26 16:15:42 +00:00
"""create a "block" database entry from an activity"""
self.local_user.followers.add(self.remote_user)
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
models.UserFollowRequest.objects.create(
user_subject=self.local_user, user_object=self.remote_user
)
self.assertTrue(models.UserFollows.objects.exists())
self.assertTrue(models.UserFollowRequest.objects.exists())
activity = {
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://example.com/9e1f41ac-9ddd-4159",
"type": "Block",
"actor": "https://example.com/users/rat",
"object": "https://example.com/user/mouse",
}
with patch(
2021-09-07 01:39:14 +00:00
"bookwyrm.activitystreams.remove_user_statuses_task.delay"
2021-12-09 23:02:53 +00:00
) as redis_mock, patch("bookwyrm.lists_stream.remove_user_lists_task.delay"):
views.inbox.activity_task(activity)
self.assertTrue(redis_mock.called)
views.inbox.activity_task(activity)
block = models.UserBlocks.objects.get()
self.assertEqual(block.user_subject, self.remote_user)
self.assertEqual(block.user_object, self.local_user)
self.assertEqual(block.remote_id, "https://example.com/9e1f41ac-9ddd-4159")
self.assertFalse(models.UserFollows.objects.exists())
self.assertFalse(models.UserFollowRequest.objects.exists())
2021-09-07 01:55:48 +00:00
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
2021-12-09 23:02:53 +00:00
@patch("bookwyrm.lists_stream.add_user_lists_task.delay")
@patch("bookwyrm.lists_stream.remove_user_lists_task.delay")
def test_handle_unblock(self, *_):
2021-04-26 16:15:42 +00:00
"""unblock a user"""
self.remote_user.blocks.add(self.local_user)
block = models.UserBlocks.objects.get()
block.remote_id = "https://example.com/9e1f41ac-9ddd-4159"
block.save()
self.assertEqual(block.user_subject, self.remote_user)
self.assertEqual(block.user_object, self.local_user)
activity = {
"type": "Undo",
"actor": "hi",
"id": "bleh",
"to": ["https://www.w3.org/ns/activitystreams#public"],
"cc": ["https://example.com/user/mouse/followers"],
"object": {
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://example.com/9e1f41ac-9ddd-4159",
"type": "Block",
"actor": "https://example.com/users/rat",
"object": "https://example.com/user/mouse",
},
}
with patch(
2021-09-07 01:39:14 +00:00
"bookwyrm.activitystreams.add_user_statuses_task.delay"
) as redis_mock:
views.inbox.activity_task(activity)
self.assertTrue(redis_mock.called)
self.assertFalse(models.UserBlocks.objects.exists())