|
15 | 15 | # under the License. |
16 | 16 |
|
17 | 17 |
|
18 | | -import uuid |
| 18 | +import time |
19 | 19 | import urllib2 |
20 | 20 | from StringIO import StringIO |
21 | 21 | from xml.etree.ElementTree import XML |
@@ -486,6 +486,54 @@ def test_logout(self): |
486 | 486 | response = self.context.get("/services") |
487 | 487 | self.assertEqual(response.status, 200) |
488 | 488 |
|
| 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 | + |
489 | 537 | class TestNamespace(unittest.TestCase): |
490 | 538 | def test_namespace(self): |
491 | 539 | tests = [ |
|
0 commit comments