Skip to content

Commit

Permalink
[GH-143] Show correct page and error when user searches a 0-3 length …
Browse files Browse the repository at this point in the history
…string. (#146)

* Set result_detail value (must be summmary or full) when query length is too short

* remember the search preferences when showing error messages

* move context check into a new function

* Add unit tests for short name query and empty form

* [Bot] Update version to 2.2.5

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
goulter and github-actions[bot] authored Nov 16, 2022
1 parent dc0383f commit 17d0b2d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
38 changes: 35 additions & 3 deletions husky_directory/blueprints/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,38 @@ def handle_search_exception(e: Exception, context: RenderingContext):
"email [email protected] describing your problem."
)

@staticmethod
def set_preferences_for_cookie(context: RenderingContext):
"""
When the search query is too short, the request_input data is dropped, but we still need a value for
'result_detail'
"""
result_detail = "summary"
if context.request_input and context.request_input.length:
result_detail = context.request_input.length

preferences = PreferencesCookie(result_detail=result_detail).json(
exclude_unset=True, exclude_none=True
)
return preferences

def check_context(self, context: RenderingContext, request: Request):
if context.request_input:
return context
else:
context.request_input = SearchDirectoryFormInput(
method=request.form["method"],
population=request.form["population"],
length=request.form["length"],
person_href=None,
render_method=request.form["method"],
render_query="",
render_population=request.form["population"],
render_length=request.form["length"],
include_test_identities=False,
)
return context

def search_listing(
self,
request: Request,
Expand All @@ -160,15 +192,15 @@ def search_listing(
self.logger.exception(str(e))
SearchBlueprint.handle_search_exception(e, context)
finally:
context = self.check_context(context, request)
response: Response = make_response(
render_template(
"views/search_results.html", **context.dict(exclude_none=True)
),
context.status_code,
)
preferences = PreferencesCookie(
result_detail=context.request_input.length
).json(exclude_unset=True, exclude_none=True)

preferences = self.set_preferences_for_cookie(context)
response.set_cookie(
settings.session_settings.preferences_cookie_name, value=preferences
)
Expand Down
3 changes: 2 additions & 1 deletion husky_directory/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ <h3>Search by</h3>
value="{{ search_value }}"
style="color:black; height:43px; z-index:0;" id="query"
{{ 'autofocus' if search_value is blank }}
name="query">
name="query"
>
<span class="input-group-btn">
<button class="btn search"
type="submit"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "uw-husky-directory"
version = "2.2.4"
version = "2.2.5"
description = "An updated version of the UW Directory"
authors = ["Thomas Thorogood <[email protected]>"]
license = "MIT"
Expand Down
64 changes: 64 additions & 0 deletions tests/blueprints/test_search_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,70 @@ def initialize(


class TestSearchBlueprint(BlueprintSearchTestBase):
@pytest.mark.parametrize(
"search_field, search_value, expected_value",
[
(
"name",
"",
"Invalid input for query (Name query string must contain at least 2 characters)",
),
(
"phone",
"",
"Invalid input for query (Query string must contain at least 3 characters)",
),
(
"department",
"",
"Invalid input for query (Query string must contain at least 3 characters)",
),
(
"box_number",
"",
"Invalid input for query (Query string must contain at least 3 characters)",
),
(
"email",
"",
"Invalid input for query (Query string must contain at least 3 characters)",
),
],
)
def test_set_preferences_for_cookie(
self, search_field, search_value, expected_value
):
response = self.flask_client.post(
"/",
data={
"query": search_value,
"method": search_field,
"population": "employees",
"length": "summary",
},
)
assert response.status_code == 400
with self.html_validator.validate_response(response) as html:
assert not html.find("table", summary="results")
assert html.find(string=re.compile("Encountered error"))
self.html_validator.assert_has_tag_with_text(
"b",
expected_value,
)

def test_empty_form(self):
"""
if somehow a hacker passes an empty form to the back, we still won't return anything.
"""
response = self.flask_client.post(
"/",
data={},
)
assert response.status_code == 200
with self.html_validator.validate_response(response) as html:
assert not html.find("table", summary="results")
self.html_validator.assert_has_tag_with_text("p", "No matches for")

def test_render_summary_success(
self, search_method: str = "name", search_query: str = "lovelace"
):
Expand Down

0 comments on commit 17d0b2d

Please sign in to comment.