Use a certificate issued from the trustme CA in tests (#357)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jamie Hewland · committed by Seth Michael Larson · parent 05ac117 · Browse files · 2 files·+35 added−28 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+35 added−28 removed
- tests:+35 added−28 removed
Provenance
- Commit
338a360- Tree
7f7ba78- Author
Jamie Hewland <jhewland@gmail.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
05ac117
2 files changed+35 added−28 removed
modified:tests/dispatch/test_connections.py+4 added-4 removed
13 unmodified lines14 response = await conn.request("GET", server.url, data=b"Hello, world!")15 assert response.status_code == 20016 17 18-async def test_https_get_with_ssl_defaults(https_server, backend):18+async def test_https_get_with_ssl_defaults(https_server, ca_cert_pem_file, backend):19 """20 An HTTPS request, with default SSL configuration set on the client.21 """22 async with HTTPConnection(23- origin=https_server.url, verify=False, backend=backend23+ origin=https_server.url, verify=ca_cert_pem_file, backend=backend24 ) as conn:25 response = await conn.request("GET", https_server.url)26 await response.read()27 assert response.status_code == 20028 assert response.content == b"Hello, world!"29 30 31-async def test_https_get_with_sll_overrides(https_server, backend):31+async def test_https_get_with_sll_overrides(https_server, ca_cert_pem_file, backend):32 """33 An HTTPS request, with SSL configuration set on the request.34 """35 async with HTTPConnection(origin=https_server.url, backend=backend) as conn:36- response = await conn.request("GET", https_server.url, verify=False)36+ response = await conn.request("GET", https_server.url, verify=ca_cert_pem_file)37 await response.read()38 assert response.status_code == 20039 assert response.content == b"Hello, world!"modified:tests/conftest.py+31 added-24 removed
5 unmodified lines6 import typing7 8 import pytest9 import trustme10+from cryptography.hazmat.backends import default_backend11 from cryptography.hazmat.primitives.serialization import (12+ load_pem_private_key,13 BestAvailableEncryption,14 Encoding,15 PrivateFormat,16 )123 unmodified lines140 )141 await send({"type": "http.response.body", "body": json.dumps(body).encode()})142 143 142-class CAWithPKEncryption(trustme.CA):143- """Implementation of trustme.CA() that can emit144- private keys that are encrypted with a password.145- """144+SERVER_SCOPE = "session"145 147- @property148- def encrypted_private_key_pem(self):149- return trustme.Blob(150- self._private_key.private_bytes(151- Encoding.PEM,152- PrivateFormat.TraditionalOpenSSL,153- BestAvailableEncryption(password=b"password"),154- )155- )146 147+@pytest.fixture(scope=SERVER_SCOPE)148+def cert_authority():149+ return trustme.CA()150 158-SERVER_SCOPE = "session"151 152+@pytest.fixture(scope=SERVER_SCOPE)153+def ca_cert_pem_file(cert_authority):154+ with cert_authority.cert_pem.tempfile() as tmp:155+ yield tmp156 157+158 @pytest.fixture(scope=SERVER_SCOPE)162-def example_cert():163- ca = CAWithPKEncryption()164- ca.issue_cert("example.org")165- return ca159+def localhost_cert(cert_authority):160+ return cert_authority.issue_cert("localhost")161 162 163 @pytest.fixture(scope=SERVER_SCOPE)169-def cert_pem_file(example_cert):170- with example_cert.cert_pem.tempfile() as tmp:164+def cert_pem_file(localhost_cert):165+ with localhost_cert.cert_chain_pems[0].tempfile() as tmp:166 yield tmp167 168 169 @pytest.fixture(scope=SERVER_SCOPE)175-def cert_private_key_file(example_cert):176- with example_cert.private_key_pem.tempfile() as tmp:170+def cert_private_key_file(localhost_cert):171+ with localhost_cert.private_key_pem.tempfile() as tmp:172 yield tmp173 174 175 @pytest.fixture(scope=SERVER_SCOPE)181-def cert_encrypted_private_key_file(example_cert):182- with example_cert.encrypted_private_key_pem.tempfile() as tmp:176+def cert_encrypted_private_key_file(localhost_cert):177+ # Deserialize the private key and then reserialize with a password178+ private_key = load_pem_private_key(179+ localhost_cert.private_key_pem.bytes(), password=None, backend=default_backend()180+ )181+ encrypted_private_key_pem = trustme.Blob(182+ private_key.private_bytes(183+ Encoding.PEM,184+ PrivateFormat.TraditionalOpenSSL,185+ BestAvailableEncryption(password=b"password"),186+ )187+ )188+ with encrypted_private_key_pem.tempfile() as tmp:189 yield tmp190 191 192 class TestServer(Server):82 unmodified lines275 app=app,276 lifespan="off",277 ssl_certfile=cert_pem_file,278 ssl_keyfile=cert_private_key_file,279+ host="localhost",280 port=8001,281 loop="asyncio",282 )283 server = TestServer(config=config)1 unmodified line