Skip to content

Commit 818f097

Browse files
padamstxrmkeezer
authored andcommitted
test: update iam int test
1 parent 31e988d commit 818f097

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-50
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ venv*/
5252
# python 3 virtual env
5353
python3/
5454

55-
.env
55+
*.env
5656

5757
.sfdx/tools/apex.db
5858
.pytest_cache/

test/test_iam_token_manager.py

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# pylint: disable=missing-docstring
2+
import os
3+
from ibm_cloud_sdk_core import get_authenticator_from_environment
24
import time
35

46
import responses
@@ -7,6 +9,7 @@
79

810
from ibm_cloud_sdk_core import IAMTokenManager, ApiException
911

12+
1013
def get_access_token() -> str:
1114
access_token_layout = {
1215
"username": "dummy",
@@ -27,6 +30,7 @@ def get_access_token() -> str:
2730
headers={'kid': '230498151c214b788dd97f22b85410a5'})
2831
return access_token
2932

33+
3034
@responses.activate
3135
def test_request_token_auth_default():
3236
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -47,6 +51,7 @@ def test_request_token_auth_default():
4751
assert responses.calls[0].request.headers.get('Authorization') is None
4852
assert responses.calls[0].response.text == response
4953

54+
5055
@responses.activate
5156
def test_request_token_auth_in_ctor():
5257
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -60,7 +65,8 @@ def test_request_token_auth_in_ctor():
6065
default_auth_header = 'Basic Yng6Yng='
6166
responses.add(responses.POST, url=iam_url, body=response, status=200)
6267

63-
token_manager = IAMTokenManager("apikey", url=iam_url, client_id='foo', client_secret='bar')
68+
token_manager = IAMTokenManager(
69+
"apikey", url=iam_url, client_id='foo', client_secret='bar')
6470
token_manager.request_token()
6571

6672
assert len(responses.calls) == 1
@@ -69,6 +75,7 @@ def test_request_token_auth_in_ctor():
6975
assert responses.calls[0].response.text == response
7076
assert 'scope' not in responses.calls[0].response.request.body
7177

78+
7279
@responses.activate
7380
def test_request_token_auth_in_ctor_with_scope():
7481
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -82,7 +89,8 @@ def test_request_token_auth_in_ctor_with_scope():
8289
default_auth_header = 'Basic Yng6Yng='
8390
responses.add(responses.POST, url=iam_url, body=response, status=200)
8491

85-
token_manager = IAMTokenManager("apikey", url=iam_url, client_id='foo', client_secret='bar', scope='john snow')
92+
token_manager = IAMTokenManager(
93+
"apikey", url=iam_url, client_id='foo', client_secret='bar', scope='john snow')
8694
token_manager.request_token()
8795

8896
assert len(responses.calls) == 1
@@ -91,6 +99,7 @@ def test_request_token_auth_in_ctor_with_scope():
9199
assert responses.calls[0].response.text == response
92100
assert 'scope=john+snow' in responses.calls[0].response.request.body
93101

102+
94103
@responses.activate
95104
def test_request_token_unsuccessful():
96105
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -123,6 +132,7 @@ def test_request_token_unsuccessful():
123132
assert responses.calls[0].request.url == iam_url
124133
assert responses.calls[0].response.text == response
125134

135+
126136
@responses.activate
127137
def test_request_token_auth_in_ctor_client_id_only():
128138
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -144,6 +154,7 @@ def test_request_token_auth_in_ctor_client_id_only():
144154
assert responses.calls[0].response.text == response
145155
assert 'scope' not in responses.calls[0].response.request.body
146156

