
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
bebleo-smtpd-fixture
Advanced tools
A mock SMTP server designed for use as a test fixture that implements encryption and authentication to mimic a real world server.
A simple SMTP server based on aiosmtpd
for use as a fixture with pytest that supports encryption and authentication. All this does is receives messages and appends them to a list as an email.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.
Install using pip:
pip install bebleo-smtpd-fixture
Or, if you're using setuptools it can be included in the tests_require
section of a setup.py
file:
setup(
...
tests_require = [
"pytest",
"bebleo-smtpd-fixture",
],
)
To use the smtpd
fixture import it into your test file and then use it like any other pytest fixture. for example:
# test_mail.py
from smtplib import SMTP
from bebleo_smtpd_fixture import smtpd
def test_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"
with SMTP(smtpd.hostname, smtpd.port) as client:
client.sendmail(from_addr, to_addrs, msg)
assert len(smtpd.messages) == 1
To use STARTTLS:
from smtplib import SMTP
import pytest
from bebleo_smtpd_fixture import smtpd
@pytest.fixture
def mock_use_starttls(monkeypatch):
monkeypatch.setenv('SMTPD_USE_STARTTLS', 'True')
def test_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"
with SMTP(smtpd.hostname, smtpd.port) as client:
client.starttls() # Note that you need to call starttls first.
client.sendmail(from_addr, to_addrs, msg)
assert len(smtpd.messages) == 1
The 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 under SMTP_SSL_CERTS_PATH
.
Configuration can be handled through environment variables:
Variable | Default | Description |
---|---|---|
SMTPD_HOST | 127.0.0.1 | The hostname that the fixture will listen on. |
SMTPD_PORT | 8025 | The port that the fixture will listen on. |
SMPTD_USERNAME | user | |
SMTPD_PASSWORD | password | |
SMTPD_USE_SSL | False | Whether the fixture should use fixed TLS/SSL for transactions. If using smtplib requires that SMTP_SSL be used instead of SMTP . |
SMTPD_USE_STARTTLS | False | Whether the fixture should use StartTLS to encrypt the connections. If using smptlib requires that the SMTP.starttls() be called before other commands are issued. |
SMTPD_ENFORCE_AUTH | False | If 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 .env
file they'll be loaded automatically.
Ā©2020, Written with ā and ⤠in Montreal, QC
FAQs
A mock SMTP server designed for use as a test fixture that implements encryption and authentication to mimic a real world server.
We found that bebleo-smtpd-fixture demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Ā It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.