moviewyrm/bookwyrm/tests/views/preferences/test_block.py

87 lines
3.3 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-26 21:02:14 +00:00
from unittest.mock import patch
2021-09-28 20:44:49 +00:00
2021-01-26 21:02:14 +00:00
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
2021-10-01 05:22:20 +00:00
from bookwyrm.tests.validate_html import validate_html
2021-01-26 21:02:14 +00:00
2021-11-12 17:17:00 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
2021-01-26 21:02:14 +00:00
class BlockViews(TestCase):
2021-04-26 16:15:42 +00:00
"""view user and edit profile"""
2021-03-08 16:49:10 +00:00
2021-01-26 21:02:14 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-26 21:02:14 +00:00
self.factory = RequestFactory()
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-08-03 17:25:53 +00:00
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
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()
2021-01-26 21:02:14 +00:00
2021-03-23 16:14:31 +00:00
def test_block_get(self, _):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-01-26 21:02:14 +00:00
view = views.Block.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-26 21:02:14 +00:00
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
2021-10-01 05:22:20 +00:00
validate_html(result.render())
2021-01-26 21:02:14 +00:00
self.assertEqual(result.status_code, 200)
2021-03-23 16:14:31 +00:00
def test_block_post(self, _):
2021-04-26 16:15:42 +00:00
"""create a "block" database entry from an activity"""
2021-01-26 21:02:14 +00:00
view = views.Block.as_view()
self.local_user.followers.add(self.remote_user)
2021-03-23 16:14:31 +00:00
models.UserFollowRequest.objects.create(
user_subject=self.local_user, user_object=self.remote_user
)
2021-01-26 21:02:14 +00:00
self.assertTrue(models.UserFollows.objects.exists())
self.assertTrue(models.UserFollowRequest.objects.exists())
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-26 21:02:14 +00:00
request.user = self.local_user
2021-12-10 04:30:53 +00:00
with patch("bookwyrm.activitystreams.remove_user_statuses_task.delay"), patch(
"bookwyrm.lists_stream.remove_user_lists_task.delay"
):
2021-01-26 21:02:14 +00:00
view(request, self.remote_user.id)
block = models.UserBlocks.objects.get()
self.assertEqual(block.user_subject, self.local_user)
self.assertEqual(block.user_object, self.remote_user)
self.assertFalse(models.UserFollows.objects.exists())
self.assertFalse(models.UserFollowRequest.objects.exists())
2021-03-23 16:14:31 +00:00
def test_unblock(self, _):
2021-04-26 16:15:42 +00:00
"""undo a block"""
2021-01-26 21:02:14 +00:00
self.local_user.blocks.add(self.remote_user)
2021-03-08 16:49:10 +00:00
request = self.factory.post("")
2021-01-26 21:02:14 +00:00
request.user = self.local_user
2021-12-10 04:30:53 +00:00
with patch("bookwyrm.activitystreams.add_user_statuses_task.delay"), patch(
"bookwyrm.lists_stream.add_user_lists_task.delay"
):
views.unblock(request, self.remote_user.id)
2021-01-26 21:02:14 +00:00
self.assertFalse(models.UserBlocks.objects.exists())