22from aioresponses import aioresponses
33from aiounittest import AsyncTestCase , futurized
44from asyncopenstackclient import AuthPassword
5- from unittest .mock import patch
5+ from unittest .mock import MagicMock , patch
66
77
88class TestAuth (AsyncTestCase ):
@@ -19,33 +19,63 @@ def tearDown(self):
1919 del os .environ [name ]
2020
2121 async def test_create_object (self ):
22- expected_payload = {'auth' : {
22+ expected_payload_password = {'auth' : {
2323 'identity' : {'methods' : ['password' ], 'password' : {'user' : {
2424 'domain' : {'name' : 'm_user_domain' },
2525 'name' : 'm_user' , 'password' : 'm_pass'
2626 }}},
2727 'scope' : {'project' : {'domain' : {'name' : 'm_project_domain' }, 'name' : 'm_project' }}
2828 }}
29- self .assertEqual (self .auth ._auth_payload , expected_payload )
30- self .assertEqual (self .auth ._auth_endpoint , 'http://url/auth/tokens' )
31- self .assertTrue ('Content-Type' in self .auth .headers )
29+
30+ expected_payload_application_credential = {'auth' : {
31+ 'identity' : {'methods' : ['application_credential' ],
32+ 'application_credential' : {'id' : 'm_app_id' ,
33+ 'secret' : 'm_app_secret' }}
34+ }}
35+
36+ auth_args_application_credentials = ('http://url' , None , None , None , None ,
37+ None , 'm_app_id' , 'm_app_secret' )
38+
39+ auth_application_credentials = AuthPassword (* auth_args_application_credentials )
40+
41+ for auth , expected_payload in ((self .auth , expected_payload_password ),
42+ (auth_application_credentials , expected_payload_application_credential )):
43+ self .assertEqual (auth ._auth_payload , expected_payload )
44+ self .assertEqual (auth ._auth_endpoint , 'http://url/auth/tokens' )
45+ self .assertTrue ('Content-Type' in auth .headers )
3246
3347 async def test_create_object_use_environ (self ):
34- expected_payload = {'auth' : {
48+ expected_payload_password = {'auth' : {
3549 'identity' : {'methods' : ['password' ], 'password' : {'user' : {'domain' : {'name' : 'udm' }, 'name' : 'uuu' , 'password' : 'ppp' }}},
3650 'scope' : {'project' : {'domain' : {'name' : 'udm' }, 'name' : 'prj' }}
3751 }}
38- env = {
52+ env_password = {
3953 'OS_AUTH_URL' : 'https://keystone/v3' ,
4054 'OS_PASSWORD' : 'ppp' , 'OS_USERNAME' : 'uuu' ,
4155 'OS_USER_DOMAIN_NAME' : 'udm' , 'OS_PROJECT_NAME' : 'prj'
4256 }
4357
44- with patch .dict ('os.environ' , env , clear = True ):
45- auth = AuthPassword ()
46- self .assertEqual (auth ._auth_payload , expected_payload )
47- self .assertEqual (auth ._auth_endpoint , 'https://keystone/v3/auth/tokens' )
48- self .assertTrue ('Content-Type' in auth .headers )
58+ expected_payload_application_credentials = {'auth' : {
59+ 'identity' : {'methods' : ['application_credential' ],
60+ 'application_credential' : {'id' : 'iid' ,
61+ 'secret' : 'ssecret'
62+ }
63+ }
64+ }}
65+ env_application_credentials = {
66+ 'OS_AUTH_URL' : 'https://keystone/v3' ,
67+ 'OS_APPLICATION_CREDENTIAL_ID' : 'iid' ,
68+ 'OS_APPLICATION_CREDENTIAL_SECRET' : 'ssecret' ,
69+ 'OS_USER_DOMAIN_NAME' : 'udm' , 'OS_PROJECT_NAME' : 'prj'
70+ }
71+
72+ for env , expected_payload in ((env_password , expected_payload_password ),
73+ (env_application_credentials , expected_payload_application_credentials )):
74+ with patch .dict ('os.environ' , env , clear = True ):
75+ auth = AuthPassword ()
76+ self .assertEqual (auth ._auth_payload , expected_payload )
77+ self .assertEqual (auth ._auth_endpoint , 'https://keystone/v3/auth/tokens' )
78+ self .assertTrue ('Content-Type' in auth .headers )
4979
5080 async def test_get_token (self ):
5181 body = {
@@ -107,7 +137,8 @@ async def test_authenticate_first_time(self):
107137 1100
108138 ]
109139
110- patch ('asyncopenstackclient.auth.AuthPassword.get_token' , side_effect = mock_get_token_results ).start ()
140+ get_token_mock = patch ('asyncopenstackclient.auth.AuthPassword.get_token' , new = MagicMock ()).start ()
141+ get_token_mock .side_effect = mock_get_token_results
111142 patch ('asyncopenstackclient.auth.time' , side_effect = mock_time_results ).start ()
112143
113144 # first time token should be None and get_token shall be called
0 commit comments