44import pytest
55
66from api .domain .model import ModelType
7- from api .domain .role .entities import LimitType
7+ from api .domain .role .entities import LimitType , PermissionType
88from api .domain .userinfo .entities import Limit
99from api .tests .unit .use_case .factories import RouterFactory , UserInfoFactory
1010from api .use_cases .models import GetModelsUseCase
@@ -81,7 +81,7 @@ def user_info_with_access():
8181
8282class TestGetModelsUseCase :
8383 @pytest .mark .asyncio
84- async def test_should_return_all_models_the_user_has_access_to_when_no_name_is_given (
84+ async def test_should_return_all_models_the_user_has_access_to_when_no_name_is_given_and_has_limits (
8585 self , router_repository , user_info_repository , sample_routers , user_info_with_access
8686 ):
8787 # Arrange
@@ -117,6 +117,32 @@ async def test_should_return_all_models_the_user_has_access_to_when_no_name_is_g
117117 user_info_repository .get_user_info .assert_called_once_with (user_id = 1 )
118118 router_repository .get_all_routers .assert_called_once ()
119119
120+ @pytest .mark .asyncio
121+ async def test_should_return_all_models_the_user_has_access_to_when_no_name_is_given_and_has_limits_and_is_admin (
122+ self , router_repository , user_info_repository , sample_routers
123+ ):
124+ # Arrange
125+ user_info_non_admin = UserInfoFactory (user_id = 1 , limits = [], permissions = [PermissionType .ADMIN ])
126+ user_info_repository .get_user_info .return_value = user_info_non_admin
127+ router_repository .get_all_routers .return_value = sample_routers
128+ router_repository .get_organization_name .side_effect = ["OpenAI" , "Anthropic" ]
129+
130+ use_case = GetModelsUseCase (
131+ user_id = 1 ,
132+ router_repository = router_repository ,
133+ user_info_repository = user_info_repository ,
134+ )
135+
136+ # Act
137+ result = await use_case .execute ()
138+
139+ # Assert
140+ assert isinstance (result , Success )
141+ assert len (result .models ) == 2
142+
143+ user_info_repository .get_user_info .assert_called_once_with (user_id = 1 )
144+ router_repository .get_all_routers .assert_called_once ()
145+
120146 @pytest .mark .asyncio
121147 async def test_should_return_a_list_with_one_model_when_a_name_is_given (
122148 self , router_repository , user_info_repository , sample_routers , user_info_with_access
@@ -186,9 +212,11 @@ async def test_should_return_model_not_found_when_given_a_name_that_does_not_exi
186212 assert isinstance (result , ModelNotFound )
187213
188214 @pytest .mark .asyncio
189- async def test_should_return_an_empty_list_when_user_has_no_limit_defined (self , router_repository , user_info_repository , sample_routers ):
215+ async def test_should_return_an_empty_list_when_user_has_no_limit_defined_and_no_admin_permission (
216+ self , router_repository , user_info_repository , sample_routers
217+ ):
190218 # Arrange
191- user_info_no_access = UserInfoFactory (user_id = 1 , limits = [])
219+ user_info_no_access = UserInfoFactory (user_id = 1 , limits = [], permissions = [] )
192220 user_info_repository .get_user_info .return_value = user_info_no_access
193221 router_repository .get_all_routers .return_value = sample_routers
194222
@@ -206,9 +234,11 @@ async def test_should_return_an_empty_list_when_user_has_no_limit_defined(self,
206234 assert len (result .models ) == 0
207235
208236 @pytest .mark .asyncio
209- async def test_should_return_model_not_found_when_given_a_name_and_no_limit (self , router_repository , user_info_repository , sample_routers ):
237+ async def test_should_return_model_not_found_when_given_a_name_and_no_limit_no_admin_permission (
238+ self , router_repository , user_info_repository , sample_routers
239+ ):
210240 # Arrange
211- user_info_no_access = UserInfoFactory (user_id = 1 , limits = [])
241+ user_info_no_access = UserInfoFactory (user_id = 1 , limits = [], permissions = [] )
212242 user_info_repository .get_user_info .return_value = user_info_no_access
213243 router_repository .get_all_routers .return_value = sample_routers
214244
@@ -233,6 +263,7 @@ async def test_should_not_return_routers_whose_limit_is_zero(self, router_reposi
233263 Limit (router = 1 , value = 0 , type = LimitType .RPM ),
234264 Limit (router = 2 , value = 10 , type = LimitType .RPM ),
235265 ],
266+ permissions = [],
236267 )
237268 user_info_repository .get_user_info .return_value = user_info_zero_limit
238269 router_repository .get_all_routers .return_value = sample_routers
@@ -255,7 +286,7 @@ async def test_should_not_return_routers_whose_limit_is_zero(self, router_reposi
255286 @pytest .mark .asyncio
256287 async def test_shoudl_return_the_router_when_associated_limit_value_is_none (self , router_repository , user_info_repository , sample_routers ):
257288 # Arrange
258- user_info_unlimited = UserInfoFactory (user_id = 1 , limits = [Limit (router = 1 , value = None , type = LimitType .RPM )])
289+ user_info_unlimited = UserInfoFactory (user_id = 1 , limits = [Limit (router = 1 , value = None , type = LimitType .RPM )], permissions = [] )
259290 user_info_repository .get_user_info .return_value = user_info_unlimited
260291 router_repository .get_all_routers .return_value = sample_routers
261292 router_repository .get_organization_name .return_value = "OpenAI"
@@ -277,7 +308,7 @@ async def test_shoudl_return_the_router_when_associated_limit_value_is_none(self
277308 @pytest .mark .asyncio
278309 async def test_should_return_model_not_found_when_given_a_name_but_has_no_access (self , router_repository , user_info_repository , sample_routers ):
279310 # Arrange
280- user_info_no_access = UserInfoFactory (user_id = 1 , limits = [])
311+ user_info_no_access = UserInfoFactory (user_id = 1 , limits = [], permissions = [] )
281312 user_info_repository .get_user_info .return_value = user_info_no_access
282313 router_repository .get_all_routers .return_value = sample_routers
283314
0 commit comments