diff --git a/openbb_platform/core/openbb_core/provider/utils/helpers.py b/openbb_platform/core/openbb_core/provider/utils/helpers.py index 866ee3d82e5..1d6c9c21042 100644 --- a/openbb_platform/core/openbb_core/provider/utils/helpers.py +++ b/openbb_platform/core/openbb_core/provider/utils/helpers.py @@ -478,13 +478,17 @@ def combine_certificates(cert: str, bundle: str | None = None) -> str: from pathlib import Path from warnings import warn - if not Path(cert).exists(): + cert_path = Path(cert) + + if not cert_path.exists(): raise FileNotFoundError(f"Certificate file '{cert}' not found") - if cert.split(".", maxsplit=1)[0].endswith("_combined"): + if cert_path.stem.endswith("_combined"): return cert - combined_cert = cert.split(".", maxsplit=1)[0] + "_combined." + cert.split(".")[1] + combined_cert = str( + cert_path.with_name(f"{cert_path.stem}_combined{cert_path.suffix}") + ) if Path(combined_cert).exists(): return combined_cert diff --git a/openbb_platform/core/tests/provider/utils/test_helpers.py b/openbb_platform/core/tests/provider/utils/test_helpers.py index 4ad5483149a..1ad19ce7500 100644 --- a/openbb_platform/core/tests/provider/utils/test_helpers.py +++ b/openbb_platform/core/tests/provider/utils/test_helpers.py @@ -740,6 +740,40 @@ def test_combine_certificates_returns_existing_combined_file(tmp_path): assert combine_certificates(str(cert)) == str(combined) +def test_combine_certificates_dotted_directory(tmp_path): + """A dot in a parent directory name must not be taken as the file extension.""" + from openbb_core.provider.utils.helpers import combine_certificates + + cert_dir = tmp_path / ".certs" + cert_dir.mkdir() + cert = cert_dir / "ca.pem" + cert.write_text("CERT") + bundle = tmp_path / "bundle.pem" + bundle.write_text("BUNDLE") + + out = Path(combine_certificates(str(cert), str(bundle))) + + assert out == cert_dir / "ca_combined.pem" + assert out.read_text() == "BUNDLECERT" + out.unlink() + + +def test_combine_certificates_without_extension(tmp_path): + """A certificate file without an extension is combined instead of raising.""" + from openbb_core.provider.utils.helpers import combine_certificates + + cert = tmp_path / "ca" + cert.write_text("CERT") + bundle = tmp_path / "bundle.pem" + bundle.write_text("BUNDLE") + + out = Path(combine_certificates(str(cert), str(bundle))) + + assert out == tmp_path / "ca_combined" + assert out.read_text() == "BUNDLECERT" + out.unlink() + + def test_combine_certificates_cleanup_tolerates_missing_file(tmp_path, monkeypatch): """The registered atexit cleanup must not raise if the file is already gone.""" import atexit