Skip to content
Myr

Add tests for config

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Separate cert and key paths into its own fixture

authored by Yeray Diaz Diaz · committed by Yeray Diaz Diaz · parent 2fe07c3 · Browse files · 2 files+52 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+52 added−12 removed

  • tests:+52 added−12 removed

2 files+52 added−12 removed

modified:tests/conftest.py+13 added-12 removed
1import asyncio2import os23import pytest4import trustme5from uvicorn.config import Config
45 unmodified lines
51 await send({"type": "http.response.body", "body": b"Hello, world!"})525354@pytest.fixture55def cert_and_key_paths():56 ca = trustme.CA()57 ca.issue_cert("example.org")58 with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:59 yield cert_temp_path, key_temp_path606162@pytest.fixture63async def server():64 config = Config(app=app, lifespan="off")65 server = Server(config=config)66 task = asyncio.ensure_future(server.serve())
6 unmodified lines
73 await task747576@pytest.fixture70async def https_server():71 ca = trustme.CA()72 server_cert = ca.issue_cert("example.org")73 with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:74 config = Config(75 app=app,76 lifespan="off",77 ssl_certfile=cert_temp_path,78 ssl_keyfile=key_temp_path,79 port=8001,80 )77async def https_server(cert_and_key_paths):78 cert_path, key_path = cert_and_key_paths79 config = Config(80 app=app, lifespan="off", ssl_certfile=cert_path, ssl_keyfile=key_path, port=800181 )82 server = Server(config=config)83 task = asyncio.ensure_future(server.serve())84 try:85 while not server.started:
5 unmodified lines
modified:tests/test_config.py+39 added
1import os2import ssl34import pytest5
7 unmodified lines
13 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED141516@pytest.mark.asyncio17async def test_load_ssl_config_verify_non_existing_path():18 ssl_config = httpcore.SSLConfig(verify="/path/to/nowhere")19 with pytest.raises(IOError):20 await ssl_config.load_ssl_context()212223@pytest.mark.asyncio24async def test_load_ssl_config_verify_existing_file():25 ssl_config = httpcore.SSLConfig(verify=httpcore.config.DEFAULT_CA_BUNDLE_PATH)26 context = await ssl_config.load_ssl_context()27 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED282930@pytest.mark.asyncio31async def test_load_ssl_config_verify_directory():32 path = os.path.dirname(httpcore.config.DEFAULT_CA_BUNDLE_PATH)33 ssl_config = httpcore.SSLConfig(verify=path)34 context = await ssl_config.load_ssl_context()35 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED363738@pytest.mark.asyncio39async def test_load_ssl_config_cert_and_key(cert_and_key_paths):40 cert_path, key_path = cert_and_key_paths41 ssl_config = httpcore.SSLConfig(cert=(cert_path, key_path))42 context = await ssl_config.load_ssl_context()43 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED444546@pytest.mark.asyncio47async def test_load_ssl_config_cert_without_key_raises(cert_and_key_paths):48 cert_path, _ = cert_and_key_paths49 ssl_config = httpcore.SSLConfig(cert=cert_path)50 with pytest.raises(ssl.SSLError):51 await ssl_config.load_ssl_context()525354@pytest.mark.asyncio55async def test_load_ssl_config_no_verify(verify=False):56 ssl_config = httpcore.SSLConfig(verify=False)57 context = await ssl_config.load_ssl_context()58 assert context.verify_mode == ssl.VerifyMode.CERT_NONE
38 unmodified lines