From db18014325f13205d2ceda58acdd81c936058508 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 15 Oct 2020 17:32:24 -0700 Subject: [PATCH] Adds test for incoming follow request --- bookwyrm/incoming.py | 11 +++--- bookwyrm/tests/test_incoming_follow.py | 47 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 bookwyrm/tests/test_incoming_follow.py diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index b223ab16..5aa975b4 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -124,10 +124,10 @@ def handle_follow(activity): return # figure out who the actor is - user = get_or_create_remote_user(activity['actor']) + actor = get_or_create_remote_user(activity['actor']) try: relationship = models.UserFollowRequest.objects.create( - user_subject=user, + user_subject=actor, user_object=to_follow, relationship_id=activity['id'] ) @@ -143,14 +143,15 @@ def handle_follow(activity): status_builder.create_notification( to_follow, 'FOLLOW', - related_user=user + related_user=actor ) - outgoing.handle_accept(user, to_follow, relationship) + outgoing.handle_accept(actor, to_follow, relationship) else: + # Accept will be triggered manually status_builder.create_notification( to_follow, 'FOLLOW_REQUEST', - related_user=user + related_user=actor ) diff --git a/bookwyrm/tests/test_incoming_follow.py b/bookwyrm/tests/test_incoming_follow.py new file mode 100644 index 00000000..a3ac3ebe --- /dev/null +++ b/bookwyrm/tests/test_incoming_follow.py @@ -0,0 +1,47 @@ +import json +from django.test import TestCase + +from bookwyrm import models, incoming + + +class Follow(TestCase): + ''' not too much going on in the books model but here we are ''' + def setUp(self): + 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', + ) + self.local_user = models.User.objects.create_user( + 'mouse', 'mouse@mouse.com', 'mouseword') + self.local_user.remote_id = 'http://local.com/user/mouse' + self.local_user.save() + + + def test_handle_follow(self): + activity = { + "@context": "https://www.w3.org/ns/activitystreams", + "id": "https://example.com/users/rat/follows/123", + "type": "Follow", + "actor": "https://example.com/users/rat", + "object": "http://local.com/user/mouse" + } + + incoming.handle_follow(activity) + + # notification created + notification = models.Notification.objects.get() + self.assertEqual(notification.user, self.local_user) + self.assertEqual(notification.notification_type, 'FOLLOW') + + # the request should have been deleted + requests = models.UserFollowRequest.objects.all() + self.assertEqual(list(requests), []) + + # the follow relationship should exist + follow = models.UserFollows.objects.get(user_object=self.local_user) + self.assertEqual(follow.user_subject, self.remote_user) + + # an Accept should be sent out