Skip to content

Commit 3c6ab05

Browse files
committed
added test cases to see if non-ascii process is triggered
1 parent 171721d commit 3c6ab05

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

tests/test_main.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -765,22 +765,19 @@ def test_other_mime_type(tmpdir):
765765
email
766766
767767
"""), encoding="utf8")
768-
769768
# Simple unsecure server config
770769
config_path = Path(tmpdir/"mailmerge_server.conf")
771770
config_path.write_text(textwrap.dedent("""\
772771
[smtp_server]
773772
host = open-smtp.example.com
774773
port = 25
775774
"""), encoding="utf8")
776-
777775
# Run mailmerge
778776
runner = click.testing.CliRunner()
779777
with tmpdir.as_cwd():
780778
result = runner.invoke(main, [])
781779
assert not result.exception
782780
assert result.exit_code == 0
783-
784781
# Verify output
785782
stdout = copy.deepcopy(result.output)
786783
stdout = re.sub(r"Date:.+", "Date: REDACTED", stdout, re.MULTILINE)
@@ -820,27 +817,23 @@ def test_database_bom(tmpdir):
820817
821818
Hello {{name}}
822819
"""), encoding="utf8")
823-
824820
# Copy database containing a BOM
825821
database_path = Path(tmpdir/"mailmerge_database.csv")
826822
database_with_bom = utils.TESTDATA/"mailmerge_database_with_BOM.csv"
827823
shutil.copyfile(database_with_bom, database_path)
828-
829824
# Simple unsecure server config
830825
config_path = Path(tmpdir/"mailmerge_server.conf")
831826
config_path.write_text(textwrap.dedent("""\
832827
[smtp_server]
833828
host = open-smtp.example.com
834829
port = 25
835830
"""), encoding="utf8")
836-
837831
# Run mailmerge
838832
runner = click.testing.CliRunner()
839833
with tmpdir.as_cwd():
840834
result = runner.invoke(main, ["--output-format", "text"])
841835
assert not result.exception
842836
assert result.exit_code == 0
843-
844837
# Verify output
845838
stdout = copy.deepcopy(result.output)
846839
stdout = re.sub(r"Date:.+", "Date: REDACTED", stdout, re.MULTILINE)
@@ -871,29 +864,25 @@ def test_database_tsv(tmpdir):
871864
872865
Hello {{name}}
873866
"""), encoding="utf8")
874-
875867
# Tab-separated format database
876868
database_path = Path(tmpdir/"mailmerge_database.csv")
877869
database_path.write_text(textwrap.dedent("""\
878870
email\tname
879871
880872
"""), encoding="utf8")
881-
882873
# Simple unsecure server config
883874
config_path = Path(tmpdir/"mailmerge_server.conf")
884875
config_path.write_text(textwrap.dedent("""\
885876
[smtp_server]
886877
host = open-smtp.example.com
887878
port = 25
888879
"""), encoding="utf8")
889-
890880
# Run mailmerge
891881
runner = click.testing.CliRunner()
892882
with tmpdir.as_cwd():
893883
result = runner.invoke(main, ["--output-format", "text"])
894884
assert not result.exception
895885
assert result.exit_code == 0
896-
897886
# Verify output
898887
stdout = copy.deepcopy(result.output)
899888
stdout = re.sub(r"Date:.+", "Date: REDACTED", stdout, re.MULTILINE)
@@ -924,29 +913,25 @@ def test_database_semicolon(tmpdir):
924913
925914
Hello {{name}}
926915
"""), encoding="utf8")
927-
928916
# Semicolon-separated format database
929917
database_path = Path(tmpdir/"mailmerge_database.csv")
930918
database_path.write_text(textwrap.dedent("""\
931919
email;name
932920
933921
"""), encoding="utf8")
934-
935922
# Simple unsecure server config
936923
config_path = Path(tmpdir/"mailmerge_server.conf")
937924
config_path.write_text(textwrap.dedent("""\
938925
[smtp_server]
939926
host = open-smtp.example.com
940927
port = 25
941928
"""), encoding="utf8")
942-
943929
# Run mailmerge
944930
runner = click.testing.CliRunner()
945931
with tmpdir.as_cwd():
946932
result = runner.invoke(main, ["--output-format", "text"])
947933
assert not result.exception
948934
assert result.exit_code == 0
949-
950935
# Verify output
951936
stdout = copy.deepcopy(result.output)
952937
stdout = re.sub(r"Date:.+", "Date: REDACTED", stdout, re.MULTILINE)
@@ -965,3 +950,50 @@ def test_database_semicolon(tmpdir):
965950
>>> Limit was 1 message. To remove the limit, use the --no-limit option.
966951
>>> This was a dry run. To send messages, use the --no-dry-run option.
967952
""") # noqa: E501
953+
954+
955+
def test_utf8smtp_trigger(tmpdir):
956+
"""Verify triggers to rcpt_options=['UTF8SMTP']."""
957+
template_path = Path(tmpdir/"mailmerge_template.txt")
958+
template_path.write_text(textwrap.dedent("""\
959+
960+
961+
SUBJECT: UTF8SMTP Test
962+
Hello{{name}}!
963+
"""), encoding="utf8")
964+
database_path = Path(tmpdir/"mailmerge_database.csv")
965+
database_path.write_text(textwrap.dedent("""\
966+
email;name
967+
[email protected]; Max Müller
968+
"""), encoding="utf8")
969+
config_path = Path(tmpdir/"mailmerge_server.conf")
970+
config_path.write_text(textwrap.dedent("""\
971+
[smtp_server]
972+
host = open-smtp.example.com
973+
port = 25
974+
"""), encoding="utf8")
975+
runner = click.testing.CliRunner()
976+
with tmpdir.as_cwd():
977+
result = runner.invoke(main, [
978+
"--dry-run",
979+
"--no-limit",
980+
"--output-format", "text",
981+
])
982+
assert result.exit_code == 0
983+
assert not result.exception
984+
stdout = result.output
985+
stdout = re.sub(r"Date:.+", "Date: REDACTED", stdout, re.MULTILINE)
986+
assert stdout == textwrap.dedent("""\
987+
>>> message 1
988+
989+
990+
SUBJECT: UTF8SMTP Test
991+
MIME-Version: 1.0
992+
Content-Type: text/plain; charset="utf-8"
993+
Content-Transfer-Encoding: base64
994+
Date: REDACTED
995+
Hello Max Müller!
996+
997+
>>> message 1 sent
998+
>>> This was a dry run. To send messages, use the --no-dry-run option.
999+
""") # noqa: E501

tests/test_sendmail_client.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,47 @@ def test_socket_error(mocker, tmp_path):
609609

610610
# Verify exception string
611611
assert "Dummy error message" in str(err.value)
612+
613+
614+
def test_utf8smtp_trigger(mocker, tmp_path):
615+
"""Verify UTF8SMTP trigger."""
616+
# Config for SSL SMTP server
617+
config_path = tmp_path / "server.conf"
618+
config_path.write_text(textwrap.dedent("""\
619+
[smtp_server]
620+
host = smtp.gmail.com
621+
port = 465
622+
security = SSL/TLS
623+
username = awdeorio
624+
"""))
625+
626+
sendmail_client = SendmailClient(config_path, dry_run=False)
627+
628+
message = email.message_from_string(textwrap.dedent("""\
629+
630+
SUBJECT: UTF8 Test
631+
632+
633+
Hello!
634+
"""))
635+
636+
# Patch SMTP_SSL and getpass
637+
mock_smtp_ssl = mocker.patch('smtplib.SMTP_SSL')
638+
mock_getpass = mocker.patch('getpass.getpass')
639+
mock_getpass.return_value = "password"
640+
641+
# Send the message
642+
sendmail_client.sendmail(
643+
644+
recipients=["mü[email protected]"],
645+
message=message,
646+
)
647+
648+
# Grab mocked SMTP instance
649+
smtp = mock_smtp_ssl.return_value.__enter__.return_value
650+
651+
# Assert UTF8SMTP was passed
652+
smtp.sendmail.assert_called_once()
653+
kwargs = smtp.sendmail.call_args.kwargs
654+
assert "rcpt_options" in kwargs
655+
assert kwargs["rcpt_options"] == "UTF8SMTP"

0 commit comments

Comments
 (0)