Skip to content

Commit

Permalink
Merge pull request #449 from splunk/set-cookie-issue-438
Browse files Browse the repository at this point in the history
Set cookie issue 438
  • Loading branch information
ashah-splunk committed Apr 27, 2022
2 parents 8881cd8 + 368f936 commit c282acc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
8 changes: 4 additions & 4 deletions splunklib/binding.py
Expand Up @@ -507,13 +507,13 @@ def get_cookies(self):
return self.http._cookies

def has_cookies(self):
"""Returns true if the ``HttpLib`` member of this instance has at least
one cookie stored.
"""Returns true if the ``HttpLib`` member of this instance has auth token stored.
:return: ``True`` if there is at least one cookie, else ``False``
:return: ``True`` if there is auth token present, else ``False``
:rtype: ``bool``
"""
return len(self.get_cookies()) > 0
auth_token_key = "splunkd_"
return any(auth_token_key in key for key in self.get_cookies().keys())

# Shared per-context request headers
@property
Expand Down
13 changes: 6 additions & 7 deletions tests/test_binding.py
Expand Up @@ -586,23 +586,22 @@ def test_got_updated_cookie_with_get(self):
self.assertTrue(found)

def test_login_fails_with_bad_cookie(self):
new_context = binding.connect(**{"cookie": "bad=cookie"})
# We should get an error if using a bad cookie
try:
new_context.get("apps/local")
new_context = binding.connect(**{"cookie": "bad=cookie"})
self.fail()
except AuthenticationError as ae:
self.assertEqual(str(ae), "Request failed: Session is not logged in.")
self.assertEqual(str(ae), "Login failed.")

def test_login_with_multiple_cookies(self):
bad_cookie = 'bad=cookie'
new_context = binding.connect(**{"cookie": bad_cookie})
# We should get an error if using a bad cookie
new_context = binding.Context()
new_context.get_cookies().update({"bad": "cookie"})
try:
new_context.get("apps/local")
new_context = new_context.login()
self.fail()
except AuthenticationError as ae:
self.assertEqual(str(ae), "Request failed: Session is not logged in.")
self.assertEqual(str(ae), "Login failed.")
# Bring in a valid cookie now
for key, value in self.context.get_cookies().items():
new_context.get_cookies()[key] = value
Expand Down
10 changes: 4 additions & 6 deletions tests/test_service.py
Expand Up @@ -214,15 +214,14 @@ def test_login_fails_with_bad_cookie(self):
service2 = client.Service()
self.assertEqual(len(service2.get_cookies()), 0)
service2.get_cookies().update(bad_cookie)
service2.login()
self.assertEqual(service2.get_cookies(), {'bad': 'cookie'})

# Should get an error with a bad cookie
try:
service2.apps.get()
service2.login()
self.fail()
except AuthenticationError as ae:
self.assertEqual(str(ae), "Request failed: Session is not logged in.")
self.assertEqual(str(ae), "Login failed.")

def test_autologin_with_cookie(self):
self.service.login()
Expand Down Expand Up @@ -264,14 +263,13 @@ def test_login_with_multiple_cookies(self):
self.assertIsNotNone(self.service.get_cookies())

service2 = client.Service(**{"cookie": bad_cookie})
service2.login()

# Should get an error with a bad cookie
try:
service2.apps.get()
service2.login()
self.fail()
except AuthenticationError as ae:
self.assertEqual(str(ae), "Request failed: Session is not logged in.")
self.assertEqual(str(ae), "Login failed.")

# Add on valid cookies, and try to use all of them
service2.get_cookies().update(self.service.get_cookies())
Expand Down

0 comments on commit c282acc

Please sign in to comment.