Skip to content
Myr

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

2 files+35 added−28 removed

modified:tests/dispatch/test_connections.py+4 added-4 removed
13 unmodified lines
14 response = await conn.request("GET", server.url, data=b"Hello, world!")15 assert response.status_code == 200161718async def test_https_get_with_ssl_defaults(https_server, backend):18async 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!"293031async def test_https_get_with_sll_overrides(https_server, backend):31async 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 lines
6import typing78import pytest9import trustme10from cryptography.hazmat.backends import default_backend11from cryptography.hazmat.primitives.serialization import (12 load_pem_private_key,13 BestAvailableEncryption,14 Encoding,15 PrivateFormat,16)
123 unmodified lines
140 )141 await send({"type": "http.response.body", "body": json.dumps(body).encode()})142143142class CAWithPKEncryption(trustme.CA):143 """Implementation of trustme.CA() that can emit144 private keys that are encrypted with a password.145 """144SERVER_SCOPE = "session"145147 @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 )146147@pytest.fixture(scope=SERVER_SCOPE)148def cert_authority():149 return trustme.CA()150158SERVER_SCOPE = "session"151152@pytest.fixture(scope=SERVER_SCOPE)153def ca_cert_pem_file(cert_authority):154 with cert_authority.cert_pem.tempfile() as tmp:155 yield tmp156157158@pytest.fixture(scope=SERVER_SCOPE)162def example_cert():163 ca = CAWithPKEncryption()164 ca.issue_cert("example.org")165 return ca159def localhost_cert(cert_authority):160 return cert_authority.issue_cert("localhost")161162163@pytest.fixture(scope=SERVER_SCOPE)169def cert_pem_file(example_cert):170 with example_cert.cert_pem.tempfile() as tmp:164def cert_pem_file(localhost_cert):165 with localhost_cert.cert_chain_pems[0].tempfile() as tmp:166 yield tmp167168169@pytest.fixture(scope=SERVER_SCOPE)175def cert_private_key_file(example_cert):176 with example_cert.private_key_pem.tempfile() as tmp:170def cert_private_key_file(localhost_cert):171 with localhost_cert.private_key_pem.tempfile() as tmp:172 yield tmp173174175@pytest.fixture(scope=SERVER_SCOPE)181def cert_encrypted_private_key_file(example_cert):182 with example_cert.encrypted_private_key_pem.tempfile() as tmp:176def 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 tmp190191192class TestServer(Server):
82 unmodified lines
275 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