Merge branch 'main' into create-book
This commit is contained in:
@ -1 +0,0 @@
|
||||
from . import *
|
@ -208,7 +208,10 @@ class BaseActivity(TestCase):
|
||||
# sets the celery task call to the function call
|
||||
with patch(
|
||||
'bookwyrm.activitypub.base_activity.set_related_field.delay'):
|
||||
update_data.to_model(model=models.Status, instance=status)
|
||||
with patch('bookwyrm.models.status.Status.ignore_activity') \
|
||||
as discarder:
|
||||
discarder.return_value = False
|
||||
update_data.to_model(model=models.Status, instance=status)
|
||||
self.assertIsNone(status.attachments.first())
|
||||
|
||||
|
||||
|
@ -42,6 +42,10 @@ class AbstractConnector(TestCase):
|
||||
return search_result
|
||||
def parse_search_data(self, data):
|
||||
return data
|
||||
def format_isbn_search_result(self, search_result):
|
||||
return search_result
|
||||
def parse_isbn_search_data(self, data):
|
||||
return data
|
||||
def is_work_data(self, data):
|
||||
return data['type'] == 'work'
|
||||
def get_edition_from_work_data(self, data):
|
||||
|
@ -18,6 +18,7 @@ class AbstractConnector(TestCase):
|
||||
books_url='https://example.com/books',
|
||||
covers_url='https://example.com/covers',
|
||||
search_url='https://example.com/search?q=',
|
||||
isbn_search_url='https://example.com/isbn',
|
||||
)
|
||||
|
||||
class TestConnector(abstract_connector.AbstractMinimalConnector):
|
||||
@ -28,6 +29,10 @@ class AbstractConnector(TestCase):
|
||||
pass
|
||||
def parse_search_data(self, data):
|
||||
return data
|
||||
def format_isbn_search_result(self, search_result):
|
||||
return search_result
|
||||
def parse_isbn_search_data(self, data):
|
||||
return data
|
||||
self.test_connector = TestConnector('example.com')
|
||||
|
||||
|
||||
@ -39,6 +44,7 @@ class AbstractConnector(TestCase):
|
||||
self.assertEqual(connector.books_url, 'https://example.com/books')
|
||||
self.assertEqual(connector.covers_url, 'https://example.com/covers')
|
||||
self.assertEqual(connector.search_url, 'https://example.com/search?q=')
|
||||
self.assertEqual(connector.isbn_search_url, 'https://example.com/isbn')
|
||||
self.assertIsNone(connector.name)
|
||||
self.assertEqual(connector.identifier, 'example.com')
|
||||
self.assertIsNone(connector.max_query_count)
|
||||
|
@ -27,6 +27,7 @@ class Openlibrary(TestCase):
|
||||
books_url='https://openlibrary.org',
|
||||
covers_url='https://covers.openlibrary.org',
|
||||
search_url='https://openlibrary.org/search?q=',
|
||||
isbn_search_url='https://openlibrary.org/isbn',
|
||||
)
|
||||
self.connector = Connector('openlibrary.org')
|
||||
|
||||
@ -149,6 +150,34 @@ class Openlibrary(TestCase):
|
||||
self.assertEqual(result.connector, self.connector)
|
||||
|
||||
|
||||
def test_parse_isbn_search_result(self):
|
||||
''' extract the results from the search json response '''
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ol_isbn_search.json')
|
||||
search_data = json.loads(datafile.read_bytes())
|
||||
result = self.connector.parse_isbn_search_data(search_data)
|
||||
self.assertIsInstance(result, list)
|
||||
self.assertEqual(len(result), 1)
|
||||
|
||||
|
||||
def test_format_isbn_search_result(self):
|
||||
''' translate json from openlibrary into SearchResult '''
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ol_isbn_search.json')
|
||||
search_data = json.loads(datafile.read_bytes())
|
||||
results = self.connector.parse_isbn_search_data(search_data)
|
||||
self.assertIsInstance(results, list)
|
||||
|
||||
result = self.connector.format_isbn_search_result(results[0])
|
||||
self.assertIsInstance(result, SearchResult)
|
||||
self.assertEqual(result.title, 'Les ombres errantes')
|
||||
self.assertEqual(
|
||||
result.key, 'https://openlibrary.org/books/OL16262504M')
|
||||
self.assertEqual(result.author, 'Pascal Quignard')
|
||||
self.assertEqual(result.year, '2002')
|
||||
self.assertEqual(result.connector, self.connector)
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_load_edition_data(self):
|
||||
''' format url from key and make request '''
|
||||
|
45
bookwyrm/tests/data/ol_isbn_search.json
Normal file
45
bookwyrm/tests/data/ol_isbn_search.json
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
"ISBN:9782070427796": {
|
||||
"url": "https://openlibrary.org/books/OL16262504M/Les_ombres_errantes",
|
||||
"key": "/books/OL16262504M",
|
||||
"title": "Les ombres errantes",
|
||||
"authors": [
|
||||
{
|
||||
"url": "https://openlibrary.org/authors/OL269675A/Pascal_Quignard",
|
||||
"name": "Pascal Quignard"
|
||||
}
|
||||
],
|
||||
"by_statement": "Pascal Quignard.",
|
||||
"identifiers": {
|
||||
"goodreads": [
|
||||
"1835483"
|
||||
],
|
||||
"librarything": [
|
||||
"983474"
|
||||
],
|
||||
"isbn_10": [
|
||||
"207042779X"
|
||||
],
|
||||
"openlibrary": [
|
||||
"OL16262504M"
|
||||
]
|
||||
},
|
||||
"classifications": {
|
||||
"dewey_decimal_class": [
|
||||
"848/.91403"
|
||||
]
|
||||
},
|
||||
"publishers": [
|
||||
{
|
||||
"name": "Gallimard"
|
||||
}
|
||||
],
|
||||
"publish_places": [
|
||||
{
|
||||
"name": "Paris"
|
||||
}
|
||||
],
|
||||
"publish_date": "2002",
|
||||
"notes": "Hardback published Grasset, 2002."
|
||||
}
|
||||
}
|
@ -81,7 +81,7 @@ class Book(TestCase):
|
||||
book.save()
|
||||
self.assertEqual(book.edition_info, 'worm, Glorbish language, 2020')
|
||||
self.assertEqual(
|
||||
book.alt_text, 'Test Edition cover (worm, Glorbish language, 2020)')
|
||||
book.alt_text, 'Test Edition (worm, Glorbish language, 2020)')
|
||||
|
||||
|
||||
def test_get_rank(self):
|
||||
|
@ -150,7 +150,7 @@ class Status(TestCase):
|
||||
self.assertEqual(activity['attachment'][0].url, 'https://%s%s' % \
|
||||
(settings.DOMAIN, self.book.cover.url))
|
||||
self.assertEqual(
|
||||
activity['attachment'][0].name, 'Test Edition cover')
|
||||
activity['attachment'][0].name, 'Test Edition')
|
||||
|
||||
def test_comment_to_activity(self, _):
|
||||
''' subclass of the base model version with a "pure" serializer '''
|
||||
@ -177,7 +177,7 @@ class Status(TestCase):
|
||||
self.assertEqual(activity['attachment'][0].url, 'https://%s%s' % \
|
||||
(settings.DOMAIN, self.book.cover.url))
|
||||
self.assertEqual(
|
||||
activity['attachment'][0].name, 'Test Edition cover')
|
||||
activity['attachment'][0].name, 'Test Edition')
|
||||
|
||||
def test_quotation_to_activity(self, _):
|
||||
''' subclass of the base model version with a "pure" serializer '''
|
||||
@ -207,7 +207,7 @@ class Status(TestCase):
|
||||
self.assertEqual(activity['attachment'][0].url, 'https://%s%s' % \
|
||||
(settings.DOMAIN, self.book.cover.url))
|
||||
self.assertEqual(
|
||||
activity['attachment'][0].name, 'Test Edition cover')
|
||||
activity['attachment'][0].name, 'Test Edition')
|
||||
|
||||
def test_review_to_activity(self, _):
|
||||
''' subclass of the base model version with a "pure" serializer '''
|
||||
@ -238,7 +238,7 @@ class Status(TestCase):
|
||||
self.assertEqual(activity['attachment'][0].url, 'https://%s%s' % \
|
||||
(settings.DOMAIN, self.book.cover.url))
|
||||
self.assertEqual(
|
||||
activity['attachment'][0].name, 'Test Edition cover')
|
||||
activity['attachment'][0].name, 'Test Edition')
|
||||
|
||||
def test_favorite(self, _):
|
||||
''' fav a status '''
|
||||
|
@ -74,7 +74,7 @@ class Inbox(TestCase):
|
||||
mock_valid.return_value = False
|
||||
result = self.client.post(
|
||||
'/user/mouse/inbox',
|
||||
'{"type": "Test", "object": "exists"}',
|
||||
'{"type": "Announce", "object": "exists"}',
|
||||
content_type="application/json"
|
||||
)
|
||||
self.assertEqual(result.status_code, 401)
|
||||
@ -484,7 +484,7 @@ class Inbox(TestCase):
|
||||
'actor': 'https://example.com/users/rat',
|
||||
'type': 'Like',
|
||||
'published': 'Mon, 25 May 2020 19:31:20 GMT',
|
||||
'object': 'https://example.com/status/1',
|
||||
'object': self.status.remote_id,
|
||||
}
|
||||
|
||||
views.inbox.activity_task(activity)
|
||||
@ -494,6 +494,21 @@ class Inbox(TestCase):
|
||||
self.assertEqual(fav.remote_id, 'https://example.com/fav/1')
|
||||
self.assertEqual(fav.user, self.remote_user)
|
||||
|
||||
def test_ignore_favorite(self):
|
||||
''' don't try to save an unknown status '''
|
||||
activity = {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'id': 'https://example.com/fav/1',
|
||||
'actor': 'https://example.com/users/rat',
|
||||
'type': 'Like',
|
||||
'published': 'Mon, 25 May 2020 19:31:20 GMT',
|
||||
'object': 'https://unknown.status/not-found',
|
||||
}
|
||||
|
||||
views.inbox.activity_task(activity)
|
||||
|
||||
self.assertFalse(models.Favorite.objects.exists())
|
||||
|
||||
def test_handle_unfavorite(self):
|
||||
''' fav a status '''
|
||||
activity = {
|
||||
|
54
bookwyrm/tests/views/test_isbn.py
Normal file
54
bookwyrm/tests/views/test_isbn.py
Normal file
@ -0,0 +1,54 @@
|
||||
''' test for app action functionality '''
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.http import JsonResponse
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.connectors import abstract_connector
|
||||
from bookwyrm.settings import DOMAIN
|
||||
|
||||
|
||||
class IsbnViews(TestCase):
|
||||
''' tag views'''
|
||||
def setUp(self):
|
||||
''' we need basic test data and mocks '''
|
||||
self.factory = RequestFactory()
|
||||
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',
|
||||
)
|
||||
self.work = models.Work.objects.create(title='Test Work')
|
||||
self.book = models.Edition.objects.create(
|
||||
title='Test Book',
|
||||
isbn_13='1234567890123',
|
||||
remote_id='https://example.com/book/1',
|
||||
parent_work=self.work
|
||||
)
|
||||
models.Connector.objects.create(
|
||||
identifier='self',
|
||||
connector_file='self_connector',
|
||||
local=True
|
||||
)
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
|
||||
def test_isbn_json_response(self):
|
||||
''' searches local data only and returns book data in json format '''
|
||||
view = views.Isbn.as_view()
|
||||
request = self.factory.get('')
|
||||
with patch('bookwyrm.views.isbn.is_api_request') as is_api:
|
||||
is_api.return_value = True
|
||||
response = view(request, isbn='1234567890123')
|
||||
self.assertIsInstance(response, JsonResponse)
|
||||
|
||||
data = json.loads(response.content)
|
||||
self.assertEqual(len(data), 1)
|
||||
self.assertEqual(data[0]['title'], 'Test Book')
|
||||
self.assertEqual(
|
||||
data[0]['key'], 'https://%s/book/%d' % (DOMAIN, self.book.id))
|
||||
|
@ -64,6 +64,10 @@ class ShelfViews(TestCase):
|
||||
pass
|
||||
def parse_search_data(self, data):
|
||||
pass
|
||||
def format_isbn_search_result(self, search_result):
|
||||
return search_result
|
||||
def parse_isbn_search_data(self, data):
|
||||
return data
|
||||
models.Connector.objects.create(
|
||||
identifier='example.com',
|
||||
connector_file='openlibrary',
|
||||
|
Reference in New Issue
Block a user