test_send.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from email.message import EmailMessage
  2. import pytest
  3. from redmail import EmailSender, send_email
  4. class MockServer:
  5. def __init__(self, host, port):
  6. self.host = host
  7. self.port = port
  8. self.is_login = False
  9. def starttls(self):
  10. return
  11. def login(self, user=None, password=None):
  12. self.is_login = True
  13. return
  14. def send_message(self, msg):
  15. return
  16. def quit(self):
  17. return
  18. def test_send():
  19. email = EmailSender(host="localhost", port=0, cls_smtp=MockServer)
  20. assert email.connection is None
  21. msg = email.send(
  22. subject="An example",
  23. receivers=['koli.mikael@example.com']
  24. )
  25. assert isinstance(msg, EmailMessage)
  26. assert email.connection is None
  27. def test_send_multi():
  28. email = EmailSender(host="localhost", port=0, cls_smtp=MockServer)
  29. assert email.connection is None
  30. with email:
  31. assert email.connection is not None
  32. msg = email.send(
  33. subject="An example",
  34. receivers=['koli.mikael@example.com']
  35. )
  36. assert isinstance(msg, EmailMessage)
  37. assert email.connection is not None
  38. msg = email.send(
  39. subject="An example",
  40. receivers=['koli.mikael@example.com']
  41. )
  42. assert isinstance(msg, EmailMessage)
  43. assert email.connection is not None
  44. assert email.connection is None
  45. def test_send_function():
  46. # This should fail but we test everything else goes through
  47. with pytest.raises(ConnectionRefusedError):
  48. send_email(host="localhost", port=0, subject="An example")