Skip to content

Commit

Permalink
Merge pull request ASPP#48 from keszybz/separate-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Debilski authored Jan 15, 2018
2 parents b0714d0 + 320ced1 commit 0f9425d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 101 deletions.
107 changes: 7 additions & 100 deletions massmail
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Options:
-z SERVER the SMTP server to use. This argument is required
-P PORT the SMTP port to use.
-P PORT the SMTP port to use. Must be between 1 and 65535.
-u SMTP user name. If not set, use anonymous SMTP
connection
Expand Down Expand Up @@ -115,7 +115,12 @@ def error(s):
sys.exit(-1)

def parse_command_line_options(arguments):
# parse options
"""Parse options.
Returns a dictionary of options.
Arguments are checked for validity.
"""
try:
opts, args = getopt.getopt(arguments, "hfs:F:S:B:R:e:u:p:P:z:")
except getopt.GetoptError as err:
Expand Down Expand Up @@ -288,103 +293,5 @@ def main(arguments):
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('wt') as f:
f.write('$EMAIL$;$VALUE$\ntestrecv@test;this is a test')
f.flush()
cmd_options = [
'-F', 'testfrom@test',
'-z', 'localhost',
f.name,
]
options = parse_command_line_options(cmd_options)
keywords, email_count = parse_parameter_file(options)
assert keywords == expected_keywords

def test_local_sending():
parameter_string = '$EMAIL$;$NAME$;$VALUE$\n[email protected];TestName;531'
email_body = 'Dear $NAME$,\nthis is a test: $VALUE$\nBest regards'
email_to = '[email protected]'
email_from = '[email protected]'
email_subject = 'Test Subject'
email_encoding = 'utf-8'

expected_email = email.mime.text.MIMEText('Dear TestName,\nthis is a test: 531\nBest regards'.encode(email_encoding), 'plain', email_encoding)
expected_email['To'] = email_to
expected_email['From'] = email_from
expected_email['Subject'] = email.header.Header(email_subject.encode(email_encoding), email_encoding)

with tempfile.NamedTemporaryFile('wt') as f:
f.write(parameter_string)
f.flush()
cmd_options = [
'-F', email_from,
'-S', email_subject,
'-z', 'localhost',
'-e', email_encoding,
f.name
]
options = parse_command_line_options(cmd_options)
keywords, email_count = parse_parameter_file(options)
msgs = create_email_bodies(options, keywords, email_count, email_body)
add_email_headers(options, msgs)
assert msgs['[email protected]'].as_string() == expected_email.as_string()

@contextlib.contextmanager
def replace_stdin(text):
input = io.StringIO(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(['python2',
'-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('wt') 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 b'MAIL FROM:<[email protected]>' in output
assert b'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__':
main(sys.argv[1:])
1 change: 1 addition & 0 deletions massmail.py
1 change: 0 additions & 1 deletion test_massmail.py

This file was deleted.

25 changes: 25 additions & 0 deletions test_massmail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import tempfile

import massmail

def test_dummy():
pass

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

def test_parse_parameter_file():
expected_keywords = {u'$VALUE$': [u'this is a test'], u'$EMAIL$': [u'testrecv@test']}
with tempfile.NamedTemporaryFile('wt') as f:
f.write('$EMAIL$;$VALUE$\ntestrecv@test;this is a test')
f.flush()
cmd_options = [
'-F', 'testfrom@test',
'-z', 'localhost',
f.name,
]
options = massmail.parse_command_line_options(cmd_options)
keywords, email_count = massmail.parse_parameter_file(options)
assert keywords == expected_keywords
87 changes: 87 additions & 0 deletions test_sending.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import contextlib
import email
import io
import sys
import os
import time
import subprocess
import base64
import tempfile

import massmail

@contextlib.contextmanager
def replace_stdin(text):
input = io.StringIO(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(['python2',
'-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_local_sending():
parameter_string = '$EMAIL$;$NAME$;$VALUE$\n[email protected];TestName;531'
email_body = 'Dear $NAME$,\nthis is a test: $VALUE$\nBest regards'
email_to = '[email protected]'
email_from = '[email protected]'
email_subject = 'Test Subject'
email_encoding = 'utf-8'

expected_email = email.mime.text.MIMEText('Dear TestName,\nthis is a test: 531\nBest regards'.encode(email_encoding), 'plain', email_encoding)
expected_email['To'] = email_to
expected_email['From'] = email_from
expected_email['Subject'] = email.header.Header(email_subject.encode(email_encoding), email_encoding)

with tempfile.NamedTemporaryFile('wt') as f:
f.write(parameter_string)
f.flush()
cmd_options = [
'-F', email_from,
'-S', email_subject,
'-z', 'localhost',
'-e', email_encoding,
f.name
]
options = massmail.parse_command_line_options(cmd_options)
keywords, email_count = massmail.parse_parameter_file(options)
msgs = massmail.create_email_bodies(options, keywords, email_count, email_body)
massmail.add_email_headers(options, msgs)
assert msgs['[email protected]'].as_string() == expected_email.as_string()

def test_fake_sending():
address = 'localhost:1025'
with tempfile.NamedTemporaryFile('wt') 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$'):
massmail.main(['-F', '[email protected]',
'-z', address,
f.name])

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

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

0 comments on commit 0f9425d

Please sign in to comment.