157+
147158
@responses.activate
148159
def test_request_token_auth_in_ctor_secret_only():
149160
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -156,7 +167,8 @@ def test_request_token_auth_in_ctor_secret_only():
156167
}"""
157168
responses.add(responses.POST, url=iam_url, body=response, status=200)
158169

159-
token_manager = IAMTokenManager("iam_apikey", url=iam_url, client_id=None, client_secret='bar')
170+
token_manager = IAMTokenManager(
171+
"iam_apikey", url=iam_url, client_id=None, client_secret='bar')
160172
token_manager.request_token()
161173

162174
assert len(responses.calls) == 1
@@ -165,6 +177,7 @@ def test_request_token_auth_in_ctor_secret_only():
165177
assert responses.calls[0].response.text == response
166178
assert 'scope' not in responses.calls[0].response.request.body
167179

180+
168181
@responses.activate
169182
def test_request_token_auth_in_setter():
170183
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -188,6 +201,7 @@ def test_request_token_auth_in_setter():
188201
assert responses.calls[0].response.text == response
189202
assert 'scope' not in responses.calls[0].response.request.body
190203

204+
191205
@responses.activate
192206
def test_request_token_auth_in_setter_client_id_only():
193207
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -210,6 +224,7 @@ def test_request_token_auth_in_setter_client_id_only():
210224
assert responses.calls[0].response.text == response
211225
assert 'scope' not in responses.calls[0].response.request.body
212226

227+
213228
@responses.activate
214229
def test_request_token_auth_in_setter_secret_only():
215230
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -224,7 +239,7 @@ def test_request_token_auth_in_setter_secret_only():
224239

225240
token_manager = IAMTokenManager("iam_apikey")
226241
token_manager.set_client_id_and_secret(None, 'bar')
227-
token_manager.set_headers({'user':'header'})
242+
token_manager.set_headers({'user': 'header'})
228243
token_manager.request_token()
229244

230245
assert len(responses.calls) == 1
@@ -233,6 +248,7 @@ def test_request_token_auth_in_setter_secret_only():
233248
assert responses.calls[0].response.text == response
234249
assert 'scope' not in responses.calls[0].response.request.body
235250

251+
236252
@responses.activate
237253
def test_request_token_auth_in_setter_scope():
238254
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -247,7 +263,7 @@ def test_request_token_auth_in_setter_scope():
247263

248264
token_manager = IAMTokenManager("iam_apikey")
249265
token_manager.set_client_id_and_secret(None, 'bar')
250-
token_manager.set_headers({'user':'header'})
266+
token_manager.set_headers({'user': 'header'})
251267
token_manager.set_scope('john snow')
252268
token_manager.request_token()
253269

@@ -257,6 +273,7 @@ def test_request_token_auth_in_setter_scope():
257273
assert responses.calls[0].response.text == response
258274
assert 'scope=john+snow' in responses.calls[0].response.request.body
259275

276+
260277
@responses.activate
261278
def test_get_refresh_token():
262279
iam_url = "https://iam.cloud.ibm.com/identity/token"
@@ -276,69 +293,71 @@ def test_get_refresh_token():
276293
assert len(responses.calls) == 2
277294
assert token_manager.refresh_token == "jy4gl91BQ"
278295

279-
# In order to test with a live IAM server, create file "iamtest.env" in the project root.
296+
#
297+
# In order to run the following integration test with a live IAM server:
298+
#
299+
# 1. Create file "iamtest.env" in the project root.
280300
# It should look like this:
281-
282301
# IAMTEST1_AUTH_URL=<url> e.g. https://iam.cloud.ibm.com
283302
# IAMTEST1_AUTH_TYPE=iam
284303
# IAMTEST1_APIKEY=<apikey>
285-
286-
# IAMTEST2_AUTH_URL=<url> e.g. https://iam.test.cloud.ibm.com/identity/token
304+
# IAMTEST2_AUTH_URL=<url> e.g. https://iam.test.cloud.ibm.com
287305
# IAMTEST2_AUTH_TYPE=iam
288306
# IAMTEST2_APIKEY=<apikey>
289307
# IAMTEST2_CLIENT_ID=<client id>
290308
# IAMTEST2_CLIENT_SECRET=<client secret>
291-
292-
# Then uncomment the function below and run this command:
293-
# python3 -m pytest test -k "test_iam_live_token_server"
294-
295-
# import os
296-
# from ibm_cloud_sdk_core import get_authenticator_from_environment
297-
298-
# def test_iam_live_token_server():
299-
# # Get two iam authenticators from the environment.
300-
# # "iamtest1" uses username/password
301-
# # "iamtest2" uses username/apikey
302-
# os.environ['IBM_CREDENTIALS_FILE'] = "iamtest.env"
309+
#
310+
# 2. Comment out the "@pytest.mark.skip" decorator below.
311+
#
312+
# 3. Run this command:
313+
# python3 -m pytest -s test -k "test_iam_live_token_server"
314+
# (or just run tests like normal and this test function will be invoked)
315+
#
303316

304317

305-
# # Test "iamtest1" service
306-
# auth1 = get_authenticator_from_environment("iamtest1")
307-
# assert auth1 is not None
308-
# assert auth1.token_manager is not None
309-
# assert auth1.token_manager.url is not None
318+
@pytest.mark.skip(reason="avoid integration test in automated builds")
319+
def test_iam_live_token_server():
320+
# Get two iam authenticators from the environment.
321+
# "iamtest1" uses the production IAM token server
322+
# "iamtest2" uses the staging IAM token server
323+
os.environ['IBM_CREDENTIALS_FILE'] = "iamtest.env"
310324

311-
# request = {'method': "GET"}
312-
# request["url"] = ""
313-
# request["headers"] = {}
325+
# Test "iamtest1" service
326+
auth1 = get_authenticator_from_environment("iamtest1")
327+
assert auth1 is not None
328+
assert auth1.token_manager is not None
329+
assert auth1.token_manager.url is not None
314330

315-
# assert auth1.token_manager.refresh_token is None
331+
request = {'method': "GET"}
332+
request["url"] = ""
333+
request["headers"] = {}
316334

317-
# auth1.authenticate(request)
335+
assert auth1.token_manager.refresh_token is None
318336

319-
# assert auth1.token_manager.refresh_token is not None
337+
auth1.authenticate(request)
320338

321-
# assert request.get("headers") is not None
322-
# assert request["headers"].get("Authorization") is not None
323-
# assert "Bearer " in request["headers"].get("Authorization")
339+
assert request.get("headers") is not None
340+
assert request["headers"].get("Authorization") is not None
341+
assert "Bearer " in request["headers"].get("Authorization")
324342

343+
# Test "iamtest2" service
344+
auth2 = get_authenticator_from_environment("iamtest2")
345+
assert auth2 is not None
346+
assert auth2.token_manager is not None
347+
assert auth2.token_manager.url is not None
325348

326-
# # Test "iamtest2" service
327-
# auth2 = get_authenticator_from_environment("iamtest2")
328-
# assert auth2 is not None
329-
# assert auth2.token_manager is not None
330-
# assert auth2.token_manager.url is not None
349+
request = {'method': "GET"}
350+
request["url"] = ""
351+
request["headers"] = {}
331352

332-
# request = {'method': "GET"}
333-
# request["url"] = ""
334-
# request["headers"] = {}
353+
assert auth2.token_manager.refresh_token is None
335354

336-
# assert auth2.token_manager.refresh_token is None
355+
auth2.authenticate(request)
337356

338-
# auth2.authenticate(request)
357+
assert auth2.token_manager.refresh_token is not None
339358

340-
# assert auth2.token_manager.refresh_token is not None
359+
assert request.get("headers") is not None
360+
assert request["headers"].get("Authorization") is not None
361+
assert "Bearer " in request["headers"].get("Authorization")
341362

342-
# assert request.get("headers") is not None
343-
# assert request["headers"].get("Authorization") is not None
344-
# assert "Bearer " in request["headers"].get("Authorization")
363+
# print('Refresh token: ', auth2.token_manager.refresh_token)

0 commit comments

Comments
 (0)