Skip to content

Commit

Permalink
Merge pull request ASPP#23 from keszybz/test-with-fake-server
Browse files Browse the repository at this point in the history
Test with fake server
  • Loading branch information
jakobj authored Aug 28, 2017
2 parents c72e4cf + 1c3cc7f commit c233af8
Showing 1 changed file with 69 additions and 9 deletions.
78 changes: 69 additions & 9 deletions massmail
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar.
# http://www.wtfpl.net
#
#
# Full license text:
#
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004.
#
Expand All @@ -26,7 +26,12 @@
import smtplib, getopt, sys, os, email, getpass
import email.header
import email.mime.text
import io
import contextlib
import tempfile
import time
import subprocess
import base64


PROGNAME = os.path.basename(sys.argv[0])
Expand Down Expand Up @@ -60,7 +65,7 @@ Options:
-P PORT the SMTP port to use.
-u SMTP user name. If not set, use anonymous SMTP
-u SMTP user name. If not set, use anonymous SMTP
connection
-p SMTP password. If not set you will be prompted for one
Expand Down Expand Up @@ -98,7 +103,7 @@ My self.
* Example usage:
%s -F "Great Guy <[email protected]>" -S "You are a great guy" -B "Not so great Guy <[email protected]>" parameter-file < body
"""%(PROGNAME, PROGNAME)

def error(s):
Expand Down Expand Up @@ -292,9 +297,21 @@ def send_messages(options, msgs):

server.close()

def main(arguments):
options = parse_command_line_options(arguments)
keywords, email_count = parse_parameter_file(options)
msgs = create_email_bodies(options, keywords, email_count, sys.stdin.read())
add_email_headers(options, msgs)
send_messages(options, msgs)

def test_dummy():
pass

def test_command_help():
import pytest
with pytest.raises(SystemExit):
main(['-h'])

def test_parse_parameter_file():
expected_keywords = {u'$VALUE$': [u'this is a test'], u'$EMAIL$': [u'testrecv@test']}
with tempfile.NamedTemporaryFile() as f:
Expand Down Expand Up @@ -338,9 +355,52 @@ def test_local_sending():
add_email_headers(options, msgs)
assert msgs['[email protected]'].as_string() == expected_email.as_string()

@contextlib.contextmanager
def replace_stdin(text):
input = io.BytesIO(text)
old = sys.stdin
sys.stdin, old = input, sys.stdin
try:
yield
finally:
sys.stdin = old

@contextlib.contextmanager
def fake_smtp_server(address):
devnull = open(os.devnull, 'w')
server = subprocess.Popen(['python',
'-m', 'smtpd',
'-n',
'-d',
'-c', 'DebuggingServer',
address],
stdin=devnull,
stdout=devnull,
stderr=subprocess.PIPE)
try:
time.sleep(1)
yield server
finally:
server.terminate()

def test_fake_sending():
address = 'localhost:1025'
with tempfile.NamedTemporaryFile() as f:
f.write('$EMAIL$;$VALUE$\ntestrecv@test;this is a test')
f.flush()

with fake_smtp_server(address) as server:
with replace_stdin('EMAIL=$EMAIL$\nVALUE=$VALUE$'):
main(['-F', '[email protected]',
'-z', address,
f.name])

output = server.stderr.read()
assert 'MAIL FROM:<[email protected]>' in output
assert 'RCPT TO:<testrecv@test>' in output

encoded = base64.b64encode(b'EMAIL=testrecv@test\nVALUE=this is a test')
assert encoded in output

if __name__ == '__main__':
options = parse_command_line_options(sys.argv[1:])
keywords, email_count = parse_parameter_file(options)
msgs = create_email_bodies(options, keywords, email_count, sys.stdin.read())
add_email_headers(options, msgs)
send_messages(options, msgs)
main(sys.argv[1:])

0 comments on commit c233af8

Please sign in to comment.