|
| 1 | +import copy |
| 2 | +from unittest.mock import AsyncMock, MagicMock |
| 3 | + |
1 | 4 | import pytest |
| 5 | +from pytest_mock import MockerFixture |
| 6 | + |
| 7 | +from murfey.server.api.auth import submit_to_auth_endpoint |
2 | 8 |
|
3 | 9 |
|
4 | 10 | def test_check_user(): |
5 | 11 | pass |
6 | 12 |
|
7 | 13 |
|
| 14 | +@pytest.mark.parametrize( |
| 15 | + "test_params", |
| 16 | + ( # URL subpath | Auth type | Status code | Validation result |
| 17 | + ( |
| 18 | + "validate_token", |
| 19 | + "cookie", |
| 20 | + 200, |
| 21 | + True, |
| 22 | + ), |
| 23 | + ( |
| 24 | + "validate_visit_access/some_visit", |
| 25 | + "password", |
| 26 | + 200, |
| 27 | + True, |
| 28 | + ), |
| 29 | + ( |
| 30 | + "validate_instrument_access/some_instrument", |
| 31 | + "cookie", |
| 32 | + 200, |
| 33 | + False, |
| 34 | + ), |
| 35 | + ( |
| 36 | + "validate_token", |
| 37 | + "password", |
| 38 | + 200, |
| 39 | + False, |
| 40 | + ), |
| 41 | + ( |
| 42 | + "validate_visit_access/some_visit", |
| 43 | + "cookie", |
| 44 | + 400, |
| 45 | + True, |
| 46 | + ), |
| 47 | + ( |
| 48 | + "validate_instrument_access/some_instrument", |
| 49 | + "password", |
| 50 | + 400, |
| 51 | + True, |
| 52 | + ), |
| 53 | + ), |
| 54 | +) |
8 | 55 | @pytest.mark.asyncio |
9 | | -async def test_submit_to_auth_endpoint(): |
10 | | - pass |
| 56 | +async def test_submit_to_auth_endpoint( |
| 57 | + mocker: MockerFixture, |
| 58 | + test_params: tuple[str, str, int, bool], |
| 59 | +): |
| 60 | + # Unpack test params |
| 61 | + url_subpath, auth_type, status_code, validation_outcome = test_params |
| 62 | + |
| 63 | + # Patch the auth URL to use |
| 64 | + auth_url = "some_url" |
| 65 | + mocker.patch("murfey.server.api.auth.auth_url", auth_url) |
| 66 | + |
| 67 | + # Patch the security config |
| 68 | + mock_security_config = MagicMock() |
| 69 | + mock_security_config.auth_url = auth_url |
| 70 | + mock_security_config.auth_type = auth_type |
| 71 | + mock_security_config.cookie_key = "_oauth2_proxy" |
| 72 | + mocker.patch("murfey.server.api.auth.security_config", mock_security_config) |
| 73 | + |
| 74 | + # Mock the request being forwarded and its headers and cookies |
| 75 | + mock_headers = { |
| 76 | + "authorization": "Bearer dummy", |
| 77 | + "x-auth-request-access-token": "dummy", |
| 78 | + } |
| 79 | + mock_token = "123456" |
| 80 | + mock_cookies = ( |
| 81 | + {mock_security_config.cookie_key: mock_token} if auth_type == "cookie" else {} |
| 82 | + ) |
| 83 | + |
| 84 | + mock_request = MagicMock() |
| 85 | + mock_request.headers = mock_headers |
| 86 | + |
| 87 | + # Mock the async response |
| 88 | + mock_response = MagicMock() |
| 89 | + mock_response.status = status_code |
| 90 | + mock_response.json = AsyncMock( |
| 91 | + return_value={ |
| 92 | + "valid": validation_outcome, |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + # Mock the async session and the 'get' |
| 97 | + mock_get = AsyncMock() |
| 98 | + mock_get.__aenter__.return_value = mock_response |
| 99 | + |
| 100 | + mock_session = MagicMock() |
| 101 | + mock_session.get.return_value = mock_get |
| 102 | + |
| 103 | + mock_session_context = AsyncMock() |
| 104 | + mock_session_context.__aenter__.return_value = mock_session |
| 105 | + |
| 106 | + mock_client_session = mocker.patch( |
| 107 | + "murfey.server.api.auth.aiohttp.ClientSession", |
| 108 | + return_value=mock_session_context, |
| 109 | + ) |
| 110 | + |
| 111 | + # Run the function and check that the correct calls were made |
| 112 | + result = await submit_to_auth_endpoint( |
| 113 | + url_subpath=url_subpath, |
| 114 | + request=mock_request, |
| 115 | + token=mock_token, |
| 116 | + ) |
| 117 | + |
| 118 | + # Check that aiohttp.ClientSession got called with the correct parameters |
| 119 | + mock_client_session.assert_called_once_with(cookies=mock_cookies) |
| 120 | + |
| 121 | + # Compare the headers passed to 'session.get' against what is expected |
| 122 | + updated_headers = copy.deepcopy(mock_headers) |
| 123 | + if auth_type == "password": |
| 124 | + updated_headers["authorization"] = f"Bearer {mock_token}" |
| 125 | + mock_session.get.assert_called_once_with( |
| 126 | + f"{mock_security_config.auth_url}/{url_subpath}", |
| 127 | + headers=updated_headers, |
| 128 | + ) |
| 129 | + |
| 130 | + # Check that the combination of status code and JSON response are correct |
| 131 | + assert result == {"valid": (validation_outcome if status_code == 200 else False)} |
11 | 132 |
|
12 | 133 |
|
13 | 134 | @pytest.mark.asyncio |
|
0 commit comments