|
9 | 9 | import json |
10 | 10 | import uuid |
11 | 11 | from http import HTTPStatus |
| 12 | +from unittest.mock import patch |
12 | 13 |
|
13 | 14 | import pytest |
14 | 15 | from aiohttp.test_utils import TestClient |
@@ -243,3 +244,93 @@ async def test_patch_project_with_client_session_header( |
243 | 244 | ) |
244 | 245 | # This should fail validation since it's not a proper UUID |
245 | 246 | await assert_status(resp, status.HTTP_422_UNPROCESSABLE_ENTITY) |
| 247 | + |
| 248 | + |
| 249 | +@pytest.mark.parametrize( |
| 250 | + "user_role,expected", [(UserRole.USER, status.HTTP_204_NO_CONTENT)] |
| 251 | +) |
| 252 | +async def test_patch_project_with_mocked_header_parsing( |
| 253 | + client: TestClient, |
| 254 | + logged_user: UserInfoDict, |
| 255 | + user_project: ProjectDict, |
| 256 | + expected: HTTPStatus, |
| 257 | +): |
| 258 | + """Test that header_params = parse_request_headers_as(ClientSessionHeaderParams, request) works correctly""" |
| 259 | + assert client.app |
| 260 | + base_url = client.app.router["patch_project"].url_for( |
| 261 | + project_id=user_project["uuid"] |
| 262 | + ) |
| 263 | + |
| 264 | + # Generate a valid client session ID |
| 265 | + test_client_session_id = str(uuid.uuid4()) |
| 266 | + |
| 267 | + # Mock the _projects_service.patch_project_for_user to spy on the client_session_id parameter |
| 268 | + with patch( |
| 269 | + "simcore_service_webserver.projects._controller.projects_rest._projects_service.patch_project_for_user" |
| 270 | + ) as mock_patch_project_service: |
| 271 | + # Make the service call succeed |
| 272 | + mock_patch_project_service.return_value = None |
| 273 | + |
| 274 | + # Make the PATCH request with client session header |
| 275 | + resp = await client.patch( |
| 276 | + f"{base_url}", |
| 277 | + data=json.dumps( |
| 278 | + { |
| 279 | + "name": "testing-name-with-mocked-header", |
| 280 | + "description": "testing-description-with-mocked-header", |
| 281 | + } |
| 282 | + ), |
| 283 | + headers={"X-Client-Session-Id": test_client_session_id}, |
| 284 | + ) |
| 285 | + await assert_status(resp, expected) |
| 286 | + |
| 287 | + # Verify that patch_project_for_user was called with the correct client_session_id |
| 288 | + mock_patch_project_service.assert_called_once() |
| 289 | + call_args = mock_patch_project_service.call_args |
| 290 | + |
| 291 | + # Extract the client_session_id from the call arguments |
| 292 | + assert "client_session_id" in call_args.kwargs |
| 293 | + assert call_args.kwargs["client_session_id"] == test_client_session_id |
| 294 | + |
| 295 | + |
| 296 | +@pytest.mark.parametrize( |
| 297 | + "user_role,expected", [(UserRole.USER, status.HTTP_204_NO_CONTENT)] |
| 298 | +) |
| 299 | +async def test_patch_project_without_client_session_header( |
| 300 | + client: TestClient, |
| 301 | + logged_user: UserInfoDict, |
| 302 | + user_project: ProjectDict, |
| 303 | + expected: HTTPStatus, |
| 304 | +): |
| 305 | + """Test patch project works when X-Client-Session-Id header is not provided""" |
| 306 | + assert client.app |
| 307 | + base_url = client.app.router["patch_project"].url_for( |
| 308 | + project_id=user_project["uuid"] |
| 309 | + ) |
| 310 | + |
| 311 | + # Mock the _projects_service.patch_project_for_user to spy on the client_session_id parameter |
| 312 | + with patch( |
| 313 | + "simcore_service_webserver.projects._controller.projects_rest._projects_service.patch_project_for_user" |
| 314 | + ) as mock_patch_project_service: |
| 315 | + # Make the service call succeed |
| 316 | + mock_patch_project_service.return_value = None |
| 317 | + |
| 318 | + # Make the PATCH request WITHOUT client session header |
| 319 | + resp = await client.patch( |
| 320 | + f"{base_url}", |
| 321 | + data=json.dumps( |
| 322 | + { |
| 323 | + "name": "testing-name-without-header", |
| 324 | + "description": "testing-description-without-header", |
| 325 | + } |
| 326 | + ), |
| 327 | + ) |
| 328 | + await assert_status(resp, expected) |
| 329 | + |
| 330 | + # Verify that patch_project_for_user was called with client_session_id=None |
| 331 | + mock_patch_project_service.assert_called_once() |
| 332 | + call_args = mock_patch_project_service.call_args |
| 333 | + |
| 334 | + # Extract the client_session_id from the call arguments |
| 335 | + assert "client_session_id" in call_args.kwargs |
| 336 | + assert call_args.kwargs["client_session_id"] is None |
0 commit comments