moviewyrm/bookwyrm/tests/models/test_relationship_models.py

139 lines
5.9 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2020-12-04 01:18:23 +00:00
from unittest.mock import patch
from django.test import TestCase
from bookwyrm import models
class Relationship(TestCase):
2021-04-26 16:15:42 +00:00
"""following, blocking, stuff like that"""
2021-03-08 16:49:10 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need some users for this"""
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.user.set_remote_server.delay"):
2020-12-04 01:18:23 +00:00
self.remote_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat",
"rat@rat.com",
"ratword",
2020-12-04 01:18:23 +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",
2020-12-04 01:18:23 +00:00
)
self.local_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
)
self.local_user.remote_id = "http://local.com/user/mouse"
2021-02-07 00:13:59 +00:00
self.local_user.save(broadcast=False)
def test_user_follows_from_request(self):
2021-04-26 16:15:42 +00:00
"""convert a follow request into a follow"""
2021-02-08 17:38:28 +00:00
real_broadcast = models.UserFollowRequest.broadcast
2021-03-08 16:49:10 +00:00
def mock_broadcast(_, activity, user):
2021-04-26 16:15:42 +00:00
"""introspect what's being sent out"""
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["type"], "Follow")
models.UserFollowRequest.broadcast = mock_broadcast
request = models.UserFollowRequest.objects.create(
2021-03-08 16:49:10 +00:00
user_subject=self.local_user, user_object=self.remote_user
)
self.assertEqual(
2021-03-08 16:49:10 +00:00
request.remote_id, "http://local.com/user/mouse#follows/%d" % request.id
)
2021-03-08 16:49:10 +00:00
self.assertEqual(request.status, "follow_request")
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
)
2021-03-08 16:49:10 +00:00
self.assertEqual(rel.status, "follows")
self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, self.remote_user)
2021-02-08 17:38:28 +00:00
models.UserFollowRequest.broadcast = real_broadcast
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-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
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-03-08 16:49:10 +00:00
self.assertEqual(request.remote_id, "http://antoher.server/sdkfhskdjf/23")
self.assertEqual(request.status, "follow_request")
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")
self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, self.remote_user)
def test_follow_request_activity(self):
2021-04-26 16:15:42 +00:00
"""accept a request and make it a relationship"""
2021-02-08 17:38:28 +00:00
real_broadcast = models.UserFollowRequest.broadcast
2021-03-08 16:49:10 +00:00
def mock_broadcast(_, activity, user):
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +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")
models.UserFollowRequest.broadcast = mock_broadcast
models.UserFollowRequest.objects.create(
user_subject=self.local_user,
user_object=self.remote_user,
)
2021-02-08 17:38:28 +00:00
models.UserFollowRequest.broadcast = real_broadcast
def test_follow_request_accept(self):
2021-04-26 16:15:42 +00:00
"""accept a request and make it a relationship"""
2021-02-08 17:38:28 +00:00
real_broadcast = models.UserFollowRequest.broadcast
2021-03-08 16:49:10 +00:00
def mock_broadcast(_, activity, user):
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +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-17 18:16:17 +00:00
self.local_user.manually_approves_followers = True
self.local_user.save(broadcast=False)
models.UserFollowRequest.broadcast = mock_broadcast
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/",
)
request.accept()
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-02-08 17:38:28 +00:00
models.UserFollowRequest.broadcast = real_broadcast
def test_follow_request_reject(self):
2021-04-26 16:15:42 +00:00
"""accept a request and make it a relationship"""
2021-02-08 17:38:28 +00:00
real_broadcast = models.UserFollowRequest.broadcast
2021-03-08 16:49:10 +00:00
def mock_reject(_, activity, user):
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["type"], "Reject")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], request.remote_id)
models.UserFollowRequest.broadcast = mock_reject
2021-02-17 18:16:17 +00:00
self.local_user.manually_approves_followers = True
self.local_user.save(broadcast=False)
request = models.UserFollowRequest.objects.create(
user_subject=self.remote_user,
user_object=self.local_user,
)
request.reject()
self.assertFalse(models.UserFollowRequest.objects.exists())
self.assertFalse(models.UserFollows.objects.exists())
2021-02-08 17:38:28 +00:00
models.UserFollowRequest.broadcast = real_broadcast