Skip to content

Commit 0795efa

Browse files
pyrookarmkeezer
andcommitted
chore: set access_token properly
Co-authored-by: Matthew Keezer <[email protected]>
1 parent 9649049 commit 0795efa

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

ibm_cloud_sdk_core/token_managers/vpc_instance_token_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class VPCInstanceTokenManager(JWTTokenManager):
5151

5252
METADATA_SERVICE_VERSION = '2021-09-20'
5353
DEFAULT_IMS_ENDPOINT = 'http://169.254.169.254'
54+
TOKEN_NAME = 'access_token'
5455

5556
def __init__(self,
5657
iam_profile_crn: Optional[str] = None,
@@ -59,7 +60,7 @@ def __init__(self,
5960
if not url:
6061
url = self.DEFAULT_IMS_ENDPOINT
6162

62-
super().__init__(url)
63+
super().__init__(url, token_name=self.TOKEN_NAME)
6364

6465
self.iam_profile_crn = iam_profile_crn
6566
self.iam_profile_id = iam_profile_id

test/test_vpc_instance_token_manager.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from ibm_cloud_sdk_core import ApiException, VPCInstanceTokenManager
2525

2626

27+
#pylint: disable=line-too-long
28+
TEST_ACCESS_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImhlbGxvIiwicm9sZSI6InVzZXIiLCJwZXJtaXNzaW9ucyI6WyJhZG1pbmlzdHJhdG9yIiwiZGVwbG95bWVudF9hZG1pbiJdLCJzdWIiOiJoZWxsbyIsImlzcyI6IkpvaG4iLCJhdWQiOiJEU1giLCJ1aWQiOiI5OTkiLCJpYXQiOjE1NjAyNzcwNTEsImV4cCI6MTU2MDI4MTgxOSwianRpIjoiMDRkMjBiMjUtZWUyZC00MDBmLTg2MjMtOGNkODA3MGI1NDY4In0.cIodB4I6CCcX8vfIImz7Cytux3GpWyObt9Gkur5g1QI'
2729
TEST_TOKEN = 'abc123'
2830
TEST_IAM_TOKEN = 'iam-abc123'
2931
TEST_IAM_PROFILE_CRN = 'crn:iam-profile:123'
@@ -85,6 +87,7 @@ def test_retrieve_instance_identity_token(caplog):
8587
assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_access_token\' operation: http://someurl.com/instance_identity/v1/token'
8688
assert caplog.record_tuples[1][2] == 'Returned from VPC \'create_access_token\' operation."'
8789

90+
8891
@responses.activate
8992
def test_retrieve_instance_identity_token_failed(caplog):
9093
caplog.set_level(logging.DEBUG)
@@ -109,6 +112,7 @@ def test_retrieve_instance_identity_token_failed(caplog):
109112
#pylint: disable=line-too-long
110113
assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_access_token\' operation: http://someurl.com/instance_identity/v1/token'
111114

115+
112116
@responses.activate
113117
def test_request_token_with_crn(caplog):
114118
caplog.set_level(logging.DEBUG)
@@ -176,7 +180,7 @@ def mock_retrieve_instance_identity_token():
176180

177181

178182
@responses.activate
179-
def test_request_token_with_failed(caplog):
183+
def test_request_token_failed(caplog):
180184
caplog.set_level(logging.DEBUG)
181185

182186
token_manager = VPCInstanceTokenManager(
@@ -201,3 +205,31 @@ def mock_retrieve_instance_identity_token():
201205
# Check the logs.
202206
#pylint: disable=line-too-long
203207
assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_iam_token\' operation: http://169.254.169.254/instance_identity/v1/iam_token'
208+
209+
210+
@responses.activate
211+
def test_access_token():
212+
token_manager = VPCInstanceTokenManager(
213+
iam_profile_id=TEST_IAM_PROFILE_ID,
214+
)
215+
216+
response_ii = {
217+
'access_token': TEST_TOKEN,
218+
}
219+
response_iam = {
220+
'access_token': TEST_ACCESS_TOKEN,
221+
}
222+
223+
responses.add(responses.PUT, 'http://169.254.169.254/instance_identity/v1/token',
224+
body=json.dumps(response_ii), status=200)
225+
responses.add(responses.POST, 'http://169.254.169.254/instance_identity/v1/iam_token',
226+
body=json.dumps(response_iam), status=200)
227+
228+
assert token_manager.access_token is None
229+
assert token_manager.expire_time == 0
230+
assert token_manager.refresh_time == 0
231+
232+
token_manager.get_token()
233+
assert token_manager.access_token == TEST_ACCESS_TOKEN
234+
assert token_manager.expire_time > 0
235+
assert token_manager.refresh_time > 0

0 commit comments

Comments
 (0)