Skip to content

Commit 50d7a92

Browse files
author
Shakeel Mohamed
committed
Add cookie-auth tests for both service and binding
1 parent 347d9ed commit 50d7a92

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

tests/test_binding.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# under the License.
1616

1717

18-
import uuid
18+
import time
1919
import urllib2
2020
from StringIO import StringIO
2121
from xml.etree.ElementTree import XML
@@ -486,6 +486,54 @@ def test_logout(self):
486486
response = self.context.get("/services")
487487
self.assertEqual(response.status, 200)
488488

489+
class TestCookieAuthentication(unittest.TestCase):
490+
def setUp(self):
491+
self.opts = testlib.parse([], {}, ".splunkrc")
492+
self.context = binding.connect(**self.opts.kwargs)
493+
494+
# Skip these tests if running below Splunk 6.2, cookie-auth didn't exist before
495+
import splunklib.client as client
496+
service = client.Service(**self.opts.kwargs)
497+
splver = service.splunk_version
498+
if splver[:2] < (6, 2):
499+
self.skipTest("Skipping cookie-auth tests, running in %d.%d.%d, this feature was added in 6.2+" % splver)
500+
501+
def test_cookie_in_auth_headers(self):
502+
self.assertIsNotNone(self.context._auth_headers)
503+
self.assertNotEqual(self.context._auth_headers, [])
504+
self.assertEqual(len(self.context._auth_headers), 1)
505+
self.assertEqual(len(self.context._auth_headers), 1)
506+
self.assertEqual(self.context._auth_headers[0][0], "cookie")
507+
self.assertEqual(self.context._auth_headers[0][1][:8], "splunkd_")
508+
509+
def test_got_cookie_on_connect(self):
510+
self.assertIsNotNone(self.context.cookie)
511+
self.assertNotEqual(self.context.cookie, binding._NoAuthenticationToken)
512+
self.assertEqual(self.context.cookie[:8], "splunkd_")
513+
514+
def test_got_updated_cookie_with_get(self):
515+
old_cookie = self.context.cookie
516+
resp = self.context.get("apps/local")
517+
found = False
518+
for key, value in resp.headers:
519+
if key.lower() == "set-cookie":
520+
found = True
521+
self.assertEqual(value[:8], "splunkd_")
522+
# It's unlikely that the cookie will change during this short test
523+
self.assertEqual(value, old_cookie)
524+
self.assertTrue(found)
525+
526+
def test_login_fails_without_cookie_or_token(self):
527+
opts = {
528+
'host': self.opts.kwargs['host'],
529+
'port': self.opts.kwargs['port']
530+
}
531+
try:
532+
binding.connect(**opts)
533+
self.fail()
534+
except AuthenticationError as ae:
535+
self.assertEqual(ae.message, "Login failed.")
536+
489537
class TestNamespace(unittest.TestCase):
490538
def test_namespace(self):
491539
tests = [

tests/test_service.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@
1515
# under the License.
1616

1717
import testlib
18-
import logging
19-
2018
import unittest
2119

22-
import splunklib.data as data
23-
2420
import splunklib.client as client
2521
from splunklib.client import AuthenticationError
2622
from splunklib.client import Service
27-
from splunklib.binding import HTTPError
23+
from splunklib.binding import HTTPError, _NoAuthenticationToken
2824

2925

3026
class ServiceTestCase(testlib.SDKTestCase):
@@ -145,6 +141,34 @@ def _create_unauthenticated_service(self):
145141
'scheme': self.opts.kwargs['scheme']
146142
})
147143

144+
class TestCookieAuthentication(unittest.TestCase):
145+
def setUp(self):
146+
self.opts = testlib.parse([], {}, ".splunkrc")
147+
self.service = client.Service(**self.opts.kwargs)
148+
149+
# Skip these tests if running below Splunk 6.2, cookie-auth didn't exist before
150+
splver = self.service.splunk_version
151+
if splver[:2] < (6, 2):
152+
self.skipTest("Skipping cookie-auth tests, running in %d.%d.%d, this feature was added in 6.2+" % splver)
153+
154+
def test_login_and_store_cookie(self):
155+
self.assertIsNotNone(self.service.cookie)
156+
self.assertEquals(self.service.cookie, _NoAuthenticationToken)
157+
self.service.login()
158+
self.assertIsNotNone(self.service.cookie)
159+
self.assertNotEquals(self.service.cookie, _NoAuthenticationToken)
160+
161+
def test_login_with_cookie(self):
162+
self.service.login()
163+
self.assertIsNotNone(self.service.cookie)
164+
self.assertNotEqual(self.service.cookie, _NoAuthenticationToken)
165+
# Use the cookie from the other service as the only auth param (don't need user/password)
166+
service2 = client.Service(**{"cookie": self.service.cookie})
167+
service2.login()
168+
self.assertEqual(service2.cookie, self.service.cookie)
169+
self.assertEqual(service2.cookie[:8], "splunkd_")
170+
171+
148172
class TestSettings(testlib.SDKTestCase):
149173
def test_read_settings(self):
150174
settings = self.service.settings

0 commit comments

Comments
 (0)