<s> from nose . tools import assert_true , assert_false , assert_equal <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import User , Group <EOL> from django . test . client import Client <EOL> from desktop import conf , middleware <EOL> from desktop . auth import backend <EOL> from django_auth_ldap import backend as django_auth_ldap_backend <EOL> from desktop . lib . django_test_util import make_logged_in_client <EOL> from desktop . lib . test_utils import add_to_group <EOL> from hadoop . test_base import PseudoHdfsTestBase <EOL> from hadoop import pseudo_hdfs4 <EOL> from useradmin import ldap_access <EOL> from useradmin . models import get_default_user_group <EOL> from useradmin . tests import LdapTestConnection <EOL> from useradmin . views import import_ldap_groups <EOL> class TestLoginWithHadoop ( PseudoHdfsTestBase ) : <EOL> reset = [ ] <EOL> test_username = '<STR_LIT>' <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> PseudoHdfsTestBase . setup_class ( ) <EOL> cls . auth_backends = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( '<STR_LIT>' , ) <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> settings . AUTHENTICATION_BACKENDS = cls . auth_backends <EOL> def setUp ( self ) : <EOL> self . c = Client ( ) <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ '<STR_LIT>' ] ) ) <EOL> def tearDown ( self ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> for finish in self . reset : <EOL> finish ( ) <EOL> if self . fs . exists ( "<STR_LIT>" % self . test_username ) : <EOL> self . cluster . fs . do_as_superuser ( self . cluster . fs . rmtree , "<STR_LIT>" % self . test_username ) <EOL> def test_login ( self ) : <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( response . context [ '<STR_LIT>' ] ) <EOL> response = self . c . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:foo>" ) ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( self . fs . exists ( "<STR_LIT>" % self . test_username ) ) <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_false ( response . context [ '<STR_LIT>' ] ) <EOL> def test_login_home_creation_failure ( self ) : <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( response . context [ '<STR_LIT>' ] ) <EOL> cluster = pseudo_hdfs4 . shared_cluster ( ) <EOL> fs = cluster . fs <EOL> assert_false ( cluster . fs . exists ( "<STR_LIT>" % self . test_username ) ) <EOL> fs . do_as_superuser ( fs . create , "<STR_LIT>" % self . test_username ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username , <EOL> '<STR_LIT:password>' : "<STR_LIT>" , <EOL> } , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( '<STR_LIT>' in response . content , response . content ) <EOL> class TestLdapLogin ( PseudoHdfsTestBase ) : <EOL> reset = [ ] <EOL> test_username = '<STR_LIT>' <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> PseudoHdfsTestBase . setup_class ( ) <EOL> cls . ldap_backend = django_auth_ldap_backend . LDAPBackend <EOL> django_auth_ldap_backend . LDAPBackend = MockLdapBackend <EOL> cls . auth_backends = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( '<STR_LIT>' , ) <EOL> reload ( backend ) <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> django_auth_ldap_backend . LDAPBackend = cls . ldap_backend <EOL> settings . AUTHENTICATION_BACKENDS = cls . auth_backends <EOL> reload ( backend ) <EOL> def setUp ( self ) : <EOL> self . c = Client ( ) <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ '<STR_LIT>' ] ) ) <EOL> self . reset . append ( conf . LDAP . LDAP_URL . set_for_testing ( '<STR_LIT>' ) ) <EOL> def tearDown ( self ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> for finish in self . reset : <EOL> finish ( ) <EOL> if self . fs . exists ( "<STR_LIT>" % self . test_username ) : <EOL> self . cluster . fs . do_as_superuser ( self . cluster . fs . rmtree , "<STR_LIT>" % self . test_username ) <EOL> if self . fs . exists ( "<STR_LIT>" ) : <EOL> self . cluster . fs . do_as_superuser ( self . cluster . fs . rmtree , "<STR_LIT>" ) <EOL> def test_login ( self ) : <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_false ( response . context [ '<STR_LIT>' ] ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( self . fs . exists ( "<STR_LIT>" % self . test_username ) ) <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_false ( response . context [ '<STR_LIT>' ] ) <EOL> def test_login_does_not_reset_groups ( self ) : <EOL> client = make_logged_in_client ( username = self . test_username , password = "<STR_LIT:test>" ) <EOL> user = User . objects . get ( username = self . test_username ) <EOL> test_group , created = Group . objects . get_or_create ( name = self . test_username ) <EOL> default_group = get_default_user_group ( ) <EOL> user . groups . all ( ) . delete ( ) <EOL> assert_false ( user . groups . exists ( ) ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( [ default_group . name ] , list ( user . groups . values_list ( '<STR_LIT:name>' , flat = True ) ) ) <EOL> add_to_group ( self . test_username , self . test_username ) <EOL> client . get ( '<STR_LIT>' ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( set ( [ default_group . name , test_group . name ] ) , set ( user . groups . values_list ( '<STR_LIT:name>' , flat = True ) ) ) <EOL> user . groups . filter ( name = default_group . name ) . delete ( ) <EOL> assert_equal ( set ( [ test_group . name ] ) , set ( user . groups . values_list ( '<STR_LIT:name>' , flat = True ) ) ) <EOL> client . get ( '<STR_LIT>' ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( [ test_group . name ] , list ( user . groups . values_list ( '<STR_LIT:name>' , flat = True ) ) ) <EOL> user . groups . remove ( test_group ) <EOL> assert_false ( user . groups . exists ( ) ) <EOL> client . get ( '<STR_LIT>' ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( [ default_group . name ] , list ( user . groups . values_list ( '<STR_LIT:name>' , flat = True ) ) ) <EOL> def test_login_home_creation_failure ( self ) : <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_false ( response . context [ '<STR_LIT>' ] ) <EOL> cluster = pseudo_hdfs4 . shared_cluster ( ) <EOL> fs = cluster . fs <EOL> assert_false ( cluster . fs . exists ( "<STR_LIT>" % self . test_username ) ) <EOL> fs . do_as_superuser ( fs . create , "<STR_LIT>" % self . test_username ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username , <EOL> '<STR_LIT:password>' : "<STR_LIT>" , <EOL> } , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( '<STR_LIT>' in response . content , response . content ) <EOL> def test_login_ignore_case ( self ) : <EOL> self . reset . append ( conf . LDAP . IGNORE_USERNAME_CASE . set_for_testing ( True ) ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username . upper ( ) , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> self . c . logout ( ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> def test_login_force_lower_case ( self ) : <EOL> self . reset . append ( conf . LDAP . FORCE_USERNAME_LOWERCASE . set_for_testing ( True ) ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username . upper ( ) , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> self . c . logout ( ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> def test_login_force_lower_case_and_ignore_case ( self ) : <EOL> self . reset . append ( conf . LDAP . IGNORE_USERNAME_CASE . set_for_testing ( True ) ) <EOL> self . reset . append ( conf . LDAP . FORCE_USERNAME_LOWERCASE . set_for_testing ( True ) ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username . upper ( ) , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> self . c . logout ( ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : self . test_username , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> def test_import_groups_on_login ( self ) : <EOL> self . reset . append ( conf . LDAP . SYNC_GROUPS_ON_LOGIN . set_for_testing ( True ) ) <EOL> ldap_access . CACHED_LDAP_CONN = LdapTestConnection ( ) <EOL> import_ldap_groups ( ldap_access . CACHED_LDAP_CONN , '<STR_LIT>' , import_members = False , import_members_recursive = False , sync_users = False , import_by_dn = False ) <EOL> import_ldap_groups ( ldap_access . CACHED_LDAP_CONN , '<STR_LIT>' , import_members = False , import_members_recursive = False , sync_users = False , import_by_dn = False ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : "<STR_LIT>" , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , response . status_code ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( <NUM_LIT:3> , User . objects . all ( ) [ <NUM_LIT:0> ] . groups . all ( ) . count ( ) , User . objects . all ( ) [ <NUM_LIT:0> ] . groups . all ( ) ) <EOL> class TestRemoteUserLogin ( PseudoHdfsTestBase ) : <EOL> reset = [ ] <EOL> test_username = "<STR_LIT>" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> PseudoHdfsTestBase . setup_class ( ) <EOL> cls . auth_backends = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( '<STR_LIT>' , ) <EOL> cls . remote_user_middleware_header = middleware . HueRemoteUserMiddleware . header <EOL> middleware . HueRemoteUserMiddleware . header = conf . AUTH . REMOTE_USER_HEADER . get ( ) <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> middleware . HueRemoteUserMiddleware . header = cls . remote_user_middleware_header <EOL> settings . AUTHENTICATION_BACKENDS = cls . auth_backends <EOL> def setUp ( self ) : <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ '<STR_LIT>' ] ) ) <EOL> self . reset . append ( conf . AUTH . REMOTE_USER_HEADER . set_for_testing ( '<STR_LIT>' ) ) <EOL> self . c = Client ( ) <EOL> def tearDown ( self ) : <EOL> for finish in self . reset : <EOL> finish ( ) <EOL> User . objects . all ( ) . delete ( ) <EOL> if self . fs . exists ( "<STR_LIT>" % self . test_username ) : <EOL> self . cluster . fs . do_as_superuser ( self . cluster . fs . rmtree , "<STR_LIT>" % self . test_username ) <EOL> if self . fs . exists ( "<STR_LIT>" % ( self . test_username , '<STR_LIT:2>' ) ) : <EOL> self . cluster . fs . do_as_superuser ( self . cluster . fs . rmtree , "<STR_LIT>" % ( self . test_username , '<STR_LIT:2>' ) ) <EOL> def test_normal ( self ) : <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_false ( response . context [ '<STR_LIT>' ] ) <EOL> assert_equal ( <NUM_LIT:0> , len ( User . objects . all ( ) ) ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : self . test_username } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> def test_ignore_case ( self ) : <EOL> self . reset . append ( conf . AUTH . IGNORE_USERNAME_CASE . set_for_testing ( True ) ) <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_false ( response . context [ '<STR_LIT>' ] ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : self . test_username } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : self . test_username . upper ( ) } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : "<STR_LIT>" % ( self . test_username . upper ( ) , '<STR_LIT:2>' ) } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:2> , len ( User . objects . all ( ) . order_by ( '<STR_LIT:username>' ) ) ) <EOL> assert_equal ( "<STR_LIT>" % ( self . test_username , '<STR_LIT:2>' ) , User . objects . all ( ) . order_by ( '<STR_LIT:username>' ) [ <NUM_LIT:1> ] . username ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : "<STR_LIT>" % ( self . test_username , '<STR_LIT:2>' ) } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:2> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( "<STR_LIT>" % ( self . test_username , '<STR_LIT:2>' ) , User . objects . all ( ) . order_by ( '<STR_LIT:username>' ) [ <NUM_LIT:1> ] . username ) <EOL> def test_force_lower_case ( self ) : <EOL> self . reset . append ( conf . AUTH . FORCE_USERNAME_LOWERCASE . set_for_testing ( True ) ) <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_false ( response . context [ '<STR_LIT>' ] ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : self . test_username } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : self . test_username . upper ( ) } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> def test_ignore_case_and_force_lower_case ( self ) : <EOL> reset = conf . AUTH . FORCE_USERNAME_LOWERCASE . set_for_testing ( False ) <EOL> try : <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : self . test_username . upper ( ) } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username . upper ( ) , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> finally : <EOL> reset ( ) <EOL> self . reset . append ( conf . AUTH . FORCE_USERNAME_LOWERCASE . set_for_testing ( True ) ) <EOL> self . reset . append ( conf . AUTH . IGNORE_USERNAME_CASE . set_for_testing ( True ) ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : self . test_username . upper ( ) } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( self . test_username . upper ( ) , User . objects . all ( ) [ <NUM_LIT:0> ] . username ) <EOL> response = self . c . post ( '<STR_LIT>' , { } , ** { "<STR_LIT>" : "<STR_LIT>" % ( self . test_username . upper ( ) , '<STR_LIT:2>' ) } ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:2> , len ( User . objects . all ( ) ) ) <EOL> assert_equal ( "<STR_LIT>" % ( self . test_username , '<STR_LIT:2>' ) , User . objects . all ( ) . order_by ( '<STR_LIT:username>' ) [ <NUM_LIT:1> ] . username ) <EOL> class TestMultipleBackendLogin ( PseudoHdfsTestBase ) : <EOL> reset = [ ] <EOL> test_username = "<STR_LIT>" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> PseudoHdfsTestBase . setup_class ( ) <EOL> cls . ldap_backend = django_auth_ldap_backend . LDAPBackend <EOL> django_auth_ldap_backend . LDAPBackend = MockLdapBackend <EOL> cls . auth_backends = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> reload ( backend ) <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> django_auth_ldap_backend . LDAPBackend = cls . ldap_backend <EOL> settings . AUTHENTICATION_BACKENDS = cls . auth_backends <EOL> reload ( backend ) <EOL> def setUp ( self ) : <EOL> self . c = Client ( ) <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . reset . append ( conf . LDAP . LDAP_URL . set_for_testing ( '<STR_LIT>' ) ) <EOL> def tearDown ( self ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> for finish in self . reset : <EOL> finish ( ) <EOL> if self . fs . exists ( "<STR_LIT>" % self . test_username ) : <EOL> self . cluster . fs . do_as_superuser ( self . cluster . fs . rmtree , "<STR_LIT>" % self . test_username ) <EOL> def test_login_with_ldap ( self ) : <EOL> ldap_access . CACHED_LDAP_CONN = LdapTestConnection ( ) <EOL> response = self . c . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : "<STR_LIT>" , <EOL> '<STR_LIT:password>' : "<STR_LIT>" <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , response . status_code ) <EOL> assert_equal ( <NUM_LIT:1> , len ( User . objects . all ( ) ) ) <EOL> def test_fallback_to_db ( self ) : <EOL> ldap_access . CACHED_LDAP_CONN = LdapTestConnection ( ) <EOL> client = make_logged_in_client ( username = self . test_username , password = "<STR_LIT:test>" ) <EOL> client . get ( '<STR_LIT>' ) <EOL> user = User . objects . get ( username = self . test_username ) <EOL> response = self . c . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:foo>" ) ) <EOL> assert_equal ( <NUM_LIT> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( self . fs . exists ( "<STR_LIT>" % self . test_username ) ) <EOL> class TestLogin ( PseudoHdfsTestBase ) : <EOL> reset = [ ] <EOL> test_username = "<STR_LIT>" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> PseudoHdfsTestBase . setup_class ( ) <EOL> cls . auth_backends = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( '<STR_LIT>' , ) <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> settings . AUTHENTICATION_BACKENDS = cls . auth_backends <EOL> def setUp ( self ) : <EOL> self . c = Client ( ) <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ '<STR_LIT>' ] ) ) <EOL> def tearDown ( self ) : <EOL> for finish in self . reset : <EOL> finish ( ) <EOL> User . objects . all ( ) . delete ( ) <EOL> if self . fs . exists ( "<STR_LIT>" % self . test_username ) : <EOL> self . cluster . fs . do_as_superuser ( self . cluster . fs . rmtree , "<STR_LIT>" % self . test_username ) <EOL> def test_bad_first_user ( self ) : <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ "<STR_LIT>" ] ) ) <EOL> response = self . c . get ( '<STR_LIT>' ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( response . context [ '<STR_LIT>' ] ) <EOL> response = self . c . post ( '<STR_LIT>' , dict ( username = "<STR_LIT>" , password = "<STR_LIT:foo>" ) ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( '<STR_LIT>' in response . content , response ) <EOL> def test_non_jframe_login ( self ) : <EOL> client = make_logged_in_client ( username = self . test_username , password = "<STR_LIT:test>" ) <EOL> client . get ( '<STR_LIT>' ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_true ( any ( [ "<STR_LIT>" in _template . filename for _template in response . templates ] ) , response . content ) <EOL> def test_login_expiration ( self ) : <EOL> """<STR_LIT>""" <EOL> old_settings = settings . ADMINS <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ "<STR_LIT>" ] ) ) <EOL> self . reset . append ( conf . AUTH . EXPIRES_AFTER . set_for_testing ( <NUM_LIT:0> ) ) <EOL> self . reset . append ( conf . AUTH . EXPIRE_SUPERUSERS . set_for_testing ( False ) ) <EOL> client = make_logged_in_client ( username = self . test_username , password = "<STR_LIT:test>" ) <EOL> client . get ( '<STR_LIT>' ) <EOL> user = User . objects . get ( username = self . test_username ) <EOL> try : <EOL> user . is_superuser = True <EOL> user . save ( ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> client . get ( '<STR_LIT>' ) <EOL> settings . ADMINS = [ ( self . test_username , '<STR_LIT>' ) ] <EOL> user . is_superuser = False <EOL> user . save ( ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( '<STR_LIT>' in response . content , response . content ) <EOL> settings . ADMINS = [ ] <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> assert_true ( "<STR_LIT>" in response . content , response . content ) <EOL> finally : <EOL> settings . ADMINS = old_settings <EOL> def test_login_expiration_with_superusers ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ "<STR_LIT>" ] ) ) <EOL> self . reset . append ( conf . AUTH . EXPIRES_AFTER . set_for_testing ( <NUM_LIT:0> ) ) <EOL> self . reset . append ( conf . AUTH . EXPIRE_SUPERUSERS . set_for_testing ( True ) ) <EOL> client = make_logged_in_client ( username = self . test_username , password = "<STR_LIT:test>" ) <EOL> client . get ( '<STR_LIT>' ) <EOL> user = User . objects . get ( username = self . test_username ) <EOL> user . is_superuser = True <EOL> user . save ( ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> class TestLoginNoHadoop ( object ) : <EOL> reset = [ ] <EOL> test_username = "<STR_LIT>" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> User . objects . all ( ) . delete ( ) <EOL> cls . auth_backends = settings . AUTHENTICATION_BACKENDS <EOL> settings . AUTHENTICATION_BACKENDS = ( '<STR_LIT>' , ) <EOL> @ classmethod <EOL> def teardown_class ( cls ) : <EOL> settings . AUTHENTICATION_BACKENDS = cls . auth_backends <EOL> def setUp ( self ) : <EOL> self . c = Client ( ) <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ '<STR_LIT>' ] ) ) <EOL> def tearDown ( self ) : <EOL> for finish in self . reset : <EOL> finish ( ) <EOL> User . objects . all ( ) . delete ( ) <EOL> if Group . objects . filter ( name = self . test_username ) . exists ( ) : <EOL> Group . objects . filter ( name = self . test_username ) . delete ( ) <EOL> def test_login_does_not_reset_groups ( self ) : <EOL> self . reset . append ( conf . AUTH . BACKEND . set_for_testing ( [ "<STR_LIT>" ] ) ) <EOL> client = make_logged_in_client ( username = self . test_username , password = "<STR_LIT:test>" ) <EOL> client . get ( '<STR_LIT>' ) <EOL> user = User . objects . get ( username = self . test_username ) <EOL> group , created = Group . objects . get_or_create ( name = self . test_username ) <EOL> user . groups . all ( ) . delete ( ) <EOL> assert_false ( user . groups . exists ( ) ) <EOL> response = client . post ( '<STR_LIT>' , dict ( username = self . test_username , password = "<STR_LIT:test>" ) , follow = True ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , "<STR_LIT>" ) <EOL> class MockLdapBackend ( object ) : <EOL> settings = django_auth_ldap_backend . LDAPSettings ( ) <EOL> def get_or_create_user ( self , username , ldap_user ) : <EOL> return User . objects . get_or_create ( username ) <EOL> def authenticate ( self , username = None , password = None , server = None ) : <EOL> user , created = self . get_or_create_user ( username , None ) <EOL> return user <EOL> def get_user ( self , user_id ) : <EOL> return User . objects . get ( id = user_id ) </s>
