Parcourir la source

test: increased test coverage

Mikael Koli il y a 4 ans
Parent
commit
36a6323878
2 fichiers modifiés avec 82 ajouts et 2 suppressions
  1. 50 1
      redmail/test/email/test_inline_media.py
  2. 32 1
      redmail/test/email/test_template.py

+ 50 - 1
redmail/test/email/test_inline_media.py

@@ -105,6 +105,35 @@ def test_with_image_obj(get_image_obj):
         'Content-Type': 'multipart/alternative'
     } == headers
 
+def test_with_image_error():
+    sender = EmailSender(host=None, port=1234)
+    with pytest.raises(ValueError):
+        msg = sender.get_message(
+            sender="me@gmail.com",
+            receivers="you@gmail.com",
+            subject="Some news",
+            html='<h1>Hi,</h1> Nice to meet you. Look at this: <img src="{{ my_image }}">',
+            body_images={"my_image": "this is invalid"}
+        )
+
+    invalid_type_obj = type("TempClass", (), {})()
+    with pytest.raises(TypeError):
+        msg = sender.get_message(
+            sender="me@gmail.com",
+            receivers="you@gmail.com",
+            subject="Some news",
+            html='<h1>Hi,</h1> Nice to meet you. Look at this: <img src="{{ my_image }}">',
+            body_images={"my_image": invalid_type_obj}
+        )
+
+    with pytest.raises(KeyError):
+        msg = sender.get_message(
+            sender="me@gmail.com",
+            receivers="you@gmail.com",
+            subject="Some news",
+            html='<h1>Hi,</h1> Nice to meet you. Look at this:>',
+            body_images={"my_image": {}}
+        )
 
 
 @pytest.mark.parametrize(
@@ -176,4 +205,24 @@ def test_with_html_table_no_error(get_df, tmpdir):
 
     # TODO: Test the HTML is as required
 
-    assert html
+    assert html
+
+
+def test_embed_tables_pandas_missing():
+    sender = EmailSender(host=None, port=1234)
+
+    from redmail.email import body
+    pd_mdl = body.pd # This may be already None if env does not have Pandas
+    try:
+        # src uses this to reference Pandas (if missing --> None)
+        body.pd = None
+        with pytest.raises(ImportError):
+            msg = sender.get_message(
+                sender="me@gmail.com",
+                receivers="you@gmail.com",
+                subject="Some news",
+                html='The table {{my_table}}',
+                body_tables={"my_table": [{"col1": 1, "col2": "a"}, {"col1": 2, "col2": "b"}]}
+            )
+    finally:
+        body.pd = pd_mdl

+ 32 - 1
redmail/test/email/test_template.py

@@ -45,4 +45,35 @@ def test_template(tmpdir):
     html = remove_email_extra(msg.get_payload()[1].get_payload())
 
     assert expected_html == html
-    assert expected_text == text
+    assert expected_text == text
+
+def test_body_and_template_error(tmpdir):
+
+    html_templates = tmpdir.mkdir("html_tmpl")
+    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>""")
+
+    text_templates = tmpdir.mkdir("text_tmpl")
+    text_templates.join("example.txt").write("""Hi {{ friend }}, \nhave you checked this open source project '{{ project_name }}'? \n- {{ sender.full_name }}""")
+
+    sender = EmailSender(host="localhost", port=0)
+    sender.set_template_paths(
+        html=str(html_templates),
+        text=str(text_templates),
+    )
+
+    with pytest.raises(ValueError):
+        msg = sender.get_message(
+            sender="me@gmail.com",
+            receivers="you@gmail.com",
+            subject="Some news",
+            html='This is some body',
+            html_template="example.html"
+        )
+    with pytest.raises(ValueError):
+        msg = sender.get_message(
+            sender="me@gmail.com",
+            receivers="you@gmail.com",
+            subject="Some news",
+            text='This is some body',
+            text_template="example.txt"
+        )