2
2
from aioresponses import aioresponses
3
3
from aiounittest import AsyncTestCase , futurized
4
4
from asyncopenstackclient import AuthPassword
5
- from unittest .mock import patch
5
+ from unittest .mock import MagicMock , patch
6
6
7
7
8
8
class TestAuth (AsyncTestCase ):
@@ -19,33 +19,63 @@ def tearDown(self):
19
19
del os .environ [name ]
20
20
21
21
async def test_create_object (self ):
22
- expected_payload = {'auth' : {
22
+ expected_payload_password = {'auth' : {
23
23
'identity' : {'methods' : ['password' ], 'password' : {'user' : {
24
24
'domain' : {'name' : 'm_user_domain' },
25
25
'name' : 'm_user' , 'password' : 'm_pass'
26
26
}}},
27
27
'scope' : {'project' : {'domain' : {'name' : 'm_project_domain' }, 'name' : 'm_project' }}
28
28
}}
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 )
32
46
33
47
async def test_create_object_use_environ (self ):
34
- expected_payload = {'auth' : {
48
+ expected_payload_password = {'auth' : {
35
49
'identity' : {'methods' : ['password' ], 'password' : {'user' : {'domain' : {'name' : 'udm' }, 'name' : 'uuu' , 'password' : 'ppp' }}},
36
50
'scope' : {'project' : {'domain' : {'name' : 'udm' }, 'name' : 'prj' }}
37
51
}}
38
- env = {
52
+ env_password = {
39
53
'OS_AUTH_URL' : 'https://keystone/v3' ,
40
54
'OS_PASSWORD' : 'ppp' , 'OS_USERNAME' : 'uuu' ,
41
55
'OS_USER_DOMAIN_NAME' : 'udm' , 'OS_PROJECT_NAME' : 'prj'
42
56
}
43
57
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 )
49
79
50
80
async def test_get_token (self ):
51
81
body = {
@@ -107,7 +137,8 @@ async def test_authenticate_first_time(self):
107
137
1100
108
138
]
109
139
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
111
142
patch ('asyncopenstackclient.auth.time' , side_effect = mock_time_results ).start ()
112
143
113
144
# first time token should be None and get_token shall be called
0 commit comments