moviewyrm/bookwyrm/tests/views/test_follow.py

160 lines
6.2 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
class BookViews(TestCase):
2021-04-26 16:15:42 +00:00
"""books books books"""
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()
self.local_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse@local.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
2021-01-13 21:05:16 +00:00
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.user.set_remote_server"):
2021-01-13 21:05:16 +00:00
self.remote_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat",
"rat@email.com",
"ratword",
2021-01-13 21:05:16 +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-13 21:05:16 +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-03-08 16:49:10 +00:00
self.work = models.Work.objects.create(title="Test Work")
2021-01-13 21:05:16 +00:00
self.book = models.Edition.objects.create(
2021-03-08 16:49:10 +00:00
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=self.work,
2021-01-13 21:05:16 +00:00
)
2021-02-13 00:16:22 +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-02-13 00:16:22 +00:00
def test_handle_follow_local_manually_approves(self):
2021-04-26 16:15:42 +00:00
"""send a follow request"""
2021-02-13 00:16:22 +00:00
rat = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat@local.com",
"rat@rat.com",
"ratword",
local=True,
localname="rat",
remote_id="https://example.com/users/rat",
2021-02-13 00:16:22 +00:00
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
def test_handle_follow_local(self):
2021-04-26 16:15:42 +00:00
"""send a follow request"""
2021-02-13 00:16:22 +00:00
rat = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat@local.com",
"rat@rat.com",
"ratword",
local=True,
localname="rat",
remote_id="https://example.com/users/rat",
2021-02-13 00:16:22 +00:00
)
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-01-13 21:05:16 +00:00
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)
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
self.local_user.save(broadcast=False)
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)
def test_handle_reject(self):
2021-04-26 16:15:42 +00:00
"""reject a follow request"""
self.local_user.manually_approves_followers = True
self.local_user.save(broadcast=False)
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)