Skip to content

Commit 489d528

Browse files
committed
test: improve coverage and minor fixes
1 parent 6936273 commit 489d528

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

test/test_container_authenticator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ def test_container_authenticator():
1717
authenticator.set_cr_token_filename('path/to/token')
1818
assert authenticator.token_manager.cr_token_filename == 'path/to/token'
1919

20-
authenticator.set_iam_profile_name('iam-user-123')
21-
assert authenticator.token_manager.iam_profile_name == 'iam-user-123'
20+
# Set the IAM profile to None to trigger a validation which will fail,
21+
# because both of the profile and ID are None.
22+
with pytest.raises(ValueError) as err:
23+
authenticator.set_iam_profile_name(None)
24+
assert str(err.value) == 'At least one of iam_profile_name or iam_profile_id must be specified.'
2225

2326
authenticator.set_iam_profile_id('iam-id-123')
2427
assert authenticator.token_manager.iam_profile_id == 'iam-id-123'
2528

29+
authenticator.set_iam_profile_name('iam-user-123')
30+
assert authenticator.token_manager.iam_profile_name == 'iam-user-123'
31+
2632
authenticator.set_client_id_and_secret('tom', 'jerry')
2733
assert authenticator.token_manager.client_id == 'tom'
2834
assert authenticator.token_manager.client_secret == 'jerry'

test/test_container_token_manager.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_request_token_success():
196196
def test_authenticate_success():
197197
authenticator = ContainerAuthenticator(
198198
cr_token_filename=cr_token_file,
199-
iam_profile_name='iam-user-123')
199+
iam_profile_name=MOCK_IAM_PROFILE_NAME)
200200

201201
request = {'headers': {}}
202202

@@ -222,7 +222,7 @@ def test_authenticate_success():
222222
def test_authenticate_fail_no_cr_token():
223223
authenticator = ContainerAuthenticator(
224224
cr_token_filename='bogus-cr-token-file',
225-
iam_profile_name='iam-user-123',
225+
iam_profile_name=MOCK_IAM_PROFILE_NAME,
226226
url='https://bogus.iam.endpoint')
227227

228228
request = {'headers': {}}
@@ -237,7 +237,7 @@ def test_authenticate_fail_no_cr_token():
237237
def test_authenticate_fail_iam():
238238
authenticator = ContainerAuthenticator(
239239
cr_token_filename=cr_token_file,
240-
iam_profile_name='iam-user-123',
240+
iam_profile_name=MOCK_IAM_PROFILE_NAME,
241241
scope='status-bad-request')
242242

243243
request = {'headers': {}}
@@ -259,3 +259,17 @@ def test_client_id_and_secret():
259259
token_manager.set_scope('check-basic-auth')
260260
access_token = token_manager.get_token()
261261
assert access_token == TEST_ACCESS_TOKEN_1
262+
263+
@mock_iam_response
264+
def test_setter_methods():
265+
token_manager = ContainerTokenManager(
266+
cr_token_filename='bogus-cr-token-file',
267+
iam_profile_id=MOCK_IAM_PROFILE_NAME,
268+
)
269+
270+
token_manager.set_iam_profile_id('iam-id-123')
271+
token_manager.set_iam_profile_name(None)
272+
token_manager.set_cr_token_filename(cr_token_file)
273+
274+
access_token = token_manager.get_token()
275+
assert access_token == TEST_ACCESS_TOKEN_1

0 commit comments

Comments
 (0)