Skip to content

Commit

Permalink
Feat: upgrade email template tester
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Jan 15, 2025
1 parent b5e4097 commit 4627e5c
Showing 1 changed file with 23 additions and 58 deletions.
81 changes: 23 additions & 58 deletions tests/template_tester.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

0 comments on commit 4627e5c

Please sign in to comment.