diff --git a/poast/cli.py b/poast/cli.py index 56648bc..f681b51 100644 --- a/poast/cli.py +++ b/poast/cli.py @@ -4,7 +4,7 @@ import getpass import logging from logging.config import fileConfig -from smtplib import SMTP_SSL, SMTPRecipientsRefused +from smtplib import SMTP, SMTP_SSL, SMTPRecipientsRefused from tempfile import NamedTemporaryFile from time import sleep @@ -55,13 +55,17 @@ def queue(path, mongo, mongo_database, mongo_collection, people_db, sender, @click.option('--password', prompt=True, hide_input=True) @click.option('--smtp-host', default='localhost') @click.option('--smtp-port', default=465, type=int) -def mail(path, username, password, smtp_host, smtp_port): - s = SMTP_SSL(smtp_host, smtp_port) - try: - s.login(username, password) - except: - s.quit() - raise +@click.option('--ssl/--no-ssl', default=True) +def mail(path, username, password, smtp_host, smtp_port, ssl): + if not ssl: + s = SMTP(smtp_host, smtp_port) + else: + s = SMTP_SSL(smtp_host, smtp_port) + try: + s.login(username, password) + except: + s.quit() + raise try: for msg in delivery_queue(path): receiver = msg['To'] diff --git a/tests/conftest.py b/tests/conftest.py index 59e4a70..5f02785 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,15 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import +import asyncore import io import json import os +import socket import shutil +import smtpd import tempfile +import threading from mongobox import MongoBox from pymongo import MongoClient @@ -52,3 +56,39 @@ def tmp_dir(): path = tempfile.mkdtemp() yield path shutil.rmtree(path) + + +@pytest.yield_fixture +def smtp_server(): + port = _get_open_port() + server = TestSMTPServer(('localhost', port)) + server.start() + yield server + server.close() + + +def _get_open_port(host="localhost"): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind((host, 0)) + port = s.getsockname()[1] + s.close() + return port + + +class TestSMTPServer(smtpd.SMTPServer): + def __init__(self, localaddr): + self.received = [] + self._port = localaddr[1] + smtpd.SMTPServer.__init__(self, localaddr, None) + + def process_message(self, peer, mailfrom, rcpttos, data): + self.received.append(data) + + def start(self): + self.thread = threading.Thread(target=asyncore.loop, + kwargs={'timeout': 0.1}) + self.thread.start() + + def close(self): + smtpd.SMTPServer.close(self) + self.thread.join() diff --git a/tests/test_cli.py b/tests/test_cli.py index f73b4bf..b2db17e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3,6 +3,7 @@ import io import os +from email import message_from_file, message_from_string from click.testing import CliRunner import pytest @@ -21,5 +22,5 @@ def test_queue_writes_messages_to_directory(runner, tmp_dir, mongo_db, mongo): msgs = os.listdir(tmp_dir) assert len(msgs) == 1 with io.open(os.path.join(tmp_dir, msgs[0])) as fp: - msg = fp.read() - assert u'Þorgerðr Hǫlgabrúðr' in msg + msg = message_from_file(fp) + assert msg['To'] == 'thor@example.com'