test_smtp.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import smtplib
  2. import pytest
  3. try: # python 3
  4. from email.mime.text import MIMEText
  5. except ImportError: # python 2?
  6. from email.MIMEText import MIMEText
  7. from pytest_localserver import plugin
  8. smtp = pytest.importorskip("pytest_localserver.smtp")
  9. def send_plain_email(to, from_, subject, txt, server=("localhost", 25)):
  10. """
  11. Sends a simple plain text message via SMTP.
  12. """
  13. if type(to) in (tuple, list):
  14. to = ", ".join(to)
  15. # Create a text/plain message
  16. msg = MIMEText(txt)
  17. msg["Subject"] = subject
  18. msg["From"] = from_
  19. msg["To"] = to
  20. host, port = server[:2]
  21. server = smtplib.SMTP(host, port)
  22. server.set_debuglevel(1)
  23. server.sendmail(from_, to, msg.as_string())
  24. server.quit()
  25. # define test fixture here again in order to run tests without having to
  26. # install the plugin anew every single time
  27. smtpserver = plugin.smtpserver
  28. def test_smtpserver_funcarg(smtpserver):
  29. assert isinstance(smtpserver, smtp.Server)
  30. assert smtpserver.is_alive()
  31. assert smtpserver.accepting and smtpserver.addr
  32. def test_smtpserver_addr(smtpserver):
  33. host, port = smtpserver.addr
  34. assert isinstance(host, str)
  35. assert isinstance(port, int)
  36. assert port > 0
  37. def test_server_is_killed(smtpserver):
  38. assert smtpserver.is_alive()
  39. smtpserver.stop()
  40. assert not smtpserver.is_alive()
  41. def test_server_is_deleted(smtpserver):
  42. assert smtpserver.is_alive()
  43. smtpserver.__del__() # need to call magic method here!
  44. assert not smtpserver.is_alive()
  45. def test_smtpserver_has_empty_outbox_at_startup(smtpserver):
  46. assert len(smtpserver.outbox) == 0
  47. def test_send_email(smtpserver):
  48. # send one e-mail
  49. send_plain_email(
  50. "alice@example.com",
  51. "webmaster@example.com",
  52. "Your e-mail is getting there",
  53. "Seems like this test actually works!",
  54. smtpserver.addr,
  55. )
  56. msg = smtpserver.outbox[-1]
  57. assert msg["To"] == "alice@example.com"
  58. assert msg["From"] == "webmaster@example.com"
  59. assert msg["Subject"] == "Your e-mail is getting there"
  60. assert msg.details.rcpttos == ["alice@example.com"]
  61. assert msg.details.peer
  62. assert msg.details.mailfrom
  63. # send another e-mail
  64. send_plain_email(
  65. "bob@example.com",
  66. "webmaster@example.com",
  67. "His e-mail too",
  68. "Seems like this test actually works!",
  69. smtpserver.addr,
  70. )
  71. msg = smtpserver.outbox[-1]
  72. assert msg["To"] == "bob@example.com"
  73. assert msg["From"] == "webmaster@example.com"
  74. assert msg["Subject"] == "His e-mail too"
  75. # two mails are now in outbox
  76. assert len(smtpserver.outbox) == 2