From fa295d1278ef43fe2e90902006a5d073d78173ad Mon Sep 17 00:00:00 2001 From: john-griffith Date: Thu, 2 Feb 2012 22:15:58 -0700 Subject: get_user behavior in ldapdriver * Addresses bug 744462 Rather than ldapdriver.get_user() returning None for non-existent user, this change implements exception.LDAPUserNotFound to more closely match the behavior of other drivers (db). Change was made in public method only, and _check_user_exists() which uses get_user() now catches and returns None if applicable. Implemented test of NotFound exception in base auth test class. Change-Id: Ia13af759931ca0c7327d54184730537bafbe52ae --- nova/auth/ldapdriver.py | 7 ++++++- nova/tests/test_auth.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nova/auth/ldapdriver.py b/nova/auth/ldapdriver.py index 4173ecbd9..e1fa70d65 100644 --- a/nova/auth/ldapdriver.py +++ b/nova/auth/ldapdriver.py @@ -223,6 +223,8 @@ class LdapDriver(object): def get_user(self, uid): """Retrieve user by id""" attr = self.__get_ldap_user(uid) + if attr is None: + raise exception.LDAPUserNotFound(user_id=uid) return self.__to_user(attr) @sanitize @@ -495,7 +497,10 @@ class LdapDriver(object): def __user_exists(self, uid): """Check if user exists""" - return self.get_user(uid) is not None + try: + return self.get_user(uid) is not None + except exception.LDAPUserNotFound: + return False def __ldap_user_exists(self, uid): """Check if the user exists in ldap""" diff --git a/nova/tests/test_auth.py b/nova/tests/test_auth.py index a80da43f4..e645b3698 100644 --- a/nova/tests/test_auth.py +++ b/nova/tests/test_auth.py @@ -394,6 +394,11 @@ class _AuthManagerBaseTestCase(test.TestCase): self.assertEqual(old_user.secret, user.secret) self.assertEqual(old_user.is_admin(), user.is_admin()) + def test_get_nonexistent_user_raises_notfound_exception(self): + self.assertRaises(exception.NotFound, + self.manager.get_user, + 'joeblow') + class AuthManagerLdapTestCase(_AuthManagerBaseTestCase): auth_driver = 'nova.auth.ldapdriver.FakeLdapDriver' -- cgit