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 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()

View file

@ -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
)

View file

@ -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