-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into pyproject
- Loading branch information
Showing
5 changed files
with
210 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,7 @@ This example will send progress reports to students. The template uses more of | |
TO: {{email}} | ||
SUBJECT: EECS 280 Mid-semester Progress Report | ||
FROM: My Self <[email protected]> | ||
REPLY-TO: My Reply Self <[email protected]> | ||
Dear {{name}}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,6 +248,16 @@ def create_sample_input_files(template_path, database_path, config_path): | |
# username = YOUR_USERNAME_HERE | ||
# ratelimit = 0 | ||
# Example: XOAUTH | ||
# Enter your token at the password prompt. For Microsoft OAuth | ||
# authentication, a token can be obtained with the oauth2ms tool | ||
# https://github.com/harishkrupo/oauth2ms | ||
# [smtp_server] | ||
# host = smtp.office365.com | ||
# port = 587 | ||
# security = XOAUTH | ||
# username = [email protected] | ||
# Example: No security | ||
# [smtp_server] | ||
# host = newman.eecs.umich.edu | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "mailmerge" | ||
version = "2.2.1" | ||
version = "2.2.3" | ||
description = "A simple, command line mail merge tool" | ||
keywords = ["mail merge", "mailmerge", "email"] | ||
authors = [{ email = "[email protected]" }, { name = "Andrew DeOrio" }] | ||
|
@@ -20,7 +20,6 @@ dependencies = [ | |
|
||
[project.optional-dependencies] | ||
dev = [ | ||
"pdbpp", | ||
"twine", | ||
"tox", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
import smtplib | ||
import email | ||
import email.parser | ||
import base64 | ||
import ssl | ||
import pytest | ||
from mailmerge import SendmailClient, MailmergeError | ||
|
||
|
@@ -250,6 +252,90 @@ def test_security_starttls(mocker, tmp_path): | |
assert smtp.sendmail.call_count == 1 | ||
|
||
|
||
def test_security_xoauth(mocker, tmp_path): | ||
"""Verify XOAUTH security configuration.""" | ||
# Config for XOAUTH SMTP server | ||
config_path = tmp_path/"server.conf" | ||
config_path.write_text(textwrap.dedent("""\ | ||
[smtp_server] | ||
host = smtp.office365.com | ||
port = 587 | ||
security = XOAUTH | ||
username = [email protected] | ||
""")) | ||
|
||
# Simple template | ||
sendmail_client = SendmailClient(config_path, dry_run=False) | ||
message = email.message_from_string("Hello world") | ||
|
||
# Mock SMTP | ||
mock_smtp = mocker.patch('smtplib.SMTP') | ||
mock_smtp_ssl = mocker.patch('smtplib.SMTP_SSL') | ||
|
||
# Mock the password entry | ||
mock_getpass = mocker.patch('getpass.getpass') | ||
mock_getpass.return_value = "password" | ||
|
||
# Send a message | ||
sendmail_client.sendmail( | ||
sender="[email protected]", | ||
recipients=["[email protected]"], | ||
message=message, | ||
) | ||
|
||
# Verify SMTP library calls | ||
assert mock_getpass.call_count == 1 | ||
assert mock_smtp.call_count == 1 | ||
assert mock_smtp_ssl.call_count == 0 | ||
smtp = mock_smtp.return_value.__enter__.return_value | ||
assert smtp.ehlo.call_count == 2 | ||
assert smtp.starttls.call_count == 1 | ||
assert smtp.login.call_count == 0 | ||
assert smtp.docmd.call_count == 2 | ||
assert smtp.sendmail.call_count == 1 | ||
|
||
# Verify authentication token format. The first call to docmd() is always | ||
# the same. Second call to docmd() contains a base64 encoded username and | ||
# password. | ||
assert smtp.docmd.call_args_list[0].args[0] == "AUTH XOAUTH2" | ||
user_pass = smtp.docmd.call_args_list[1].args[0] | ||
user_pass = base64.b64decode(user_pass) | ||
assert user_pass == \ | ||
b'[email protected]\x01auth=Bearer password\x01\x01' | ||
|
||
|
||
def test_security_xoauth_bad_username(mocker, tmp_path): | ||
"""Verify exception is thrown for UTF-8 username.""" | ||
# Config for XOAUTH SMTP server | ||
config_path = tmp_path/"server.conf" | ||
config_path.write_text(textwrap.dedent("""\ | ||
[smtp_server] | ||
host = smtp.office365.com | ||
port = 587 | ||
security = XOAUTH | ||
username = Laȝ[email protected] | ||
""")) | ||
|
||
# Simple template | ||
sendmail_client = SendmailClient(config_path, dry_run=False) | ||
message = email.message_from_string("Hello world") | ||
|
||
# Mock the password entry | ||
mock_getpass = mocker.patch('getpass.getpass') | ||
mock_getpass.return_value = "password" | ||
|
||
# Send a message | ||
with pytest.raises(MailmergeError) as err: | ||
sendmail_client.sendmail( | ||
sender="[email protected]", | ||
recipients=["[email protected]"], | ||
message=message, | ||
) | ||
|
||
# Verify exception string | ||
assert "Username and XOAUTH access token must be ASCII" in str(err.value) | ||
|
||
|
||
def test_security_plain(mocker, tmp_path): | ||
"""Verify plain security configuration.""" | ||
# Config for Plain SMTP server | ||
|
@@ -293,7 +379,7 @@ def test_security_plain(mocker, tmp_path): | |
|
||
|
||
def test_security_ssl(mocker, tmp_path): | ||
"""Verify open (Never) security configuration.""" | ||
"""Verify SSL/TLS security configuration.""" | ||
# Config for SSL SMTP server | ||
config_path = tmp_path/"server.conf" | ||
config_path.write_text(textwrap.dedent("""\ | ||
|
@@ -312,6 +398,10 @@ def test_security_ssl(mocker, tmp_path): | |
mock_smtp = mocker.patch('smtplib.SMTP') | ||
mock_smtp_ssl = mocker.patch('smtplib.SMTP_SSL') | ||
|
||
# Mock SSL | ||
mock_ssl_create_default_context = \ | ||
mocker.patch('ssl.create_default_context') | ||
|
||
# Mock the password entry | ||
mock_getpass = mocker.patch('getpass.getpass') | ||
mock_getpass.return_value = "password" | ||
|
@@ -327,13 +417,53 @@ def test_security_ssl(mocker, tmp_path): | |
assert mock_getpass.call_count == 1 | ||
assert mock_smtp.call_count == 0 | ||
assert mock_smtp_ssl.call_count == 1 | ||
assert mock_ssl_create_default_context.called | ||
assert "context" in mock_smtp_ssl.call_args[1] # SSL cert chain | ||
smtp = mock_smtp_ssl.return_value.__enter__.return_value | ||
assert smtp.ehlo.call_count == 0 | ||
assert smtp.starttls.call_count == 0 | ||
assert smtp.login.call_count == 1 | ||
assert smtp.sendmail.call_count == 1 | ||
|
||
|
||
def test_ssl_error(mocker, tmp_path): | ||
"""Verify SSL/TLS with an SSL error.""" | ||
# Config for SSL SMTP server | ||
config_path = tmp_path/"server.conf" | ||
config_path.write_text(textwrap.dedent("""\ | ||
[smtp_server] | ||
host = smtp.mail.umich.edu | ||
port = 465 | ||
security = SSL/TLS | ||
username = YOUR_USERNAME_HERE | ||
""")) | ||
|
||
# Simple template | ||
sendmail_client = SendmailClient(config_path, dry_run=False) | ||
message = email.message_from_string("Hello world") | ||
|
||
# Mock ssl.create_default_context() to raise an exception | ||
mocker.patch( | ||
'ssl.create_default_context', | ||
side_effect=ssl.SSLError(1, "CERTIFICATE_VERIFY_FAILED") | ||
) | ||
|
||
# Mock the password entry | ||
mock_getpass = mocker.patch('getpass.getpass') | ||
mock_getpass.return_value = "password" | ||
|
||
# Send a message | ||
with pytest.raises(MailmergeError) as err: | ||
sendmail_client.sendmail( | ||
sender="[email protected]", | ||
recipients=["[email protected]"], | ||
message=message, | ||
) | ||
|
||
# Verify exception string | ||
assert "CERTIFICATE_VERIFY_FAILED" in str(err.value) | ||
|
||
|
||
def test_missing_username(tmp_path): | ||
"""Verify exception on missing username.""" | ||
config_path = tmp_path/"server.conf" | ||
|