| 1 |
# Cherrypy-foundation |
| 2 |
# Copyright (C) 2026 IKUS Software |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
import importlib |
| 17 |
from datetime import datetime, timezone |
| 18 |
from unittest import skipUnless |
| 19 |
|
| 20 |
import cherrypy |
| 21 |
from cherrypy.test import helper |
| 22 |
from parameterized import parameterized |
| 23 |
|
| 24 |
from cherrypy_foundation.components import StaticMiddleware |
| 25 |
from cherrypy_foundation.tests import SeleniumUnitTest |
| 26 |
from cherrypy_foundation.url import url_for |
| 27 |
|
| 28 |
from .. import i18n # noqa |
| 29 |
from .. import jinja2 # noqa |
| 30 |
|
| 31 |
HAS_JINJAX = importlib.util.find_spec("jinjax") is not None |
| 32 |
|
| 33 |
env = cherrypy.tools.jinja2.create_env( |
| 34 |
package_name=__package__, |
| 35 |
globals={ |
| 36 |
'const1': 'STATIC VALUE', |
| 37 |
}, |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
def extra_processor(): |
| 42 |
return {'var2': 'bar'} |
| 43 |
|
| 44 |
|
| 45 |
@cherrypy.tools.i18n(on=False) |
| 46 |
@cherrypy.tools.sessions(on=False) |
| 47 |
class Static: |
| 48 |
|
| 49 |
components = StaticMiddleware() |
| 50 |
|
| 51 |
|
| 52 |
@cherrypy.tools.jinja2(on=False, env=env, extra_processor=extra_processor) |
| 53 |
class Root: |
| 54 |
|
| 55 |
static = Static() |
| 56 |
|
| 57 |
@cherrypy.expose |
| 58 |
@cherrypy.tools.jinja2(template='test_jinja2.html') |
| 59 |
def index(self): |
| 60 |
return {'var1': 'test-jinja2'} |
| 61 |
|
| 62 |
@cherrypy.expose |
| 63 |
@cherrypy.tools.jinja2(template='test_jinjax.html') |
| 64 |
def jinjax(self): |
| 65 |
return {'var1': 'test-jinjax'} |
| 66 |
|
| 67 |
@cherrypy.expose |
| 68 |
@cherrypy.tools.jinja2(template='test_jinjax_i18n.html') |
| 69 |
@cherrypy.tools.i18n( |
| 70 |
default='fr', |
| 71 |
default_timezone='America/Toronto', |
| 72 |
mo_dir=importlib.resources.files(__package__) / 'locales', |
| 73 |
domain='messages', |
| 74 |
cookie_name='locale', # For LocaleSelection |
| 75 |
) |
| 76 |
def localized(self): |
| 77 |
return { |
| 78 |
'my_datetime': datetime(year=2025, month=11, day=26, hour=11, minute=16, tzinfo=timezone.utc), |
| 79 |
'my_date': datetime(year=2025, month=12, day=22, hour=14, minute=8, tzinfo=timezone.utc), |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
class Jinja2Test(helper.CPWebCase, SeleniumUnitTest): |
| 84 |
default_lang = None |
| 85 |
interactive = False |
| 86 |
|
| 87 |
@classmethod |
| 88 |
def setup_server(cls): |
| 89 |
cherrypy.tree.mount(Root(), '/') |
| 90 |
|
| 91 |
def test_get_page(self): |
| 92 |
# Given a page render using jinja2 |
| 93 |
# When querying the page |
| 94 |
self.getPage("/") |
| 95 |
# Then the page return without error |
| 96 |
self.assertStatus(200) |
| 97 |
# Then the page is render dynamically using page context |
| 98 |
self.assertInBody('test-jinja2') |
| 99 |
self.assertInBody('bar') |
| 100 |
self.assertInBody('STATIC VALUE') |
| 101 |
|
| 102 |
@skipUnless(HAS_JINJAX, reason='Required jinjax') |
| 103 |
def test_get_page_jinjax(self): |
| 104 |
# Given a page render using jinjax |
| 105 |
# When querying the page |
| 106 |
self.getPage("/jinjax") |
| 107 |
# Then the page return without error |
| 108 |
self.assertStatus(200) |
| 109 |
# Then the page is render dynamically using page context |
| 110 |
self.assertInBody('<a class="btn btn-primary" href="http://example.com">foo</a>') |
| 111 |
|
| 112 |
@skipUnless(HAS_JINJAX, reason='Required jinjax') |
| 113 |
@parameterized.expand( |
| 114 |
[ |
| 115 |
('server_default', {}, 'fr'), |
| 116 |
('accept_lang_fr', {'Accept-Language': 'fr'}, 'fr'), |
| 117 |
('accept_lang_en', {'Accept-Language': 'en'}, 'en'), |
| 118 |
] |
| 119 |
) |
| 120 |
def test_get_page_i18n(self, _name, headers, expected_lang): |
| 121 |
# Given a localized page render with jinja2 |
| 122 |
# When querying the page |
| 123 |
self.getPage("/localized", headers=list(headers.items())) |
| 124 |
# Then the page return without error |
| 125 |
self.assertStatus(200) |
| 126 |
# Then the page is render dynamically using page context |
| 127 |
self.assertInBody('<script src="/static/components/LocaleSelection.js"></script>') |
| 128 |
if expected_lang == 'fr': |
| 129 |
self.assertInBody('lang="fr"') |
| 130 |
self.assertInBody('français') |
| 131 |
self.assertInBody('Du texte à traduire') |
| 132 |
self.assertInBody('mercredi 26 novembre 2025, 06:16:00 heure normale de l’Est nord-américain') |
| 133 |
self.assertInBody('lundi, décembre 22, 2025') |
| 134 |
else: |
| 135 |
self.assertInBody('lang="en"') |
| 136 |
self.assertInBody('English') |
| 137 |
self.assertInBody('Some text to translate') |
| 138 |
self.assertInBody('Wednesday, November 26, 2025, 6:16:00\u202fAM Eastern Standard Time') |
| 139 |
self.assertInBody('Monday, December 22, 2025') |
| 140 |
|
| 141 |
@skipUnless(HAS_JINJAX, reason='Required jinjax') |
| 142 |
def test_get_page_i18n_selenium(self): |
| 143 |
# Given a localized page render with jinja2 |
| 144 |
with self.selenium() as driver: |
| 145 |
# When querying the page |
| 146 |
driver.get(url_for('localized')) |
| 147 |
# Then page load without error in english (enforced chronium lang) |
| 148 |
self.assertFalse(driver.get_log('browser')) |
| 149 |
self.assertEqual('en_US', driver.find_element('css selector', 'html').get_attribute('lang')) |
| 150 |
# When user select a language |
| 151 |
btn = driver.find_element('css selector', 'button[data-locale=fr]') |
| 152 |
btn.click() |
| 153 |
# Then page is reloaded with in French. |
| 154 |
self.assertEqual('fr', driver.find_element('css selector', 'html').get_attribute('lang')) |