Files
odoo_source/addons/website/tests/test_lang_url.py
T
Romain Derie 9b3de32478 [FIX] http_routing, website: fix frontend lang in http dispatcher
This commit fixes 2 issues, both coming from a misbehavior in
`get_nearest_lang()`:
1. Anyone could reach the website in a lang available in backend but not in
   frontend. Eg, french is activated but not a website lang, going to `/fr`
   would show the page in french.
2. As a logged in user coming from backend in a lang not available in frontend
   (has request.lang set to that lang), the website would show a 500 error page
   since it would not filter out the current request lang.

Both these issues are fixed here by ensuring langs are filtered out if they do
not belong to the frontend (website langs).

Step to reproduce (bug 1):
  - Install french in backend lang (not on website)
  - Visit `127.0.0.X/fr_FR`, the frontend will be displayed in french even if
    it not a lang available in frontend.

Step to reproduce (bug 2):
  - Install french on frontend and remove english from frontend
  - Navigate to the backend /web
  - Navigate to frontend, it will crash

Fixes #40572 and fixes #40078

closes odoo/odoo#41146

X-original-commit: 4bfba037fbbf34c178abd532f7bf52b94ae40b27
Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
2019-12-02 08:31:26 +00:00

63 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.http_routing.models.ir_http import url_lang
from odoo.addons.website.tools import MockRequest
from odoo.tests import HttpCase, tagged
@tagged('-at_install', 'post_install')
class TestLangUrl(HttpCase):
def setUp(self):
super(TestLangUrl, self).setUp()
# Simulate multi lang without loading translations
self.website = self.env.ref('website.default_website')
self.lang_fr = self.env['res.lang']._activate_lang('fr_FR')
self.lang_fr.write({'url_code': 'fr'})
self.website.language_ids = self.env.ref('base.lang_en') + self.lang_fr
self.website.default_lang_id = self.env.ref('base.lang_en')
def test_01_url_lang(self):
with MockRequest(self.env, website=self.website):
self.assertEqual(url_lang('', '[lang]'), '/[lang]/hello/', "`[lang]` is used to be replaced in the url_return after installing a language, it should not be replaced or removed.")
def test_02_url_redirect(self):
url = '/fr_WHATEVER/contactus'
r = self.url_open(url)
self.assertEqual(r.status_code, 200)
self.assertTrue(r.url.endswith('/fr/contactus'), "fr_WHATEVER should be forwarded to 'fr_FR' lang as closest match")
url = '/fr_FR/contactus'
r = self.url_open(url)
self.assertEqual(r.status_code, 200)
self.assertTrue(r.url.endswith('/fr/contactus'), "lang in url should use url_code ('fr' in this case)")
def test_03_url_cook_lang_not_available(self):
""" An activated res.lang should not be displayed in the frontend if not a website lang. """
self.website.language_ids = self.env.ref('base.lang_en')
r = self.url_open('/fr/contactus')
self.assertTrue('lang="en-US"' in r.text, "french should not be displayed as not a frontend lang")
def test_04_url_cook_lang_not_available(self):
""" `nearest_lang` should filter out lang not available in frontend.
Eg: 1. go in backend in english -> request.context['lang'] = `en_US`
2. go in frontend, the request.context['lang'] is passed through
`nearest_lang` which should not return english. More then a
misbehavior it will crash in website language selector template.
"""
# 1. Load backend
self.authenticate('admin', 'admin')
r = self.url_open('/web')
self.assertTrue('"lang": "en_US"' in r.text, "ensure english was loaded")
# 2. Remove en_US from frontend
self.website.language_ids = self.lang_fr
self.website.default_lang_id = self.lang_fr
# 3. Ensure visiting /contactus do not crash
url = '/contactus'
r = self.url_open(url)
self.assertEqual(r.status_code, 200)
self.assertTrue('lang="fr-FR"' in r.text, "Ensure contactus did not soft crash + loaded in correct lang")