litellm/tests/unit/proxy/image_endpoints/test_azure_routes.py
devin-ai-integration[bot] 1dbde0845e
test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758)
* test: move guardrails, image, audio, batches, search legacy tests to unit

* test: restore the custom guardrail and remaining chunk 1 unit rows

* test: move the ocr matrix to e2e

* test: drop comments from moved tests and fix vertex ocr credentials

* test: move litellm utils and unified google legacy tests to their tiers

* test: move logging callback and pass-through unit legacy tests to their tiers

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

* test: move store_model_in_db legacy tests and start the admin ui migration

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

* test: finish the proxy job rows and drop their CircleCI jobs

Moves the remaining multi-instance, openai endpoint, otel, pass-through and
top-level proxy tests into tests/integration with scripted upstreams, rewrites
the weak replacements to assert the outbound wire and exact responses, and
removes the build_and_test job group this PR owns from CircleCI

* test: tighten the key generation and error log assertions, rename migrated unit files to their mapped names

* test: shard the pagerduty unit tests and record the rust harness file on the coverage allowlist

* test: drive the SSO sign-in tests through a scripted IdP and isolate the cost center stand-in

* test: type the e2e beta header, prompt cache and secret manager values

* test: assert the missing model error message on the OpenAI routes

* test: drop the deleted otel base class import and restore mcp redaction and exact langfuse usage checks

* test: make the langfuse fixture directory a package for the layout check

* test: restore collected no-match cases, real langfuse and gcs payload checks, and gate the aws secret lane

* test: keep the ocr e2e pdf fixture next to the test

* test: put meta below covers on the streaming tool search e2e

* test: run the OCR matrix against a scripted upstream and fix two runner-only e2e failures

* test: count only OCR calls in the OCR matrix recorder

* test: pin OCR parse bodies and billed cost, group new e2e cases into classes

* ci: restore the python image anchor dropped in the main sync

---------

Co-authored-by: yuneng <yuneng@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-11 00:17:21 -07:00

138 lines
4.6 KiB
Python

import asyncio
from pathlib import Path
from unittest import mock
import pytest
from fastapi.testclient import TestClient
import litellm
from litellm.proxy.proxy_server import app, initialize
example_image_generation_result = {
"created": 1589478378,
"data": [{"url": "https://example.com/image.png"}],
}
example_image_edit_result = {
"created": 1589478400,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="
}
],
}
def mock_patch_aimage_generation():
"""Patch the underlying image generation call used by the Router."""
mock_obj = mock.AsyncMock(return_value=example_image_generation_result)
mock_obj.__name__ = "aimage_generation"
return mock.patch(
"litellm.aimage_generation",
new_callable=lambda: mock_obj,
)
def mock_patch_aimage_edit():
"""Patch the underlying image edit call used by the Router."""
mock_obj = mock.AsyncMock(return_value=example_image_edit_result)
mock_obj.__name__ = "aimage_edit"
return mock.patch(
"litellm.aimage_edit",
new_callable=lambda: mock_obj,
)
@pytest.fixture(scope="function")
def client_no_auth():
from litellm.proxy.proxy_server import cleanup_router_config_variables
cleanup_router_config_variables()
repo_root = Path(__file__).resolve().parents[4]
config_fp = (
repo_root
/ "tests"
/ "unit"
/ "proxy"
/ "test_configs"
/ "test_config_no_auth.yaml"
)
config_fp = str(config_fp)
# Create mock objects with __name__ attribute
mock_generation = mock.AsyncMock(return_value=example_image_generation_result)
mock_generation.__name__ = "aimage_generation"
mock_edit = mock.AsyncMock(return_value=example_image_edit_result)
mock_edit.__name__ = "aimage_edit"
with (
mock.patch(
"litellm.aimage_generation",
new_callable=lambda: mock_generation,
) as patched_generation,
mock.patch(
"litellm.aimage_edit",
new_callable=lambda: mock_edit,
) as patched_edit,
):
asyncio.run(initialize(config=config_fp, debug=True))
client = TestClient(app)
yield client, patched_generation, patched_edit
def test_azure_image_generation_route(client_no_auth):
client, mock_aimage_generation, _ = client_no_auth
test_data = {"prompt": "A cute baby sea otter", "n": 1, "size": "1024x1024"}
response = client.post(
"/openai/deployments/dall-e-3/images/generations", json=test_data
)
mock_aimage_generation.assert_called_once()
call_kwargs = mock_aimage_generation.call_args.kwargs
assert "dall-e-3" in call_kwargs["model"]
assert call_kwargs["prompt"] == "A cute baby sea otter"
assert call_kwargs["n"] == 1
assert call_kwargs["size"] == "1024x1024"
assert response.status_code == 200
assert response.json()["data"]
def test_azure_image_edit_route(client_no_auth):
litellm.turn_on_debug()
client, _, mock_aimage_edit = client_no_auth
files = {"image": ("test_image.png", b"\x89PNG\r\n\x1a\nimage-bytes", "image/png")}
data = {"prompt": "A cute baby sea otter"}
response = client.post("/openai/deployments/dall-e-3/images/edits", files=files, data=data)
mock_aimage_edit.assert_called_once()
called_kwargs = mock_aimage_edit.call_args.kwargs
assert "dall-e-3" in called_kwargs["model"]
assert called_kwargs["prompt"] == "A cute baby sea otter"
assert response.status_code == 200
assert response.json()["data"]
def test_azure_image_generation_route_rejects_url_valued_path_model(client_no_auth):
"""A URL-valued deployment segment is refused before any provider call."""
client, mock_aimage_generation, _ = client_no_auth
response = client.post(
"/openai/deployments/oobabooga/https://example.invalid/images/generations",
json={"prompt": "A cute baby sea otter", "n": 1, "size": "1024x1024"},
)
assert response.status_code == 400
assert "URL-valued" in response.text
mock_aimage_generation.assert_not_called()
def test_azure_image_generation_route_allows_ordinary_path_model(client_no_auth):
"""A deployment name that merely contains a provider prefix still routes."""
client, mock_aimage_generation, _ = client_no_auth
response = client.post(
"/openai/deployments/dall-e-3/images/generations",
json={"prompt": "A cute baby sea otter", "n": 1, "size": "1024x1024"},
)
assert response.status_code == 200
mock_aimage_generation.assert_called_once()