moviewyrm/bookwyrm/tests/views/test_search.py

143 lines
5.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-13 20:03:27 +00:00
import json
2021-09-16 22:27:06 +00:00
import pathlib
2021-01-13 20:03:27 +00:00
from unittest.mock import patch
2021-05-01 17:39:05 +00:00
from django.contrib.auth.models import AnonymousUser
2021-01-13 20:03:27 +00:00
from django.http import JsonResponse
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
2021-09-16 22:27:06 +00:00
import responses
2021-01-13 20:03:27 +00:00
from bookwyrm import models, views
from bookwyrm.settings import DOMAIN
class Views(TestCase):
2021-04-26 16:15:42 +00:00
"""tag views"""
2021-03-08 16:49:10 +00:00
2021-01-13 20:03:27 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-13 20:03:27 +00:00
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_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",
remote_id="https://example.com/book/1",
parent_work=self.work,
)
models.SiteSettings.objects.create()
2021-01-13 20:03:27 +00:00
def test_search_json_response(self):
2021-04-26 16:15:42 +00:00
"""searches local data only and returns book data in json format"""
2021-01-13 20:03:27 +00:00
view = views.Search.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("", {"q": "Test Book"})
with patch("bookwyrm.views.search.is_api_request") as is_api:
2021-01-13 20:03:27 +00:00
is_api.return_value = True
response = view(request)
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"], "https://%s/book/%d" % (DOMAIN, self.book.id))
2021-01-13 20:03:27 +00:00
2021-05-20 23:32:44 +00:00
def test_search_no_query(self):
"""just the search page"""
view = views.Search.as_view()
# we need a connector for this, sorry
request = self.factory.get("")
with patch("bookwyrm.views.search.is_api_request") as is_api:
is_api.return_value = False
response = view(request)
self.assertIsInstance(response, TemplateResponse)
response.render()
2021-09-16 22:27:06 +00:00
@responses.activate
2021-05-01 17:39:05 +00:00
def test_search_books(self):
2021-04-26 16:15:42 +00:00
"""searches remote connectors"""
2021-01-13 20:03:27 +00:00
view = views.Search.as_view()
2021-03-08 16:49:10 +00:00
2021-01-13 20:03:27 +00:00
models.Connector.objects.create(
2021-03-08 16:49:10 +00:00
identifier="example.com",
connector_file="openlibrary",
base_url="https://example.com",
books_url="https://example.com/books",
covers_url="https://example.com/covers",
search_url="https://example.com/search?q=",
2021-01-13 20:03:27 +00:00
)
2021-09-16 22:27:06 +00:00
datafile = pathlib.Path(__file__).parent.joinpath("../data/ol_search.json")
search_data = json.loads(datafile.read_bytes())
responses.add(
responses.GET,
"https://example.com/search?q=Test%20Book",
json=search_data
2021-01-13 20:03:27 +00:00
)
2021-05-01 17:39:05 +00:00
request = self.factory.get("", {"q": "Test Book", "remote": True})
2021-02-01 21:06:06 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.search.is_api_request") as is_api:
2021-01-13 20:03:27 +00:00
is_api.return_value = False
2021-09-16 22:27:06 +00:00
response = view(request)
2021-01-13 20:03:27 +00:00
self.assertIsInstance(response, TemplateResponse)
response.render()
2021-09-16 22:27:06 +00:00
connector_results = response.context_data["results"]
self.assertEqual(
connector_results[0]["results"][0].title, "Test Book"
)
self.assertEqual(
connector_results[1]["results"][0].title,
"This Is How You Lose the Time War"
)
2021-01-13 20:03:27 +00:00
2021-05-01 17:39:05 +00:00
def test_search_users(self):
2021-04-26 16:15:42 +00:00
"""searches remote connectors"""
2021-01-13 20:03:27 +00:00
view = views.Search.as_view()
2021-05-01 17:39:05 +00:00
request = self.factory.get("", {"q": "mouse", "type": "user"})
2021-02-01 21:06:06 +00:00
request.user = self.local_user
2021-05-01 17:39:05 +00:00
response = view(request)
self.assertIsInstance(response, TemplateResponse)
response.render()
self.assertEqual(response.context_data["results"][0], self.local_user)
def test_search_users_logged_out(self):
"""searches remote connectors"""
view = views.Search.as_view()
request = self.factory.get("", {"q": "mouse", "type": "user"})
anonymous_user = AnonymousUser
anonymous_user.is_authenticated = False
request.user = anonymous_user
response = view(request)
response.render()
2021-05-03 17:46:02 +00:00
self.assertFalse("results" in response.context_data)
2021-05-01 17:39:05 +00:00
def test_search_lists(self):
"""searches remote connectors"""
2021-05-02 01:05:21 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
booklist = models.List.objects.create(
user=self.local_user, name="test list"
)
2021-05-01 17:39:05 +00:00
view = views.Search.as_view()
request = self.factory.get("", {"q": "test", "type": "list"})
request.user = self.local_user
response = view(request)
2021-01-13 20:03:27 +00:00
self.assertIsInstance(response, TemplateResponse)
response.render()
2021-05-01 17:39:05 +00:00
self.assertEqual(response.context_data["results"][0], booklist)