|
| 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" |
0 commit comments