2020-12-15 21:59:37 +00:00
|
|
|
''' sending out activities '''
|
2021-01-02 21:09:40 +00:00
|
|
|
import csv
|
2020-12-15 21:59:37 +00:00
|
|
|
import json
|
|
|
|
import pathlib
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2020-12-30 20:38:34 +00:00
|
|
|
from django.http import JsonResponse
|
2020-12-15 21:59:37 +00:00
|
|
|
from django.test import TestCase
|
2020-12-30 20:38:34 +00:00
|
|
|
from django.test.client import RequestFactory
|
2020-12-15 21:59:37 +00:00
|
|
|
import responses
|
|
|
|
|
2021-01-12 22:43:59 +00:00
|
|
|
from bookwyrm import models, outgoing
|
2020-12-15 21:59:37 +00:00
|
|
|
|
|
|
|
|
2021-01-01 17:35:14 +00:00
|
|
|
# pylint: disable=too-many-public-methods
|
2020-12-15 21:59:37 +00:00
|
|
|
class Outgoing(TestCase):
|
|
|
|
''' sends out activities '''
|
|
|
|
def setUp(self):
|
|
|
|
''' we'll need some data '''
|
2020-12-30 20:38:34 +00:00
|
|
|
self.factory = RequestFactory()
|
2020-12-15 21:59:37 +00:00
|
|
|
with patch('bookwyrm.models.user.set_remote_server'):
|
|
|
|
self.remote_user = models.User.objects.create_user(
|
2021-01-04 18:20:49 +00:00
|
|
|
'rat', 'rat@email.com', 'ratword',
|
2020-12-15 21:59:37 +00:00
|
|
|
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(
|
2021-01-04 18:20:49 +00:00
|
|
|
'mouse@local.com', 'mouse@mouse.com', 'mouseword',
|
2020-12-27 23:18:48 +00:00
|
|
|
local=True, localname='mouse',
|
2020-12-15 21:59:37 +00:00
|
|
|
remote_id='https://example.com/users/mouse',
|
|
|
|
)
|
|
|
|
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath(
|
|
|
|
'data/ap_user.json'
|
|
|
|
)
|
|
|
|
self.userdata = json.loads(datafile.read_bytes())
|
|
|
|
del self.userdata['icon']
|
|
|
|
|
2020-12-21 23:44:11 +00:00
|
|
|
work = models.Work.objects.create(title='Test Work')
|
2020-12-15 21:59:37 +00:00
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title='Example Edition',
|
|
|
|
remote_id='https://example.com/book/1',
|
2020-12-21 23:44:11 +00:00
|
|
|
parent_work=work
|
2020-12-15 21:59:37 +00:00
|
|
|
)
|
|
|
|
self.shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf',
|
|
|
|
identifier='test-shelf',
|
2020-12-21 22:54:27 +00:00
|
|
|
user=self.local_user
|
2020-12-15 21:59:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-30 20:38:34 +00:00
|
|
|
def test_outbox(self):
|
|
|
|
''' returns user's statuses '''
|
|
|
|
request = self.factory.get('')
|
|
|
|
result = outgoing.outbox(request, 'mouse')
|
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
|
|
|
|
def test_outbox_bad_method(self):
|
|
|
|
''' can't POST to outbox '''
|
|
|
|
request = self.factory.post('')
|
|
|
|
result = outgoing.outbox(request, 'mouse')
|
|
|
|
self.assertEqual(result.status_code, 405)
|
|
|
|
|
|
|
|
def test_outbox_unknown_user(self):
|
|
|
|
''' should 404 for unknown and remote users '''
|
|
|
|
request = self.factory.post('')
|
|
|
|
result = outgoing.outbox(request, 'beepboop')
|
|
|
|
self.assertEqual(result.status_code, 405)
|
|
|
|
result = outgoing.outbox(request, 'rat')
|
|
|
|
self.assertEqual(result.status_code, 405)
|
|
|
|
|
2020-12-30 20:41:19 +00:00
|
|
|
def test_outbox_privacy(self):
|
|
|
|
''' don't show dms et cetera in outbox '''
|
|
|
|
models.Status.objects.create(
|
|
|
|
content='PRIVATE!!', user=self.local_user, privacy='direct')
|
|
|
|
models.Status.objects.create(
|
|
|
|
content='bffs ONLY', user=self.local_user, privacy='followers')
|
|
|
|
models.Status.objects.create(
|
|
|
|
content='unlisted status', user=self.local_user, privacy='unlisted')
|
|
|
|
models.Status.objects.create(
|
|
|
|
content='look at this', user=self.local_user, privacy='public')
|
|
|
|
|
|
|
|
request = self.factory.get('')
|
|
|
|
result = outgoing.outbox(request, 'mouse')
|
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.content)
|
|
|
|
self.assertEqual(data['type'], 'OrderedCollection')
|
|
|
|
self.assertEqual(data['totalItems'], 2)
|
|
|
|
|
2020-12-30 21:14:16 +00:00
|
|
|
def test_outbox_filter(self):
|
|
|
|
''' if we only care about reviews, only get reviews '''
|
|
|
|
models.Review.objects.create(
|
|
|
|
content='look at this', name='hi', rating=1,
|
|
|
|
book=self.book, user=self.local_user)
|
|
|
|
models.Status.objects.create(
|
|
|
|
content='look at this', user=self.local_user)
|
|
|
|
|
|
|
|
request = self.factory.get('', {'type': 'bleh'})
|
|
|
|
result = outgoing.outbox(request, 'mouse')
|
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.content)
|
|
|
|
self.assertEqual(data['type'], 'OrderedCollection')
|
|
|
|
self.assertEqual(data['totalItems'], 2)
|
|
|
|
|
|
|
|
request = self.factory.get('', {'type': 'Review'})
|
|
|
|
result = outgoing.outbox(request, 'mouse')
|
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.content)
|
|
|
|
self.assertEqual(data['type'], 'OrderedCollection')
|
|
|
|
self.assertEqual(data['totalItems'], 1)
|
|
|
|
|
2020-12-30 20:38:34 +00:00
|
|
|
|
2020-12-15 21:59:37 +00:00
|
|
|
|
|
|
|
def test_existing_user(self):
|
|
|
|
''' simple database lookup by username '''
|
2021-01-04 20:00:53 +00:00
|
|
|
result = outgoing.handle_remote_webfinger('@mouse@local.com')
|
2020-12-21 23:44:11 +00:00
|
|
|
self.assertEqual(result, self.local_user)
|
2020-12-15 21:59:37 +00:00
|
|
|
|
2021-01-04 20:00:53 +00:00
|
|
|
result = outgoing.handle_remote_webfinger('mouse@local.com')
|
2020-12-21 23:44:11 +00:00
|
|
|
self.assertEqual(result, self.local_user)
|
2020-12-15 21:59:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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 = outgoing.handle_remote_webfinger('@mouse@example.com')
|
|
|
|
self.assertIsInstance(result, models.User)
|
|
|
|
self.assertEqual(result.username, 'mouse@example.com')
|
|
|
|
|
|
|
|
|
2021-01-11 03:29:39 +00:00
|
|
|
def test_handle_reading_status_to_read(self):
|
|
|
|
''' posts shelve activities '''
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier='to-read')
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.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')
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.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')
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.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 '''
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.handle_reading_status(
|
|
|
|
self.local_user, self.shelf, self.book, 'public')
|
|
|
|
self.assertFalse(models.GeneratedNote.objects.exists())
|
|
|
|
|
|
|
|
|
2021-01-02 21:09:40 +00:00
|
|
|
def test_handle_imported_book(self):
|
|
|
|
''' goodreads import added a book, this adds related connections '''
|
|
|
|
shelf = self.local_user.shelf_set.filter(identifier='read').first()
|
|
|
|
self.assertIsNone(shelf.books.first())
|
|
|
|
|
|
|
|
import_job = models.ImportJob.objects.create(user=self.local_user)
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv')
|
|
|
|
csv_file = open(datafile, 'r')
|
|
|
|
for index, entry in enumerate(list(csv.DictReader(csv_file))):
|
|
|
|
import_item = models.ImportItem.objects.create(
|
|
|
|
job_id=import_job.id, index=index, data=entry, book=self.book)
|
|
|
|
break
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.handle_imported_book(
|
|
|
|
self.local_user, import_item, False, 'public')
|
|
|
|
|
|
|
|
shelf.refresh_from_db()
|
|
|
|
self.assertEqual(shelf.books.first(), self.book)
|
|
|
|
|
|
|
|
readthrough = models.ReadThrough.objects.get(user=self.local_user)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
2021-01-02 21:26:42 +00:00
|
|
|
# I can't remember how to create dates and I don't want to look it up.
|
2021-01-02 21:09:40 +00:00
|
|
|
self.assertEqual(readthrough.start_date.year, 2020)
|
|
|
|
self.assertEqual(readthrough.start_date.month, 10)
|
|
|
|
self.assertEqual(readthrough.start_date.day, 21)
|
|
|
|
self.assertEqual(readthrough.finish_date.year, 2020)
|
|
|
|
self.assertEqual(readthrough.finish_date.month, 10)
|
|
|
|
self.assertEqual(readthrough.finish_date.day, 25)
|
|
|
|
|
|
|
|
|
|
|
|
def test_handle_imported_book_already_shelved(self):
|
|
|
|
''' goodreads import added a book, this adds related connections '''
|
|
|
|
shelf = self.local_user.shelf_set.filter(identifier='to-read').first()
|
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
shelf=shelf, added_by=self.local_user, book=self.book)
|
|
|
|
|
|
|
|
import_job = models.ImportJob.objects.create(user=self.local_user)
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv')
|
|
|
|
csv_file = open(datafile, 'r')
|
|
|
|
for index, entry in enumerate(list(csv.DictReader(csv_file))):
|
|
|
|
import_item = models.ImportItem.objects.create(
|
|
|
|
job_id=import_job.id, index=index, data=entry, book=self.book)
|
|
|
|
break
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.handle_imported_book(
|
|
|
|
self.local_user, import_item, False, 'public')
|
|
|
|
|
|
|
|
shelf.refresh_from_db()
|
|
|
|
self.assertEqual(shelf.books.first(), self.book)
|
|
|
|
self.assertIsNone(
|
|
|
|
self.local_user.shelf_set.get(identifier='read').books.first())
|
2021-01-02 21:26:42 +00:00
|
|
|
readthrough = models.ReadThrough.objects.get(user=self.local_user)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
|
|
|
self.assertEqual(readthrough.start_date.year, 2020)
|
|
|
|
self.assertEqual(readthrough.start_date.month, 10)
|
|
|
|
self.assertEqual(readthrough.start_date.day, 21)
|
|
|
|
self.assertEqual(readthrough.finish_date.year, 2020)
|
|
|
|
self.assertEqual(readthrough.finish_date.month, 10)
|
|
|
|
self.assertEqual(readthrough.finish_date.day, 25)
|
|
|
|
|
|
|
|
|
2021-01-10 17:36:11 +00:00
|
|
|
def test_handle_import_twice(self):
|
|
|
|
''' re-importing books '''
|
|
|
|
shelf = self.local_user.shelf_set.filter(identifier='read').first()
|
|
|
|
import_job = models.ImportJob.objects.create(user=self.local_user)
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv')
|
|
|
|
csv_file = open(datafile, 'r')
|
|
|
|
for index, entry in enumerate(list(csv.DictReader(csv_file))):
|
|
|
|
import_item = models.ImportItem.objects.create(
|
|
|
|
job_id=import_job.id, index=index, data=entry, book=self.book)
|
|
|
|
break
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.handle_imported_book(
|
|
|
|
self.local_user, import_item, False, 'public')
|
|
|
|
outgoing.handle_imported_book(
|
|
|
|
self.local_user, import_item, False, 'public')
|
|
|
|
|
|
|
|
shelf.refresh_from_db()
|
|
|
|
self.assertEqual(shelf.books.first(), self.book)
|
|
|
|
|
|
|
|
readthrough = models.ReadThrough.objects.get(user=self.local_user)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
|
|
|
# I can't remember how to create dates and I don't want to look it up.
|
|
|
|
self.assertEqual(readthrough.start_date.year, 2020)
|
2021-01-02 21:26:42 +00:00
|
|
|
self.assertEqual(readthrough.start_date.month, 10)
|
|
|
|
self.assertEqual(readthrough.start_date.day, 21)
|
|
|
|
self.assertEqual(readthrough.finish_date.year, 2020)
|
|
|
|
self.assertEqual(readthrough.finish_date.month, 10)
|
|
|
|
self.assertEqual(readthrough.finish_date.day, 25)
|
|
|
|
|
|
|
|
|
|
|
|
def test_handle_imported_book_review(self):
|
|
|
|
''' goodreads review import '''
|
|
|
|
import_job = models.ImportJob.objects.create(user=self.local_user)
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv')
|
|
|
|
csv_file = open(datafile, 'r')
|
|
|
|
entry = list(csv.DictReader(csv_file))[2]
|
|
|
|
import_item = models.ImportItem.objects.create(
|
|
|
|
job_id=import_job.id, index=0, data=entry, book=self.book)
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.handle_imported_book(
|
|
|
|
self.local_user, import_item, True, 'unlisted')
|
|
|
|
review = models.Review.objects.get(book=self.book, user=self.local_user)
|
|
|
|
self.assertEqual(review.content, 'mixed feelings')
|
|
|
|
self.assertEqual(review.rating, 2)
|
|
|
|
self.assertEqual(review.published_date.year, 2019)
|
|
|
|
self.assertEqual(review.published_date.month, 7)
|
|
|
|
self.assertEqual(review.published_date.day, 8)
|
|
|
|
self.assertEqual(review.privacy, 'unlisted')
|
|
|
|
|
|
|
|
|
|
|
|
def test_handle_imported_book_reviews_disabled(self):
|
|
|
|
''' goodreads review import '''
|
|
|
|
import_job = models.ImportJob.objects.create(user=self.local_user)
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv')
|
|
|
|
csv_file = open(datafile, 'r')
|
|
|
|
entry = list(csv.DictReader(csv_file))[2]
|
|
|
|
import_item = models.ImportItem.objects.create(
|
|
|
|
job_id=import_job.id, index=0, data=entry, book=self.book)
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
outgoing.handle_imported_book(
|
|
|
|
self.local_user, import_item, False, 'unlisted')
|
|
|
|
self.assertFalse(models.Review.objects.filter(
|
|
|
|
book=self.book, user=self.local_user
|
|
|
|
).exists())
|