|
24 | 24 | from hexkit.utils import now_utc_ms_prec |
25 | 25 |
|
26 | 26 | from tests.fixtures import ConfigFixture |
27 | | -from uos.core.models import ResearchDataUploadBox |
| 27 | +from uos.core.models import GrantWithBoxInfo, ResearchDataUploadBox |
28 | 28 | from uos.inject import prepare_rest_app |
29 | 29 | from uos.ports.inbound.orchestrator import UploadOrchestratorPort |
30 | 30 | from uos.ports.outbound.http import UCSClientPort |
@@ -288,15 +288,15 @@ async def test_update_research_data_upload_box( |
288 | 288 | async def test_grant_upload_access( |
289 | 289 | config: ConfigFixture, ds_auth_headers, user_auth_headers, bad_auth_headers |
290 | 290 | ): |
291 | | - """Test the POST /access-grant endpoint""" |
| 291 | + """Test the POST /access-grants endpoint""" |
292 | 292 | orchestrator = AsyncMock() |
293 | 293 | async with ( |
294 | 294 | prepare_rest_app( |
295 | 295 | config=config.config, upload_orchestrator_override=orchestrator |
296 | 296 | ) as app, |
297 | 297 | AsyncTestClient(app=app) as rest_client, |
298 | 298 | ): |
299 | | - url = "/access-grant" |
| 299 | + url = "/access-grants" |
300 | 300 | request_data = { |
301 | 301 | "user_id": str(uuid4()), |
302 | 302 | "iva_id": str(uuid4()), |
@@ -395,3 +395,113 @@ async def test_list_upload_box_files( |
395 | 395 | orchestrator.get_upload_box_files.side_effect = TypeError() |
396 | 396 | response = await rest_client.get(url, headers=user_auth_headers) |
397 | 397 | assert response.status_code == 500 |
| 398 | + |
| 399 | + |
| 400 | +async def test_revoke_upload_access_grant( |
| 401 | + config: ConfigFixture, ds_auth_headers, user_auth_headers, bad_auth_headers |
| 402 | +): |
| 403 | + """Test the DELETE /access-grants/{grant_id} endpoint""" |
| 404 | + orchestrator = AsyncMock() |
| 405 | + test_grant_id = uuid4() |
| 406 | + |
| 407 | + async with ( |
| 408 | + prepare_rest_app( |
| 409 | + config=config.config, upload_orchestrator_override=orchestrator |
| 410 | + ) as app, |
| 411 | + AsyncTestClient(app=app) as rest_client, |
| 412 | + ): |
| 413 | + url = f"/access-grants/{test_grant_id}" |
| 414 | + |
| 415 | + # unauthenticated |
| 416 | + response = await rest_client.delete(url) |
| 417 | + assert response.status_code == 403 |
| 418 | + |
| 419 | + # bad credentials |
| 420 | + response = await rest_client.delete(url, headers=bad_auth_headers) |
| 421 | + assert response.status_code == 401 |
| 422 | + |
| 423 | + # normal response but user is not a data steward (no data_steward role) |
| 424 | + response = await rest_client.delete(url, headers=user_auth_headers) |
| 425 | + assert response.status_code == 403 |
| 426 | + |
| 427 | + # normal response with data steward role |
| 428 | + orchestrator.revoke_upload_access_grant.return_value = None |
| 429 | + response = await rest_client.delete(url, headers=ds_auth_headers) |
| 430 | + assert response.status_code == 204 |
| 431 | + |
| 432 | + # handle grant not found error from core |
| 433 | + orchestrator.reset_mock() |
| 434 | + orchestrator.revoke_upload_access_grant.side_effect = ( |
| 435 | + UploadOrchestratorPort.GrantNotFoundError(grant_id=test_grant_id) |
| 436 | + ) |
| 437 | + response = await rest_client.delete(url, headers=ds_auth_headers) |
| 438 | + assert response.status_code == 404 |
| 439 | + |
| 440 | + # handle other exception |
| 441 | + orchestrator.reset_mock() |
| 442 | + orchestrator.revoke_upload_access_grant.side_effect = TypeError() |
| 443 | + response = await rest_client.delete(url, headers=ds_auth_headers) |
| 444 | + assert response.status_code == 500 |
| 445 | + |
| 446 | + |
| 447 | +async def test_get_upload_access_grants( |
| 448 | + config: ConfigFixture, ds_auth_headers, user_auth_headers, bad_auth_headers |
| 449 | +): |
| 450 | + """Test the GET /access-grants endpoint""" |
| 451 | + orchestrator = AsyncMock() |
| 452 | + async with ( |
| 453 | + prepare_rest_app( |
| 454 | + config=config.config, upload_orchestrator_override=orchestrator |
| 455 | + ) as app, |
| 456 | + AsyncTestClient(app=app) as rest_client, |
| 457 | + ): |
| 458 | + url = "/access-grants" |
| 459 | + |
| 460 | + # unauthenticated |
| 461 | + response = await rest_client.get(url) |
| 462 | + assert response.status_code == 403 |
| 463 | + |
| 464 | + # bad credentials |
| 465 | + response = await rest_client.get(url, headers=bad_auth_headers) |
| 466 | + assert response.status_code == 401 |
| 467 | + |
| 468 | + # normal response but user is not a data steward (no data_steward role) |
| 469 | + response = await rest_client.get(url, headers=user_auth_headers) |
| 470 | + assert response.status_code == 403 |
| 471 | + |
| 472 | + test_grants = [ |
| 473 | + GrantWithBoxInfo( |
| 474 | + id=uuid4(), |
| 475 | + user_id=uuid4(), |
| 476 | + iva_id=uuid4(), |
| 477 | + box_id=TEST_BOX_ID, |
| 478 | + created=now_utc_ms_prec(), |
| 479 | + valid_from=now_utc_ms_prec(), |
| 480 | + valid_until=now_utc_ms_prec() + timedelta(days=7), |
| 481 | + user_name="Test User", |
| 482 | + |
| 483 | + user_title="Dr.", |
| 484 | + title="Test Box", |
| 485 | + description="Test box description", |
| 486 | + ) |
| 487 | + ] |
| 488 | + orchestrator.get_upload_access_grants.return_value = test_grants |
| 489 | + response = await rest_client.get(url, headers=ds_auth_headers) |
| 490 | + assert response.status_code == 200 |
| 491 | + assert response.json() == [ |
| 492 | + grant.model_dump(mode="json") for grant in test_grants |
| 493 | + ] |
| 494 | + |
| 495 | + # test with query parameters |
| 496 | + response = await rest_client.get( |
| 497 | + url, |
| 498 | + headers=ds_auth_headers, |
| 499 | + params={"user_id": str(uuid4()), "valid": "true"}, |
| 500 | + ) |
| 501 | + assert response.status_code == 200 |
| 502 | + |
| 503 | + # handle other exception |
| 504 | + orchestrator.reset_mock() |
| 505 | + orchestrator.get_upload_access_grants.side_effect = TypeError() |
| 506 | + response = await rest_client.get(url, headers=ds_auth_headers) |
| 507 | + assert response.status_code == 500 |
0 commit comments