Merge pull request #3410 from timothyjrogers/trim-search-whitespace

Trim search whitespace
This commit is contained in:
Mouse Reeve 2024-08-11 17:12:04 -07:00 committed by GitHub
commit cf753afaa9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 5 deletions

View file

@ -103,6 +103,20 @@ class Views(TestCase):
connector_results = response.context_data["remote_results"] connector_results = response.context_data["remote_results"]
self.assertEqual(connector_results[0]["results"][0].title, "Mock Book") self.assertEqual(connector_results[0]["results"][0].title, "Mock Book")
def test_search_books_extra_whitespace(self):
"""just the search page"""
view = views.Search.as_view()
request = self.factory.get("", {"q": " Test Book ", "remote": False})
request.user = self.local_user
with patch("bookwyrm.views.search.is_api_request") as is_api:
is_api.return_value = False
response = view(request)
self.assertIsInstance(response, TemplateResponse)
validate_html(response.render())
local_results = response.context_data["results"]
self.assertEqual(local_results[0].title, "Test Book")
def test_search_book_anonymous(self): def test_search_book_anonymous(self):
"""Don't search remote for logged out user""" """Don't search remote for logged out user"""
view = views.Search.as_view() view = views.Search.as_view()
@ -150,6 +164,17 @@ class Views(TestCase):
validate_html(response.render()) validate_html(response.render())
self.assertEqual(response.context_data["results"][0], self.local_user) self.assertEqual(response.context_data["results"][0], self.local_user)
def test_search_users_extra_whitespace(self):
"""searches remote connectors"""
view = views.Search.as_view()
request = self.factory.get("", {"q": " mouse ", "type": "user"})
request.user = self.local_user
response = view(request)
self.assertIsInstance(response, TemplateResponse)
validate_html(response.render())
self.assertEqual(response.context_data["results"][0], self.local_user)
def test_search_users_logged_out(self): def test_search_users_logged_out(self):
"""searches remote connectors""" """searches remote connectors"""
view = views.Search.as_view() view = views.Search.as_view()
@ -181,3 +206,21 @@ class Views(TestCase):
self.assertIsInstance(response, TemplateResponse) self.assertIsInstance(response, TemplateResponse)
validate_html(response.render()) validate_html(response.render())
self.assertEqual(response.context_data["results"][0], booklist) self.assertEqual(response.context_data["results"][0], booklist)
def test_search_lists_extra_whitespace(self):
"""searches remote connectors"""
with (
patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"),
patch("bookwyrm.lists_stream.remove_list_task.delay"),
):
booklist = models.List.objects.create(
user=self.local_user, name="test list"
)
view = views.Search.as_view()
request = self.factory.get("", {"q": " test ", "type": "list"})
request.user = self.local_user
response = view(request)
self.assertIsInstance(response, TemplateResponse)
validate_html(response.render())
self.assertEqual(response.context_data["results"][0], booklist)

View file

@ -53,7 +53,7 @@ class Search(View):
def api_book_search(request): def api_book_search(request):
"""Return books via API response""" """Return books via API response"""
query = request.GET.get("q") query = request.GET.get("q").strip()
query = isbn_check_and_format(query) query = isbn_check_and_format(query)
min_confidence = request.GET.get("min_confidence", 0) min_confidence = request.GET.get("min_confidence", 0)
# only return local book results via json so we don't cascade # only return local book results via json so we don't cascade
@ -65,7 +65,7 @@ def api_book_search(request):
def book_search(request): def book_search(request):
"""the real business is elsewhere""" """the real business is elsewhere"""
query = request.GET.get("q") query = request.GET.get("q").strip()
# check if query is isbn # check if query is isbn
query = isbn_check_and_format(query) query = isbn_check_and_format(query)
min_confidence = request.GET.get("min_confidence", 0) min_confidence = request.GET.get("min_confidence", 0)
@ -123,8 +123,7 @@ def author_search(request):
def user_search(request): def user_search(request):
"""user search: search for a user""" """user search: search for a user"""
viewer = request.user viewer = request.user
query = request.GET.get("q") query = request.GET.get("q").strip()
query = query.strip()
data = {"type": "user", "query": query} data = {"type": "user", "query": query}
# use webfinger for mastodon style account@domain.com username to load the user if # use webfinger for mastodon style account@domain.com username to load the user if
@ -162,7 +161,7 @@ def user_search(request):
def list_search(request): def list_search(request):
"""any relevent lists?""" """any relevent lists?"""
query = request.GET.get("q") query = request.GET.get("q").strip()
data = {"query": query, "type": "list"} data = {"query": query, "type": "list"}
results = ( results = (
models.List.privacy_filter( models.List.privacy_filter(