Skip to content

Commit

Permalink
Move check for missing certificates
Browse files Browse the repository at this point in the history
Because rainsing an error for a missing certificate file(s) once the loop has been started causes the fisture to hang this moves the check to the __init__ method.

Closes #11
  • Loading branch information
bebleo committed Jul 29, 2020
1 parent 88d518e commit 898dc74
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
11 changes: 6 additions & 5 deletions bebleo_smtpd_fixture/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ def __init__(self,
enable_SMTPUTF8=enable_SMTPUTF8,
ssl_context=__ssl_context)

self._starttls_context = None
if config.SMTPD_USE_STARTTLS:
certs = self._get_ssl_context(config.SMTPD_SSL_CERTS_PATH)
self._starttls_context = certs

log.info(f"SMTPD running on {self.hostname}:{self.port}")

def factory(self):
tls_context = None
if self.use_starttls:
tls_context = self._get_ssl_context()

return AuthSMTP(handler=self.handler,
require_starttls=self.use_starttls,
tls_context=tls_context)
tls_context=self._starttls_context)

def _get_ssl_context(self, certs_path=None):
if certs_path is None:
Expand Down
23 changes: 18 additions & 5 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import logging
from smtplib import SMTP, SMTPSenderRefused, SMTPServerDisconnected
from smtplib import SMTP, SMTPSenderRefused

import pytest
from aiosmtpd.controller import Controller
from aiosmtpd.handlers import Sink

from bebleo_smtpd_fixture.smtp import AuthSMTP
from bebleo_smtpd_fixture.controller import AuthController
from bebleo_smtpd_fixture.config import Config
from bebleo_smtpd_fixture.fixture import _Authenticator

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -40,8 +43,18 @@ def test_use_starttls(mock_smtpd_use_starttls, smtpd, msg):
code, resp = client.send_message(msg)


def test_missing_certs(mock_certs, smtpd, msg):
with pytest.raises(SMTPServerDisconnected) as error:
with SMTP(smtpd.hostname, smtpd.port) as client:
def test_missing_certs(mock_certs, request, msg):
with pytest.raises(FileNotFoundError) as error:
_config = Config()
_authenticator = _Authenticator(config=_config)
server = AuthController(hostname=_config.SMTPD_HOST,
port=_config.SMTPD_PORT,
config=_config,
authenticator=_authenticator)
request.addfinalizer(server.stop)
server.start()

with SMTP(server.hostname, server.port) as client:
client.send_message(msg)
assert error.type == SMTPServerDisconnected

assert error.type == FileNotFoundError

0 comments on commit 898dc74

Please sign in to comment.