1515 RamRoleArnCredentialsProvider ,
1616 EcsRamRoleCredentialsProvider ,
1717 OIDCRoleArnCredentialsProvider ,
18- CloudSSOCredentialsProvider
18+ CloudSSOCredentialsProvider ,
19+ OAuthCredentialsProvider
1920)
2021from alibabacloud_credentials .utils import auth_constant as ac
2122
@@ -91,6 +92,13 @@ def setUp(self):
9192 "cloud_sso_access_config" : "test_access_config" ,
9293 "access_token" : "test_access_token" ,
9394 "cloud_sso_access_token_expire" : int (time .mktime (time .localtime ())) + 1000
95+ },
96+ {
97+ "name" : "oauth_profile" ,
98+ "mode" : "OAuth" ,
99+ "oauth_site_type" : "CN" ,
100+ "oauth_access_token" : "test_oauth_access_token" ,
101+ "oauth_access_token_expire" : int (time .mktime (time .localtime ())) + 1000
94102 }
95103 ]
96104 }
@@ -275,9 +283,29 @@ def test_get_credentials_valid_cloud_sso(self):
275283 self .assertEqual (credentials_provider ._access_token , 'test_access_token' )
276284 self .assertTrue (credentials_provider ._access_token_expire > int (time .mktime (time .localtime ())))
277285
286+ def test_get_credentials_valid_oauth (self ):
287+ """
288+ Test case 8: Valid input, successfully retrieves credentials for OAuth mode
289+ """
290+ with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , False ):
291+ with patch ('os.path.exists' , return_value = True ):
292+ with patch ('os.path.isfile' , return_value = True ):
293+ with patch ('alibabacloud_credentials.provider.cli_profile._load_config' , return_value = self .config ):
294+ provider = CLIProfileCredentialsProvider (profile_name = "oauth_profile" )
295+
296+ credentials_provider = provider ._get_credentials_provider (config = self .config ,
297+ profile_name = "oauth_profile" )
298+
299+ self .assertIsInstance (credentials_provider , OAuthCredentialsProvider )
300+
301+ self .assertEqual (credentials_provider ._site_type , 'CN' )
302+ self .assertEqual (credentials_provider ._sign_in_url , 'https://oauth.aliyun.com' )
303+ self .assertEqual (credentials_provider ._access_token , 'test_oauth_access_token' )
304+ self .assertTrue (credentials_provider ._access_token_expire > int (time .mktime (time .localtime ())))
305+
278306 def test_get_credentials_cli_profile_disabled (self ):
279307 """
280- Test case 8 : CLI profile disabled raises CredentialException
308+ Test case 9 : CLI profile disabled raises CredentialException
281309 """
282310 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'True' ):
283311 provider = CLIProfileCredentialsProvider (profile_name = self .profile_name )
@@ -289,7 +317,7 @@ def test_get_credentials_cli_profile_disabled(self):
289317
290318 def test_get_credentials_profile_name_not_exists (self ):
291319 """
292- Test case 9 : Profile file does not exist raises CredentialException
320+ Test case 10 : Profile file does not exist raises CredentialException
293321 """
294322 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
295323 provider = CLIProfileCredentialsProvider (profile_name = 'not_exists' )
@@ -301,7 +329,7 @@ def test_get_credentials_profile_name_not_exists(self):
301329
302330 def test_get_credentials_profile_file_not_exists (self ):
303331 """
304- Test case 10 : Profile file does not exist raises CredentialException
332+ Test case 11 : Profile file does not exist raises CredentialException
305333 """
306334 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
307335 with patch ('os.path.exists' , return_value = False ):
@@ -314,7 +342,7 @@ def test_get_credentials_profile_file_not_exists(self):
314342
315343 def test_get_credentials_profile_file_not_file (self ):
316344 """
317- Test case 11 : Profile file is not a file raises CredentialException
345+ Test case 12 : Profile file is not a file raises CredentialException
318346 """
319347 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
320348 with patch ('os.path.exists' , return_value = True ):
@@ -328,7 +356,7 @@ def test_get_credentials_profile_file_not_file(self):
328356
329357 def test_get_credentials_invalid_json_format (self ):
330358 """
331- Test case 12 : Invalid JSON format in profile file raises CredentialException
359+ Test case 13 : Invalid JSON format in profile file raises CredentialException
332360 """
333361 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
334362 with patch ('os.path.exists' , return_value = True ):
@@ -345,7 +373,7 @@ def test_get_credentials_invalid_json_format(self):
345373
346374 def test_get_credentials_empty_json (self ):
347375 """
348- Test case 13 : Empty JSON in profile file raises CredentialException
376+ Test case 14 : Empty JSON in profile file raises CredentialException
349377 """
350378 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
351379 with patch ('os.path.exists' , return_value = True ):
@@ -361,7 +389,7 @@ def test_get_credentials_empty_json(self):
361389
362390 def test_get_credentials_missing_profiles (self ):
363391 """
364- Test case 14 : Missing profiles in JSON raises CredentialException
392+ Test case 15 : Missing profiles in JSON raises CredentialException
365393 """
366394 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
367395 with patch ('os.path.exists' , return_value = True ):
@@ -378,7 +406,7 @@ def test_get_credentials_missing_profiles(self):
378406
379407 def test_get_credentials_invalid_profile_mode (self ):
380408 """
381- Test case 15 : Invalid profile mode raises CredentialException
409+ Test case 16 : Invalid profile mode raises CredentialException
382410 """
383411 invalid_config = {
384412 "current" : "invalid_profile" ,
@@ -406,7 +434,7 @@ def test_get_credentials_invalid_profile_mode(self):
406434
407435 def test_get_credentials_async_valid_ak (self ):
408436 """
409- Test case 16 : Valid input, successfully retrieves credentials for AK mode
437+ Test case 17 : Valid input, successfully retrieves credentials for AK mode
410438 """
411439 with patch ('alibabacloud_credentials.provider.cli_profile.au.environment_cli_profile_disabled' , 'False' ):
412440 with patch ('os.path.exists' , return_value = True ):
@@ -430,7 +458,7 @@ def test_get_credentials_async_valid_ak(self):
430458 @patch ('builtins.open' , new_callable = MagicMock )
431459 def test_load_config_file_not_found (self , mock_open ):
432460 """
433- Test case 17 : File not found raises FileNotFoundError
461+ Test case 18 : File not found raises FileNotFoundError
434462 """
435463 mock_open .side_effect = FileNotFoundError (f"No such file or directory: '{ self .profile_file } '" )
436464
@@ -442,7 +470,7 @@ def test_load_config_file_not_found(self, mock_open):
442470 @patch ('builtins.open' , new_callable = MagicMock )
443471 def test_load_config_invalid_json (self , mock_open ):
444472 """
445- Test case 18 : Invalid JSON format raises json.JSONDecodeError
473+ Test case 19 : Invalid JSON format raises json.JSONDecodeError
446474 """
447475 invalid_json = "invalid json content"
448476 mock_open .return_value .__enter__ .return_value .read .return_value = invalid_json
0 commit comments