fix: require api auth in server factory
This commit is contained in:
@@ -24,6 +24,9 @@ except ImportError:
|
||||
|
||||
pytest_plugins = ("pytest_asyncio",)
|
||||
|
||||
API_KEY = "secret"
|
||||
AUTH_HEADERS = {"Authorization": f"Bearer {API_KEY}"}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Unit tests for SSE helpers
|
||||
@@ -101,11 +104,12 @@ async def aiohttp_client():
|
||||
async def test_stream_true_returns_sse(aiohttp_client) -> None:
|
||||
"""stream=true should return text/event-stream with SSE chunks."""
|
||||
agent = _make_streaming_agent(["Hello", " world"])
|
||||
app = create_app(agent, model_name="test-model")
|
||||
app = create_app(agent, model_name="test-model", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": True},
|
||||
)
|
||||
assert resp.status == 200
|
||||
@@ -136,11 +140,12 @@ async def test_stream_false_returns_json(aiohttp_client) -> None:
|
||||
agent.close_mcp = AsyncMock()
|
||||
agent._last_usage = {}
|
||||
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": False},
|
||||
)
|
||||
assert resp.status == 200
|
||||
@@ -159,11 +164,12 @@ async def test_stream_default_is_false(aiohttp_client) -> None:
|
||||
agent.close_mcp = AsyncMock()
|
||||
agent._last_usage = {}
|
||||
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}]},
|
||||
)
|
||||
assert resp.status == 200
|
||||
@@ -176,11 +182,12 @@ async def test_stream_default_is_false(aiohttp_client) -> None:
|
||||
async def test_stream_sse_chunk_ids_are_consistent(aiohttp_client) -> None:
|
||||
"""All SSE chunks in a single stream should share the same id."""
|
||||
agent = _make_streaming_agent(["A", "B", "C"])
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "go"}], "stream": True},
|
||||
)
|
||||
body = await resp.text()
|
||||
@@ -214,11 +221,12 @@ async def test_stream_passes_on_stream_callbacks(aiohttp_client) -> None:
|
||||
agent.close_mcp = AsyncMock()
|
||||
agent._last_usage = {}
|
||||
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": True},
|
||||
)
|
||||
assert resp.status == 200
|
||||
@@ -247,11 +255,12 @@ async def test_stream_segment_end_does_not_close_sse(aiohttp_client) -> None:
|
||||
agent.close_mcp = AsyncMock()
|
||||
agent._last_usage = {}
|
||||
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "use a tool"}], "stream": True},
|
||||
)
|
||||
|
||||
@@ -286,11 +295,12 @@ async def test_stream_uses_final_response_when_no_deltas(aiohttp_client) -> None
|
||||
agent.close_mcp = AsyncMock()
|
||||
agent._last_usage = {}
|
||||
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": True},
|
||||
)
|
||||
|
||||
@@ -328,11 +338,12 @@ async def test_stream_with_session_id(aiohttp_client) -> None:
|
||||
agent.close_mcp = AsyncMock()
|
||||
agent._last_usage = {}
|
||||
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={
|
||||
"messages": [{"role": "user", "content": "hi"}],
|
||||
"stream": True,
|
||||
@@ -357,11 +368,12 @@ async def test_streaming_backend_failure_does_not_emit_success_terminator(aiohtt
|
||||
agent.close_mcp = AsyncMock()
|
||||
agent._last_usage = {}
|
||||
|
||||
app = create_app(agent, model_name="m")
|
||||
app = create_app(agent, model_name="m", api_key=API_KEY)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": True},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user