forked from mirrors/bookwyrm
Test incoming in one file
This commit is contained in:
parent
badc5d0dab
commit
7f6390f722
6 changed files with 166 additions and 181 deletions
bookwyrm/tests
|
@ -1 +0,0 @@
|
|||
from . import *
|
|
@ -1,51 +0,0 @@
|
|||
import json
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models, incoming
|
||||
|
||||
|
||||
class Favorite(TestCase):
|
||||
def setUp(self):
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
with patch('bookwyrm.models.user.get_remote_reviews.delay'):
|
||||
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', local=True,
|
||||
remote_id='http://local.com/user/mouse')
|
||||
|
||||
self.status = models.Status.objects.create(
|
||||
user=self.local_user,
|
||||
content='Test status',
|
||||
remote_id='http://local.com/status/1',
|
||||
)
|
||||
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ap_user.json'
|
||||
)
|
||||
self.user_data = json.loads(datafile.read_bytes())
|
||||
|
||||
|
||||
|
||||
def test_handle_favorite(self):
|
||||
activity = {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'id': 'http://example.com/fav/1',
|
||||
'actor': 'https://example.com/users/rat',
|
||||
'published': 'Mon, 25 May 2020 19:31:20 GMT',
|
||||
'object': 'http://local.com/status/1',
|
||||
}
|
||||
|
||||
incoming.handle_favorite(activity)
|
||||
|
||||
fav = models.Favorite.objects.get(remote_id='http://example.com/fav/1')
|
||||
self.assertEqual(fav.status, self.status)
|
||||
self.assertEqual(fav.remote_id, 'http://example.com/fav/1')
|
||||
self.assertEqual(fav.user, self.remote_user)
|
|
@ -1,77 +0,0 @@
|
|||
from unittest.mock import patch
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models, incoming
|
||||
|
||||
|
||||
class IncomingFollow(TestCase):
|
||||
def setUp(self):
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
with patch('bookwyrm.models.user.get_remote_reviews.delay'):
|
||||
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', local=True)
|
||||
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"
|
||||
}
|
||||
|
||||
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):
|
||||
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"
|
||||
}
|
||||
|
||||
self.local_user.manually_approves_followers = True
|
||||
self.local_user.save()
|
||||
|
||||
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_REQUEST')
|
||||
|
||||
# the request should exist
|
||||
request = models.UserFollowRequest.objects.get()
|
||||
self.assertEqual(request.user_subject, self.remote_user)
|
||||
self.assertEqual(request.user_object, self.local_user)
|
||||
|
||||
# the follow relationship should not exist
|
||||
follow = models.UserFollows.objects.all()
|
||||
self.assertEqual(list(follow), [])
|
|
@ -1,52 +0,0 @@
|
|||
from unittest.mock import patch
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models, incoming
|
||||
|
||||
|
||||
class IncomingFollowAccept(TestCase):
|
||||
def setUp(self):
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
with patch('bookwyrm.models.user.get_remote_reviews.delay'):
|
||||
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', local=True)
|
||||
self.local_user.remote_id = 'http://local.com/user/mouse'
|
||||
self.local_user.save()
|
||||
|
||||
|
||||
def test_handle_follow_accept(self):
|
||||
activity = {
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
"id": "https://example.com/users/rat/follows/123#accepts",
|
||||
"type": "Accept",
|
||||
"actor": "https://example.com/users/rat",
|
||||
"object": {
|
||||
"id": "https://example.com/users/rat/follows/123",
|
||||
"type": "Follow",
|
||||
"actor": "http://local.com/user/mouse",
|
||||
"object": "https://example.com/users/rat"
|
||||
}
|
||||
}
|
||||
|
||||
models.UserFollowRequest.objects.create(
|
||||
user_subject=self.local_user,
|
||||
user_object=self.remote_user
|
||||
)
|
||||
self.assertEqual(models.UserFollowRequest.objects.count(), 1)
|
||||
|
||||
incoming.handle_follow_accept(activity)
|
||||
|
||||
# request should be deleted
|
||||
self.assertEqual(models.UserFollowRequest.objects.count(), 0)
|
||||
|
||||
# relationship should be created
|
||||
follows = self.remote_user.followers
|
||||
self.assertEqual(follows.count(), 1)
|
||||
self.assertEqual(follows.first(), self.local_user)
|
30
bookwyrm/tests/models/test_shelf_model.py
Normal file
30
bookwyrm/tests/models/test_shelf_model.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
''' testing models '''
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models, settings
|
||||
|
||||
|
||||
class Shelf(TestCase):
|
||||
''' some activitypub oddness ahead '''
|
||||
def setUp(self):
|
||||
''' look, a shelf '''
|
||||
self.user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword', local=True)
|
||||
self.shelf = models.Shelf.objects.create(
|
||||
name='Test Shelf', identifier='test-shelf', user=self.user)
|
||||
|
||||
def test_remote_id(self):
|
||||
''' shelves use custom remote ids '''
|
||||
expected_id = 'https://%s/user/mouse/shelf/test-shelf' % settings.DOMAIN
|
||||
self.assertEqual(self.shelf.get_remote_id(), expected_id)
|
||||
|
||||
|
||||
def test_to_activity(self):
|
||||
''' jsonify it '''
|
||||
activity_json = self.shelf.to_activity()
|
||||
self.assertIsInstance(activity_json, dict)
|
||||
self.assertEqual(activity_json['id'], self.shelf.remote_id)
|
||||
self.assertEqual(activity_json['totalItems'], 0)
|
||||
self.assertEqual(activity_json['type'], 'OrderedCollection')
|
||||
self.assertEqual(activity_json['name'], 'Test Shelf')
|
||||
self.assertEqual(activity_json['owner'], self.user.remote_id)
|
136
bookwyrm/tests/test_incoming.py
Normal file
136
bookwyrm/tests/test_incoming.py
Normal file
|
@ -0,0 +1,136 @@
|
|||
''' test incoming activities '''
|
||||
from unittest.mock import patch
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models, incoming
|
||||
|
||||
|
||||
class Incoming(TestCase):
|
||||
''' a lot here: all handlers for receiving activitypub requests '''
|
||||
def setUp(self):
|
||||
''' we need basic things, like users '''
|
||||
self.local_user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||
self.local_user.remote_id = 'http://local.com/user/mouse'
|
||||
self.local_user.save()
|
||||
with patch('bookwyrm.models.user.get_remote_reviews.delay'):
|
||||
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.status = models.Status.objects.create(
|
||||
user=self.local_user,
|
||||
content='Test status',
|
||||
remote_id='http://local.com/status/1',
|
||||
)
|
||||
|
||||
|
||||
def test_handle_follow(self):
|
||||
''' remote user wants to follow local user '''
|
||||
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'):
|
||||
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):
|
||||
''' needs approval before following '''
|
||||
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"
|
||||
}
|
||||
|
||||
self.local_user.manually_approves_followers = True
|
||||
self.local_user.save()
|
||||
|
||||
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
||||
incoming.handle_follow(activity)
|
||||
|
||||
# notification created
|
||||
notification = models.Notification.objects.get()
|
||||
self.assertEqual(notification.user, self.local_user)
|
||||
self.assertEqual(notification.notification_type, 'FOLLOW_REQUEST')
|
||||
|
||||
# the request should exist
|
||||
request = models.UserFollowRequest.objects.get()
|
||||
self.assertEqual(request.user_subject, self.remote_user)
|
||||
self.assertEqual(request.user_object, self.local_user)
|
||||
|
||||
# the follow relationship should not exist
|
||||
follow = models.UserFollows.objects.all()
|
||||
self.assertEqual(list(follow), [])
|
||||
|
||||
|
||||
def test_handle_follow_accept(self):
|
||||
''' a remote user approved a follow request from local '''
|
||||
activity = {
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
"id": "https://example.com/users/rat/follows/123#accepts",
|
||||
"type": "Accept",
|
||||
"actor": "https://example.com/users/rat",
|
||||
"object": {
|
||||
"id": "https://example.com/users/rat/follows/123",
|
||||
"type": "Follow",
|
||||
"actor": "http://local.com/user/mouse",
|
||||
"object": "https://example.com/users/rat"
|
||||
}
|
||||
}
|
||||
|
||||
models.UserFollowRequest.objects.create(
|
||||
user_subject=self.local_user,
|
||||
user_object=self.remote_user
|
||||
)
|
||||
self.assertEqual(models.UserFollowRequest.objects.count(), 1)
|
||||
|
||||
incoming.handle_follow_accept(activity)
|
||||
|
||||
# request should be deleted
|
||||
self.assertEqual(models.UserFollowRequest.objects.count(), 0)
|
||||
|
||||
# relationship should be created
|
||||
follows = self.remote_user.followers
|
||||
self.assertEqual(follows.count(), 1)
|
||||
self.assertEqual(follows.first(), self.local_user)
|
||||
|
||||
|
||||
def test_handle_favorite(self):
|
||||
''' fav a status '''
|
||||
activity = {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'id': 'http://example.com/fav/1',
|
||||
'actor': 'https://example.com/users/rat',
|
||||
'published': 'Mon, 25 May 2020 19:31:20 GMT',
|
||||
'object': 'http://local.com/status/1',
|
||||
}
|
||||
|
||||
incoming.handle_favorite(activity)
|
||||
|
||||
fav = models.Favorite.objects.get(remote_id='http://example.com/fav/1')
|
||||
self.assertEqual(fav.status, self.status)
|
||||
self.assertEqual(fav.remote_id, 'http://example.com/fav/1')
|
||||
self.assertEqual(fav.user, self.remote_user)
|
Loading…
Reference in a new issue