moviewyrm/bookwyrm/tests/views/test_isbn.py

62 lines
2.3 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-03-01 20:09:21 +00:00
import json
from unittest.mock import patch
from django.http import JsonResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
from bookwyrm.settings import DOMAIN
class IsbnViews(TestCase):
2021-04-26 16:15:42 +00:00
"""tag views"""
2021-03-08 16:49:10 +00:00
2021-03-01 20:09:21 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-03-01 20:09:21 +00:00
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-09 23:03:01 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-08-03 17:25:53 +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",
)
2021-08-02 23:05:40 +00:00
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.SiteSettings.objects.create()
2021-03-01 20:09:21 +00:00
def test_isbn_json_response(self):
2021-04-26 16:15:42 +00:00
"""searches local data only and returns book data in json format"""
2021-03-01 20:09:21 +00:00
view = views.Isbn.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
with patch("bookwyrm.views.isbn.is_api_request") as is_api:
2021-03-01 20:09:21 +00:00
is_api.return_value = True
2021-03-08 16:49:10 +00:00
response = view(request, isbn="1234567890123")
2021-03-01 20:09:21 +00:00
self.assertIsInstance(response, JsonResponse)
data = json.loads(response.content)
self.assertEqual(len(data), 1)
2021-03-08 16:49:10 +00:00
self.assertEqual(data[0]["title"], "Test Book")
self.assertEqual(data[0]["key"], f"https://{DOMAIN}/book/{self.book.id}")
2021-12-09 23:03:01 +00:00
def test_isbn_html_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 = False
response = view(request, isbn="1234567890123")
self.assertEqual(response.status_code, 200)
response.render()