Skip to content

Commit

Permalink
fix(search): Ensure additional search connectors apply for the Search…
Browse files Browse the repository at this point in the history
… API
  • Loading branch information
albertisfu committed Jan 10, 2025
1 parent 9b58ac8 commit 14efb89
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cl/alerts/management/commands/cl_send_recap_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
)
from cl.search.exception import (
BadProximityQuery,
DisallowedWildcardPattern,
UnbalancedParenthesesQuery,
UnbalancedQuotesQuery,
)
Expand Down Expand Up @@ -455,6 +456,7 @@ def query_alerts(
UnbalancedParenthesesQuery,
UnbalancedQuotesQuery,
BadProximityQuery,
DisallowedWildcardPattern,
TransportError,
ConnectionError,
RequestError,
Expand Down
1 change: 1 addition & 0 deletions cl/lib/elasticsearch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3094,6 +3094,7 @@ def do_es_api_query(
UnbalancedParenthesesQuery,
UnbalancedQuotesQuery,
BadProximityQuery,
DisallowedWildcardPattern,
) as e:
raise ElasticBadRequestError(detail=e.message)

Expand Down
2 changes: 1 addition & 1 deletion cl/search/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ class ElasticBadRequestError(APIException):
class DisallowedWildcardPattern(SyntaxQueryError):
"""Query contains a disallowed wildcard pattern"""

message = "The query contains a disallowed wildcard pattern."
message = "The query contains a disallowed expensive wildcard pattern."
2 changes: 2 additions & 0 deletions cl/search/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from cl.search.documents import ESRECAPDocument, OpinionClusterDocument
from cl.search.exception import (
BadProximityQuery,
DisallowedWildcardPattern,
UnbalancedParenthesesQuery,
UnbalancedQuotesQuery,
)
Expand Down Expand Up @@ -255,6 +256,7 @@ def wrapped_view(request, *args, **kwargs):
UnbalancedParenthesesQuery,
UnbalancedQuotesQuery,
BadProximityQuery,
DisallowedWildcardPattern,
ApiError,
) as e:
logger.warning("Couldn't load the feed page. Error was: %s", e)
Expand Down
64 changes: 63 additions & 1 deletion cl/search/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ def test_support_search_connectors(self) -> None:

for test_case in tests:
with self.subTest(label=test_case["label"]):
# Frontend
response = self.client.get(
reverse("show_results"),
test_case["search_params"],
Expand All @@ -1087,6 +1088,24 @@ def test_support_search_connectors(self) -> None:
msg=f"Failed on: {test_case['label']} missing {expected_str}",
)

# API
api_response = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
test_case["search_params"],
)
self.assertEqual(
len(api_response.data["results"]),
test_case["expected_count"],
msg=f"Failed on API: {test_case['label']}",
)
decoded_content = api_response.content.decode()
for expected_str in test_case["expected_in_content"]:
self.assertIn(
expected_str,
decoded_content,
msg=f"Failed on Frontend: {test_case['label']} missing {expected_str}",
)

def test_support_search_connectors_filters(self) -> None:
"""Verify that new supported custom search connectors yield the
expected results.
Expand Down Expand Up @@ -1137,6 +1156,7 @@ def test_support_search_connectors_filters(self) -> None:

for test_case in tests:
with self.subTest(label=test_case["label"]):
# Frontend
response = self.client.get(
reverse("show_results"),
test_case["search_params"],
Expand All @@ -1152,7 +1172,25 @@ def test_support_search_connectors_filters(self) -> None:
self.assertIn(
expected_str,
decoded_content,
msg=f"Failed on: {test_case['label']} missing {expected_str}",
msg=f"Failed on Frontend: {test_case['label']} missing {expected_str}",
)

# API
api_response = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
test_case["search_params"],
)
self.assertEqual(
len(api_response.data["results"]),
test_case["expected_count"],
msg=f"Failed on API: {test_case['label']}",
)
decoded_content = api_response.content.decode()
for expected_str in test_case["expected_in_content"]:
self.assertIn(
expected_str,
decoded_content,
msg=f"Failed on Frontend: {test_case['label']} missing {expected_str}",
)

def test_disallowed_wildcard_pattern(self) -> None:
Expand Down Expand Up @@ -1210,6 +1248,30 @@ def test_disallowed_wildcard_pattern(self) -> None:
msg=f"Failed on: {test_case['label']}, no disallowed wildcard pattern error.",
)

# API V4
api_response = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
test_case["search_params"],
)
self.assertEqual(api_response.status_code, 400)
self.assertEqual(
api_response.data["detail"],
"The query contains a disallowed expensive wildcard pattern.",
msg="Failed for V4",
)

# API V3
api_response = self.client.get(
reverse("search-list", kwargs={"version": "v3"}),
test_case["search_params"],
)
self.assertEqual(api_response.status_code, 400)
self.assertEqual(
api_response.data["detail"],
"The query contains a disallowed expensive wildcard pattern.",
msg="Failed for V3",
)


class SearchAPIV4CommonTest(ESIndexTestCase, TestCase):
"""Common tests for the Search API V4 endpoints."""
Expand Down

0 comments on commit 14efb89

Please sign in to comment.