Skip to content

Commit cedd209

Browse files
committed
2 parents 9c33b02 + b70db4b commit cedd209

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/sasctl/core.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,15 @@ def is_ipaddress(hst):
377377
# Try to get credentials from .authinfo or .netrc files.
378378
# If no file path was specified, the default locations will
379379
# be checked.
380-
if "swat" in sys.modules:
380+
try:
381381
auth = swat.utils.authinfo.query_authinfo(
382382
domain, user=username, path=authinfo
383383
)
384384
self._settings["username"] = auth.get("user")
385385
self._settings["password"] = auth.get("password")
386+
except AttributeError:
387+
# If swat package or authinfo module not available
388+
pass
386389

387390
# Not able to load credentials using SWAT. Try Netrc.
388391
# TODO: IF a username was specified, verify that the credentials
@@ -508,11 +511,11 @@ def as_swat(self, server=None, **kwargs):
508511

509512
# Starting in SWAT v1.8 oauth tokens could be passed directly in the password param.
510513
# Otherwise, use the username & password to re-authenticate.
514+
# Use this sessions info to connect to CAS unless user has explicitly give a value (even if None)
511515
if version.parse(swat.__version__) >= version.parse("1.8"):
512-
kwargs["username"] = None
513-
kwargs["password"] = self.auth.access_token
516+
kwargs.setdefault("username", None)
517+
kwargs.setdefault("password", self.auth.access_token)
514518
else:
515-
# Use this sessions info to connect to CAS unless user has explicitly give a value (even if None)
516519
kwargs.setdefault("username", self.username)
517520
kwargs.setdefault("password", self._settings["password"])
518521

tests/unit/test_session.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -403,31 +403,44 @@ def test_as_swat():
403403
HOST = 'example.com'
404404
USERNAME = 'username'
405405
PASSWORD = 'password'
406+
AUTH_TOKEN = 'token'
406407

407408
with mock.patch('sasctl.core.Session.get_auth'):
408409
with Session(HOST, USERNAME, PASSWORD) as s:
409410
with mock.patch('swat.CAS') as CAS:
410-
# Verify default parameters were passed
411-
_ = s.as_swat()
412-
CAS.assert_called_with(
413-
hostname='https://%s/cas-shared-default-http/' % HOST,
414-
username=USERNAME,
415-
password=PASSWORD,
416-
)
417-
418-
# Verify connection to a non-default CAS instance
419-
SERVER_NAME = 'my-cas-server'
420-
_ = s.as_swat(SERVER_NAME)
421-
CAS.assert_called_with(
422-
hostname='https://%s/%s-http/' % (HOST, SERVER_NAME),
423-
username=USERNAME,
424-
password=PASSWORD,
425-
)
426411

427-
# Verify default parameters can be overridden
428-
_ = s.as_swat(username='testuser', password=None)
429-
CAS.assert_called_with(
430-
hostname='https://%s/cas-shared-default-http/' % HOST,
431-
username='testuser',
432-
password=None,
433-
)
412+
# Verify default username/password parameters were passed.
413+
with mock.patch('swat.__version__', '1.6.1'):
414+
_ = s.as_swat()
415+
CAS.assert_called_with(
416+
hostname='https://%s/cas-shared-default-http/' % HOST,
417+
username=USERNAME,
418+
password=PASSWORD,
419+
)
420+
421+
# Verify auth token is passed with new SWAT versions
422+
with mock.patch('swat.__version__', '1.9'):
423+
s.auth.access_token = AUTH_TOKEN
424+
_ = s.as_swat()
425+
CAS.assert_called_with(
426+
hostname='https://%s/cas-shared-default-http/' % HOST,
427+
username=None,
428+
password=AUTH_TOKEN,
429+
)
430+
431+
# Verify connection to a non-default CAS instance
432+
SERVER_NAME = 'my-cas-server'
433+
_ = s.as_swat(SERVER_NAME)
434+
CAS.assert_called_with(
435+
hostname='https://%s/%s-http/' % (HOST, SERVER_NAME),
436+
username=None,
437+
password=AUTH_TOKEN,
438+
)
439+
440+
# Verify default parameters can be overridden
441+
_ = s.as_swat(username='testuser', password=None)
442+
CAS.assert_called_with(
443+
hostname='https://%s/cas-shared-default-http/' % HOST,
444+
username='testuser',
445+
password=None,
446+
)

0 commit comments

Comments
 (0)