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
Provenance
- Commit
8d53257- Tree
a3d6d08- Author
Yeray Diaz Diaz <yeray.diaz@farfetch.com>- Committer
Yeray Diaz Diaz <yeray.diaz@farfetch.com>- Parent
2fe07c3
2 files changed+52 added−12 removed
modified:tests/conftest.py+13 added-12 removed
1 import asyncio2-import os2 3 import pytest4 import trustme5 from uvicorn.config import Config45 unmodified lines51 await send({"type": "http.response.body", "body": b"Hello, world!"})52 53 54 @pytest.fixture55+def 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_path60+61+62+@pytest.fixture63 async def server():64 config = Config(app=app, lifespan="off")65 server = Server(config=config)66 task = asyncio.ensure_future(server.serve())6 unmodified lines73 await task74 75 76 @pytest.fixture70-async 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- )77+async 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 linesmodified:tests/test_config.py+39 added
1+import os2 import ssl3 4 import pytest5 7 unmodified lines13 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED14 15 16 @pytest.mark.asyncio17+async 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()21+22+23+@pytest.mark.asyncio24+async 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_REQUIRED28+29+30+@pytest.mark.asyncio31+async 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_REQUIRED36+37+38+@pytest.mark.asyncio39+async 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_REQUIRED44+45+46+@pytest.mark.asyncio47+async 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()52+53+54+@pytest.mark.asyncio55 async 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_NONE38 unmodified lines