2020-12-30 19:37:49 +00:00
|
|
|
''' test for app action functionality '''
|
2021-01-13 21:54:01 +00:00
|
|
|
import json
|
2020-12-30 19:37:49 +00:00
|
|
|
from unittest.mock import patch
|
2021-01-13 21:54:01 +00:00
|
|
|
import pathlib
|
2020-12-30 19:37:49 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
2021-01-13 21:54:01 +00:00
|
|
|
import responses
|
2020-12-30 19:37:49 +00:00
|
|
|
|
2021-01-13 20:18:19 +00:00
|
|
|
from bookwyrm import models, views
|
2021-01-13 20:03:27 +00:00
|
|
|
from bookwyrm.settings import USER_AGENT
|
2020-12-30 19:37:49 +00:00
|
|
|
|
2021-01-13 20:18:19 +00:00
|
|
|
class ViewsHelpers(TestCase):
|
|
|
|
''' viewing and creating statuses '''
|
2020-12-30 19:37:49 +00:00
|
|
|
def setUp(self):
|
|
|
|
''' we need basic test data and mocks '''
|
|
|
|
self.factory = RequestFactory()
|
2021-01-13 20:18:19 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
'mouse@local.com', 'mouse@mouse.com', 'mouseword',
|
|
|
|
local=True, localname='mouse',
|
|
|
|
remote_id='https://example.com/users/mouse',
|
|
|
|
)
|
2020-12-31 15:52:08 +00:00
|
|
|
self.work = models.Work.objects.create(title='Test Work')
|
|
|
|
self.book = models.Edition.objects.create(
|
2021-01-13 20:18:19 +00:00
|
|
|
title='Test Book',
|
|
|
|
remote_id='https://example.com/book/1',
|
|
|
|
parent_work=self.work
|
2020-12-30 19:37:49 +00:00
|
|
|
)
|
2020-12-30 22:57:57 +00:00
|
|
|
with patch('bookwyrm.models.user.set_remote_server.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',
|
|
|
|
)
|
2021-01-13 21:54:01 +00:00
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath(
|
|
|
|
'../data/ap_user.json'
|
|
|
|
)
|
|
|
|
self.userdata = json.loads(datafile.read_bytes())
|
|
|
|
del self.userdata['icon']
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
self.shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf',
|
|
|
|
identifier='test-shelf',
|
|
|
|
user=self.local_user
|
|
|
|
)
|
2021-01-13 21:54:01 +00:00
|
|
|
|
2020-12-30 21:59:51 +00:00
|
|
|
|
2021-01-13 20:18:19 +00:00
|
|
|
def test_get_edition(self):
|
|
|
|
''' given an edition or a work, returns an edition '''
|
|
|
|
self.assertEqual(
|
|
|
|
views.helpers.get_edition(self.book.id), self.book)
|
|
|
|
self.assertEqual(
|
|
|
|
views.helpers.get_edition(self.work.id), self.book)
|
2020-12-30 21:59:51 +00:00
|
|
|
|
|
|
|
def test_get_user_from_username(self):
|
|
|
|
''' works for either localname or username '''
|
|
|
|
self.assertEqual(
|
2021-02-23 20:41:37 +00:00
|
|
|
views.helpers.get_user_from_username(
|
|
|
|
self.local_user, 'mouse'), self.local_user)
|
2020-12-30 21:59:51 +00:00
|
|
|
self.assertEqual(
|
2021-01-13 20:18:19 +00:00
|
|
|
views.helpers.get_user_from_username(
|
2021-02-23 20:41:37 +00:00
|
|
|
self.local_user, 'mouse@local.com'), self.local_user)
|
2020-12-30 21:59:51 +00:00
|
|
|
with self.assertRaises(models.User.DoesNotExist):
|
2021-02-23 20:41:37 +00:00
|
|
|
views.helpers.get_user_from_username(
|
|
|
|
self.local_user, 'mojfse@example.com')
|
2020-12-30 19:37:49 +00:00
|
|
|
|
|
|
|
|
2020-12-30 22:57:57 +00:00
|
|
|
def test_is_api_request(self):
|
|
|
|
''' should it return html or json '''
|
|
|
|
request = self.factory.get('/path')
|
|
|
|
request.headers = {'Accept': 'application/json'}
|
2021-01-13 20:18:19 +00:00
|
|
|
self.assertTrue(views.helpers.is_api_request(request))
|
2020-12-30 22:57:57 +00:00
|
|
|
|
|
|
|
request = self.factory.get('/path.json')
|
|
|
|
request.headers = {'Accept': 'Praise'}
|
2021-01-13 20:18:19 +00:00
|
|
|
self.assertTrue(views.helpers.is_api_request(request))
|
2020-12-30 22:57:57 +00:00
|
|
|
|
|
|
|
request = self.factory.get('/path')
|
|
|
|
request.headers = {'Accept': 'Praise'}
|
2021-01-13 20:18:19 +00:00
|
|
|
self.assertFalse(views.helpers.is_api_request(request))
|
2020-12-30 22:57:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_activity_feed(self):
|
|
|
|
''' loads statuses '''
|
|
|
|
rat = models.User.objects.create_user(
|
|
|
|
'rat', 'rat@rat.rat', 'password', local=True)
|
|
|
|
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
public_status = models.Comment.objects.create(
|
|
|
|
content='public status', book=self.book, user=self.local_user)
|
|
|
|
direct_status = models.Status.objects.create(
|
|
|
|
content='direct', user=self.local_user, privacy='direct')
|
|
|
|
|
|
|
|
rat_public = models.Status.objects.create(
|
|
|
|
content='blah blah', user=rat)
|
|
|
|
rat_unlisted = models.Status.objects.create(
|
|
|
|
content='blah blah', user=rat, privacy='unlisted')
|
|
|
|
remote_status = models.Status.objects.create(
|
|
|
|
content='blah blah', user=self.remote_user)
|
|
|
|
followers_status = models.Status.objects.create(
|
|
|
|
content='blah', user=rat, privacy='followers')
|
|
|
|
rat_mention = models.Status.objects.create(
|
|
|
|
content='blah blah blah', user=rat, privacy='followers')
|
|
|
|
rat_mention.mention_users.set([self.local_user])
|
2020-12-30 22:57:57 +00:00
|
|
|
|
2021-01-13 20:18:19 +00:00
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-01-05 03:05:21 +00:00
|
|
|
self.local_user,
|
2021-02-24 19:59:21 +00:00
|
|
|
privacy=['public', 'unlisted', 'followers'],
|
2021-01-05 03:05:21 +00:00
|
|
|
following_only=True,
|
|
|
|
queryset=models.Comment.objects
|
|
|
|
)
|
2020-12-30 22:57:57 +00:00
|
|
|
self.assertEqual(len(statuses), 1)
|
|
|
|
self.assertEqual(statuses[0], public_status)
|
|
|
|
|
2021-01-13 20:18:19 +00:00
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-01-05 03:05:21 +00:00
|
|
|
self.local_user,
|
2021-02-24 19:59:21 +00:00
|
|
|
privacy=['public', 'followers'],
|
2021-01-05 03:05:21 +00:00
|
|
|
local_only=True
|
|
|
|
)
|
2020-12-30 22:57:57 +00:00
|
|
|
self.assertEqual(len(statuses), 2)
|
|
|
|
self.assertEqual(statuses[1], public_status)
|
|
|
|
self.assertEqual(statuses[0], rat_public)
|
|
|
|
|
2021-02-24 20:24:19 +00:00
|
|
|
statuses = views.helpers.get_activity_feed(
|
|
|
|
self.local_user, privacy=['direct'])
|
2020-12-30 22:57:57 +00:00
|
|
|
self.assertEqual(len(statuses), 1)
|
|
|
|
self.assertEqual(statuses[0], direct_status)
|
|
|
|
|
2021-01-13 20:18:19 +00:00
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-01-05 03:05:21 +00:00
|
|
|
self.local_user,
|
2021-02-24 19:59:21 +00:00
|
|
|
privacy=['public', 'followers'],
|
2021-01-05 03:05:21 +00:00
|
|
|
)
|
2020-12-30 22:57:57 +00:00
|
|
|
self.assertEqual(len(statuses), 3)
|
|
|
|
self.assertEqual(statuses[2], public_status)
|
|
|
|
self.assertEqual(statuses[1], rat_public)
|
|
|
|
self.assertEqual(statuses[0], remote_status)
|
|
|
|
|
2021-01-13 20:18:19 +00:00
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-01-05 03:05:21 +00:00
|
|
|
self.local_user,
|
2021-02-24 19:59:21 +00:00
|
|
|
privacy=['public', 'unlisted', 'followers'],
|
2021-01-05 03:05:21 +00:00
|
|
|
following_only=True
|
|
|
|
)
|
2020-12-30 22:57:57 +00:00
|
|
|
self.assertEqual(len(statuses), 2)
|
|
|
|
self.assertEqual(statuses[1], public_status)
|
|
|
|
self.assertEqual(statuses[0], rat_mention)
|
|
|
|
|
|
|
|
rat.followers.add(self.local_user)
|
2021-01-13 20:18:19 +00:00
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-01-05 03:05:21 +00:00
|
|
|
self.local_user,
|
2021-02-24 19:59:21 +00:00
|
|
|
privacy=['public', 'unlisted', 'followers'],
|
2021-01-05 03:05:21 +00:00
|
|
|
following_only=True
|
|
|
|
)
|
2020-12-30 22:57:57 +00:00
|
|
|
self.assertEqual(len(statuses), 5)
|
|
|
|
self.assertEqual(statuses[4], public_status)
|
|
|
|
self.assertEqual(statuses[3], rat_public)
|
|
|
|
self.assertEqual(statuses[2], rat_unlisted)
|
|
|
|
self.assertEqual(statuses[1], followers_status)
|
|
|
|
self.assertEqual(statuses[0], rat_mention)
|
|
|
|
|
|
|
|
|
2021-01-25 00:13:26 +00:00
|
|
|
def test_get_activity_feed_blocks(self):
|
|
|
|
''' feed generation with blocked users '''
|
|
|
|
rat = models.User.objects.create_user(
|
|
|
|
'rat', 'rat@rat.rat', 'password', local=True)
|
|
|
|
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
public_status = models.Comment.objects.create(
|
|
|
|
content='public status', book=self.book, user=self.local_user)
|
|
|
|
rat_public = models.Status.objects.create(
|
|
|
|
content='blah blah', user=rat)
|
2021-01-25 00:13:26 +00:00
|
|
|
|
2021-02-07 06:37:19 +00:00
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-02-24 19:59:21 +00:00
|
|
|
self.local_user, privacy=['public'])
|
2021-02-07 06:37:19 +00:00
|
|
|
self.assertEqual(len(statuses), 2)
|
2021-01-25 00:13:26 +00:00
|
|
|
|
|
|
|
# block relationship
|
|
|
|
rat.blocks.add(self.local_user)
|
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-02-24 19:59:21 +00:00
|
|
|
self.local_user, privacy=['public'])
|
2021-01-25 00:13:26 +00:00
|
|
|
self.assertEqual(len(statuses), 1)
|
|
|
|
self.assertEqual(statuses[0], public_status)
|
|
|
|
|
|
|
|
statuses = views.helpers.get_activity_feed(
|
2021-02-24 19:59:21 +00:00
|
|
|
rat, privacy=['public'])
|
2021-01-25 00:13:26 +00:00
|
|
|
self.assertEqual(len(statuses), 1)
|
|
|
|
self.assertEqual(statuses[0], rat_public)
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-03 13:19:03 +00:00
|
|
|
def test_is_bookwyrm_request(self):
|
2021-01-04 18:08:45 +00:00
|
|
|
''' checks if a request came from a bookwyrm instance '''
|
2021-01-03 13:19:03 +00:00
|
|
|
request = self.factory.get('', {'q': 'Test Book'})
|
2021-02-23 19:34:15 +00:00
|
|
|
self.assertFalse(views.helpers.is_bookwyrm_request(request))
|
2021-01-03 13:19:03 +00:00
|
|
|
|
2021-01-04 18:08:45 +00:00
|
|
|
request = self.factory.get(
|
|
|
|
'', {'q': 'Test Book'},
|
|
|
|
HTTP_USER_AGENT=\
|
|
|
|
"http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)"
|
|
|
|
)
|
2021-02-23 19:34:15 +00:00
|
|
|
self.assertFalse(views.helpers.is_bookwyrm_request(request))
|
2021-01-03 13:19:03 +00:00
|
|
|
|
2021-01-04 18:08:45 +00:00
|
|
|
request = self.factory.get(
|
|
|
|
'', {'q': 'Test Book'}, HTTP_USER_AGENT=USER_AGENT)
|
2021-02-23 19:34:15 +00:00
|
|
|
self.assertTrue(views.helpers.is_bookwyrm_request(request))
|
2021-01-13 21:54:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_existing_user(self):
|
|
|
|
''' simple database lookup by username '''
|
|
|
|
result = views.helpers.handle_remote_webfinger('@mouse@local.com')
|
|
|
|
self.assertEqual(result, self.local_user)
|
|
|
|
|
|
|
|
result = views.helpers.handle_remote_webfinger('mouse@local.com')
|
|
|
|
self.assertEqual(result, self.local_user)
|
|
|
|
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_load_user(self):
|
|
|
|
''' find a remote user using webfinger '''
|
|
|
|
username = 'mouse@example.com'
|
|
|
|
wellknown = {
|
|
|
|
"subject": "acct:mouse@example.com",
|
|
|
|
"links": [{
|
|
|
|
"rel": "self",
|
|
|
|
"type": "application/activity+json",
|
|
|
|
"href": "https://example.com/user/mouse"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
'https://example.com/.well-known/webfinger?resource=acct:%s' \
|
|
|
|
% username,
|
|
|
|
json=wellknown,
|
|
|
|
status=200)
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
'https://example.com/user/mouse',
|
|
|
|
json=self.userdata,
|
|
|
|
status=200)
|
|
|
|
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
|
|
|
result = views.helpers.handle_remote_webfinger('@mouse@example.com')
|
|
|
|
self.assertIsInstance(result, models.User)
|
|
|
|
self.assertEqual(result.username, 'mouse@example.com')
|
|
|
|
|
|
|
|
|
|
|
|
def test_handle_reading_status_to_read(self):
|
|
|
|
''' posts shelve activities '''
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier='to-read')
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
2021-01-13 21:54:01 +00:00
|
|
|
views.helpers.handle_reading_status(
|
|
|
|
self.local_user, shelf, self.book, 'public')
|
|
|
|
status = models.GeneratedNote.objects.get()
|
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(status.mention_books.first(), self.book)
|
|
|
|
self.assertEqual(status.content, 'wants to read')
|
|
|
|
|
|
|
|
def test_handle_reading_status_reading(self):
|
|
|
|
''' posts shelve activities '''
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier='reading')
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
2021-01-13 21:54:01 +00:00
|
|
|
views.helpers.handle_reading_status(
|
|
|
|
self.local_user, shelf, self.book, 'public')
|
|
|
|
status = models.GeneratedNote.objects.get()
|
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(status.mention_books.first(), self.book)
|
|
|
|
self.assertEqual(status.content, 'started reading')
|
|
|
|
|
|
|
|
def test_handle_reading_status_read(self):
|
|
|
|
''' posts shelve activities '''
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier='read')
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
2021-01-13 21:54:01 +00:00
|
|
|
views.helpers.handle_reading_status(
|
|
|
|
self.local_user, shelf, self.book, 'public')
|
|
|
|
status = models.GeneratedNote.objects.get()
|
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(status.mention_books.first(), self.book)
|
|
|
|
self.assertEqual(status.content, 'finished reading')
|
|
|
|
|
|
|
|
def test_handle_reading_status_other(self):
|
|
|
|
''' posts shelve activities '''
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
2021-01-13 21:54:01 +00:00
|
|
|
views.helpers.handle_reading_status(
|
|
|
|
self.local_user, self.shelf, self.book, 'public')
|
|
|
|
self.assertFalse(models.GeneratedNote.objects.exists())
|
2021-01-23 19:40:41 +00:00
|
|
|
|
|
|
|
def test_object_visible_to_user(self):
|
|
|
|
''' does a user have permission to view an object '''
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='public')
|
|
|
|
self.assertTrue(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
obj = models.Shelf.objects.create(
|
|
|
|
name='test', user=self.remote_user, privacy='unlisted')
|
|
|
|
self.assertTrue(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='followers')
|
|
|
|
self.assertFalse(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='direct')
|
|
|
|
self.assertFalse(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='direct')
|
|
|
|
obj.mention_users.add(self.local_user)
|
|
|
|
self.assertTrue(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
def test_object_visible_to_user_follower(self):
|
|
|
|
''' what you can see if you follow a user '''
|
|
|
|
self.remote_user.followers.add(self.local_user)
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='followers')
|
|
|
|
self.assertTrue(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='direct')
|
|
|
|
self.assertFalse(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='direct')
|
|
|
|
obj.mention_users.add(self.local_user)
|
|
|
|
self.assertTrue(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
def test_object_visible_to_user_blocked(self):
|
|
|
|
''' you can't see it if they block you '''
|
|
|
|
self.remote_user.blocks.add(self.local_user)
|
|
|
|
obj = models.Status.objects.create(
|
|
|
|
content='hi', user=self.remote_user, privacy='public')
|
|
|
|
self.assertFalse(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|
|
|
|
|
|
|
|
obj = models.Shelf.objects.create(
|
|
|
|
name='test', user=self.remote_user, privacy='unlisted')
|
|
|
|
self.assertFalse(
|
|
|
|
views.helpers.object_visible_to_user(self.local_user, obj))
|