Browse Source

test: increased test coverage

Mikael Koli 4 years ago
parent
commit
a350ca1667
3 changed files with 96 additions and 3 deletions
  1. 11 1
      redmail/test/email/test_body.py
  2. 18 0
      redmail/test/email/test_send.py
  3. 67 2
      redmail/test/test_models.py

+ 11 - 1
redmail/test/email/test_body.py

@@ -173,4 +173,14 @@ def test_set_defaults():
         'Content-Type': 'text/plain; charset="utf-8"', 
         'Content-Transfer-Encoding': '7bit', 
         'MIME-Version': '1.0'
-    } == dict(msg.items())
+    } == dict(msg.items())
+
+def test_cc_bcc():
+    email = EmailSender(host=None, port=1234)
+    msg = email.get_message(sender="me@example.com", subject="Something", cc=['you@example.com'], bcc=['he@example.com', 'she@example.com'])
+    assert dict(msg.items()) == {'from': 'me@example.com', 'subject': 'Something', 'cc': 'you@example.com', 'bcc': 'he@example.com, she@example.com'}
+
+def test_missing_subject():
+    email = EmailSender(host=None, port=1234)
+    with pytest.raises(ValueError):
+        email.get_message(sender="me@example.com", receivers=['you@example.com'])

+ 18 - 0
redmail/test/email/test_send.py

@@ -0,0 +1,18 @@
+
+import pytest
+
+from redmail import EmailSender, send_email
+
+def test_send():
+    email = EmailSender(host="localhost", port=0)
+    # This should fail but we test everything else goes through
+    with pytest.raises(ConnectionRefusedError):
+        email.send(
+            subject="An example",
+            receivers=['koli.mikael@example.com']
+        )
+
+def test_send():
+    email = EmailSender(host="localhost", port=0)
+    # This should fail but we test everything else goes through
+    send_email(host="localhost", port=0, )

+ 67 - 2
redmail/test/test_models.py

@@ -1,5 +1,5 @@
 
-from redmail.models import EmailAddress
+from redmail.models import EmailAddress, Error
 
 import pytest
 
@@ -17,4 +17,69 @@ def test_address(addr, expected):
     assert expected['last_name'] == address.last_name
     assert expected['full_name'] == address.full_name
     assert expected['organization'] == address.organization
-    assert str(address) == addr
+    assert str(address) == addr
+
+@pytest.mark.parametrize("exc_as", ['current stack', 'passed exception'])
+@pytest.mark.parametrize("cont_type,starts,ends",
+    [
+        pytest.param(
+            'text', 
+            'Traceback (most recent call last):\n  File "', 
+            '\n    raise RuntimeError("deliberate error")\n\nRuntimeError: deliberate error', 
+            id="text"
+        ),
+        pytest.param(
+            'html', 
+            '<div class="error">\n                <h4 class="header">Traceback (most recent call last):</h4>\n                <pre class="traceback"><code>  File &quot;',
+            '\n    raise RuntimeError(&quot;deliberate error&quot;)</code></pre>\n                <div class="exception">\n                    <span class="exception-type">RuntimeError</span>: <span class="exception-value">deliberate error</span>\n                </div>\n            </div>',
+            id="html"
+        ),
+        pytest.param(
+            'html-inline', 
+            '\n        <div>\n            <h4>Traceback (most recent call last):</h4>\n            <pre><code>  File &quot;', 
+            '\nraise RuntimeError(&quot;deliberate error&quot;)</code></pre>\n            <span style="color: red; font-weight: bold">deliberate error</span>: <span>RuntimeError</span>\n        </div>', 
+            id="html-inline"
+        ),
+    ]
+)
+def test_error(cont_type, starts, ends, exc_as):
+    try:
+        raise RuntimeError("deliberate error")
+    except RuntimeError as exc:
+        if exc_as == 'current stack':
+            err = Error(content_type=cont_type)
+            exc_type = err.exception_type
+            exc_value = err.exception_value
+            tb = err.traceback
+            string = str(err)
+            assert bool(err)
+        e = exc
+    if exc_as == 'passed exception':
+        err = Error(content_type=cont_type, exception=e)
+        exc_type = err.exception_type
+        exc_value = err.exception_value
+        tb = err.traceback
+        string = str(err)
+        assert bool(err)
+    
+    assert exc_type == 'RuntimeError'
+    assert exc_value == 'deliberate error'
+    assert len(tb) == 1
+    assert tb[0].startswith('  File "')
+    assert tb[0].endswith(', in test_error\n    raise RuntimeError("deliberate error")\n')
+
+    assert string.startswith(starts)
+    assert string.endswith(ends)
+    
+def test_error_no_error():
+    err = Error()
+    assert err.exception_type is None
+    assert err.exception_value is None
+    assert err.traceback is None
+
+    assert not bool(err)
+
+def test_error_invalid_type():
+    err = Error("Not existing")
+    with pytest.raises(ValueError):
+        str(err)