<s> import ldap <EOL> from mock import patch <EOL> MOCK_LDAP_USERS = { ( '<STR_LIT>' , '<STR_LIT>' ) : [ [ '<STR_LIT>' , { '<STR_LIT:key>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] ] , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : [ [ '<STR_LIT>' , { '<STR_LIT:key>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] ] } <EOL> def simple_bind_s ( username , password ) : <EOL> if ( username , password ) not in MOCK_LDAP_USERS : <EOL> raise ldap . INVALID_CREDENTIALS <EOL> return True <EOL> def test_keys ( keys , results ) : <EOL> results = results [ <NUM_LIT:1> ] <EOL> for k , v in keys : <EOL> if k not in results : <EOL> return False <EOL> if results [ k ] == v or results [ k ] == [ v ] : <EOL> return True <EOL> def search_s ( base , scope , filtstr = None , attrlist = None ) : <EOL> if filtstr : <EOL> keys = [ filt . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) for filt in filtstr . split ( '<STR_LIT:U+002C>' ) ] <EOL> for ( user , _ ) , results in MOCK_LDAP_USERS . items ( ) : <EOL> for result in results : <EOL> if test_keys ( keys , result ) : <EOL> return results <EOL> else : <EOL> for ( user , _ ) , results in MOCK_LDAP_USERS . items ( ) : <EOL> if base == user : <EOL> return results <EOL> class LDAPTestFixture ( object ) : <EOL> def setUp ( self ) : <EOL> self . _init_patch = patch ( '<STR_LIT>' ) <EOL> self . _ldap_init = self . _init_patch . start ( ) <EOL> self . _ldap_init ( ) . simple_bind_s = simple_bind_s <EOL> self . _ldap_init ( ) . search_s = search_s <EOL> def tearDown ( self ) : <EOL> self . _init_patch . stop ( ) </s>
