From 4ec557fc5dec8d75b0e634f3941c87e151b142f2 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 27 Nov 2020 14:15:13 -0800 Subject: [PATCH] fixes unit tests for incoming and outgoing follows --- bookwyrm/tests/incoming/test_follow.py | 54 +++++++------ bookwyrm/tests/outgoing/test_follow.py | 108 +++++++++++++------------ bookwyrm/tests/test_signing.py | 8 +- 3 files changed, 90 insertions(+), 80 deletions(-) diff --git a/bookwyrm/tests/incoming/test_follow.py b/bookwyrm/tests/incoming/test_follow.py index 377c70205..50e47d88b 100644 --- a/bookwyrm/tests/incoming/test_follow.py +++ b/bookwyrm/tests/incoming/test_follow.py @@ -1,3 +1,4 @@ +from unittest.mock import patch from django.test import TestCase from bookwyrm import models, incoming @@ -18,29 +19,30 @@ class IncomingFollow(TestCase): 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) + 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" + } + + with patch('bookwyrm.broadcast.broadcast_task.delay') as _: + 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) def test_handle_follow_manually_approved(self): @@ -55,7 +57,8 @@ class IncomingFollow(TestCase): self.local_user.manually_approves_followers = True self.local_user.save() - incoming.handle_follow(activity) + with patch('bookwyrm.broadcast.broadcast_task.delay') as _: + incoming.handle_follow(activity) # notification created notification = models.Notification.objects.get() @@ -81,7 +84,8 @@ class IncomingFollow(TestCase): "object": "http://local.com/user/nonexistent-user" } - incoming.handle_follow(activity) + with patch('bookwyrm.broadcast.broadcast_task.delay') as _: + incoming.handle_follow(activity) # do nothing notifications = models.Notification.objects.all() diff --git a/bookwyrm/tests/outgoing/test_follow.py b/bookwyrm/tests/outgoing/test_follow.py index a1ea3f690..89e677abf 100644 --- a/bookwyrm/tests/outgoing/test_follow.py +++ b/bookwyrm/tests/outgoing/test_follow.py @@ -1,3 +1,4 @@ +from unittest.mock import patch from django.test import TestCase from bookwyrm import models, outgoing @@ -19,54 +20,59 @@ class Following(TestCase): ) -# def test_handle_follow(self): -# self.assertEqual(models.UserFollowRequest.objects.count(), 0) -# -# outgoing.handle_follow(self.local_user, self.remote_user) -# rel = models.UserFollowRequest.objects.get() -# -# self.assertEqual(rel.user_subject, self.local_user) -# self.assertEqual(rel.user_object, self.remote_user) -# self.assertEqual(rel.status, 'follow_request') -# -# -# def test_handle_unfollow(self): -# self.remote_user.followers.add(self.local_user) -# self.assertEqual(self.remote_user.followers.count(), 1) -# outgoing.handle_unfollow(self.local_user, self.remote_user) -# -# self.assertEqual(self.remote_user.followers.count(), 0) -# -# -# def test_handle_accept(self): -# rel = models.UserFollowRequest.objects.create( -# user_subject=self.local_user, -# user_object=self.remote_user -# ) -# rel_id = rel.id -# -# outgoing.handle_accept(rel) -# # request should be deleted -# self.assertEqual( -# models.UserFollowRequest.objects.filter(id=rel_id).count(), 0 -# ) -# # follow relationship should exist -# self.assertEqual(self.remote_user.followers.first(), self.local_user) -# -# -# def test_handle_reject(self): -# rel = models.UserFollowRequest.objects.create( -# user_subject=self.local_user, -# user_object=self.remote_user -# ) -# rel_id = rel.id -# -# outgoing.handle_reject(rel) -# # request should be deleted -# self.assertEqual( -# models.UserFollowRequest.objects.filter(id=rel_id).count(), 0 -# ) -# # follow relationship should not exist -# self.assertEqual( -# models.UserFollows.objects.filter(id=rel_id).count(), 0 -# ) + def test_handle_follow(self): + self.assertEqual(models.UserFollowRequest.objects.count(), 0) + + with patch('bookwyrm.broadcast.broadcast_task.delay') as _: + outgoing.handle_follow(self.local_user, self.remote_user) + + rel = models.UserFollowRequest.objects.get() + + self.assertEqual(rel.user_subject, self.local_user) + self.assertEqual(rel.user_object, self.remote_user) + self.assertEqual(rel.status, 'follow_request') + + + def test_handle_unfollow(self): + self.remote_user.followers.add(self.local_user) + self.assertEqual(self.remote_user.followers.count(), 1) + with patch('bookwyrm.broadcast.broadcast_task.delay') as _: + outgoing.handle_unfollow(self.local_user, self.remote_user) + + self.assertEqual(self.remote_user.followers.count(), 0) + + + def test_handle_accept(self): + rel = models.UserFollowRequest.objects.create( + user_subject=self.local_user, + user_object=self.remote_user + ) + rel_id = rel.id + + with patch('bookwyrm.broadcast.broadcast_task.delay') as _: + outgoing.handle_accept(rel) + # request should be deleted + self.assertEqual( + models.UserFollowRequest.objects.filter(id=rel_id).count(), 0 + ) + # follow relationship should exist + self.assertEqual(self.remote_user.followers.first(), self.local_user) + + + def test_handle_reject(self): + rel = models.UserFollowRequest.objects.create( + user_subject=self.local_user, + user_object=self.remote_user + ) + rel_id = rel.id + + with patch('bookwyrm.broadcast.broadcast_task.delay') as _: + outgoing.handle_reject(rel) + # request should be deleted + self.assertEqual( + models.UserFollowRequest.objects.filter(id=rel_id).count(), 0 + ) + # follow relationship should not exist + self.assertEqual( + models.UserFollows.objects.filter(id=rel_id).count(), 0 + ) diff --git a/bookwyrm/tests/test_signing.py b/bookwyrm/tests/test_signing.py index e393062ed..a5039306b 100644 --- a/bookwyrm/tests/test_signing.py +++ b/bookwyrm/tests/test_signing.py @@ -155,12 +155,12 @@ class Signature(TestCase): self.assertEqual(response.status_code, 200) # Try with new key: - #response = self.send_test_request(sender=new_sender) - #self.assertEqual(response.status_code, 200) + response = self.send_test_request(sender=new_sender) + self.assertEqual(response.status_code, 200) # Now the old key will fail: - #response = self.send_test_request(sender=self.fake_remote) - #self.assertEqual(response.status_code, 401) + response = self.send_test_request(sender=self.fake_remote) + self.assertEqual(response.status_code, 401) @responses.activate