Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions poast/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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']
Expand Down
40 changes: 40 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
5 changes: 3 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import io
import os
from email import message_from_file, message_from_string

from click.testing import CliRunner
import pytest
Expand All @@ -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'