From 7f1e78faf593b0c82501a184a03a0906ffa8ff46 Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Thu, 29 Aug 2024 14:26:19 +0200 Subject: [PATCH] [IMP] test_themes: add test to detect accidental cross-theme references This commit adds a test to ensure that assets and images referenced from a theme do actually belong to that theme. task-4146886 Part-of: odoo/design-themes#889 Signed-off-by: Arthur Detroux (ard) --- test_themes/tests/__init__.py | 1 + test_themes/tests/test_theme_scope.py | 34 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test_themes/tests/test_theme_scope.py diff --git a/test_themes/tests/__init__.py b/test_themes/tests/__init__.py index d8feb8d8b..bbb1c8d27 100644 --- a/test_themes/tests/__init__.py +++ b/test_themes/tests/__init__.py @@ -6,6 +6,7 @@ from . import test_crawl from . import test_new_page_templates +from . import test_theme_scope from . import test_theme_upgrade # This test should be last. from . import test_theme_standalone diff --git a/test_themes/tests/test_theme_scope.py b/test_themes/tests/test_theme_scope.py new file mode 100644 index 000000000..ea78b866b --- /dev/null +++ b/test_themes/tests/test_theme_scope.py @@ -0,0 +1,34 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import logging + +from odoo.tests import tagged, TransactionCase + +_logger = logging.getLogger(__name__) + +@tagged('post_install', '-at_install') +class TestThemeScope(TransactionCase): + + def test_scope(self): + websites_themes = self.env['website'].get_test_themes_websites() + assets_count = 0 + attachments_count = 0 + fails = [] + for website in websites_themes: + prefix = f'{website.theme_id.name}/' + slash_prefix = f'/{website.theme_id.name}/' + theme_module = self.env['ir.module.module'].search([ + ('name', '=', website.theme_id.name), + ]) + assets = theme_module._get_module_data('ir.asset') + for asset in assets: + if not asset.path.startswith(prefix) and not asset.path.startswith(slash_prefix): + fails.append(f"Asset {asset.id} {asset.key} references outside of theme {website.theme_id.name}: {asset.path}") + assets_count += len(assets) + attachments = theme_module._get_module_data('ir.attachment') + for attachment in attachments: + if not attachment.url.startswith(slash_prefix): + fails.append(f"Attachment {attachment.id} {attachment.key} references outside of theme {website.theme_id.name}: {attachment.url}") + attachments_count += len(attachments) + _logger.info(f"Verified {assets_count} assets and {attachments_count} attachments") + self.assertFalse(fails, "\n".join(fails))