Uses better mock pattern in relationships model

This commit is contained in:
Mouse Reeve 2021-08-03 09:46:04 -07:00
parent cfbe1b29cd
commit 88967e589b

View file

@ -1,4 +1,5 @@
""" testing models """ """ testing models """
import json
from unittest.mock import patch from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
@ -20,25 +21,21 @@ class Relationship(TestCase):
inbox="https://example.com/users/rat/inbox", inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox", outbox="https://example.com/users/rat/outbox",
) )
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
) )
self.local_user.remote_id = "http://local.com/user/mouse" self.local_user.remote_id = "http://local.com/user/mouse"
self.local_user.save(broadcast=False) self.local_user.save(broadcast=False, update_fields=["remote_id"])
def test_user_follows_from_request(self): def test_user_follows_from_request(self):
"""convert a follow request into a follow""" """convert a follow request into a follow"""
real_broadcast = models.UserFollowRequest.broadcast with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
def mock_broadcast(_, activity, user):
"""introspect what's being sent out"""
self.assertEqual(user.remote_id, self.local_user.remote_id)
self.assertEqual(activity["type"], "Follow")
models.UserFollowRequest.broadcast = mock_broadcast
request = models.UserFollowRequest.objects.create( request = models.UserFollowRequest.objects.create(
user_subject=self.local_user, user_object=self.remote_user user_subject=self.local_user, user_object=self.remote_user
) )
activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["type"], "Follow")
self.assertEqual( self.assertEqual(
request.remote_id, "http://local.com/user/mouse#follows/%d" % request.id request.remote_id, "http://local.com/user/mouse#follows/%d" % request.id
) )
@ -51,7 +48,6 @@ class Relationship(TestCase):
self.assertEqual(rel.status, "follows") self.assertEqual(rel.status, "follows")
self.assertEqual(rel.user_subject, self.local_user) self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, self.remote_user) self.assertEqual(rel.user_object, self.remote_user)
models.UserFollowRequest.broadcast = real_broadcast
def test_user_follows_from_request_custom_remote_id(self): def test_user_follows_from_request_custom_remote_id(self):
"""store a specific remote id for a relationship provided by remote""" """store a specific remote id for a relationship provided by remote"""
@ -70,36 +66,26 @@ class Relationship(TestCase):
self.assertEqual(rel.user_subject, self.local_user) self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, self.remote_user) self.assertEqual(rel.user_object, self.remote_user)
def test_follow_request_activity(self): @patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
def test_follow_request_activity(self, broadcast_mock):
"""accept a request and make it a relationship""" """accept a request and make it a relationship"""
real_broadcast = models.UserFollowRequest.broadcast
def mock_broadcast(_, activity, user):
self.assertEqual(user.remote_id, self.local_user.remote_id)
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( models.UserFollowRequest.objects.create(
user_subject=self.local_user, user_subject=self.local_user,
user_object=self.remote_user, user_object=self.remote_user,
) )
models.UserFollowRequest.broadcast = real_broadcast activity = json.loads(broadcast_mock.call_args[0][1])
def test_follow_request_accept(self):
"""accept a request and make it a relationship"""
real_broadcast = models.UserFollowRequest.broadcast
def mock_broadcast(_, activity, user):
self.assertEqual(user.remote_id, self.local_user.remote_id)
self.assertEqual(activity["type"], "Accept")
self.assertEqual(activity["actor"], self.local_user.remote_id) self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], "https://www.hi.com/") self.assertEqual(activity["object"], self.remote_user.remote_id)
self.assertEqual(activity["type"], "Follow")
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
def test_follow_request_accept(self, broadcast_mock):
"""accept a request and make it a relationship"""
self.local_user.manually_approves_followers = True self.local_user.manually_approves_followers = True
self.local_user.save(broadcast=False) self.local_user.save(
models.UserFollowRequest.broadcast = mock_broadcast broadcast=False, update_fields=["manually_approves_followers"]
)
request = models.UserFollowRequest.objects.create( request = models.UserFollowRequest.objects.create(
user_subject=self.remote_user, user_subject=self.remote_user,
user_object=self.local_user, user_object=self.local_user,
@ -107,32 +93,34 @@ class Relationship(TestCase):
) )
request.accept() request.accept()
activity = json.loads(broadcast_mock.call_args[0][1])
self.assertEqual(activity["type"], "Accept")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], "https://www.hi.com/")
self.assertFalse(models.UserFollowRequest.objects.exists()) self.assertFalse(models.UserFollowRequest.objects.exists())
self.assertTrue(models.UserFollows.objects.exists()) self.assertTrue(models.UserFollows.objects.exists())
rel = models.UserFollows.objects.get() rel = models.UserFollows.objects.get()
self.assertEqual(rel.user_subject, self.remote_user) self.assertEqual(rel.user_subject, self.remote_user)
self.assertEqual(rel.user_object, self.local_user) self.assertEqual(rel.user_object, self.local_user)
models.UserFollowRequest.broadcast = real_broadcast
def test_follow_request_reject(self): @patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
def test_follow_request_reject(self, broadcast_mock):
"""accept a request and make it a relationship""" """accept a request and make it a relationship"""
real_broadcast = models.UserFollowRequest.broadcast
def mock_reject(_, activity, user):
self.assertEqual(user.remote_id, self.local_user.remote_id)
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
self.local_user.manually_approves_followers = True self.local_user.manually_approves_followers = True
self.local_user.save(broadcast=False) self.local_user.save(
broadcast=False, update_fields=["manually_approves_followers"]
)
request = models.UserFollowRequest.objects.create( request = models.UserFollowRequest.objects.create(
user_subject=self.remote_user, user_subject=self.remote_user,
user_object=self.local_user, user_object=self.local_user,
) )
request.reject() request.reject()
activity = json.loads(broadcast_mock.call_args[0][1])
self.assertEqual(activity["type"], "Reject")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], request.remote_id)
self.assertFalse(models.UserFollowRequest.objects.exists()) self.assertFalse(models.UserFollowRequest.objects.exists())
self.assertFalse(models.UserFollows.objects.exists()) self.assertFalse(models.UserFollows.objects.exists())
models.UserFollowRequest.broadcast = real_broadcast