moviewyrm/bookwyrm/tests/views/test_follow.py

175 lines
7 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-03-13 23:04:23 +00:00
import json
2021-01-13 21:05:16 +00:00
from unittest.mock import patch
2021-03-13 23:04:23 +00:00
2021-01-13 21:05:16 +00:00
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.add_user_statuses_task.delay")
2021-08-03 17:25:53 +00:00
class FollowViews(TestCase):
"""follows"""
2021-03-08 16:49:10 +00:00
2021-01-13 21:05:16 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-13 21:05:16 +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-03-08 16:49:10 +00:00
self.group = Group.objects.create(name="editor")
2021-01-13 21:05:16 +00:00
self.group.permissions.add(
Permission.objects.create(
2021-03-08 16:49:10 +00:00
name="edit_book",
codename="edit_book",
content_type=ContentType.objects.get_for_model(models.User),
).id
2021-01-13 21:05:16 +00:00
)
2021-08-02 23:05:40 +00:00
self.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=self.work,
)
2021-01-13 21:05:16 +00:00
2021-09-06 23:59:58 +00:00
def test_handle_follow_remote(self, _):
2021-04-26 16:15:42 +00:00
"""send a follow request"""
2021-03-08 16:49:10 +00:00
request = self.factory.post("", {"user": self.remote_user.username})
2021-01-13 21:05:16 +00:00
request.user = self.local_user
self.assertEqual(models.UserFollowRequest.objects.count(), 0)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 21:05:16 +00:00
views.follow(request)
rel = models.UserFollowRequest.objects.get()
self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, self.remote_user)
2021-03-08 16:49:10 +00:00
self.assertEqual(rel.status, "follow_request")
2021-01-13 21:05:16 +00:00
2021-09-06 23:59:58 +00:00
def test_handle_follow_local_manually_approves(self, _):
2021-04-26 16:15:42 +00:00
"""send a follow request"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-08-03 17:25:53 +00:00
rat = models.User.objects.create_user(
"rat@local.com",
"rat@rat.com",
"ratword",
local=True,
localname="rat",
remote_id="https://example.com/users/rat",
manually_approves_followers=True,
)
2021-03-08 16:49:10 +00:00
request = self.factory.post("", {"user": rat})
2021-02-13 00:16:22 +00:00
request.user = self.local_user
self.assertEqual(models.UserFollowRequest.objects.count(), 0)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-02-13 00:16:22 +00:00
views.follow(request)
rel = models.UserFollowRequest.objects.get()
self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, rat)
2021-03-08 16:49:10 +00:00
self.assertEqual(rel.status, "follow_request")
2021-02-13 00:16:22 +00:00
2021-09-06 23:59:58 +00:00
def test_handle_follow_local(self, _):
2021-04-26 16:15:42 +00:00
"""send a follow request"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-08-03 17:25:53 +00:00
rat = models.User.objects.create_user(
"rat@local.com",
"rat@rat.com",
"ratword",
local=True,
localname="rat",
remote_id="https://example.com/users/rat",
)
2021-03-08 16:49:10 +00:00
request = self.factory.post("", {"user": rat})
2021-02-13 00:16:22 +00:00
request.user = self.local_user
self.assertEqual(models.UserFollowRequest.objects.count(), 0)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-02-13 00:16:22 +00:00
views.follow(request)
rel = models.UserFollows.objects.get()
self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, rat)
2021-03-08 16:49:10 +00:00
self.assertEqual(rel.status, "follows")
2021-02-13 00:16:22 +00:00
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
def test_handle_unfollow(self, *_):
2021-04-26 16:15:42 +00:00
"""send an unfollow"""
2021-03-08 16:49:10 +00:00
request = self.factory.post("", {"user": self.remote_user.username})
2021-01-13 21:05:16 +00:00
request.user = self.local_user
self.remote_user.followers.add(self.local_user)
self.assertEqual(self.remote_user.followers.count(), 1)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
2021-01-13 21:05:16 +00:00
views.unfollow(request)
2021-02-17 19:28:54 +00:00
self.assertEqual(mock.call_count, 1)
2021-03-13 23:04:23 +00:00
activity = json.loads(mock.call_args_list[0][0][1])
self.assertEqual(activity["type"], "Undo")
2021-01-13 21:05:16 +00:00
self.assertEqual(self.remote_user.followers.count(), 0)
2021-09-06 23:59:58 +00:00
def test_handle_accept(self, _):
2021-04-26 16:15:42 +00:00
"""accept a follow request"""
2021-02-17 19:28:54 +00:00
self.local_user.manually_approves_followers = True
2021-08-03 23:21:29 +00:00
self.local_user.save(
broadcast=False, update_fields=["manually_approves_followers"]
)
2021-03-08 16:49:10 +00:00
request = self.factory.post("", {"user": self.remote_user.username})
2021-01-13 21:05:16 +00:00
request.user = self.local_user
rel = models.UserFollowRequest.objects.create(
2021-03-08 16:49:10 +00:00
user_subject=self.remote_user, user_object=self.local_user
2021-01-13 21:05:16 +00:00
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 21:05:16 +00:00
views.accept_follow_request(request)
# request should be deleted
2021-03-08 16:49:10 +00:00
self.assertEqual(models.UserFollowRequest.objects.filter(id=rel.id).count(), 0)
2021-01-13 21:05:16 +00:00
# follow relationship should exist
self.assertEqual(self.local_user.followers.first(), self.remote_user)
2021-09-06 23:59:58 +00:00
def test_handle_reject(self, _):
2021-04-26 16:15:42 +00:00
"""reject a follow request"""
self.local_user.manually_approves_followers = True
2021-08-03 23:21:29 +00:00
self.local_user.save(
broadcast=False, update_fields=["manually_approves_followers"]
)
2021-03-08 16:49:10 +00:00
request = self.factory.post("", {"user": self.remote_user.username})
2021-01-13 21:05:16 +00:00
request.user = self.local_user
rel = models.UserFollowRequest.objects.create(
2021-03-08 16:49:10 +00:00
user_subject=self.remote_user, user_object=self.local_user
2021-01-13 21:05:16 +00:00
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 21:05:16 +00:00
views.delete_follow_request(request)
# request should be deleted
2021-03-08 16:49:10 +00:00
self.assertEqual(models.UserFollowRequest.objects.filter(id=rel.id).count(), 0)
2021-01-13 21:05:16 +00:00
# follow relationship should not exist
2021-03-08 16:49:10 +00:00
self.assertEqual(models.UserFollows.objects.filter(id=rel.id).count(), 0)