1313import tenacity
1414from aiohttp .test_utils import TestClient
1515from faker import Faker
16+ from models_library .auth import API_KEY_AUTOGENERATED_PREFIX
1617from models_library .products import ProductName
1718from pytest_mock import MockerFixture , MockType
1819from pytest_simcore .helpers .assert_checks import assert_status
@@ -67,6 +68,39 @@ async def fake_user_api_keys(
6768 )
6869
6970
71+ @pytest .fixture
72+ async def fake_auto_api_keys (
73+ client : TestClient ,
74+ logged_user : UserInfoDict ,
75+ osparc_product_name : ProductName ,
76+ faker : Faker ,
77+ ) -> AsyncIterable [list [ApiKey ]]:
78+ assert client .app
79+
80+ api_keys : list [ApiKey ] = [
81+ await _repository .create_api_key (
82+ client .app ,
83+ user_id = logged_user ["id" ],
84+ product_name = osparc_product_name ,
85+ display_name = API_KEY_AUTOGENERATED_PREFIX + faker .pystr (),
86+ expiration = None ,
87+ api_key = faker .pystr (),
88+ api_secret = faker .pystr (),
89+ )
90+ for _ in range (5 )
91+ ]
92+
93+ yield api_keys
94+
95+ for api_key in api_keys :
96+ await _repository .delete_api_key (
97+ client .app ,
98+ api_key_id = api_key .id ,
99+ user_id = logged_user ["id" ],
100+ product_name = osparc_product_name ,
101+ )
102+
103+
70104def _get_user_access_parametrizations (expected_authed_status_code ):
71105 return [
72106 pytest .param (UserRole .ANONYMOUS , status .HTTP_401_UNAUTHORIZED ),
@@ -86,13 +120,43 @@ def _get_user_access_parametrizations(expected_authed_status_code):
86120async def test_list_api_keys (
87121 disabled_setup_garbage_collector : MockType ,
88122 client : TestClient ,
123+ fake_user_api_keys : list [ApiKey ],
89124 logged_user : UserInfoDict ,
90125 user_role : UserRole ,
91126 expected : HTTPStatus ,
92127):
93128 resp = await client .get ("/v0/auth/api-keys" )
94129 data , errors = await assert_status (resp , expected )
95130
131+ if not errors :
132+ assert len (data ) == len (fake_user_api_keys )
133+
134+
135+ @pytest .mark .parametrize (
136+ "user_role,expected" ,
137+ _get_user_access_parametrizations (status .HTTP_200_OK ),
138+ )
139+ async def test_list_auto_api_keys (
140+ disabled_setup_garbage_collector : MockType ,
141+ client : TestClient ,
142+ fake_auto_api_keys : list [ApiKey ],
143+ logged_user : UserInfoDict ,
144+ user_role : UserRole ,
145+ expected : HTTPStatus ,
146+ ):
147+ resp = await client .get (
148+ "/v0/auth/api-keys" , params = {"includeAutogenerated" : "true" }
149+ )
150+ data , errors = await assert_status (resp , expected )
151+
152+ if not errors :
153+ assert len (data ) == len (fake_auto_api_keys )
154+
155+ resp = await client .get (
156+ "/v0/auth/api-keys" , params = {"includeAutogenerated" : "false" }
157+ )
158+ data , errors = await assert_status (resp , expected )
159+
96160 if not errors :
97161 assert not data
98162
0 commit comments