moviewyrm/bookwyrm/tests/views/test_block.py

77 lines
2.9 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
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
2021-03-23 16:14:31 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
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()
self.local_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
2021-01-26 21:02:14 +00:00
self.remote_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat",
"rat@rat.com",
"ratword",
2021-01-26 21:02:14 +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-26 21:02:14 +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)
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-03-23 16:14:31 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.remove_user_statuses"):
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-03-23 16:14:31 +00:00
with patch("bookwyrm.activitystreams.ActivityStream.add_user_statuses"):
2021-01-26 21:02:14 +00:00
views.block.unblock(request, self.remote_user.id)
self.assertFalse(models.UserBlocks.objects.exists())