def _addRecord(self, dn, **kw): record = fakeldap.addTreeItems(dn) for key, value in kw.items(): if key.lower() == 'userpassword': value = [fakeldap.hash_pwd(value)] elif isinstance(value, basestring): value = [value] record[key] = value
def test_bind_with_valid_unicode_credentials_from_connection(self): from dataflake.ldapconnection.tests import fakeldap conn = self._makeSimple() conn.api_encoding = None attrs = {'userPassword': fakeldap.hash_pwd('secret')} conn.insert(u'dc=localhost', u'cn=føø', attrs=attrs) conn.bind_dn = u'cn=føø,dc=localhost' conn.bind_pwd = u'secret' response = conn.search( u'dc=localhost' , fltr=u'(cn=føø)' ) self.assertEqual(response['size'], 1)
def test_bind_with_invalid_unicode_credentials_from_connection(self): import ldap from dataflake.ldapconnection.tests import fakeldap conn = self._makeSimple() conn.api_encoding = None attrs = {'userPassword': fakeldap.hash_pwd('secret')} conn.insert(u'dc=localhost', u'cn=føø', attrs=attrs) conn.bind_dn = u'cn=føø,dc=localhost' conn.bind_pwd = u'geheim' self.assertRaises( ldap.INVALID_CREDENTIALS , conn.search , u'dc=localhost' , fltr=u'(cn=føø)' )
def _addUser(self, name, mail=None): conn = self._makeOne() user_dn = 'cn=%s,ou=users,dc=localhost' % name user_pwd = '%s_secret' % name if conn.hash_password: pwd = fakeldap.hash_pwd(user_pwd) else: pwd = user_pwd user = [ ('cn', [name]) , ('userPassword', [pwd]) , ('objectClass', ['top', 'person']) ] if mail is not None: user.append(('mail', [mail])) conn.add_s(user_dn, user) return (user_dn, user_pwd)
def test_disconnect_clears_connection_cache(self): from dataflake.ldapconnection.tests import fakeldap conn = self._makeSimple() attrs = {'userPassword': fakeldap.hash_pwd('pass')} conn.insert('dc=localhost', 'cn=foo', attrs=attrs) response = conn.search( 'dc=localhost' , fltr='(cn=foo)' , bind_dn='cn=foo,dc=localhost' , bind_pwd='pass' ) self.assertEquals(response['size'], 1) connection = conn._getConnection() self.assertNotEquals(connection, None) self.assertEquals(connection._last_bind[1], ('cn=foo,dc=localhost', 'pass')) conn.disconnect() self.assertEquals(conn._getConnection(), None)
def test_rebind_with_same_password(self): from dataflake.ldapconnection.tests import fakeldap conn = self._makeSimple() attrs = {'userPassword': fakeldap.hash_pwd('pass')} conn.insert( 'dc=localhost' , 'cn=foo' , attrs=attrs , bind_dn='cn=Manager,dc=localhost' , bind_pwd='pass' ) connection = conn._getConnection() self.assertEqual(connection._last_bind[1], ('cn=Manager,dc=localhost', 'pass')) conn.search( 'dc=localhost' , fltr='(cn=foo)' , bind_dn='cn=foo,dc=localhost' , bind_pwd='pass' ) connection = conn._getConnection() self.assertEqual(connection._last_bind[1], ('cn=foo,dc=localhost', 'pass'))
def test_hash_unicode_pwd(self): from dataflake.ldapconnection.tests import fakeldap pwd = fakeldap.hash_pwd(u'bjørn') self.assertTrue(isinstance(pwd, str)) self.assertTrue(pwd.startswith('{SHA}'))