Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-47027: Change ws_post_params_dependency to handle multiple occurrences of the same param #317

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/20241022_002510_steliosvoutsinas_DM_47027.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- Delete the sections that don't apply -->

### Bug fixes

- Change uws_post_params_dependency to support reading in multiple of the same param
2 changes: 1 addition & 1 deletion safir/src/safir/uws/_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async def uws_post_params_dependency(
if request.method != "POST":
raise ValueError("uws_post_params_dependency used for non-POST route")
parameters = []
for key, value in (await request.form()).items():
for key, value in (await request.form()).multi_items():
if not isinstance(value, str):
raise TypeError("File upload not supported")
parameters.append(
Expand Down
25 changes: 23 additions & 2 deletions safir/tests/uws/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from collections.abc import AsyncIterator, Iterator
from contextlib import asynccontextmanager
from datetime import timedelta
from typing import Annotated

import pytest
import pytest_asyncio
import respx
import structlog
from asgi_lifespan import LifespanManager
from fastapi import APIRouter, FastAPI
from fastapi import APIRouter, Body, FastAPI
from httpx import ASGITransport, AsyncClient
from structlog.stdlib import BoundLogger

Expand All @@ -22,17 +23,36 @@
from safir.testing.gcs import MockStorageClient, patch_google_storage
from safir.testing.slack import MockSlackWebhook, mock_slack_webhook
from safir.testing.uws import MockUWSJobRunner
from safir.uws import UWSApplication, UWSConfig
from safir.uws import UWSApplication, UWSConfig, UWSJobParameter
from safir.uws._dependencies import UWSFactory, uws_dependency

from ..support.uws import build_uws_config


@pytest.fixture
def post_params_router() -> APIRouter:
"""Return a router that echoes the parameters passed in the request."""
router = APIRouter()

@router.post("/params")
async def post_params(
params: Annotated[list[UWSJobParameter], Body()],
) -> dict[str, list[dict[str, str]]]:
return {
"params": [
{"id": p.parameter_id, "value": p.value} for p in params
]
}

return router


@pytest_asyncio.fixture
async def app(
arq_queue: MockArqQueue,
uws_config: UWSConfig,
logger: BoundLogger,
post_params_router: APIRouter,
) -> AsyncIterator[FastAPI]:
"""Return a configured test application for UWS.

Expand All @@ -56,6 +76,7 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
router = APIRouter(route_class=SlackRouteErrorHandler)
uws.install_handlers(router)
app.include_router(router, prefix="/test")
app.include_router(post_params_router, prefix="/test")
uws.install_error_handlers(app)

async with LifespanManager(app):
Expand Down
30 changes: 30 additions & 0 deletions safir/tests/uws/post_params_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tests for sync cutout requests."""

from __future__ import annotations

import pytest
from httpx import AsyncClient


@pytest.mark.asyncio
async def test_post_params_multiple_params(client: AsyncClient) -> None:
"""Test that the post dependency correctly handles multiple
occurences of the same parameter.
"""
params = [
{"parameter_id": "id", "value": "image1"},
{"parameter_id": "id", "value": "image2"},
{"parameter_id": "pos", "value": "RANGE 10 20 30 40"},
{"parameter_id": "pos", "value": "CIRCLE 10 20 5"},
]

response = await client.post("/test/params", json=params)
assert response.status_code == 200
assert response.json() == {
"params": [
{"id": "id", "value": "image1"},
{"id": "id", "value": "image2"},
{"id": "pos", "value": "RANGE 10 20 30 40"},
{"id": "pos", "value": "CIRCLE 10 20 5"},
]
}