Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set cookie issue 438 #449

Merged
merged 4 commits into from Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions splunklib/binding.py
Expand Up @@ -499,13 +499,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