From 4627e5c510b10ea3d41ff72e3c3e5d51b078a942 Mon Sep 17 00:00:00 2001 From: Thonyk Date: Wed, 15 Jan 2025 03:16:30 +0100 Subject: [PATCH] Feat: upgrade email template tester --- tests/template_tester.py | 81 ++++++++++++---------------------------- 1 file changed, 23 insertions(+), 58 deletions(-) diff --git a/tests/template_tester.py b/tests/template_tester.py index c9213f087..720391bc0 100644 --- a/tests/template_tester.py +++ b/tests/template_tester.py @@ -1,7 +1,7 @@ if __name__ == "__main__": from pathlib import Path - from jinja2 import Environment, FileSystemLoader + from jinja2 import Environment, FileSystemLoader, meta # Please run from tests folder # Initialize environnement with templates @@ -15,60 +15,25 @@ directory = path_to_file.parents[0] / Path("jinja_test_outputs") directory.mkdir(exist_ok=True) - # Templates rendering - # URL are set to MyECL only to test that the links are working properly - template_account_exists_mail = env.get_template("account_exists_mail.html") - html_account_exists_mail = template_account_exists_mail.render() - file_account_exists_mail = directory / Path("test_account_exists_mail.html") - file_account_exists_mail.touch(exist_ok=True) - with file_account_exists_mail.open(mode="w") as f: - f.write(html_account_exists_mail) - - template_activation_mail = env.get_template("activation_mail.html") - html_activation_mail = template_activation_mail.render( - calypsso_activate_url="https://myecl.fr", - ) - file_activation_mail = directory / Path("test_activation_mail.html") - file_activation_mail.touch(exist_ok=True) - with file_activation_mail.open(mode="w") as f: - f.write(html_activation_mail) - - template_migration_mail = env.get_template("migration_mail.html") - html_migration_mail = template_migration_mail.render( - migration_link="https://myecl.fr", - ) - file_migration_mail = directory / Path("test_migration_mail.html") - file_migration_mail.touch(exist_ok=True) - with file_migration_mail.open(mode="w") as f: - f.write(html_migration_mail) - - template_migration_mail_already_used = env.get_template( - "migration_mail_already_used.html", - ) - html_migration_mail_already_used = template_migration_mail_already_used.render() - file_migration_mail_already_used = directory / Path( - "test_migration_mail_already_used.html", - ) - file_migration_mail_already_used.touch(exist_ok=True) - with file_migration_mail_already_used.open(mode="w") as f: - f.write(html_migration_mail_already_used) - - template_reset_mail = env.get_template("reset_mail.html") - html_reset_mail = template_reset_mail.render(calypsso_reset_url="https://myecl.fr") - file_reset_mail = directory / Path("test_reset_mail.html") - file_reset_mail.touch(exist_ok=True) - with file_reset_mail.open(mode="w") as f: - f.write(html_reset_mail) - - template_reset_mail_does_not_exist = env.get_template( - "reset_mail_does_not_exist.html", - ) - html_reset_mail_does_not_exist = template_reset_mail_does_not_exist.render( - calypsso_register_url="https://myecl.fr", - ) - file_reset_mail_does_not_exist = directory / Path( - "test_reset_mail_does_not_exist.html", - ) - file_reset_mail_does_not_exist.touch(exist_ok=True) - with file_reset_mail_does_not_exist.open(mode="w") as f: - f.write(html_reset_mail_does_not_exist) + for template in env.list_templates( + filter_func=lambda x: x + not in [ + "custom_mail_template.html", + "base_email.html", + "template.html", + "README.md", + "style.css", + ], + ): + template_rendered = env.get_template(template) + with Path.open(path_to_file.parents[1] / "assets/templates/" / template) as f: + template_content = f.read() + ast = env.parse(template_content) + variables = meta.find_undeclared_variables(ast) + html_rendered = template_rendered.render( + {v: f"https://myecl.fr/{v}" for v in variables}, + ) + file_rendered = directory / Path(f"test_{template}") + file_rendered.touch(exist_ok=True) + with file_rendered.open(mode="w") as f: + f.write(html_rendered)