Skip to content

Commit

Permalink
Separate authentication related tests from the rest.
Browse files Browse the repository at this point in the history
Use an isolated per-test client for authentication related tests.
This should help avoid errors in test teardown trying to recursively
delete all nodes using `self.client`.
  • Loading branch information
hannosch committed Jun 6, 2014
1 parent 5b80c05 commit 49aee2e
Showing 1 changed file with 50 additions and 36 deletions.
86 changes: 50 additions & 36 deletions kazoo/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,29 @@ def test_retry_options_dict(self):
eq_(client._retry.max_tries, 99)
eq_(client._conn_retry.delay, 99)

class TestConnection(KazooTestCase):

class TestAuthentication(KazooTestCase):

def _makeAuth(self, *args, **kwargs):
from kazoo.security import make_digest_acl
return make_digest_acl(*args, **kwargs)

def test_chroot_warning(self):
k = self._get_nonchroot_client()
k.chroot = 'abba'
try:
with patch('warnings.warn') as mock_func:
k.start()
assert mock_func.called
finally:
k.stop()

def test_auth(self):
username = uuid.uuid4().hex
password = uuid.uuid4().hex

digest_auth = "%s:%s" % (username, password)
acl = self._makeAuth(username, password, all=True)

self.client.add_auth("digest", digest_auth)
self.client.default_acl = (acl,)
client = self._get_client()
client.start()
client.add_auth("digest", digest_auth)
client.default_acl = (acl,)

try:
self.client.create("/1")
self.client.create("/1/2")
self.client.ensure_path("/1/2/3")
client.create("/1")
client.create("/1/2")
client.ensure_path("/1/2/3")

eve = self._get_client()
eve.start()
Expand All @@ -149,7 +143,7 @@ def test_auth(self):
self.assertRaises(NoAuthError, eve.get, "/1/2")
finally:
# Ensure we remove the ACL protected nodes
self.client.delete("/1", recursive=True)
client.delete("/1", recursive=True)
eve.stop()
eve.close()

Expand Down Expand Up @@ -178,12 +172,14 @@ def test_unicode_auth(self):
digest_auth = "%s:%s" % (username, password)
acl = self._makeAuth(username, password, all=True)

self.client.add_auth("digest", digest_auth)
self.client.default_acl = (acl,)
client = self._get_client()
client.start()
client.add_auth("digest", digest_auth)
client.default_acl = (acl,)

try:
self.client.create("/1")
self.client.ensure_path("/1/2/3")
client.create("/1")
client.ensure_path("/1/2/3")

eve = self._get_client()
eve.start()
Expand All @@ -196,38 +192,58 @@ def test_unicode_auth(self):
self.assertRaises(NoAuthError, eve.get, "/1/2")
finally:
# Ensure we remove the ACL protected nodes
self.client.delete("/1", recursive=True)
client.delete("/1", recursive=True)
eve.stop()
eve.close()

def test_invalid_auth(self):
self.assertRaises(TypeError, self.client.add_auth,
'digest', ('user', 'pass'))
self.assertRaises(TypeError, self.client.add_auth,
None, ('user', 'pass'))
client = self._get_client()
client.start()
self.assertRaises(TypeError, client.add_auth,
'digest', ('user', 'pass'))
self.assertRaises(TypeError, client.add_auth,
None, ('user', 'pass'))

def test_async_auth(self):
client = self._get_client()
client.start()
username = uuid.uuid4().hex
password = uuid.uuid4().hex
digest_auth = "%s:%s" % (username, password)
result = self.client.add_auth_async("digest", digest_auth)
result = client.add_auth_async("digest", digest_auth)
self.assertTrue(result.get())

def test_async_auth_failure(self):
from kazoo.exceptions import AuthFailedError

client = self._get_client()
client.start()
username = uuid.uuid4().hex
password = uuid.uuid4().hex
digest_auth = "%s:%s" % (username, password)

self.assertRaises(AuthFailedError, self.client.add_auth, "unknown-scheme", digest_auth)
self.assertRaises(AuthFailedError, client.add_auth,
"unknown-scheme", digest_auth)

def test_add_auth_on_reconnect(self):
self.client.add_auth("digest", "jsmith:jsmith")
self.client._connection._socket.shutdown(socket.SHUT_RDWR)
while not self.client.connected:
client = self._get_client()
client.start()
client.add_auth("digest", "jsmith:jsmith")
client._connection._socket.shutdown(socket.SHUT_RDWR)
while not client.connected:
time.sleep(0.1)
self.assertTrue(("digest", "jsmith:jsmith") in self.client.auth_data)
self.assertTrue(("digest", "jsmith:jsmith") in client.auth_data)


class TestConnection(KazooTestCase):

def test_chroot_warning(self):
k = self._get_nonchroot_client()
k.chroot = 'abba'
try:
with patch('warnings.warn') as mock_func:
k.start()
assert mock_func.called
finally:
k.stop()

def test_session_expire(self):
from kazoo.protocol.states import KazooState
Expand Down Expand Up @@ -360,8 +376,6 @@ def test_closed(self):
client.close()
eq_(client._connection._write_pipe, None)

from kazoo.protocol.states import KeeperState

# sneak in and patch client to simulate race between a thread
# calling stop(); close() and one running a command
oldstate = client._state
Expand Down

0 comments on commit 49aee2e

Please sign in to comment.