2021-03-08 16:49:10 +00:00
|
|
|
""" testing models """
|
2021-08-03 16:46:04 +00:00
|
|
|
import json
|
2020-12-04 01:18:23 +00:00
|
|
|
from unittest.mock import patch
|
2020-10-16 17:37:33 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookwyrm import models
|
|
|
|
|
|
|
|
|
2021-09-06 23:59:58 +00:00
|
|
|
@patch("bookwyrm.activitystreams.add_user_statuses_task.delay")
|
2020-10-16 17:37:33 +00:00
|
|
|
class Relationship(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""following, blocking, stuff like that"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-10-16 17:37:33 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need some users for this"""
|
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-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 16:46:04 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
self.local_user.remote_id = "http://local.com/user/mouse"
|
2021-08-03 16:46:04 +00:00
|
|
|
self.local_user.save(broadcast=False, update_fields=["remote_id"])
|
2020-10-16 17:37:33 +00:00
|
|
|
|
2021-09-06 23:59:58 +00:00
|
|
|
def test_user_follows_from_request(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""convert a follow request into a follow"""
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
|
|
|
|
) as mock:
|
2021-08-03 16:46:04 +00:00
|
|
|
request = models.UserFollowRequest.objects.create(
|
|
|
|
user_subject=self.local_user, user_object=self.remote_user
|
|
|
|
)
|
2021-11-12 17:17:00 +00:00
|
|
|
activity = json.loads(mock.call_args[1]["args"][1])
|
2021-08-03 16:46:04 +00:00
|
|
|
self.assertEqual(activity["type"], "Follow")
|
2020-10-16 17:37:33 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
request.remote_id, "http://local.com/user/mouse#follows/%d" % request.id
|
2020-10-16 17:37:33 +00:00
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(request.status, "follow_request")
|
2020-10-16 17:37:33 +00:00
|
|
|
|
|
|
|
rel = models.UserFollows.from_request(request)
|
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
rel.remote_id, "http://local.com/user/mouse#follows/%d" % request.id
|
2020-10-16 17:37:33 +00:00
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(rel.status, "follows")
|
2020-10-16 17:37:33 +00:00
|
|
|
self.assertEqual(rel.user_subject, self.local_user)
|
|
|
|
self.assertEqual(rel.user_object, self.remote_user)
|
2021-02-08 17:38:28 +00:00
|
|
|
|
2021-09-06 23:59:58 +00:00
|
|
|
def test_user_follows_from_request_custom_remote_id(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""store a specific remote id for a relationship provided by remote"""
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-02-07 03:12:49 +00:00
|
|
|
request = models.UserFollowRequest.objects.create(
|
|
|
|
user_subject=self.local_user,
|
|
|
|
user_object=self.remote_user,
|
2021-03-08 16:49:10 +00:00
|
|
|
remote_id="http://antoher.server/sdkfhskdjf/23",
|
2021-02-07 03:12:49 +00:00
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(request.remote_id, "http://antoher.server/sdkfhskdjf/23")
|
|
|
|
self.assertEqual(request.status, "follow_request")
|
2020-10-16 17:37:33 +00:00
|
|
|
|
|
|
|
rel = models.UserFollows.from_request(request)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(rel.remote_id, "http://antoher.server/sdkfhskdjf/23")
|
|
|
|
self.assertEqual(rel.status, "follows")
|
2020-10-16 17:37:33 +00:00
|
|
|
self.assertEqual(rel.user_subject, self.local_user)
|
|
|
|
self.assertEqual(rel.user_object, self.remote_user)
|
2021-02-07 03:12:49 +00:00
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
2021-09-06 23:59:58 +00:00
|
|
|
def test_follow_request_activity(self, broadcast_mock, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""accept a request and make it a relationship"""
|
2021-02-07 03:12:49 +00:00
|
|
|
models.UserFollowRequest.objects.create(
|
|
|
|
user_subject=self.local_user,
|
|
|
|
user_object=self.remote_user,
|
|
|
|
)
|
2021-11-12 17:17:00 +00:00
|
|
|
activity = json.loads(broadcast_mock.call_args[1]["args"][1])
|
2021-08-03 16:46:04 +00:00
|
|
|
self.assertEqual(activity["actor"], self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity["object"], self.remote_user.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Follow")
|
2021-02-07 03:12:49 +00:00
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
2021-09-06 23:59:58 +00:00
|
|
|
def test_follow_request_accept(self, broadcast_mock, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""accept a request and make it a relationship"""
|
2021-02-17 18:16:17 +00:00
|
|
|
self.local_user.manually_approves_followers = True
|
2021-08-03 16:46:04 +00:00
|
|
|
self.local_user.save(
|
|
|
|
broadcast=False, update_fields=["manually_approves_followers"]
|
|
|
|
)
|
|
|
|
|
2021-02-07 03:12:49 +00:00
|
|
|
request = models.UserFollowRequest.objects.create(
|
|
|
|
user_subject=self.remote_user,
|
|
|
|
user_object=self.local_user,
|
2021-03-08 16:49:10 +00:00
|
|
|
remote_id="https://www.hi.com/",
|
2021-02-07 03:12:49 +00:00
|
|
|
)
|
|
|
|
request.accept()
|
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
activity = json.loads(broadcast_mock.call_args[1]["args"][1])
|
2021-08-03 16:46:04 +00:00
|
|
|
self.assertEqual(activity["type"], "Accept")
|
|
|
|
self.assertEqual(activity["actor"], self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity["object"]["id"], "https://www.hi.com/")
|
|
|
|
|
2021-02-07 03:12:49 +00:00
|
|
|
self.assertFalse(models.UserFollowRequest.objects.exists())
|
|
|
|
self.assertTrue(models.UserFollows.objects.exists())
|
|
|
|
rel = models.UserFollows.objects.get()
|
|
|
|
self.assertEqual(rel.user_subject, self.remote_user)
|
|
|
|
self.assertEqual(rel.user_object, self.local_user)
|
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
2021-09-06 23:59:58 +00:00
|
|
|
def test_follow_request_reject(self, broadcast_mock, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""accept a request and make it a relationship"""
|
2021-02-17 18:16:17 +00:00
|
|
|
self.local_user.manually_approves_followers = True
|
2021-08-03 16:46:04 +00:00
|
|
|
self.local_user.save(
|
|
|
|
broadcast=False, update_fields=["manually_approves_followers"]
|
|
|
|
)
|
2021-02-07 03:12:49 +00:00
|
|
|
request = models.UserFollowRequest.objects.create(
|
|
|
|
user_subject=self.remote_user,
|
|
|
|
user_object=self.local_user,
|
|
|
|
)
|
|
|
|
request.reject()
|
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
activity = json.loads(broadcast_mock.call_args[1]["args"][1])
|
2021-08-03 16:46:04 +00:00
|
|
|
self.assertEqual(activity["type"], "Reject")
|
|
|
|
self.assertEqual(activity["actor"], self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity["object"]["id"], request.remote_id)
|
|
|
|
|
2021-02-07 03:12:49 +00:00
|
|
|
self.assertFalse(models.UserFollowRequest.objects.exists())
|
|
|
|
self.assertFalse(models.UserFollows.objects.exists())
|