Skip to content

Commit d92f539

Browse files
authored
Move flash-messages to unauthed include (#18087)
1 parent 8540e47 commit d92f539

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

tests/functional/manage/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_changing_password_succeeds(self, webtest, socket_enabled):
107107
change_password_form.submit().follow(status=HTTPStatus.OK)
108108

109109
# Request the JavaScript-enabled flash messages directly to get the message
110-
resp = webtest.get("/_includes/authed/flash-messages/", status=HTTPStatus.OK)
110+
resp = webtest.get("/_includes/unauthed/flash-messages/", status=HTTPStatus.OK)
111111
success_message = resp.html.find("span", {"class": "notification-bar__message"})
112112
assert success_message.text == "Password updated"
113113

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
from http import HTTPStatus
14+
15+
from warehouse.i18n import LOCALE_ATTR
16+
17+
18+
class TestLocale:
19+
def test_unauthed_user(self, webtest):
20+
"""
21+
Test that locale changes are reflected in the response
22+
"""
23+
# The default locale is set to English
24+
resp = webtest.get("/", status=HTTPStatus.OK)
25+
assert LOCALE_ATTR not in resp.headers.getall("Set-Cookie")
26+
assert resp.html.find("html").attrs["lang"] == "en"
27+
28+
# Change to a different locale
29+
resp = webtest.get(
30+
"/locale/?locale=es",
31+
params={"locale_id": "es"},
32+
status=HTTPStatus.SEE_OTHER,
33+
)
34+
# assert that the locale cookie is set in one of the cookies
35+
assert f"{LOCALE_ATTR}=es; Path=/" in resp.headers.getall("Set-Cookie")
36+
# Follow the redirect and check if the locale is set and flash notice appears
37+
next_page = resp.follow(status=HTTPStatus.OK)
38+
assert next_page.html.find("html").attrs["lang"] == "es"
39+
# Fetch the client-side includes and confirm the flash notice
40+
resp = webtest.get("/_includes/unauthed/flash-messages/", status=HTTPStatus.OK)
41+
success_message = resp.html.find("span", {"class": "notification-bar__message"})
42+
assert success_message.text != "Locale updated" # Value in Spanish, may change
43+
44+
# Switch back to English
45+
resp = webtest.get(
46+
"/locale/?locale=en",
47+
params={"locale_id": "en"},
48+
status=HTTPStatus.SEE_OTHER,
49+
)
50+
assert f"{LOCALE_ATTR}=en; Path=/" in resp.headers.getall("Set-Cookie")
51+
next_page = resp.follow(status=HTTPStatus.OK)
52+
assert next_page.html.find("html").attrs["lang"] == "en"
53+
# Fetch the client-side includes and confirm the flash notice
54+
resp = webtest.get("/_includes/unauthed/flash-messages/", status=HTTPStatus.OK)
55+
success_message = resp.html.find("span", {"class": "notification-bar__message"})
56+
assert success_message.text == "Locale updated"

tests/unit/test_routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def add_redirect_rule(*args, **kwargs):
8989
),
9090
pretend.call(
9191
"includes.flash-messages",
92-
"/_includes/authed/flash-messages/",
92+
"/_includes/unauthed/flash-messages/",
9393
domain=warehouse,
9494
),
9595
pretend.call(

warehouse/routes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def includeme(config):
8080
domain=warehouse,
8181
)
8282
config.add_route(
83-
"includes.flash-messages", "/_includes/authed/flash-messages/", domain=warehouse
83+
"includes.flash-messages",
84+
"/_includes/unauthed/flash-messages/",
85+
domain=warehouse,
8486
)
8587
config.add_route(
8688
"includes.session-notifications",

0 commit comments

Comments
 (0)