From f30c8f5132eb362368ddf9e4a016659bc742610b Mon Sep 17 00:00:00 2001 From: Mohammad Sobhieh Date: Wed, 18 Nov 2026 22:27:42 -0800 Subject: [PATCH] zulip: Allow cert_bundle to be a directory. --- zulip/tests/test_client.py | 50 ++++++++++++++++++++++++++++++++++++++ zulip/zulip/__init__.py | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 zulip/tests/test_client.py diff --git a/zulip/tests/test_client.py b/zulip/tests/test_client.py new file mode 100644 index 000000000..e78ffbe55 --- /dev/null +++ b/zulip/tests/test_client.py @@ -0,0 +1,50 @@ +import os +import tempfile +from unittest import TestCase +from unittest.mock import patch + +from typing_extensions import override + +import zulip +from zulip import ConfigNotFoundError + + +class TestCertBundle(TestCase): + @override + def setUp(self) -> None: + # Avoid a real network request in Client.__init__, which calls + # get_server_settings(). + patcher = patch.object( + zulip.Client, + "get_server_settings", + return_value={"zulip_version": "10.0", "zulip_feature_level": 300}, + ) + self.addCleanup(patcher.stop) + patcher.start() + + def make_client(self, cert_bundle: str) -> zulip.Client: + return zulip.Client( + email="bot@example.com", + api_key="a" * 32, + site="https://chat.zulip.org", + cert_bundle=cert_bundle, + ) + + def test_cert_bundle_file(self) -> None: + with tempfile.NamedTemporaryFile() as cert_file: + client = self.make_client(cert_file.name) + self.assertEqual(client.tls_verification, cert_file.name) + + def test_cert_bundle_directory(self) -> None: + # The requests library also accepts a directory of CA certificates + # (e.g. /etc/ssl/certs) as the verify parameter, so a directory + # should be accepted as a cert bundle as well. + with tempfile.TemporaryDirectory() as cert_dir: + client = self.make_client(cert_dir) + self.assertEqual(client.tls_verification, cert_dir) + + def test_cert_bundle_missing(self) -> None: + missing = os.path.join(tempfile.gettempdir(), "zulip-nonexistent-cert-bundle") + with self.assertRaises(ConfigNotFoundError) as cm: + self.make_client(missing) + self.assertEqual(str(cm.exception), f"tls bundle '{missing}' does not exist") diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 6d725a10b..8cb501caa 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -501,7 +501,7 @@ def __init__( ) self.tls_verification: Union[bool, str] = False elif cert_bundle is not None: - if not os.path.isfile(cert_bundle): + if not os.path.isfile(cert_bundle) and not os.path.isdir(cert_bundle): raise ConfigNotFoundError(f"tls bundle '{cert_bundle}' does not exist") self.tls_verification = cert_bundle else: