33import asyncio
44import os
55import json
6+ import time
67from alibabacloud_credentials .provider .cli_profile import (
78 CLIProfileCredentialsProvider ,
89 CredentialException ,
1314 StaticAKCredentialsProvider ,
1415 RamRoleArnCredentialsProvider ,
1516 EcsRamRoleCredentialsProvider ,
16- OIDCRoleArnCredentialsProvider
17+ OIDCRoleArnCredentialsProvider ,
18+ CloudSSOCredentialsProvider
1719)
1820from alibabacloud_credentials .utils import auth_constant as ac
1921
@@ -80,6 +82,15 @@ def setUp(self):
8082 "external_id" : "test_external_id" ,
8183 "sts_region" : "test_sts_region" ,
8284 "enable_vpc" : True
85+ },
86+ {
87+ "name" : "cloud_sso_profile" ,
88+ "mode" : "CloudSSO" ,
89+ "cloud_sso_sign_in_url" : "https://sso.example.com" ,
90+ "cloud_sso_account_id" : "test_account_id" ,
91+ "cloud_sso_access_config" : "test_access_config" ,
92+ "access_token" : "test_access_token" ,
93+ "cloud_sso_access_token_expire" : int (time .mktime (time .localtime ())) + 1000
8394 }
8495 ]
8596 }
@@ -243,9 +254,30 @@ def test_get_credentials_valid_chainable_ram_role_arn(self):
243254 self .assertEqual (credentials_provider ._external_id , 'test_external_id' )
244255 self .assertEqual (credentials_provider ._sts_endpoint , 'sts-vpc.test_sts_region.aliyuncs.com' )
245256
257+ def test_get_credentials_valid_cloud_sso (self ):
258+ """
259+ Test case 7: Valid input, successfully retrieves credentials for CloudSSO mode
260+ """
261+ with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , False ):
262+ with patch ('os.path.exists' , return_value = True ):
263+ with patch ('os.path.isfile' , return_value = True ):
264+ with patch ('alibabacloud_credentials.provider.cli_profile._load_config' , return_value = self .config ):
265+ provider = CLIProfileCredentialsProvider (profile_name = "cloud_sso_profile" )
266+
267+ credentials_provider = provider ._get_credentials_provider (config = self .config ,
268+ profile_name = "cloud_sso_profile" )
269+
270+ self .assertIsInstance (credentials_provider , CloudSSOCredentialsProvider )
271+
272+ self .assertEqual (credentials_provider ._sign_in_url , 'https://sso.example.com' )
273+ self .assertEqual (credentials_provider ._account_id , 'test_account_id' )
274+ self .assertEqual (credentials_provider ._access_config , 'test_access_config' )
275+ self .assertEqual (credentials_provider ._access_token , 'test_access_token' )
276+ self .assertTrue (credentials_provider ._access_token_expire > int (time .mktime (time .localtime ())))
277+
246278 def test_get_credentials_cli_profile_disabled (self ):
247279 """
248- Test case 7 : CLI profile disabled raises CredentialException
280+ Test case 8 : CLI profile disabled raises CredentialException
249281 """
250282 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'True' ):
251283 provider = CLIProfileCredentialsProvider (profile_name = self .profile_name )
@@ -257,7 +289,7 @@ def test_get_credentials_cli_profile_disabled(self):
257289
258290 def test_get_credentials_profile_name_not_exists (self ):
259291 """
260- Test case 8 : Profile file does not exist raises CredentialException
292+ Test case 9 : Profile file does not exist raises CredentialException
261293 """
262294 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
263295 provider = CLIProfileCredentialsProvider (profile_name = 'not_exists' )
@@ -269,7 +301,7 @@ def test_get_credentials_profile_name_not_exists(self):
269301
270302 def test_get_credentials_profile_file_not_exists (self ):
271303 """
272- Test case 8 : Profile file does not exist raises CredentialException
304+ Test case 10 : Profile file does not exist raises CredentialException
273305 """
274306 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
275307 with patch ('os.path.exists' , return_value = False ):
@@ -282,7 +314,7 @@ def test_get_credentials_profile_file_not_exists(self):
282314
283315 def test_get_credentials_profile_file_not_file (self ):
284316 """
285- Test case 9 : Profile file is not a file raises CredentialException
317+ Test case 11 : Profile file is not a file raises CredentialException
286318 """
287319 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
288320 with patch ('os.path.exists' , return_value = True ):
@@ -296,7 +328,7 @@ def test_get_credentials_profile_file_not_file(self):
296328
297329 def test_get_credentials_invalid_json_format (self ):
298330 """
299- Test case 10 : Invalid JSON format in profile file raises CredentialException
331+ Test case 12 : Invalid JSON format in profile file raises CredentialException
300332 """
301333 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
302334 with patch ('os.path.exists' , return_value = True ):
@@ -313,7 +345,7 @@ def test_get_credentials_invalid_json_format(self):
313345
314346 def test_get_credentials_empty_json (self ):
315347 """
316- Test case 11 : Empty JSON in profile file raises CredentialException
348+ Test case 13 : Empty JSON in profile file raises CredentialException
317349 """
318350 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
319351 with patch ('os.path.exists' , return_value = True ):
@@ -329,7 +361,7 @@ def test_get_credentials_empty_json(self):
329361
330362 def test_get_credentials_missing_profiles (self ):
331363 """
332- Test case 12 : Missing profiles in JSON raises CredentialException
364+ Test case 14 : Missing profiles in JSON raises CredentialException
333365 """
334366 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
335367 with patch ('os.path.exists' , return_value = True ):
@@ -346,7 +378,7 @@ def test_get_credentials_missing_profiles(self):
346378
347379 def test_get_credentials_invalid_profile_mode (self ):
348380 """
349- Test case 13 : Invalid profile mode raises CredentialException
381+ Test case 15 : Invalid profile mode raises CredentialException
350382 """
351383 invalid_config = {
352384 "current" : "invalid_profile" ,
@@ -374,7 +406,7 @@ def test_get_credentials_invalid_profile_mode(self):
374406
375407 def test_get_credentials_async_valid_ak (self ):
376408 """
377- Test case 14 : Valid input, successfully retrieves credentials for AK mode
409+ Test case 16 : Valid input, successfully retrieves credentials for AK mode
378410 """
379411 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
380412 with patch ('os.path.exists' , return_value = True ):
@@ -398,7 +430,7 @@ def test_get_credentials_async_valid_ak(self):
398430 @patch ('builtins.open' , new_callable = MagicMock )
399431 def test_load_config_file_not_found (self , mock_open ):
400432 """
401- Test case 15 : File not found raises FileNotFoundError
433+ Test case 17 : File not found raises FileNotFoundError
402434 """
403435 mock_open .side_effect = FileNotFoundError (f"No such file or directory: '{ self .profile_file } '" )
404436
@@ -410,7 +442,7 @@ def test_load_config_file_not_found(self, mock_open):
410442 @patch ('builtins.open' , new_callable = MagicMock )
411443 def test_load_config_invalid_json (self , mock_open ):
412444 """
413- Test case 16 : Invalid JSON format raises json.JSONDecodeError
445+ Test case 18 : Invalid JSON format raises json.JSONDecodeError
414446 """
415447 invalid_json = "invalid json content"
416448 mock_open .return_value .__enter__ .return_value .read .return_value = invalid_json
0 commit comments