예제 #1
0
async def test_custom_cert_and_key(request, tmp_path_factory, msg):
    path = tmp_path_factory.mktemp("certs")
    generate_certs(path, separate_key=True)
    _config = Config()
    _config.use_ssl = True
    _config.ssl_cert_files = (path.joinpath("cert.pem"),
                              path.joinpath("key.pem"))

    server = AuthController(config=_config)
    request.addfinalizer(server.stop)
    server.start()

    with SMTP_SSL(server.hostname, server.port) as client:
        client.send_message(msg)

    assert len(server.messages) == 1
예제 #2
0
async def test_missing_certs(request, msg):
    with pytest.raises(FileNotFoundError) as error:
        _config = Config()
        _config.use_starttls = True
        _config.ssl_certs_path = "."
        _authenticator = _Authenticator(config=_config)
        server = AuthController(hostname=_config.host,
                                port=_config.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 == FileNotFoundError
예제 #3
0
def test_init_envfile():
    original_env = os.environ.copy()
    config_file = Path(__file__).parent.joinpath("assets/.test.env")
    config = Config(filename=config_file, override=True)

    assert config.port == 5025

    os.environ.clear()
    os.environ.update(original_env)
예제 #4
0
def test_unset_event_handler(handler):
    config = Config()
    result = []
    prop = "auth_require_tls"  # chosen because it is alphabetically mo. 1
    func = functools.partial(handler.handle, result)

    config.OnChanged += func
    setattr(config, prop, 1)
    assert result.pop() is True

    config.OnChanged -= func
    setattr(config, prop, 0)
    assert not result
예제 #5
0
def test_TLS_not_supported(request, tmp_path_factory, msg, user):
    path = tmp_path_factory.mktemp("certs")
    _generate_certs(path)
    ssl_cert_files = str(path.joinpath("cert.pem"))
    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    context.load_verify_locations(ssl_cert_files)
    context.load_cert_chain(ssl_cert_files)

    config = Config()
    config.enforce_auth = True
    config.use_ssl = True
    server = AuthController(config=config,
                            authenticator=_Authenticator(config),
                            ssl_context=context)
    request.addfinalizer(server.stop)
    server.start()

    with pytest.raises(SMTPServerDisconnected):
        with SMTP(server.hostname, server.port) as client:
            # this should return a 523 Encryption required error
            # but instead returns an SMTPServerDisconnected Error
            client.send_message(msg)
            assert len(server.messages) == 1
예제 #6
0
async def test_config_file(request, msg):
    _original_env = os.environ.copy()
    config_file = Path(__file__).parent.joinpath("assets/.test.env")
    _config = Config(filename=config_file, override=True)
    server = AuthController(hostname=_config.host,
                            port=_config.port,
                            config=_config)
    request.addfinalizer(server.stop)
    server.start()

    with SMTP(server.hostname, server.port) as client:
        client.send_message(msg)

    assert server.port == 5025

    os.environ.clear()
    os.environ.update(_original_env)
예제 #7
0
def test_event_handler_fires(prop, handler):
    config = Config()
    result = []
    config.OnChanged += functools.partial(handler.handle, result)
    setattr(config, prop, 1)
    assert result.pop() is True
예제 #8
0
def test_set_values(attr, value, expected, type):
    config = Config()
    setattr(config, attr, value)
    assert isinstance(getattr(config, attr), type)
    assert getattr(config, attr) == expected
예제 #9
0
def test_init():
    config = Config()
    assert isinstance(config, Config)