(sap) fix validate credentials, add negative tests for credential fetching

This commit is contained in:
Vasilisa Parshikova 2026-03-23 16:16:10 +04:00
parent b1633852b8
commit 2476008001
2 changed files with 19 additions and 5 deletions

View file

@ -286,8 +286,7 @@ def fetch_credentials(
resource_group = resolve_resource_group(sources)
if resource_group is not None:
credentials["resource_group"] = resource_group
credentials["resource_group"] = resource_group
if "cert_url" in credentials:
credentials["auth_url"] = credentials.pop("cert_url")
@ -313,8 +312,8 @@ def validate_credentials(
modes = [
bool(client_secret),
(cert_str is not None and key_str is not None),
(cert_file_path is not None and key_file_path is not None),
bool(cert_str) and bool(key_str),
bool(cert_file_path) and bool(key_file_path),
]
if sum(bool(m) for m in modes) != 1:
raise ValueError(

View file

@ -105,4 +105,19 @@ def test_partial_credentials_missing_auth_url(monkeypatch):
creds.pop('resource_group')
with pytest.raises(ValueError, match="SAP AI Core credentials not found."):
sap_credentials.validate_credentials(**creds)
sap_credentials.validate_credentials(**creds)
def test_credentials_without_authentication_mode(monkeypatch):
_prep_env(monkeypatch)
# Set all required fields but no authentication mode (no client_secret, no certs)
monkeypatch.setenv("AICORE_CLIENT_ID", "test-client-id")
monkeypatch.setenv("AICORE_AUTH_URL", "test-auth-url")
monkeypatch.setenv("AICORE_BASE_URL", "test-base-url")
creds = sap_credentials.fetch_credentials()
creds.pop('resource_group')
# validate_credentials should raise because no authentication mode is provided
with pytest.raises(ValueError, match="SAP AI Core credentials are incomplete."):
sap_credentials.validate_credentials(**creds)