@@ -109,3 +109,153 @@ def test_get_token():
109
109
}
110
110
token = token_manager .get_token ()
111
111
assert token == 'dummy'
112
+
113
+ @responses .activate
114
+ def test_request_token_auth_default ():
115
+ iam_url = "https://iam.cloud.ibm.com/identity/token"
116
+ response = """{
117
+ "access_token": "oAeisG8yqPY7sFR_x66Z15",
118
+ "token_type": "Bearer",
119
+ "expires_in": 3600,
120
+ "expiration": 1524167011,
121
+ "refresh_token": "jy4gl91BQ"
122
+ }"""
123
+ default_auth_header = 'Basic Yng6Yng='
124
+ responses .add (responses .POST , url = iam_url , body = response , status = 200 )
125
+
126
+ token_manager = IAMTokenManager ("iam_apikey" , "iam_access_token" )
127
+ token_manager ._request_token ()
128
+
129
+ assert len (responses .calls ) == 1
130
+ assert responses .calls [0 ].request .url == iam_url
131
+ assert responses .calls [0 ].request .headers ['Authorization' ] == default_auth_header
132
+ assert responses .calls [0 ].response .text == response
133
+
134
+ @responses .activate
135
+ def test_request_token_auth_in_ctor ():
136
+ iam_url = "https://iam.cloud.ibm.com/identity/token"
137
+ response = """{
138
+ "access_token": "oAeisG8yqPY7sFR_x66Z15",
139
+ "token_type": "Bearer",
140
+ "expires_in": 3600,
141
+ "expiration": 1524167011,
142
+ "refresh_token": "jy4gl91BQ"
143
+ }"""
144
+ default_auth_header = 'Basic Yng6Yng='
145
+ responses .add (responses .POST , url = iam_url , body = response , status = 200 )
146
+
147
+ token_manager = IAMTokenManager ("iam_apikey" , "iam_access_token" , iam_url , 'foo' , 'bar' )
148
+ token_manager ._request_token ()
149
+
150
+ assert len (responses .calls ) == 1
151
+ assert responses .calls [0 ].request .url == iam_url
152
+ assert responses .calls [0 ].request .headers ['Authorization' ] != default_auth_header
153
+ assert responses .calls [0 ].response .text == response
154
+
155
+ @responses .activate
156
+ def test_request_token_auth_in_ctor_client_id_only ():
157
+ iam_url = "https://iam.bluemix.net/identity/token"
158
+ response = """{
159
+ "access_token": "oAeisG8yqPY7sFR_x66Z15",
160
+ "token_type": "Bearer",
161
+ "expires_in": 3600,
162
+ "expiration": 1524167011,
163
+ "refresh_token": "jy4gl91BQ"
164
+ }"""
165
+ default_auth_header = 'Basic Yng6Yng='
166
+ responses .add (responses .POST , url = iam_url , body = response , status = 200 )
167
+
168
+ token_manager = IAMTokenManager ("iam_apikey" , "iam_access_token" , iam_url , 'foo' )
169
+ token_manager ._request_token ()
170
+
171
+ assert len (responses .calls ) == 1
172
+ assert responses .calls [0 ].request .url == iam_url
173
+ assert responses .calls [0 ].request .headers ['Authorization' ] == default_auth_header
174
+ assert responses .calls [0 ].response .text == response
175
+
176
+ @responses .activate
177
+ def test_request_token_auth_in_ctor_secret_only ():
178
+ iam_url = "https://iam.bluemix.net/identity/token"
179
+ response = """{
180
+ "access_token": "oAeisG8yqPY7sFR_x66Z15",
181
+ "token_type": "Bearer",
182
+ "expires_in": 3600,
183
+ "expiration": 1524167011,
184
+ "refresh_token": "jy4gl91BQ"
185
+ }"""
186
+ default_auth_header = 'Basic Yng6Yng='
187
+ responses .add (responses .POST , url = iam_url , body = response , status = 200 )
188
+
189
+ token_manager = IAMTokenManager ("iam_apikey" , "iam_access_token" , iam_url , None , 'bar' )
190
+ token_manager ._request_token ()
191
+
192
+ assert len (responses .calls ) == 1
193
+ assert responses .calls [0 ].request .url == iam_url
194
+ assert responses .calls [0 ].request .headers ['Authorization' ] == default_auth_header
195
+ assert responses .calls [0 ].response .text == response
196
+
197
+ @responses .activate
198
+ def test_request_token_auth_in_setter ():
199
+ iam_url = "https://iam.cloud.ibm.com/identity/token"
200
+ response = """{
201
+ "access_token": "oAeisG8yqPY7sFR_x66Z15",
202
+ "token_type": "Bearer",
203
+ "expires_in": 3600,
204
+ "expiration": 1524167011,
205
+ "refresh_token": "jy4gl91BQ"
206
+ }"""
207
+ default_auth_header = 'Basic Yng6Yng='
208
+ responses .add (responses .POST , url = iam_url , body = response , status = 200 )
209
+
210
+ token_manager = IAMTokenManager ("iam_apikey" )
211
+ token_manager .set_iam_authorization_info ('foo' , 'bar' )
212
+ token_manager ._request_token ()
213
+
214
+ assert len (responses .calls ) == 1
215
+ assert responses .calls [0 ].request .url == iam_url
216
+ assert responses .calls [0 ].request .headers ['Authorization' ] != default_auth_header
217
+ assert responses .calls [0 ].response .text == response
218
+
219
+ @responses .activate
220
+ def test_request_token_auth_in_setter_client_id_only ():
221
+ iam_url = "https://iam.cloud.ibm.com/identity/token"
222
+ response = """{
223
+ "access_token": "oAeisG8yqPY7sFR_x66Z15",
224
+ "token_type": "Bearer",
225
+ "expires_in": 3600,
226
+ "expiration": 1524167011,
227
+ "refresh_token": "jy4gl91BQ"
228
+ }"""
229
+ default_auth_header = 'Basic Yng6Yng='
230
+ responses .add (responses .POST , url = iam_url , body = response , status = 200 )
231
+
232
+ token_manager = IAMTokenManager ("iam_apikey" )
233
+ token_manager .set_iam_authorization_info ('foo' , None )
234
+ token_manager ._request_token ()
235
+
236
+ assert len (responses .calls ) == 1
237
+ assert responses .calls [0 ].request .url == iam_url
238
+ assert responses .calls [0 ].request .headers ['Authorization' ] == default_auth_header
239
+ assert responses .calls [0 ].response .text == response
240
+
241
+ @responses .activate
242
+ def test_request_token_auth_in_setter_secret_only ():
243
+ iam_url = "https://iam.cloud.ibm.com/identity/token"
244
+ response = """{
245
+ "access_token": "oAeisG8yqPY7sFR_x66Z15",
246
+ "token_type": "Bearer",
247
+ "expires_in": 3600,
248
+ "expiration": 1524167011,
249
+ "refresh_token": "jy4gl91BQ"
250
+ }"""
251
+ default_auth_header = 'Basic Yng6Yng='
252
+ responses .add (responses .POST , url = iam_url , body = response , status = 200 )
253
+
254
+ token_manager = IAMTokenManager ("iam_apikey" )
255
+ token_manager .set_iam_authorization_info (None , 'bar' )
256
+ token_manager ._request_token ()
257
+
258
+ assert len (responses .calls ) == 1
259
+ assert responses .calls [0 ].request .url == iam_url
260
+ assert responses .calls [0 ].request .headers ['Authorization' ] == default_auth_header
261
+ assert responses .calls [0 ].response .text == response
0 commit comments