Skip to content

Instantly share code, notes, and snippets.

@devsli
Last active March 13, 2016 14:26

Revisions

  1. devsli revised this gist Mar 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test_sample.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    def test_send(SmtpServer):
    sender = smtplib.SMTP()

    sender.connect('localhost', 1025)
    sender.connect('localhost', 1125)
    sender.login('john.doe44', 'correct horse battery staple')

    sender_email = '[email protected]'
  2. devsli revised this gist Mar 13, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions conftest.py
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ def test_get_msg(SmtpServer):
    Read more about `multiprocessing <https://docs.python.org/2/library/multiprocessing.html>`_
    """
    sent = Queue()
    port = 1025
    port = 1125
    host = 'localhost'

    class _TestSmtpServer(secure_smtpd.SMTPServer):
    @@ -38,7 +38,7 @@ def validate(self, user, password):
    return True

    def run(host, port, sent_queue):
    s = _TestSmtpServer(sent_queue, ('0.0.0.0', 1025), None,
    s = _TestSmtpServer(sent_queue, ('0.0.0.0', port), None,
    require_authentication=True,
    credential_validator=DufferValidator())
    s.run()
  3. devsli revised this gist Mar 5, 2016. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions conftest.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    import pytest
    import secure_smtpd
    from multiprocessing import Queue, Process


    @pytest.yield_fixture(scope='module')
    def SmtpServer():
    """
    @@ -16,9 +21,6 @@ def test_get_msg(SmtpServer):
    Read more about `multiprocessing <https://docs.python.org/2/library/multiprocessing.html>`_
    """
    import secure_smtpd
    from multiprocessing import Queue, Process

    sent = Queue()
    port = 1025
    host = 'localhost'
  4. devsli revised this gist Mar 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.rst
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@ Secure SMTPd py.test fixture

    Test outgoing messages of your app.

    It's possible to replace `` `secure-smtpd <https://github.com/bcoe/secure-smtpd>`_ ``
    It's possible to replace `secure-smtpd <https://github.com/bcoe/secure-smtpd>`_
    with default ``smtpd`` if ``AUTH`` is not forced by your application.
  5. devsli revised this gist Mar 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.rst
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@ Secure SMTPd py.test fixture

    Test outgoing messages of your app.

    It's possible to replace ```secure-smtpd <https://github.com/bcoe/secure-smtpd>`_``
    It's possible to replace `` `secure-smtpd <https://github.com/bcoe/secure-smtpd>`_ ``
    with default ``smtpd`` if ``AUTH`` is not forced by your application.
  6. devsli revised this gist Mar 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.rst
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@ Secure SMTPd py.test fixture

    Test outgoing messages of your app.

    It's possible to replace `secure-smtpd <https://github.com/bcoe/secure-smtpd>`_
    It's possible to replace ```secure-smtpd <https://github.com/bcoe/secure-smtpd>`_``
    with default ``smtpd`` if ``AUTH`` is not forced by your application.
  7. devsli revised this gist Mar 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test_sample.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def test_send(SmtpServer):

    sender.sendmail(sender_email, recipient_email, text)

    addr, sender, recipients, body = SmtpServer['sent'].get(timeout=10)
    addr, sender, recipients, body = SmtpServer.get(timeout=10)

    assert sender_email == sender
    assert recipient_email == recipients.pop()
  8. devsli renamed this gist Mar 5, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. devsli renamed this gist Mar 5, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. devsli renamed this gist Mar 5, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. devsli created this gist Mar 5, 2016.
    7 changes: 7 additions & 0 deletions README.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    Secure SMTPd py.test fixture
    ============================

    Test outgoing messages of your app.

    It's possible to replace `secure-smtpd <https://github.com/bcoe/secure-smtpd>`_
    with default ``smtpd`` if ``AUTH`` is not forced by your application.
    47 changes: 47 additions & 0 deletions conftest.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    @pytest.yield_fixture(scope='module')
    def SmtpServer():
    """
    Return dummy SMTP server to check outgoing messages.
    The ``multiprocessing.Queue`` of ``tuple`` objects is yelded
    Sent queue example usage::
    def test_get_msg(SmtpServer):
    addr, sender, recipients, body = SmtpServer.get()
    Note that execution will be freezed until something is retrieved
    from ``sent`` queue. You can use ``Queue.get_nowait()`` or
    ``timeout`` parameter to avoid such behavior.
    Read more about `multiprocessing <https://docs.python.org/2/library/multiprocessing.html>`_
    """
    import secure_smtpd
    from multiprocessing import Queue, Process

    sent = Queue()
    port = 1025
    host = 'localhost'

    class _TestSmtpServer(secure_smtpd.SMTPServer):
    def __init__(self, sent_queue, *args, **kwargs):
    secure_smtpd.SMTPServer.__init__(self, *args, **kwargs)
    self.sent_queue = sent_queue

    def process_message(self, *args):
    self.sent_queue.put(args)

    class DufferValidator(object):
    def validate(self, user, password):
    return True

    def run(host, port, sent_queue):
    s = _TestSmtpServer(sent_queue, ('0.0.0.0', 1025), None,
    require_authentication=True,
    credential_validator=DufferValidator())
    s.run()

    srv_proc = Process(target=run, args=(host, port, sent))
    srv_proc.start()
    yield sent
    srv_proc.terminate()
    22 changes: 22 additions & 0 deletions test_sample.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import pytest
    import smtplib

    def test_send(SmtpServer):
    sender = smtplib.SMTP()

    sender.connect('localhost', 1025)
    sender.login('john.doe44', 'correct horse battery staple')

    sender_email = '[email protected]'
    recipient_email = '[email protected]'
    text = 'Hello! Here your invoice.'

    sender.sendmail(sender_email, recipient_email, text)

    addr, sender, recipients, body = SmtpServer['sent'].get(timeout=10)

    assert sender_email == sender
    assert recipient_email == recipients.pop()
    assert body == text

    assert SmtpServer.empty() == True