Package: bebleo-smtpd-fixture
pytest SMTPD FixtureA simple SMTP server based onaiosmtpdfor use as a fixture with pytest that supports encryption and authentication. All this does is receives messages and appends them to a list as anemail.Message.This fisture is intended to address use-cases where to test an application that sends an email it needs to be intercepted for subsequent processing. For example, sending an email with a code for password reset or two-factor authentication. This fixture allows a test to trigger the email being sent, ensure that it's sent, and read the code from the email.InstallingInstall using pip:pipinstallbebleo-smtpd-fixtureOr, if you're using setuptools it can be included in thetests_requiresection of asetup.pyfile:setup(...tests_require=["pytest","bebleo-smtpd-fixture",],)UsingTo use thesmtpdfixture import it into your test file and then use it like any other pytest fixture. for example:# test_mail.pyfromsmtplibimportSMTPfrombebleo_smtpd_fixtureimportsmtpddeftest_sendmail(smtpd):from_addr="from.addr@example.org"to_addrs="to.addr@example.org"msg=f"From:{from_addr}\r\nTo:{to_addrs}\r\nSubject: Foo\r\n\r\nFoo bar"withSMTP(smtpd.hostname,smtpd.port)asclient:client.sendmail(from_addr,to_addrs,msg)assertlen(smtpd.messages)==1To use STARTTLS:fromsmtplibimportSMTPimportpytestfrombebleo_smtpd_fixtureimportsmtpd@pytest.fixturedefmock_use_starttls(monkeypatch):monkeypatch.setenv('SMTPD_USE_STARTTLS','True')deftest_sendmail(mock_use_starttls,smtpd):from_="from.addr@example.org"to_="to.addr@example.org"msg=f"From:{from_}\r\nTo:{to_}\r\nSubject: Foo\r\n\r\nFoo bar"withSMTP(smtpd.hostname,smtpd.port)asclient:client.starttls()# Note that you need to call starttls first.client.sendmail(from_addr,to_addrs,msg)assertlen(smtpd.messages)==1The certificates included with the fixture will work for addresses localhost, localhost.localdomain, 127.0.0.1, 0.0.0.1, ::1. If using other addresses the key (key.pem) and certificate (cert.pem) must be in a location specified underSMTP_SSL_CERTS_PATH.ConfigurationConfiguration can be handled through environment variables:VariableDefaultDescriptionSMTPD_HOST127.0.0.1The hostname that the fixture will listen on.SMTPD_PORT8025The port that the fixture will listen on.SMPTD_USERNAMEuserSMTPD_PASSWORDpasswordSMTPD_USE_SSLFalseWhether the fixture should use fixed TLS/SSL for transactions. If using smtplib requires thatSMTP_SSLbe used instead ofSMTP.SMTPD_USE_STARTTLSFalseWhether the fixture should use StartTLS to encrypt the connections. If usingsmptlibrequires that theSMTP.starttls()be called before other commands are issued.SMTPD_ENFORCE_AUTHFalseIf set to true then the fixture refuses MAIL, RCPT, DATA commands until authentication is completed.SMTPD_SSL_CERTS_PATH\certs\The path to the key and certificate for encrypted communication.If these variables are included in a.envfile they'll be loaded automatically.Known IssuesFirewalls may interfere with the operation of the smtp server.Authenticating with LOGIN and PLAIN mechanisms fails over TLS/SSL, but works with STARTTLS.Issue #10Currently no support for termination through signals.Issue #4Key and certificate for encrypted communications must be called key.pem and cert.pem respectively.Issue #15©2020, Written with ☕ and ❤ in Montreal, QC