litellm/tests/unit/test_azure_ad_token_credential_resolution.py
yuneng-jiang f6882246d4
test: move tests/test_litellm root and small trees into tests/unit (#43186)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 11:30:43 -07:00

213 lines
8.6 KiB
Python

"""
Regression for #30235.
``Router.get_deployment_credentials_with_provider`` (router.py:8954) is
used by the proxy's ``/v1/files``, ``/v1/batches`` and passthrough
routing code paths to resolve the upstream credentials for a deployment
by model_id::
return CredentialLiteLLMParams(
**deployment.litellm_params.model_dump(exclude_none=True)
).model_dump(exclude_none=True)
That re-validation is strict. Any field NOT declared on
``CredentialLiteLLMParams`` gets dropped on the way through, even when
it was present on the original ``litellm_params``.
Pre-fix, ``azure_ad_token`` was undeclared, so Azure deployments
configured with OAuth/M2M (``azure_ad_token`` in place of ``api_key``)
silently lost their token on every file upload and the proxy returned::
Missing credentials. Please pass one of api_key, azure_ad_token,
azure_ad_token_provider, ...
Tests below pin two things:
1. ``CredentialLiteLLMParams`` directly accepts and round-trips
``azure_ad_token``.
2. ``Router.get_deployment_credentials_with_provider`` preserves
``azure_ad_token`` from a deployment's ``litellm_params``.
"""
from unittest.mock import MagicMock, patch
import pytest
class TestCredentialLiteLLMParamsAzureAdToken:
def test_azure_ad_token_round_trips_through_model_dump(self):
from litellm.types.router import CredentialLiteLLMParams
params = CredentialLiteLLMParams(
api_base="https://my.openai.azure.com",
api_version="2024-08-01-preview",
azure_ad_token="oauth-bearer-token-xyz",
)
dumped = params.model_dump(exclude_none=True)
assert dumped["azure_ad_token"] == "oauth-bearer-token-xyz", (
"azure_ad_token dropped from CredentialLiteLLMParams.model_dump() — "
"every callsite that round-trips litellm_params through this class "
"will lose the token (#30235)"
)
def test_azure_ad_token_is_optional(self):
"""Adding the field must not break deployments that don't use it
— confirm the default is None and it's excluded by
``exclude_none``."""
from litellm.types.router import CredentialLiteLLMParams
params = CredentialLiteLLMParams(api_key="sk-static")
dumped = params.model_dump(exclude_none=True)
assert "azure_ad_token" not in dumped
assert dumped["api_key"] == "sk-static"
def test_round_trip_preserves_full_credential_shape(self):
"""The Router's get_deployment_credentials_with_provider pattern:
construct from a dict that has azure_ad_token alongside other
fields, dump, expect azure_ad_token to ride through alongside
the other declared fields."""
from litellm.types.router import CredentialLiteLLMParams
source = {
"api_base": "https://my.openai.azure.com",
"api_version": "2024-08-01-preview",
"azure_ad_token": "tok-123",
"api_key": None, # M2M deployment has no static key
}
rebuilt = CredentialLiteLLMParams(
**{k: v for k, v in source.items() if v is not None}
).model_dump(exclude_none=True)
assert rebuilt.get("azure_ad_token") == "tok-123"
assert rebuilt.get("api_base") == "https://my.openai.azure.com"
assert "api_key" not in rebuilt
class TestRouterCredentialResolution:
"""The actual fix surface: Router.get_deployment_credentials_with_provider
must preserve azure_ad_token on the resolved credentials dict so the
files endpoint can forward it to the Azure files client."""
def test_credentials_preserve_azure_ad_token(self):
from litellm import Router
deployment_id = "azure-m2m-deployment-fixed-uuid"
router = Router(
model_list=[
{
"model_name": "gpt-4o-azure-m2m",
"litellm_params": {
"model": "azure/gpt-4o",
"api_base": "https://my.openai.azure.com",
"api_version": "2024-08-01-preview",
"azure_ad_token": "tok-azure-m2m-xyz",
},
"model_info": {"id": deployment_id},
}
]
)
credentials = router.get_deployment_credentials_with_provider(
model_id=deployment_id
)
assert credentials is not None
assert credentials.get("azure_ad_token") == "tok-azure-m2m-xyz", (
"Router credential resolution dropped azure_ad_token; the "
"files / batches / passthrough callers will not be able to "
"authenticate against Azure (#30235)"
)
def test_credentials_static_api_key_unaffected(self):
"""Don't break the pre-fix happy path: a deployment with a
static api_key (no azure_ad_token) keeps its api_key and
azure_ad_token doesn't appear in the dump."""
from litellm import Router
deployment_id = "azure-static-key-deployment-fixed-uuid"
router = Router(
model_list=[
{
"model_name": "gpt-4o-azure-static",
"litellm_params": {
"model": "azure/gpt-4o",
"api_base": "https://my.openai.azure.com",
"api_version": "2024-08-01-preview",
"api_key": "sk-static-key",
},
"model_info": {"id": deployment_id},
}
]
)
credentials = router.get_deployment_credentials_with_provider(
model_id=deployment_id
)
assert credentials is not None
assert credentials.get("api_key") == "sk-static-key"
assert "azure_ad_token" not in credentials
class TestRouterCredentialResolutionS3OutputBucket:
"""Same strict-dump trap as azure_ad_token (#30235), for Bedrock batch
file retrieval (#26335). Bedrock batch outputs land in a per-model
``s3_output_bucket_name`` when it differs from the input bucket. The
file-content retrieval path validates a file id against the buckets in the
trusted credential snapshot, and that snapshot is built by round-tripping
the deployment's ``litellm_params`` through ``CredentialLiteLLMParams``. If
the field is undeclared it is dropped, so the output bucket never reaches
retrieval and output-bucket file ids are rejected as foreign."""
def test_credentials_preserve_s3_output_bucket_name(self):
from litellm import Router
deployment_id = "bedrock-batch-output-bucket-fixed-uuid"
router = Router(
model_list=[
{
"model_name": "bedrock-batch",
"litellm_params": {
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"s3_bucket_name": "in-bucket",
"s3_output_bucket_name": "out-bucket",
"aws_region_name": "us-west-2",
},
"model_info": {"id": deployment_id},
}
]
)
credentials = router.get_deployment_credentials_with_provider(
model_id=deployment_id
)
assert credentials is not None
assert credentials.get("s3_output_bucket_name") == "out-bucket", (
"Router credential resolution dropped s3_output_bucket_name; "
"Bedrock batch file-content retrieval will reject output-bucket "
"file ids as foreign for model-routed deployments (#26335)"
)
assert credentials.get("s3_bucket_name") == "in-bucket"
def test_credentials_without_output_bucket_unaffected(self):
"""A deployment that configures only the input bucket keeps it and does
not gain a phantom output bucket in the resolved credentials."""
from litellm import Router
deployment_id = "bedrock-batch-input-only-fixed-uuid"
router = Router(
model_list=[
{
"model_name": "bedrock-batch-input-only",
"litellm_params": {
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"s3_bucket_name": "in-bucket",
"aws_region_name": "us-west-2",
},
"model_info": {"id": deployment_id},
}
]
)
credentials = router.get_deployment_credentials_with_provider(
model_id=deployment_id
)
assert credentials is not None
assert credentials.get("s3_bucket_name") == "in-bucket"
assert "s3_output_bucket_name" not in credentials