|
| 1 | +"""Regression tests for TLS configuration in both HTTP clients.""" |
| 2 | + |
| 3 | +import ssl |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import certifi |
| 7 | +import pytest |
| 8 | +from pytest_mock import MockerFixture |
| 9 | + |
| 10 | +from typesense.async_.api_call import AsyncApiCall |
| 11 | +from typesense.configuration import Configuration |
| 12 | +from typesense.sync.api_call import ApiCall |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(params=[ApiCall, AsyncApiCall]) |
| 16 | +def api_call_class(request): |
| 17 | + """Exercise both the async source and the generated sync client.""" |
| 18 | + return request.param |
| 19 | + |
| 20 | + |
| 21 | +def pool_ssl_context(api_call) -> ssl.SSLContext: |
| 22 | + """Return the SSL context of the client's connection pool. |
| 23 | +
|
| 24 | + This reads private httpx 0.28 attributes and may need updating on upgrades. |
| 25 | + """ |
| 26 | + return api_call._client._transport._pool._ssl_context |
| 27 | + |
| 28 | + |
| 29 | +async def close_api_call(api_call): |
| 30 | + if isinstance(api_call, AsyncApiCall): |
| 31 | + await api_call.aclose() |
| 32 | + else: |
| 33 | + api_call.close() |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize("verify", [True, False]) |
| 37 | +async def test_verification_mode(fake_config, api_call_class, verify): |
| 38 | + """The effective TLS context must honor explicit verification settings.""" |
| 39 | + fake_config.verify = verify |
| 40 | + api_call = api_call_class(fake_config) |
| 41 | + try: |
| 42 | + ssl_context = pool_ssl_context(api_call) |
| 43 | + assert ssl_context.verify_mode == ( |
| 44 | + ssl.CERT_REQUIRED if verify else ssl.CERT_NONE |
| 45 | + ) |
| 46 | + assert ssl_context.check_hostname is verify |
| 47 | + finally: |
| 48 | + await close_api_call(api_call) |
| 49 | + |
| 50 | + |
| 51 | +async def test_ssl_context(fake_config, api_call_class): |
| 52 | + """A configured SSL context must be used as is.""" |
| 53 | + context = ssl.create_default_context() |
| 54 | + fake_config.verify = context |
| 55 | + api_call = api_call_class(fake_config) |
| 56 | + try: |
| 57 | + assert pool_ssl_context(api_call) is context |
| 58 | + finally: |
| 59 | + await close_api_call(api_call) |
| 60 | + |
| 61 | + |
| 62 | +async def test_custom_ca_bundle( |
| 63 | + fake_config: Configuration, |
| 64 | + api_call_class, |
| 65 | + tmp_path: Path, |
| 66 | + mocker: MockerFixture, |
| 67 | +): |
| 68 | + """A configured CA bundle must reach the real SSL context builder.""" |
| 69 | + ca_bundle = tmp_path / "custom-ca.pem" |
| 70 | + ca_bundle.write_bytes(Path(certifi.where()).read_bytes()) |
| 71 | + fake_config.verify = str(ca_bundle) |
| 72 | + create_context = mocker.spy(ssl, "create_default_context") |
| 73 | + |
| 74 | + api_call = api_call_class(fake_config) |
| 75 | + try: |
| 76 | + create_context.assert_any_call(cafile=str(ca_bundle)) |
| 77 | + finally: |
| 78 | + await close_api_call(api_call) |
| 79 | + |
| 80 | + |
| 81 | +async def test_missing_ca_bundle(fake_config, api_call_class, tmp_path): |
| 82 | + """An invalid CA path must fail instead of silently using default trust roots.""" |
| 83 | + fake_config.verify = str(tmp_path / "missing-ca.pem") |
| 84 | + with pytest.raises(FileNotFoundError): |
| 85 | + api_call_class(fake_config) |
0 commit comments