Skip to content

Commit abfac69

Browse files
committed
fix complete upload test
1 parent 961b0d4 commit abfac69

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

services/api-server/src/simcore_service_api_server/services_http/storage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
)
1919
from models_library.api_schemas_storage.storage_schemas import (
2020
FileUploadCompleteFutureResponse,
21+
FileUploadCompleteResponse,
2122
FileUploadCompleteState,
2223
FileUploadSchema,
2324
LinkType,
@@ -30,7 +31,6 @@
3031
from settings_library.tracing import TracingSettings
3132
from simcore_service_api_server.models.schemas.files import UserFile
3233
from simcore_service_api_server.models.schemas.jobs import UserFileToProgramJob
33-
from simcore_service_storage.api.rest._files import FileUploadCompleteResponse
3434
from starlette.datastructures import URL
3535
from tenacity import (
3636
AsyncRetrying,
@@ -208,7 +208,7 @@ async def complete_file_upload(self, *, user_id: int, file: File) -> ETag:
208208
response.raise_for_status()
209209
file_upload_complete_response = Envelope[
210210
FileUploadCompleteResponse
211-
].model_validate_json(await response.json())
211+
].model_validate_json(response.text)
212212
assert file_upload_complete_response.data # nosec
213213
state_url = f"{file_upload_complete_response.data.links.state}"
214214
async for attempt in AsyncRetrying(
@@ -223,7 +223,7 @@ async def complete_file_upload(self, *, user_id: int, file: File) -> ETag:
223223
resp.raise_for_status()
224224
future_enveloped = Envelope[
225225
FileUploadCompleteFutureResponse
226-
].model_validate_json(await resp.json())
226+
].model_validate_json(resp.text)
227227
assert future_enveloped.data # nosec
228228
if future_enveloped.data.state == FileUploadCompleteState.NOK:
229229
msg = "upload not ready yet"

services/api-server/tests/unit/conftest.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pathlib import Path
1313
from typing import Any
1414
from unittest.mock import MagicMock
15+
from urllib.parse import urlparse, urlunparse
1516

1617
import aiohttp.test_utils
1718
import httpx
@@ -22,13 +23,16 @@
2223
from faker import Faker
2324
from fastapi import FastAPI, status
2425
from fastapi.encoders import jsonable_encoder
25-
from httpx import ASGITransport
26+
from httpx import ASGITransport, Request, Response
2627
from models_library.api_schemas_long_running_tasks.tasks import (
2728
TaskGet,
2829
TaskProgress,
2930
TaskStatus,
3031
)
3132
from models_library.api_schemas_storage.storage_schemas import (
33+
FileUploadCompleteFutureResponse,
34+
FileUploadCompleteResponse,
35+
FileUploadCompleteState,
3236
FileUploadSchema,
3337
HealthCheck,
3438
)
@@ -443,15 +447,51 @@ def mocked_storage_rest_api_base(
443447
)
444448

445449
# Add mocks for completion and abort endpoints
450+
def generate_future_link(request: Request, **kwargs):
451+
parsed_url = urlparse(f"{request.url}")
452+
stripped_url = urlunparse(
453+
(parsed_url.scheme, parsed_url.netloc, parsed_url.path, "", "", "")
454+
)
455+
456+
payload = FileUploadCompleteResponse.model_validate(
457+
{
458+
"links": {
459+
"state": stripped_url
460+
+ ":complete/futures/"
461+
+ str(faker.uuid4())
462+
},
463+
},
464+
)
465+
return Response(
466+
status_code=status.HTTP_200_OK,
467+
json=jsonable_encoder(
468+
Envelope[FileUploadCompleteResponse](data=payload)
469+
),
470+
)
471+
446472
respx_mock.post(
447473
re.compile(
448-
r"^http://[a-z\-_]*storage:[0-9]+/v0/locations/[0-9]+/files/.+complete"
474+
r"^http://[a-z\-_]*storage:[0-9]+/v0/locations/[0-9]+/files/.+complete(?:\?.*)?$"
449475
),
450476
name="complete_upload_file_v0_locations__location_id__files__file_id__complete_post",
477+
).side_effect = generate_future_link
478+
479+
respx_mock.post(
480+
re.compile(
481+
r"^http://[a-z\-_]*storage:[0-9]+/v0/locations/[0-9]+/files/.+complete/futures/.+"
482+
)
451483
).respond(
452-
status.HTTP_202_ACCEPTED,
453-
json=Envelope[dict](data={"state": "ok"}).model_dump(mode="json"),
484+
status_code=status.HTTP_200_OK,
485+
json=jsonable_encoder(
486+
Envelope[FileUploadCompleteFutureResponse](
487+
data=FileUploadCompleteFutureResponse(
488+
state=FileUploadCompleteState.OK,
489+
e_tag="07d1c1a4-b073-4be7-b022-f405d90e99aa",
490+
)
491+
)
492+
),
454493
)
494+
455495
respx_mock.post(
456496
re.compile(
457497
r"^http://[a-z\-_]*storage:[0-9]+/v0/locations/[0-9]+/files/.+:abort$"

services/api-server/tests/unit/test_api_files.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,10 @@ async def test_get_upload_links(
236236
follow_up_request: str,
237237
client: AsyncClient,
238238
auth: httpx.BasicAuth,
239-
storage_v0_service_mock: AioResponsesMock,
239+
mocked_storage_rest_api_base: MockRouter,
240240
):
241241
"""Test that we can get data needed for performing multipart upload directly to S3"""
242242

243-
assert storage_v0_service_mock # nosec
244-
245243
msg = {
246244
"filename": DummyFileData.file().filename,
247245
"filesize": DummyFileData.file_size(),

0 commit comments

Comments
 (0)