Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check path for unauthenticated user when selecting search language #1483

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/test_graphql_api/test_reservation_unit/test_text_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,19 @@ def test_reservation_unit__filter__by_text_search__and_other_filters(graphql):
assert response.has_errors is False, response
assert len(response.edges) == 1, response
assert response.node(0) == {"pk": reservation_unit.pk}


def test_reservation_unit__filter__by_text_search__unauthenticated_user(graphql):
# Django translations are inactive during testing, so `get_language_from_request`
# always returns `settings.LANGUAGE_CODE`, see `django.utils.translation.trans_null.get_language_from_request`

reservation_unit_data = SearchableData(description="Kuvaus")
reservation_unit = ReservationUnitFactory.create(**dataclasses.asdict(reservation_unit_data))
ReservationUnit.objects.update_search_vectors()

query = reservation_units_query(text_search="kuvaus")
response = graphql(query)

assert response.has_errors is False, response
assert len(response.edges) == 1, response
assert response.node(0) == {"pk": reservation_unit.pk}
4 changes: 3 additions & 1 deletion utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def get_text_search_language(request: HttpRequest) -> TextSearchLang:
Use preferred language if user is authenticated, otherwise use the language from the request.
"""
user: AnyUser = request.user
lang_code = user.preferred_language if user.is_authenticated else get_language_from_request(request)
lang_code = (
user.preferred_language if user.is_authenticated else get_language_from_request(request, check_path=True)
)
return "swedish" if lang_code == "sv" else "english" if lang_code == "en" else "finnish"


Expand Down
Loading