1
0

test_template.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import sys
  2. from textwrap import dedent
  3. from jinja2 import Environment
  4. from redmail import EmailSender
  5. import pytest
  6. from convert import remove_email_extra, remove_email_content_id, prune_generated_headers
  7. IS_PY37 = sys.version_info < (3, 8)
  8. def test_template(tmpdir):
  9. html_templates = tmpdir.mkdir("html_tmpl")
  10. html_templates.join("example.html").write("""<h1>Hi {{ friend }},</h1><p>have you checked this open source project '{{ project_name }}'?</p><p>- {{ sender.full_name }}</p>""")
  11. expected_html = f"<h1>Hi Jack,</h1><p>have you checked this open source project 'RedMail'?</p><p>- Me</p>\n"
  12. text_templates = tmpdir.mkdir("text_tmpl")
  13. text_templates.join("example.txt").write("""Hi {{ friend }}, \nhave you checked this open source project '{{ project_name }}'? \n- {{ sender.full_name }}""")
  14. expected_text = f"Hi Jack, \nhave you checked this open source project 'RedMail'? \n- Me\n"
  15. html_tables = tmpdir.mkdir("html_table_tmpl")
  16. html_tables.join("modest.html").write("""{{ df.to_html() }}""")
  17. text_tables = tmpdir.mkdir("text_table_tmpl")
  18. text_tables.join("pandas.txt").write("""{{ df.to_html() }}""")
  19. sender = EmailSender(host=None, port=1234)
  20. sender.set_template_paths(
  21. html=str(html_templates),
  22. text=str(text_templates),
  23. html_table=str(html_tables),
  24. text_table=str(text_tables),
  25. )
  26. msg = sender.get_message(
  27. sender="me@gmail.com",
  28. receivers=["you@gmail.com"],
  29. subject="Some news",
  30. html_template='example.html',
  31. text_template='example.txt',
  32. body_params={"friend": "Jack", 'project_name': 'RedMail'}
  33. )
  34. assert "multipart/mixed" == msg.get_content_type()
  35. alternative = msg.get_payload()[0]
  36. text_part, html_part = alternative.get_payload()
  37. text = remove_email_extra(text_part.get_payload())
  38. html = remove_email_extra(html_part.get_payload())
  39. assert expected_html == html
  40. assert expected_text == text
  41. def test_jinja_env(tmpdir):
  42. sender = EmailSender(host=None, port=1234)
  43. if IS_PY37:
  44. # CI has FQDN that has UTF-8 chars and goes to new line
  45. # for Python <=3.7. We set a realistic looking domain
  46. # name for easier testing
  47. sender.domain = "REDMAIL-1234.mail.com"
  48. env = Environment()
  49. env.globals["my_param"] = "<a value>"
  50. sender.templates_text = env
  51. sender.templates_html = env
  52. msg = sender.get_message(
  53. sender="me@example.com",
  54. receivers=["you@example.com"],
  55. subject="Some news",
  56. text="A param: {{ my_param }}",
  57. html="<h1>A param: {{ my_param }}</h1>"
  58. )
  59. content = str(msg)
  60. content = prune_generated_headers(content)
  61. content = remove_email_content_id(content)
  62. assert content == dedent("""
  63. From: me@example.com
  64. Subject: Some news
  65. To: you@example.com
  66. Message-ID: <<message_id>>
  67. Date: <date>
  68. MIME-Version: 1.0
  69. Content-Type: multipart/mixed; boundary="===============<ID>=="
  70. --===============<ID>==
  71. Content-Type: multipart/alternative;
  72. boundary="===============<ID>=="
  73. --===============<ID>==
  74. Content-Type: text/plain; charset="utf-8"
  75. Content-Transfer-Encoding: 7bit
  76. A param: <a value>
  77. --===============<ID>==
  78. Content-Type: text/html; charset="utf-8"
  79. Content-Transfer-Encoding: 7bit
  80. MIME-Version: 1.0
  81. <h1>A param: <a value></h1>
  82. --===============<ID>==--
  83. --===============<ID>==--
  84. """)[1:]
  85. def test_body_and_template_error(tmpdir):
  86. html_templates = tmpdir.mkdir("html_tmpl")
  87. html_templates.join("example.html").write("""<h1>Hi {{ friend }},</h1><p>have you checked this open source project '{{ project_name }}'?</p><p>- {{ sender.full_name }}</p>""")
  88. text_templates = tmpdir.mkdir("text_tmpl")
  89. text_templates.join("example.txt").write("""Hi {{ friend }}, \nhave you checked this open source project '{{ project_name }}'? \n- {{ sender.full_name }}""")
  90. sender = EmailSender(host="localhost", port=0)
  91. sender.set_template_paths(
  92. html=str(html_templates),
  93. text=str(text_templates),
  94. )
  95. with pytest.raises(ValueError):
  96. msg = sender.get_message(
  97. sender="me@gmail.com",
  98. receivers="you@gmail.com",
  99. subject="Some news",
  100. html='This is some body',
  101. html_template="example.html"
  102. )
  103. with pytest.raises(ValueError):
  104. msg = sender.get_message(
  105. sender="me@gmail.com",
  106. receivers="you@gmail.com",
  107. subject="Some news",
  108. text='This is some body',
  109. text_template="example.txt"
  110. )