|
27 | 27 | from eodag.config import load_default_config |
28 | 28 | from eodag.plugins.download.base import Download |
29 | 29 | from eodag.plugins.manager import PluginManager |
| 30 | +from eodag.utils.exceptions import ValidationError |
| 31 | + |
| 32 | +from stac_fastapi.eodag.config import get_settings |
30 | 33 |
|
31 | 34 |
|
32 | 35 | @pytest.mark.parametrize("post_data", [{"foo": "bar"}, {}]) |
@@ -99,6 +102,7 @@ async def run(): |
99 | 102 | collection=collection_id, |
100 | 103 | provider=None, |
101 | 104 | **{f"ecmwf:{k}": v for k, v in post_data.items()}, |
| 105 | + validate=True, |
102 | 106 | ), |
103 | 107 | ) |
104 | 108 |
|
@@ -190,6 +194,7 @@ async def run(): |
190 | 194 | collection=collection_id, |
191 | 195 | provider=None, |
192 | 196 | **{f"ecmwf:{k}": v for k, v in post_data.items()}, |
| 197 | + validate=True, |
193 | 198 | ), |
194 | 199 | ) |
195 | 200 |
|
@@ -336,3 +341,128 @@ async def test_order_not_order_id_ko(request_not_found, mock_search, mock_order) |
336 | 341 | post_data={}, |
337 | 342 | error_message="Download order failed.", |
338 | 343 | ) |
| 344 | + |
| 345 | + |
| 346 | +@pytest.mark.parametrize("validate", [True, False]) |
| 347 | +async def test_order_validate(request_valid, settings_cache_clear, validate): |
| 348 | + """Product order through eodag server must be validated according to settings""" |
| 349 | + get_settings().validate_request = validate |
| 350 | + post_data = {"foo": "bar"} |
| 351 | + federation_backend = "cop_ads" |
| 352 | + collection_id = "CAMS_EAC4" |
| 353 | + expected_search_kwargs = dict( |
| 354 | + collection=collection_id, |
| 355 | + provider=None, |
| 356 | + validate=validate, |
| 357 | + **{f"ecmwf:{k}": v for k, v in post_data.items()}, |
| 358 | + ) |
| 359 | + url = f"collections/{collection_id}/order" |
| 360 | + product = EOProduct( |
| 361 | + federation_backend, |
| 362 | + dict( |
| 363 | + geometry="POINT (0 0)", |
| 364 | + title="dummy_product", |
| 365 | + id="dummy_id", |
| 366 | + ), |
| 367 | + ) |
| 368 | + product.collection = collection_id |
| 369 | + |
| 370 | + product_dataset = "cams-global-reanalysis-eac4" |
| 371 | + endpoint = "https://ads.atmosphere.copernicus.eu/api/retrieve/v1" |
| 372 | + product.properties["eodag:order_link"] = ( |
| 373 | + f"{endpoint}/processes/{product_dataset}/execution" + '?{"inputs": {"qux": "quux"}}' |
| 374 | + ) |
| 375 | + |
| 376 | + # order an offline product |
| 377 | + product.properties["order:status"] = OFFLINE_STATUS |
| 378 | + |
| 379 | + # add auth and download plugins to make the order works |
| 380 | + plugins_manager = PluginManager(load_default_config()) |
| 381 | + download_plugin = plugins_manager.get_download_plugin(product) |
| 382 | + auth_plugin = plugins_manager.get_auth_plugin(download_plugin, product) |
| 383 | + auth_plugin.config.credentials = {"apikey": "anicekey"} |
| 384 | + product.register_downloader(download_plugin, auth_plugin) |
| 385 | + |
| 386 | + product_id = product.properties["id"] |
| 387 | + |
| 388 | + @responses.activate(registry=responses.registries.OrderedRegistry) |
| 389 | + async def run(): |
| 390 | + responses.add( |
| 391 | + responses.POST, |
| 392 | + f"{endpoint}/processes/{product_dataset}/execution", |
| 393 | + status=200, |
| 394 | + content_type="application/json", |
| 395 | + body=f'{{"status": "accepted", "jobID": "{product_id}"}}'.encode("utf-8"), |
| 396 | + auto_calculate_content_length=True, |
| 397 | + ) |
| 398 | + responses.add( |
| 399 | + responses.GET, |
| 400 | + f"{endpoint}/jobs/{product_id}", |
| 401 | + status=200, |
| 402 | + content_type="application/json", |
| 403 | + body=f'{{"status": "successful", "jobID": "{product_id}"}}'.encode("utf-8"), |
| 404 | + auto_calculate_content_length=True, |
| 405 | + ) |
| 406 | + responses.add( |
| 407 | + responses.GET, |
| 408 | + f"{endpoint}/jobs/{product_id}/results", |
| 409 | + status=200, |
| 410 | + content_type="application/json", |
| 411 | + body=(f'{{"asset": {{"value": {{"href": "http://somewhere/download/{product_id}"}} }} }}'.encode("utf-8")), |
| 412 | + auto_calculate_content_length=True, |
| 413 | + ) |
| 414 | + |
| 415 | + await request_valid( |
| 416 | + url=url, |
| 417 | + method="POST", |
| 418 | + post_data=post_data, |
| 419 | + search_result=SearchResult([product]), |
| 420 | + expected_search_kwargs=expected_search_kwargs, |
| 421 | + ) |
| 422 | + |
| 423 | + await run() |
| 424 | + |
| 425 | + |
| 426 | +async def test_order_validate_with_errors(app, app_client, mocker, settings_cache_clear): |
| 427 | + """Order a product through eodag server with invalid parameters must return informative error message""" |
| 428 | + get_settings().validate_request = True |
| 429 | + collection_id = "AG_ERA5" |
| 430 | + errors = [ |
| 431 | + ("wekeo_ecmwf", ValidationError("2 error(s). ecmwf:version: Field required; ecmwf:variable: Field required")), |
| 432 | + ("cop_cds", ValidationError("2 error(s). ecmwf:version: Field required; ecmwf:variable: Field required")), |
| 433 | + ] |
| 434 | + expected_response = { |
| 435 | + "code": "400", |
| 436 | + "description": "Something went wrong", |
| 437 | + "errors": [ |
| 438 | + { |
| 439 | + "provider": "wekeo_ecmwf", |
| 440 | + "error": "ValidationError", |
| 441 | + "status_code": 400, |
| 442 | + "message": "2 error(s). ecmwf:version: Field required; ecmwf:variable: Field required", |
| 443 | + }, |
| 444 | + { |
| 445 | + "provider": "cop_cds", |
| 446 | + "error": "ValidationError", |
| 447 | + "status_code": 400, |
| 448 | + "message": "2 error(s). ecmwf:version: Field required; ecmwf:variable: Field required", |
| 449 | + }, |
| 450 | + ], |
| 451 | + } |
| 452 | + |
| 453 | + mock_search = mocker.patch.object(app.state.dag, "search") |
| 454 | + mock_search.return_value = SearchResult([], 0, errors) |
| 455 | + |
| 456 | + response = await app_client.request( |
| 457 | + "POST", |
| 458 | + f"/collections/{collection_id}/order", |
| 459 | + json=None, |
| 460 | + follow_redirects=True, |
| 461 | + headers={}, |
| 462 | + ) |
| 463 | + response_content = response.json() |
| 464 | + |
| 465 | + assert response.status_code == 400 |
| 466 | + assert "ticket" in response_content |
| 467 | + response_content.pop("ticket", None) |
| 468 | + assert expected_response == response_content |
0 commit comments