Skip to content

Commit 920f350

Browse files
committed
tests: add language route test and get_locale unit tests
1 parent f8331f7 commit 920f350

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ atlas/debug.py
3636
Makefile.perso.mk
3737
robots.txt
3838
!atlas/static/sample/robots.txt
39+
40+
.github/*instructions.md

atlas/static/sample/templates/navbar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdown09">
5252
{% for language in configuration.AVAILABLE_LANGUAGES %}
5353
<li>
54-
<a class="dropdown-item d-flex align-items-center" href="{{ url_for(request.change_language, lang_code=language) }}">
54+
<a class="dropdown-item d-flex align-items-center" href="{{ url_for('main.change_language', lang_code=language) }}">
5555
<span class="flag-icon {{ configuration.AVAILABLE_LANGUAGES[language]['flag_icon'] }}"></span>
5656
<div class="d-none d-lg-block ms-2">{{ configuration.AVAILABLE_LANGUAGES[language]['name'] }}</div>
5757
</a>

atlas/tests/conftest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
# pylint: disable=redefined-outer-name
2-
from copy import deepcopy
1+
import os
32
from pathlib import Path
43

4+
# IMPORTANT: Set ATLAS_SETTINGS before any atlas.* import!
5+
# Set ATLAS_SETTINGS to test config by default if not already set
6+
if "ATLAS_SETTINGS" not in os.environ:
7+
os.environ["ATLAS_SETTINGS"] = str(Path(__file__).parent / "test_config.py")
8+
59
import pytest
610
from sqlalchemy import text
711

atlas/tests/test_app.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from fixtures.main_fixtures import captured_templates
33

44
from atlas.configuration.config_schema import SecretSchemaConf
5+
from atlas.tests.conftest import with_config
56

67

78
def test_context_processor(app, client, captured_templates):
@@ -10,7 +11,14 @@ def test_context_processor(app, client, captured_templates):
1011
template, context = captured_templates[0]
1112

1213
# test @app.context_processor for global vars in all app
13-
injected_context_vars = ["configuration", "translations", "now", "timedelta", "page_name"]
14+
injected_context_vars = [
15+
"configuration",
16+
"translations",
17+
"now",
18+
"timedelta",
19+
"page_name",
20+
"current_language",
21+
]
1422
for var in injected_context_vars:
1523
assert var in context
1624

@@ -19,3 +27,19 @@ def test_context_processor(app, client, captured_templates):
1927
configuration = context["configuration"]
2028
for key, val in configuration.items():
2129
assert key not in secret_fields
30+
31+
32+
def test_change_language_sets_session_and_persists(app, client, captured_templates):
33+
# Call change language route
34+
resp = client.get(url_for("main.change_language", lang_code="en"), follow_redirects=False)
35+
assert resp.status_code in (302, 303)
36+
37+
# Session should have been set by the route
38+
with client.session_transaction() as sess:
39+
assert sess.get("language") == "en"
40+
41+
# Calling another route (index) should keep the language in injected context
42+
client.get(url_for("main.index"))
43+
template, context = captured_templates[0]
44+
assert "current_language" in context
45+
assert context["current_language"] == "en"

atlas/tests/test_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
SQLALCHEMY_DATABASE_URI = (
55
"postgresql://geonatadmin:monpassachanger@localhost:5432/geonatureatlas_test"
66
)
7+
MULTILINGUAL = True

atlas/tests/test_utils.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from atlas.utils import get_locale
2+
from atlas.tests.conftest import with_config
3+
4+
5+
@with_config(MULTILINGUAL=False, DEFAULT_LANGUAGE="fr")
6+
def test_get_locale_not_multilingual_returns_default(app):
7+
with app.test_request_context("/"):
8+
assert get_locale() == "fr"
9+
10+
11+
@with_config(
12+
MULTILINGUAL=True,
13+
AVAILABLE_LANGUAGES={"fr": {}, "en": {}},
14+
DEFAULT_LANGUAGE="fr",
15+
)
16+
def test_get_locale_session_language_valid(app, client):
17+
18+
# register a simple endpoint that returns the computed locale
19+
def _locale():
20+
return get_locale()
21+
22+
app.add_url_rule("/_locale_valid", "_locale_valid", _locale)
23+
24+
with client.session_transaction() as sess:
25+
sess["language"] = "en"
26+
27+
r = client.get("/_locale_valid")
28+
assert r.data.decode() == "en"
29+
30+
31+
@with_config(
32+
MULTILINGUAL=True,
33+
AVAILABLE_LANGUAGES={"fr": {}, "en": {}},
34+
DEFAULT_LANGUAGE="fr",
35+
)
36+
def test_get_locale_session_language_invalid_fallback_accept_language(app, client):
37+
with client.session_transaction() as sess:
38+
sess["language"] = "xx"
39+
40+
r = client.get("/", headers={"Accept-Language": "en-US,en;q=0.9"})
41+
with app.test_request_context("/", headers={"Accept-Language": "en-US,en;q=0.9"}):
42+
assert get_locale() == "en"
43+
44+
45+
@with_config(
46+
MULTILINGUAL=True,
47+
AVAILABLE_LANGUAGES={"fr": {}, "en": {}},
48+
DEFAULT_LANGUAGE="fr",
49+
)
50+
def test_get_locale_no_session_no_match_returns_default(app, client):
51+
r = client.get("/", headers={"Accept-Language": "es"})
52+
with app.test_request_context("/", headers={"Accept-Language": "es"}):
53+
assert get_locale() == "fr"

0 commit comments

Comments
 (0)