fix: apply webapp theme accent in emails and deeplinks

This commit is contained in:
3252a8
2026-06-04 23:22:53 +03:00
parent 090c88603e
commit 77b124d61e
7 changed files with 247 additions and 22 deletions
+37 -2
View File
@@ -20,6 +20,7 @@ from bot.services.email_templates import (
render_support_user_reply_admin,
render_user_notification,
)
from config.webapp_themes_config import WebappThemesConfig
REPO_ROOT = Path(__file__).resolve().parents[1]
EMAIL_KEY_RE = re.compile(r"""["'](?P<key>email_[a-z0-9_]+)["']""")
@@ -31,13 +32,19 @@ EMAIL_KEY_ASSIGNMENT_HINTS = (
)
def _settings(default_language: str = "ru"):
def _settings(
default_language: str = "ru",
*,
webapp_themes_catalog: WebappThemesConfig | None = None,
primary_color: str = "#00fe7a",
):
return SimpleNamespace(
DEFAULT_LANGUAGE=default_language,
EMAIL_CODE_TTL_SECONDS=600,
WEBAPP_LOGO_URL="",
WEBAPP_PRIMARY_COLOR="#00fe7a",
WEBAPP_PRIMARY_COLOR=primary_color,
WEBAPP_TITLE="Mini Shop",
webapp_themes_catalog=webapp_themes_catalog,
)
@@ -256,6 +263,34 @@ def test_all_email_template_variants_render_without_raw_locale_keys():
_assert_content_is_localized(content, language)
def test_email_templates_use_default_webapp_theme_accent():
catalog = WebappThemesConfig(
default_theme="custom",
themes=[
{
"key": "custom",
"enabled": True,
"default": True,
"tokens": {"color_scheme": "dark", "accent": "#123abc"},
}
],
)
settings = _settings("en", webapp_themes_catalog=catalog, primary_color="#00fe7a")
content = render_login_code(
settings,
code="123456",
language_code="en",
magic_link="https://app.example.com/magic",
purpose="login",
i18n=_i18n("en"),
)
assert "color:#123abc" in content.html
assert 'bgcolor="#123abc"' in content.html
assert "#00fe7a" not in content.html
def test_uploaded_webapp_logo_is_embedded_inline(tmp_path, monkeypatch):
uploads_dir = tmp_path / "uploads"
uploads_dir.mkdir()
+34
View File
@@ -11,6 +11,7 @@ from aiohttp.test_utils import make_mocked_request
from bot.app.web import admin_api, subscription_webapp
from bot.app.web.admin_api_impl import auth as admin_auth_routes
from bot.app.web.webapp_auth import create_webapp_session_token
from config.webapp_themes_config import WebappThemesConfig
REPO_ROOT = Path(__file__).resolve().parents[1]
@@ -332,6 +333,39 @@ class WebAppRouteContractTests(unittest.TestCase):
(REPO_ROOT / "backend/bot/app/web/templates/open_app_gateway.html").is_file()
)
def test_app_deeplink_gateway_uses_webapp_theme_accent(self):
catalog = WebappThemesConfig(
default_theme="custom",
themes=[
{
"key": "custom",
"enabled": True,
"default": True,
"tokens": {"color_scheme": "dark", "accent": "#123abc"},
}
],
)
request = _Request(
app={
"settings": SimpleNamespace(
WEBAPP_ENABLED=True,
WEBAPP_TITLE="/minishop",
DEFAULT_LANGUAGE="en",
WEBAPP_PRIMARY_COLOR="#00fe7a",
webapp_themes_catalog=catalog,
)
}
)
request["csp_nonce"] = "nonce-value"
response = asyncio.run(subscription_webapp.app_deeplink_route(request))
self.assertEqual(response.status, 200)
self.assertIn('id="webapp-initial-theme"', response.text)
self.assertIn("--accent:#123abc", response.text)
self.assertIn("background: var(--accent)", response.text)
self.assertNotIn("background: #14b86f;", response.text)
def test_app_launch_i18n_keys_are_available_to_webapp_bootstrap(self):
required_keys = {
"wa_app_launch_title",
+39
View File
@@ -8,6 +8,7 @@ from config.webapp_themes_config import (
apply_webapp_theme_env_overrides,
builtin_webapp_themes_config,
default_webapp_theme_descriptors,
effective_webapp_theme_accent,
ensure_webapp_core_themes,
load_webapp_theme_dir,
public_themes_catalog_payload,
@@ -281,6 +282,44 @@ class WebappThemesConfigTests(unittest.TestCase):
self.assertTrue(win95["use_in_admin"])
self.assertNotIn("accent", win95["tokens"])
def test_effective_accent_uses_default_theme_token(self):
cfg = WebappThemesConfig(
default_theme="custom",
themes=[
{
"key": "custom",
"enabled": True,
"default": True,
"tokens": {"color_scheme": "dark", "accent": "#123abc"},
}
],
)
self.assertEqual(effective_webapp_theme_accent(cfg, "#00fe7a"), "#123abc")
def test_effective_accent_can_use_preview_theme_token(self):
cfg = WebappThemesConfig(
default_theme="dark",
themes=[
{
"key": "dark",
"enabled": True,
"default": True,
"tokens": {"color_scheme": "dark", "accent": "#123abc"},
},
{
"key": "neon",
"enabled": True,
"tokens": {"color_scheme": "dark", "accent": "#ff33aa"},
},
],
)
self.assertEqual(
effective_webapp_theme_accent(cfg, "#00fe7a", theme_key="neon"),
"#ff33aa",
)
def test_theme_accent_is_normalized_to_hex(self):
cfg = WebappThemesConfig(
default_theme="custom",