Skip to content

Commit d7d3225

Browse files
authored
Fix aws-login unexpected keyword argument error (#259)
* Fix aws-login unexpected keyword argument error Co-authored-by: David D. Riddle <ddriddle@illinois.edu> Closes #257
1 parent 1a26a3c commit d7d3225

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

src/awscli_login/credentials.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from botocore.session import Session
1313

14-
from .__main__ import main as login, logout
14+
from .__main__ import main as aws_login, login, logout
1515
from ._version import version
1616
from .account_names import edit_account_names
1717
from .config import Profile, error_handler
@@ -63,8 +63,8 @@ def init_parser():
6363
return parser
6464

6565

66-
@error_handler()
67-
def _main(profile: Profile, session: Session, interactive: bool = True):
66+
def get_credentials(profile: Profile, session: Session):
67+
"""Get credentials and print them."""
6868
profile.raise_if_logged_out()
6969
if profile.are_credentials_expired():
7070
token = login(profile, session, interactive=False)
@@ -73,6 +73,11 @@ def _main(profile: Profile, session: Session, interactive: bool = True):
7373
print_credentials(token)
7474

7575

76+
@error_handler()
77+
def _main(profile: Profile, session: Session):
78+
get_credentials(profile, session)
79+
80+
7681
def debug_info():
7782
executable = sys.executable if platform.system() != "Windows" else \
7883
sys.executable.lower()
@@ -116,7 +121,7 @@ def main():
116121
if ns.debug_info:
117122
debug_info()
118123
return
119-
return login(ns, session)
124+
return aws_login(ns, session)
120125
elif args.logout:
121126
return logout(Namespace(**json.load(args.logout)), session)
122127
elif args.alias:

src/tests/test_credentials.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from unittest.mock import (
2+
MagicMock,
3+
patch,
4+
)
5+
6+
from awscli_login.credentials import (
7+
get_credentials,
8+
)
9+
10+
from .login import Login
11+
12+
13+
class awsLoginTests(Login):
14+
""" Class to test the aws-login script. """
15+
16+
# NOTA BENE: This is a regression test for issue #257
17+
@patch("awscli_login.credentials.print_credentials")
18+
@patch("awscli_login.__main__.authenticate")
19+
@patch("awscli_login.__main__.save_sts_token")
20+
@patch("awscli_login.__main__.get_selection",
21+
return_value=["PrincipalArn2", "RoleArn2"])
22+
@patch("awscli_login.__main__.refresh",
23+
return_value=("SAML", ["PrincipalArn", "RoleArn"]))
24+
def test_get_credentials_with_refresh(
25+
self, refresh, get_selection, save_sts_token, authenticate,
26+
print_credentials):
27+
""" get_credentials should refresh expired credentials. """
28+
fake_token = {"TOKEN": "FAKE_DATA"}
29+
self.profile.are_credentials_expired = MagicMock(return_value=True)
30+
save_sts_token.return_value = fake_token
31+
self.profile.load_credentials = MagicMock(return_value=fake_token)
32+
33+
get_credentials(self.profile, self.session)
34+
35+
self.session.set_credentials.assert_called_with(None, None)
36+
self.session.create_client.assert_called_with("sts")
37+
self.profile.get_username.assert_not_called()
38+
refresh.assert_called_with(
39+
self.profile.ecp_endpoint_url,
40+
self.profile.cookies,
41+
self.profile.verify_ssl_certificate,
42+
)
43+
self.profile.get_credentials.assert_not_called()
44+
authenticate.assert_not_called()
45+
get_selection.assert_called_with(["PrincipalArn", "RoleArn"],
46+
self.profile.role_arn, False, {})
47+
save_sts_token.assert_called_with(
48+
self.profile,
49+
self.client,
50+
"SAML",
51+
["PrincipalArn2", "RoleArn2"],
52+
self.profile.duration
53+
)
54+
self.profile.load_credentials.assert_not_called()
55+
print_credentials.assert_called_with(fake_token)
56+
57+
@patch("awscli_login.credentials.print_credentials")
58+
@patch("awscli_login.credentials.login")
59+
def test_get_credentials_without_refresh(
60+
self, login, print_credentials):
61+
""" get_credentials should just print current credentials. """
62+
fake_token = {"TOKEN": "FAKE_DATA"}
63+
self.profile.are_credentials_expired = MagicMock(return_value=False)
64+
self.profile.load_credentials = MagicMock(return_value=fake_token)
65+
66+
get_credentials(self.profile, self.session)
67+
68+
login.assert_not_called()
69+
self.profile.load_credentials.assert_called()
70+
print_credentials.assert_called_with(fake_token)

0 commit comments

Comments
 (0)