|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | +# pylint: disable=too-many-arguments |
| 5 | + |
| 6 | +from typing import Any |
| 7 | +from urllib.parse import quote |
| 8 | + |
| 9 | +import pytest |
| 10 | +from aiohttp.test_utils import TestClient |
| 11 | +from faker import Faker |
| 12 | +from fastapi_pagination.cursor import CursorPage |
| 13 | +from models_library.api_schemas_storage.storage_schemas import ( |
| 14 | + DatasetMetaDataGet, |
| 15 | + FileLocation, |
| 16 | + FileMetaDataGet, |
| 17 | + FileUploadSchema, |
| 18 | + PathMetaDataGet, |
| 19 | +) |
| 20 | +from models_library.projects_nodes_io import LocationID, StorageFileID |
| 21 | +from pydantic import TypeAdapter |
| 22 | +from pytest_simcore.helpers.assert_checks import assert_status |
| 23 | +from servicelib.aiohttp import status |
| 24 | +from simcore_postgres_database.models.users import UserRole |
| 25 | + |
| 26 | +API_VERSION = "v0" |
| 27 | + |
| 28 | + |
| 29 | +PREFIX = "/" + API_VERSION + "/storage" |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + "user_role,expected", |
| 34 | + [ |
| 35 | + (UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED), |
| 36 | + (UserRole.GUEST, status.HTTP_200_OK), |
| 37 | + (UserRole.USER, status.HTTP_200_OK), |
| 38 | + (UserRole.TESTER, status.HTTP_200_OK), |
| 39 | + ], |
| 40 | +) |
| 41 | +async def test_list_storage_locations( |
| 42 | + client: TestClient, |
| 43 | + logged_user: dict[str, Any], |
| 44 | + expected: int, |
| 45 | +): |
| 46 | + url = "/v0/storage/locations" |
| 47 | + assert url.startswith(PREFIX) |
| 48 | + |
| 49 | + resp = await client.get(url, params={"user_id": logged_user["id"]}) |
| 50 | + data, error = await assert_status(resp, expected) |
| 51 | + |
| 52 | + if not error: |
| 53 | + assert "json_schema_extra" in FileLocation.model_config |
| 54 | + assert isinstance(FileLocation.model_config["json_schema_extra"], dict) |
| 55 | + assert isinstance( |
| 56 | + FileLocation.model_config["json_schema_extra"]["examples"], list |
| 57 | + ) |
| 58 | + assert len(data) == len( |
| 59 | + FileLocation.model_config["json_schema_extra"]["examples"] |
| 60 | + ) |
| 61 | + assert data == FileLocation.model_config["json_schema_extra"]["examples"] |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "user_role,expected", |
| 66 | + [ |
| 67 | + (UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED), |
| 68 | + (UserRole.GUEST, status.HTTP_200_OK), |
| 69 | + (UserRole.USER, status.HTTP_200_OK), |
| 70 | + (UserRole.TESTER, status.HTTP_200_OK), |
| 71 | + ], |
| 72 | +) |
| 73 | +async def test_list_storage_paths( |
| 74 | + client: TestClient, |
| 75 | + logged_user: dict[str, Any], |
| 76 | + expected: int, |
| 77 | + location_id: LocationID, |
| 78 | +): |
| 79 | + assert client.app |
| 80 | + url = client.app.router["list_storage_paths"].url_for(location_id=f"{location_id}") |
| 81 | + |
| 82 | + resp = await client.get(f"{url}", params={"user_id": logged_user["id"]}) |
| 83 | + data, error = await assert_status(resp, expected) |
| 84 | + if not error: |
| 85 | + TypeAdapter(CursorPage[PathMetaDataGet]).validate_python(data) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize( |
| 89 | + "user_role,expected", |
| 90 | + [ |
| 91 | + (UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED), |
| 92 | + (UserRole.GUEST, status.HTTP_200_OK), |
| 93 | + (UserRole.USER, status.HTTP_200_OK), |
| 94 | + (UserRole.TESTER, status.HTTP_200_OK), |
| 95 | + ], |
| 96 | +) |
| 97 | +async def test_list_datasets_metadata( |
| 98 | + client: TestClient, |
| 99 | + logged_user: dict[str, Any], |
| 100 | + expected: int, |
| 101 | +): |
| 102 | + url = "/v0/storage/locations/0/datasets" |
| 103 | + assert url.startswith(PREFIX) |
| 104 | + assert client.app |
| 105 | + _url = client.app.router["list_datasets_metadata"].url_for(location_id="0") |
| 106 | + |
| 107 | + assert url == str(_url) |
| 108 | + |
| 109 | + resp = await client.get(url, params={"user_id": logged_user["id"]}) |
| 110 | + data, error = await assert_status(resp, expected) |
| 111 | + |
| 112 | + if not error: |
| 113 | + assert "json_schema_extra" in DatasetMetaDataGet.model_config |
| 114 | + assert isinstance(DatasetMetaDataGet.model_config["json_schema_extra"], dict) |
| 115 | + assert isinstance( |
| 116 | + DatasetMetaDataGet.model_config["json_schema_extra"]["examples"], list |
| 117 | + ) |
| 118 | + |
| 119 | + assert len(data) == len( |
| 120 | + DatasetMetaDataGet.model_config["json_schema_extra"]["examples"] |
| 121 | + ) |
| 122 | + assert data == DatasetMetaDataGet.model_config["json_schema_extra"]["examples"] |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize( |
| 126 | + "user_role,expected", |
| 127 | + [ |
| 128 | + (UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED), |
| 129 | + (UserRole.GUEST, status.HTTP_200_OK), |
| 130 | + (UserRole.USER, status.HTTP_200_OK), |
| 131 | + (UserRole.TESTER, status.HTTP_200_OK), |
| 132 | + ], |
| 133 | +) |
| 134 | +async def test_list_dataset_files_metadata( |
| 135 | + client: TestClient, |
| 136 | + logged_user: dict[str, Any], |
| 137 | + expected: int, |
| 138 | +): |
| 139 | + url = "/v0/storage/locations/0/datasets/N:asdfsdf/metadata" |
| 140 | + assert url.startswith(PREFIX) |
| 141 | + assert client.app |
| 142 | + _url = client.app.router["list_dataset_files_metadata"].url_for( |
| 143 | + location_id="0", dataset_id="N:asdfsdf" |
| 144 | + ) |
| 145 | + |
| 146 | + assert url == str(_url) |
| 147 | + |
| 148 | + resp = await client.get(url, params={"user_id": logged_user["id"]}) |
| 149 | + data, error = await assert_status(resp, expected) |
| 150 | + |
| 151 | + if not error: |
| 152 | + assert "json_schema_extra" in FileMetaDataGet.model_config |
| 153 | + assert isinstance(FileMetaDataGet.model_config["json_schema_extra"], dict) |
| 154 | + assert isinstance( |
| 155 | + FileMetaDataGet.model_config["json_schema_extra"]["examples"], list |
| 156 | + ) |
| 157 | + assert len(data) == len( |
| 158 | + FileMetaDataGet.model_config["json_schema_extra"]["examples"] |
| 159 | + ) |
| 160 | + assert data == [ |
| 161 | + FileMetaDataGet.model_validate(e).model_dump(mode="json") |
| 162 | + for e in FileMetaDataGet.model_config["json_schema_extra"]["examples"] |
| 163 | + ] |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.parametrize( |
| 167 | + "user_role,expected", |
| 168 | + [ |
| 169 | + (UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED), |
| 170 | + (UserRole.GUEST, status.HTTP_200_OK), |
| 171 | + (UserRole.USER, status.HTTP_200_OK), |
| 172 | + (UserRole.TESTER, status.HTTP_200_OK), |
| 173 | + ], |
| 174 | +) |
| 175 | +async def test_storage_file_meta( |
| 176 | + client: TestClient, |
| 177 | + logged_user: dict[str, Any], |
| 178 | + expected: int, |
| 179 | + faker: Faker, |
| 180 | +): |
| 181 | + # tests redirect of path with quotes in path |
| 182 | + file_id = f"{faker.uuid4()}/{faker.uuid4()}/a/b/c/d/e/dat" |
| 183 | + quoted_file_id = quote(file_id, safe="") |
| 184 | + url = f"/v0/storage/locations/0/files/{quoted_file_id}/metadata" |
| 185 | + |
| 186 | + assert url.startswith(PREFIX) |
| 187 | + |
| 188 | + resp = await client.get(url, params={"user_id": logged_user["id"]}) |
| 189 | + data, error = await assert_status(resp, expected) |
| 190 | + |
| 191 | + if not error: |
| 192 | + assert "json_schema_extra" in FileMetaDataGet.model_config |
| 193 | + assert isinstance(FileMetaDataGet.model_config["json_schema_extra"], dict) |
| 194 | + assert isinstance( |
| 195 | + FileMetaDataGet.model_config["json_schema_extra"]["examples"], list |
| 196 | + ) |
| 197 | + |
| 198 | + assert data |
| 199 | + model = FileMetaDataGet.model_validate(data) |
| 200 | + assert model |
| 201 | + |
| 202 | + |
| 203 | +@pytest.mark.parametrize( |
| 204 | + "user_role,expected", |
| 205 | + [ |
| 206 | + (UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED), |
| 207 | + (UserRole.GUEST, status.HTTP_200_OK), |
| 208 | + (UserRole.USER, status.HTTP_200_OK), |
| 209 | + (UserRole.TESTER, status.HTTP_200_OK), |
| 210 | + ], |
| 211 | +) |
| 212 | +async def test_storage_list_filter( |
| 213 | + client: TestClient, |
| 214 | + logged_user: dict[str, Any], |
| 215 | + expected: int, |
| 216 | +): |
| 217 | + # tests composition of 2 queries |
| 218 | + file_id = "a/b/c/d/e/dat" |
| 219 | + url = "/v0/storage/locations/0/files/metadata?uuid_filter={}".format( |
| 220 | + quote(file_id, safe="") |
| 221 | + ) |
| 222 | + |
| 223 | + assert url.startswith(PREFIX) |
| 224 | + |
| 225 | + resp = await client.get(url, params={"user_id": logged_user["id"]}) |
| 226 | + data, error = await assert_status(resp, expected) |
| 227 | + |
| 228 | + if not error: |
| 229 | + assert "json_schema_extra" in FileMetaDataGet.model_config |
| 230 | + assert isinstance(FileMetaDataGet.model_config["json_schema_extra"], dict) |
| 231 | + assert isinstance( |
| 232 | + FileMetaDataGet.model_config["json_schema_extra"]["examples"], list |
| 233 | + ) |
| 234 | + |
| 235 | + assert len(data) == 2 |
| 236 | + for item in data: |
| 237 | + model = FileMetaDataGet.model_validate(item) |
| 238 | + assert model |
| 239 | + |
| 240 | + |
| 241 | +@pytest.fixture |
| 242 | +def file_id(faker: Faker) -> StorageFileID: |
| 243 | + return TypeAdapter(StorageFileID).validate_python( |
| 244 | + f"{faker.uuid4()}/{faker.uuid4()}/{faker.file_name()} with spaces.dat" |
| 245 | + ) |
| 246 | + |
| 247 | + |
| 248 | +@pytest.mark.parametrize( |
| 249 | + "user_role,expected", |
| 250 | + [ |
| 251 | + (UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED), |
| 252 | + (UserRole.GUEST, status.HTTP_200_OK), |
| 253 | + (UserRole.USER, status.HTTP_200_OK), |
| 254 | + (UserRole.TESTER, status.HTTP_200_OK), |
| 255 | + ], |
| 256 | +) |
| 257 | +async def test_upload_file( |
| 258 | + client: TestClient, |
| 259 | + logged_user: dict[str, Any], |
| 260 | + expected: int, |
| 261 | + file_id: StorageFileID, |
| 262 | +): |
| 263 | + url = f"/v0/storage/locations/0/files/{quote(file_id, safe='')}" |
| 264 | + |
| 265 | + assert url.startswith(PREFIX) |
| 266 | + |
| 267 | + resp = await client.put(url, params={"user_id": logged_user["id"]}) |
| 268 | + data, error = await assert_status(resp, expected) |
| 269 | + if not error: |
| 270 | + assert not error |
| 271 | + assert data |
| 272 | + file_upload_schema = FileUploadSchema.model_validate(data) |
| 273 | + |
| 274 | + # let's abort |
| 275 | + resp = await client.post( |
| 276 | + f"{file_upload_schema.links.abort_upload.path}", |
| 277 | + params={"user_id": logged_user["id"]}, |
| 278 | + ) |
| 279 | + data, error = await assert_status(resp, status.HTTP_204_NO_CONTENT) |
| 280 | + assert not error |
| 281 | + assert not data |
0 commit comments