Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SMTP service plugin #574

Merged
merged 3 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
SMTP service plugin: Add support for minimal configuration w/o TLS/AUTH
  • Loading branch information
amotl committed Aug 20, 2022
commit 1f147a35ae5ee9428b4b8e0d622f4ffbf017b0fd
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ in progress
- Improve error messages: Remove all ellipsis
- SMTP service plugin: Add software tests
- SMTP service plugin: Fix evaluation of ``htmlmsg`` parameter
- SMTP service plugin: Add support for minimal configuration w/o TLS and AUTH



Expand Down
6 changes: 3 additions & 3 deletions mqttwarn/services/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def plugin(srv, item):

server = item.config['server']
sender = item.config['sender']
starttls = item.config['starttls']
username = item.config['username']
password = item.config['password']
starttls = item.config.get('starttls')
username = item.config.get('username')
password = item.config.get('password')

if item.config.get("htmlmsg"):
msg = MIMEMultipart('alternative')
Expand Down
43 changes: 43 additions & 0 deletions tests/services/test_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,46 @@ def test_smtp_submit_error(srv, caplog):
"Error sending notification to SMTP recipient test, addresses: "
"['foo@example.org', 'bar@example.org']. Exception: Something failed" in caplog.messages
)


def test_smtp_minimal(srv, caplog):
"""
Verify that a minimal configuration also works, without encryption (STARTTLS) and
authentication (SMTP AUTH).

This scenario can support you when submitting e-mail to a dummy SMTP server on
your workstation for testing purposes. It is not suitable for production use.
"""

module = load_module_from_file("mqttwarn/services/smtp.py")

item = Item(
config={
"server": "localhost:25",
"sender": "mqttwarn <mqttwarn@localhost>",
},
target="test",
addrs=["foo@example.org"],
message="Notification message",
)

item = Struct(**item.asdict())

with caplog.at_level(logging.DEBUG):
with mock.patch("smtplib.SMTP", create=True) as smtplib_mock:
outcome = module.plugin(srv, item)
assert smtplib_mock.mock_calls == [
call("localhost:25"),
call().set_debuglevel(0),
call().ehlo(),
call().sendmail(
"mqttwarn <mqttwarn@localhost>",
["foo@example.org"],
mock.ANY,
),
call().quit(),
]

assert outcome is True
assert "Sending SMTP notification to test, addresses: ['foo@example.org']" in caplog.text
assert "Successfully sent SMTP notification" in caplog.text