fixes unit tests for incoming and outgoing follows

This commit is contained in:
Mouse Reeve 2020-11-27 14:15:13 -08:00
parent f173d674ac
commit 4ec557fc5d
3 changed files with 90 additions and 80 deletions

View file

@ -1,3 +1,4 @@
from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from bookwyrm import models, incoming from bookwyrm import models, incoming
@ -18,29 +19,30 @@ class IncomingFollow(TestCase):
self.local_user.save() self.local_user.save()
# def test_handle_follow(self): def test_handle_follow(self):
# activity = { activity = {
# "@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
# "id": "https://example.com/users/rat/follows/123", "id": "https://example.com/users/rat/follows/123",
# "type": "Follow", "type": "Follow",
# "actor": "https://example.com/users/rat", "actor": "https://example.com/users/rat",
# "object": "http://local.com/user/mouse" "object": "http://local.com/user/mouse"
# } }
#
# incoming.handle_follow(activity) with patch('bookwyrm.broadcast.broadcast_task.delay') as _:
# incoming.handle_follow(activity)
# # notification created
# notification = models.Notification.objects.get() # notification created
# self.assertEqual(notification.user, self.local_user) notification = models.Notification.objects.get()
# self.assertEqual(notification.notification_type, 'FOLLOW') self.assertEqual(notification.user, self.local_user)
# self.assertEqual(notification.notification_type, 'FOLLOW')
# # the request should have been deleted
# requests = models.UserFollowRequest.objects.all() # the request should have been deleted
# self.assertEqual(list(requests), []) requests = models.UserFollowRequest.objects.all()
# self.assertEqual(list(requests), [])
# # the follow relationship should exist
# follow = models.UserFollows.objects.get(user_object=self.local_user) # the follow relationship should exist
# self.assertEqual(follow.user_subject, self.remote_user) 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): def test_handle_follow_manually_approved(self):
@ -55,7 +57,8 @@ class IncomingFollow(TestCase):
self.local_user.manually_approves_followers = True self.local_user.manually_approves_followers = True
self.local_user.save() self.local_user.save()
incoming.handle_follow(activity) with patch('bookwyrm.broadcast.broadcast_task.delay') as _:
incoming.handle_follow(activity)
# notification created # notification created
notification = models.Notification.objects.get() notification = models.Notification.objects.get()
@ -81,7 +84,8 @@ class IncomingFollow(TestCase):
"object": "http://local.com/user/nonexistent-user" "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 # do nothing
notifications = models.Notification.objects.all() notifications = models.Notification.objects.all()

View file

@ -1,3 +1,4 @@
from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from bookwyrm import models, outgoing from bookwyrm import models, outgoing
@ -19,54 +20,59 @@ class Following(TestCase):
) )
# def test_handle_follow(self): def test_handle_follow(self):
# self.assertEqual(models.UserFollowRequest.objects.count(), 0) self.assertEqual(models.UserFollowRequest.objects.count(), 0)
#
# outgoing.handle_follow(self.local_user, self.remote_user) with patch('bookwyrm.broadcast.broadcast_task.delay') as _:
# rel = models.UserFollowRequest.objects.get() outgoing.handle_follow(self.local_user, self.remote_user)
#
# self.assertEqual(rel.user_subject, self.local_user) rel = models.UserFollowRequest.objects.get()
# self.assertEqual(rel.user_object, self.remote_user)
# self.assertEqual(rel.status, 'follow_request') 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) def test_handle_unfollow(self):
# outgoing.handle_unfollow(self.local_user, self.remote_user) self.remote_user.followers.add(self.local_user)
# self.assertEqual(self.remote_user.followers.count(), 1)
# self.assertEqual(self.remote_user.followers.count(), 0) with patch('bookwyrm.broadcast.broadcast_task.delay') as _:
# outgoing.handle_unfollow(self.local_user, self.remote_user)
#
# def test_handle_accept(self): self.assertEqual(self.remote_user.followers.count(), 0)
# rel = models.UserFollowRequest.objects.create(
# user_subject=self.local_user,
# user_object=self.remote_user def test_handle_accept(self):
# ) rel = models.UserFollowRequest.objects.create(
# rel_id = rel.id user_subject=self.local_user,
# user_object=self.remote_user
# outgoing.handle_accept(rel) )
# # request should be deleted rel_id = rel.id
# self.assertEqual(
# models.UserFollowRequest.objects.filter(id=rel_id).count(), 0 with patch('bookwyrm.broadcast.broadcast_task.delay') as _:
# ) outgoing.handle_accept(rel)
# # follow relationship should exist # request should be deleted
# self.assertEqual(self.remote_user.followers.first(), self.local_user) self.assertEqual(
# models.UserFollowRequest.objects.filter(id=rel_id).count(), 0
# )
# def test_handle_reject(self): # follow relationship should exist
# rel = models.UserFollowRequest.objects.create( self.assertEqual(self.remote_user.followers.first(), self.local_user)
# user_subject=self.local_user,
# user_object=self.remote_user
# ) def test_handle_reject(self):
# rel_id = rel.id rel = models.UserFollowRequest.objects.create(
# user_subject=self.local_user,
# outgoing.handle_reject(rel) user_object=self.remote_user
# # request should be deleted )
# self.assertEqual( rel_id = rel.id
# models.UserFollowRequest.objects.filter(id=rel_id).count(), 0
# ) with patch('bookwyrm.broadcast.broadcast_task.delay') as _:
# # follow relationship should not exist outgoing.handle_reject(rel)
# self.assertEqual( # request should be deleted
# models.UserFollows.objects.filter(id=rel_id).count(), 0 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
)

View file

@ -155,12 +155,12 @@ class Signature(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# Try with new key: # Try with new key:
#response = self.send_test_request(sender=new_sender) response = self.send_test_request(sender=new_sender)
#self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# Now the old key will fail: # Now the old key will fail:
#response = self.send_test_request(sender=self.fake_remote) response = self.send_test_request(sender=self.fake_remote)
#self.assertEqual(response.status_code, 401) self.assertEqual(response.status_code, 401)
@responses.activate @responses.activate