Skip to content

Commit

Permalink
SMTP Mockup server and unittests fro SmtpProvider, Closes #51, and re…
Browse files Browse the repository at this point in the history
…lated to #50
  • Loading branch information
pylover committed Jun 20, 2017
1 parent 64715a1 commit 70514be
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
2 changes: 2 additions & 0 deletions restfulpy/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
username: user@example.com
password: password
local_hostname: localhost
tls: true
auth: true
logging:
loggers:
Expand Down
7 changes: 5 additions & 2 deletions restfulpy/messaging/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ def send(self, to, subject, body, cc=None, bcc=None, template_filename=None, fro
port=smtp_config.port,
local_hostname=smtp_config.local_hostname
)
smtp_server.starttls()
smtp_server.login(smtp_config.username, smtp_config.password)
if smtp_config.tls:
smtp_server.starttls()

if smtp_config.auth:
smtp_server.login(smtp_config.username, smtp_config.password)

from_ = from_ or smtp_config.username

Expand Down
1 change: 1 addition & 0 deletions restfulpy/testing/mockup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

from .http import http_server, encode_multipart_data, http_static_server
from .smtp import smtp_server
31 changes: 31 additions & 0 deletions restfulpy/testing/mockup/smtp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

import smtpd
import asyncore
import threading
import contextlib


SERVER_LOCK = threading.Event()


class MockupSMTPServer(smtpd.SMTPServer):

def __init__(self, bind):
super().__init__(bind, None, decode_data=False)
self.server_address = self.socket.getsockname()[:2]
SERVER_LOCK.set()

def process_message(*args, **kwargs):
pass


@contextlib.contextmanager
def smtp_server(bind=('localhost', 0)):
server = MockupSMTPServer(bind)
thread = threading.Thread(target=asyncore.loop, daemon=True)
thread.start()
SERVER_LOCK.wait()
yield server, server.server_address
# server.close()
asyncore.close_all() #ignore_all=True)
# thread.join()
40 changes: 40 additions & 0 deletions restfulpy/tests/test_smtp_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import unittest

from nanohttp import settings, configure

from restfulpy.messaging.providers import SmtpProvider
from restfulpy.testing.mockup import smtp_server


class SmtpProviderTestCase(unittest.TestCase):
__configuration__ = '''
smtp:
host: smtp.example.com
port: 587
username: user@example.com
password: password
local_hostname: localhost
tls: false
auth: false
'''

@classmethod
def setUpClass(cls):
configure(init_value=cls.__configuration__, force=True)

def test_smtp_provider(self):
with smtp_server() as (server, bind):
settings.smtp.host = bind[0]
settings.smtp.port = bind[1]

SmtpProvider().send(
'test@example.com',
'test@example.com',
'Simple test body'
)


if __name__ == '__main__':
unittest.main()

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Testing
'requests',
'webtest',
'nose',
'nose'
]


Expand Down

0 comments on commit 70514be

Please sign in to comment.