|
7 | 7 |
|
8 | 8 |
|
9 | 9 | import asyncio |
10 | | -from collections.abc import AsyncGenerator |
| 10 | +from collections.abc import AsyncGenerator, AsyncIterator |
11 | 11 | from http import HTTPStatus |
12 | 12 | from typing import Any |
13 | 13 | from unittest.mock import AsyncMock |
|
23 | 23 | from models_library.api_schemas_webserver.users import ( |
24 | 24 | UserAccountGet, |
25 | 25 | ) |
| 26 | +from models_library.groups import AccessRightsDict |
26 | 27 | from models_library.products import ProductName |
27 | 28 | from models_library.rest_pagination import Page |
28 | 29 | from pytest_mock import MockerFixture |
|
36 | 37 | from pytest_simcore.helpers.webserver_login import ( |
37 | 38 | UserInfoDict, |
38 | 39 | ) |
| 40 | +from pytest_simcore.helpers.webserver_users import NewUser |
39 | 41 | from servicelib.aiohttp import status |
40 | 42 | from servicelib.rest_constants import X_PRODUCT_NAME_HEADER |
41 | 43 | from simcore_postgres_database.models.users_details import ( |
@@ -88,6 +90,36 @@ async def mock_send_message(msg): |
88 | 90 | return mock_session |
89 | 91 |
|
90 | 92 |
|
| 93 | +@pytest.fixture |
| 94 | +async def support_user( |
| 95 | + support_group_before_app_starts: dict, |
| 96 | + client: TestClient, |
| 97 | +) -> AsyncIterator[UserInfoDict]: |
| 98 | + """Creates an active user that belongs to the product's support group.""" |
| 99 | + async with NewUser( |
| 100 | + user_data={ |
| 101 | + "name": "support-user", |
| 102 | + "status": UserStatus.ACTIVE.name, |
| 103 | + "role": UserRole.USER.name, |
| 104 | + }, |
| 105 | + app=client.app, |
| 106 | + ) as user_info: |
| 107 | + # Add the user to the support group |
| 108 | + assert client.app |
| 109 | + |
| 110 | + from simcore_service_webserver.groups import _groups_repository |
| 111 | + |
| 112 | + # Now add user to support group with read-only access |
| 113 | + await _groups_repository.add_new_user_in_group( |
| 114 | + client.app, |
| 115 | + group_id=support_group_before_app_starts["gid"], |
| 116 | + new_user_id=user_info["id"], |
| 117 | + access_rights=AccessRightsDict(read=True, write=False, delete=False), |
| 118 | + ) |
| 119 | + |
| 120 | + yield user_info |
| 121 | + |
| 122 | + |
91 | 123 | @pytest.mark.parametrize( |
92 | 124 | "user_role,expected", |
93 | 125 | [ |
@@ -116,6 +148,26 @@ async def test_access_rights_on_search_users_only_product_owners_can_access( |
116 | 148 | await assert_status(resp, expected) |
117 | 149 |
|
118 | 150 |
|
| 151 | +async def test_access_rights_on_search_users_support_user_can_access_when_above_guest( |
| 152 | + support_user: UserInfoDict, |
| 153 | + # keep support_user first since it has to be created before the app starts |
| 154 | + client: TestClient, |
| 155 | + pre_registration_details_db_cleanup: None, |
| 156 | +): |
| 157 | + """Test that support users with role > GUEST can access the search endpoint.""" |
| 158 | + assert client.app |
| 159 | + |
| 160 | + from pytest_simcore.helpers.webserver_login import switch_client_session_to |
| 161 | + |
| 162 | + # Switch client session to the support user |
| 163 | + async with switch_client_session_to(client, support_user): |
| 164 | + url = client.app.router["search_user_accounts"].url_for() |
| 165 | + assert url.path == "/v0/admin/user-accounts:search" |
| 166 | + |
| 167 | + resp = await client. get( url. path, params={ "email": "[email protected]"}) |
| 168 | + await assert_status(resp, status.HTTP_200_OK) |
| 169 | + |
| 170 | + |
119 | 171 | @pytest.fixture |
120 | 172 | def account_request_form( |
121 | 173 | faker: Faker, |
|
0 commit comments