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
|
2021-12-06 05:26:02 +00:00
|
|
|
from django.template.response import TemplateResponse
|
2021-01-13 21:05:16 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
2021-12-06 05:26:02 +00:00
|
|
|
from bookwyrm.tests.validate_html import validate_html
|
2021-01-13 21:05:16 +00:00
|
|
|
|
|
|
|
|
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-12-06 05:26:02 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-01-13 21:05:16 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
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-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
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"""
|
2021-09-06 21:48:45 +00:00
|
|
|
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-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
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"""
|
2021-09-06 21:48:45 +00:00
|
|
|
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-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
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-11-12 17:17:00 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
|
|
|
|
) 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-11-12 17:17:00 +00:00
|
|
|
activity = json.loads(mock.call_args_list[0][1]["args"][1])
|
2021-03-13 23:04:23 +00:00
|
|
|
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-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
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"""
|
2021-02-17 20:23:55 +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-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
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)
|
2021-12-06 05:26:02 +00:00
|
|
|
|
|
|
|
def test_ostatus_follow_request(self, _):
|
|
|
|
"""check ostatus subscribe template loads"""
|
|
|
|
request = self.factory.get(
|
|
|
|
"", {"acct": "https%3A%2F%2Fexample.com%2Fusers%2Frat"}
|
|
|
|
)
|
|
|
|
request.user = self.local_user
|
|
|
|
result = views.ostatus_follow_request(request)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_remote_follow_page(self, _):
|
|
|
|
"""check remote follow page loads"""
|
|
|
|
request = self.factory.get("", {"acct": "mouse@local.com"})
|
|
|
|
request.user = self.remote_user
|
|
|
|
result = views.remote_follow_page(request)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_ostatus_follow_success(self, _):
|
|
|
|
"""check remote follow success page loads"""
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.remote_user
|
|
|
|
request.following = "mouse@local.com"
|
|
|
|
result = views.ostatus_follow_success(request)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_remote_follow(self, _):
|
|
|
|
"""check follow from remote page loads"""
|
|
|
|
request = self.factory.post("", {"user": self.remote_user.id})
|
|
|
|
request.user = self.remote_user
|
|
|
|
request.remote_user = "mouse@local.com"
|
|
|
|
result = views.remote_follow(request)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|