<s> import config <EOL> import requests <EOL> import time <EOL> import urllib2 <EOL> from utils import Base <EOL> from utils import ManageSfUtils <EOL> from utils import skip <EOL> from utils import skipIfIssueTrackerMissing , has_issue_tracker <EOL> from utils import get_issue_tracker_utils <EOL> from pysflib . sfgerrit import GerritUtils <EOL> from pysflib . sfauth import get_cookie <EOL> class TestUserdata ( Base ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . msu = ManageSfUtils ( config . GATEWAY_URL ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> pass <EOL> def setUp ( self ) : <EOL> self . projects = [ ] <EOL> self . rm = get_issue_tracker_utils ( <EOL> auth_cookie = config . USERS [ config . ADMIN_USER ] [ '<STR_LIT>' ] ) <EOL> self . gu = GerritUtils ( <EOL> config . GATEWAY_URL , <EOL> auth_cookie = config . USERS [ config . ADMIN_USER ] [ '<STR_LIT>' ] ) <EOL> def tearDown ( self ) : <EOL> for name in self . projects : <EOL> self . msu . deleteProject ( name , <EOL> config . ADMIN_USER ) <EOL> def create_project ( self , name , user , options = None , <EOL> cookie = None ) : <EOL> self . msu . createProject ( name , user , options , <EOL> cookie ) <EOL> self . projects . append ( name ) <EOL> def verify_userdata_gerrit ( self , login ) : <EOL> data = self . gu . get_account ( login ) <EOL> self . assertEqual ( config . USERS [ login ] [ '<STR_LIT>' ] , data . get ( '<STR_LIT:name>' ) ) <EOL> self . assertEqual ( config . USERS [ login ] [ '<STR_LIT:email>' ] , data . get ( '<STR_LIT:email>' ) ) <EOL> def verify_userdata_issue_tracker ( self , login ) : <EOL> users = self . rm . active_users ( ) <EOL> user = [ u for u in users if u [ <NUM_LIT:0> ] == login ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( login + "<STR_LIT:U+0020>" + config . USERS [ login ] [ '<STR_LIT>' ] , <EOL> user [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( config . USERS [ login ] [ '<STR_LIT:email>' ] , user [ <NUM_LIT:1> ] ) <EOL> def logout ( self ) : <EOL> url = config . GATEWAY_URL + '<STR_LIT>' <EOL> requests . get ( url ) <EOL> def login ( self , username , password , redirect = '<STR_LIT:/>' ) : <EOL> url = config . GATEWAY_URL + "<STR_LIT>" <EOL> data = { '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT>' : redirect } <EOL> return requests . post ( url , data = data ) <EOL> def test_login_redirect_to_jenkins ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logout ( ) <EOL> url = config . GATEWAY_URL + "<STR_LIT>" <EOL> quoted_url = urllib2 . quote ( url , safe = '<STR_LIT>' ) <EOL> response = self . login ( '<STR_LIT>' , config . ADMIN_PASSWORD , quoted_url ) <EOL> self . assertEqual ( url , response . url ) <EOL> self . verify_userdata_gerrit ( '<STR_LIT>' ) <EOL> if has_issue_tracker ( ) : <EOL> self . verify_userdata_issue_tracker ( '<STR_LIT>' ) <EOL> @ skipIfIssueTrackerMissing ( ) <EOL> def test_login_redirect_to_issue_tracker ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logout ( ) <EOL> url = self . rm . get_sf_projects_url ( ) <EOL> quoted_url = urllib2 . quote ( url , safe = '<STR_LIT>' ) <EOL> response = self . login ( '<STR_LIT>' , config . ADMIN_PASSWORD , quoted_url ) <EOL> self . assertEqual ( url , response . url ) <EOL> self . verify_userdata_gerrit ( '<STR_LIT>' ) <EOL> self . verify_userdata_issue_tracker ( '<STR_LIT>' ) <EOL> def test_invalid_user_login ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logout ( ) <EOL> response = self . login ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_hook_user_login ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logout ( ) <EOL> response = self . login ( config . HOOK_USER , <EOL> config . HOOK_USER_PASSWORD , <EOL> '<STR_LIT:/>' ) <EOL> self . assertTrue ( response . status_code < <NUM_LIT> ) <EOL> def test_create_local_user_and_login ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . msu . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except NotImplementedError : <EOL> skip ( "<STR_LIT>" ) <EOL> self . logout ( ) <EOL> url = config . GATEWAY_URL + "<STR_LIT>" <EOL> quoted_url = urllib2 . quote ( url , safe = '<STR_LIT>' ) <EOL> response = self . login ( '<STR_LIT>' , '<STR_LIT>' , quoted_url ) <EOL> self . assertEqual ( url , response . url ) <EOL> @ skipIfIssueTrackerMissing ( ) <EOL> def test_nonmember_backlog_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> non_member_role = self . rm . get_role ( <NUM_LIT:1> ) <EOL> assert non_member_role . name == '<STR_LIT>' <EOL> except : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> self . assertTrue ( '<STR_LIT>' in non_member_role . permissions ) <EOL> self . assertTrue ( '<STR_LIT>' in non_member_role . permissions ) <EOL> def test_delete_user_in_backends_by_username ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . msu . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except NotImplementedError : <EOL> skip ( "<STR_LIT>" ) <EOL> self . logout ( ) <EOL> self . login ( '<STR_LIT>' , '<STR_LIT>' , config . GATEWAY_URL ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> self . gu . get_account ( '<STR_LIT>' ) . get ( '<STR_LIT:email>' ) ) <EOL> if has_issue_tracker ( ) : <EOL> users = self . rm . active_users ( ) <EOL> users = [ u for u in users if u [ <NUM_LIT:0> ] == '<STR_LIT>' ] <EOL> self . assertEqual ( <NUM_LIT:1> , len ( users ) ) <EOL> user = users [ <NUM_LIT:0> ] <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> user [ <NUM_LIT:1> ] ) <EOL> del_url = config . GATEWAY_URL + '<STR_LIT>' <EOL> auth_cookie = get_cookie ( config . GATEWAY_HOST , <EOL> '<STR_LIT>' , config . ADMIN_PASSWORD ) <EOL> d = requests . delete ( del_url , <EOL> cookies = { '<STR_LIT>' : auth_cookie } ) <EOL> self . assertEqual ( <NUM_LIT> , <EOL> int ( d . status_code ) ) <EOL> auth_cookie = config . USERS [ config . ADMIN_USER ] [ '<STR_LIT>' ] <EOL> d = requests . delete ( del_url , <EOL> cookies = { '<STR_LIT>' : auth_cookie } ) <EOL> self . assertTrue ( int ( d . status_code ) < <NUM_LIT> , d . status_code ) <EOL> self . assertEqual ( False , <EOL> self . gu . get_account ( '<STR_LIT>' ) ) <EOL> if has_issue_tracker ( ) : <EOL> users = self . rm . active_users ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , <EOL> len ( [ u for u in users <EOL> if u [ <NUM_LIT:0> ] == '<STR_LIT>' ] ) ) <EOL> def test_delete_user_in_backends_by_email ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . msu . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except NotImplementedError : <EOL> skip ( "<STR_LIT>" ) <EOL> self . logout ( ) <EOL> self . login ( '<STR_LIT>' , '<STR_LIT>' , config . GATEWAY_URL ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> self . gu . get_account ( '<STR_LIT>' ) . get ( '<STR_LIT:email>' ) ) <EOL> if has_issue_tracker ( ) : <EOL> users = self . rm . active_users ( ) <EOL> users = [ u for u in users if u [ <NUM_LIT:0> ] == '<STR_LIT>' ] <EOL> self . assertEqual ( <NUM_LIT:1> , len ( users ) ) <EOL> user = users [ <NUM_LIT:0> ] <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> user [ <NUM_LIT:1> ] ) <EOL> del_url = config . GATEWAY_URL + '<STR_LIT>' <EOL> auth_cookie = config . USERS [ config . ADMIN_USER ] [ '<STR_LIT>' ] <EOL> d = requests . delete ( del_url , <EOL> cookies = { '<STR_LIT>' : auth_cookie } ) <EOL> self . assertTrue ( int ( d . status_code ) < <NUM_LIT> , d . status_code ) <EOL> self . assertEqual ( False , <EOL> self . gu . get_account ( '<STR_LIT>' ) ) <EOL> if has_issue_tracker ( ) : <EOL> users = self . rm . active_users ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , <EOL> len ( [ u for u in users if u [ <NUM_LIT:0> ] == '<STR_LIT>' ] ) ) <EOL> def test_delete_in_backend_and_recreate ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . msu . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except NotImplementedError : <EOL> skip ( "<STR_LIT>" ) <EOL> self . logout ( ) <EOL> self . login ( '<STR_LIT>' , '<STR_LIT>' , config . GATEWAY_URL ) <EOL> gerrit_id = self . gu . get_account ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> if has_issue_tracker ( ) : <EOL> tracker_id = self . rm . get_user_id_by_username ( '<STR_LIT>' ) <EOL> del_url = config . GATEWAY_URL + '<STR_LIT>' <EOL> auth_cookie = config . USERS [ config . ADMIN_USER ] [ '<STR_LIT>' ] <EOL> d = requests . delete ( del_url , <EOL> cookies = { '<STR_LIT>' : auth_cookie } ) <EOL> self . assertTrue ( int ( d . status_code ) < <NUM_LIT> , d . status_code ) <EOL> self . assertEqual ( False , <EOL> self . gu . get_account ( '<STR_LIT>' ) ) <EOL> if has_issue_tracker ( ) : <EOL> users = self . rm . active_users ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , <EOL> len ( [ u for u in users <EOL> if u [ <NUM_LIT:0> ] == '<STR_LIT>' ] ) ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> self . logout ( ) <EOL> self . login ( '<STR_LIT>' , '<STR_LIT>' , config . GATEWAY_URL ) <EOL> new_gerrit_id = self . gu . get_account ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> self . assertTrue ( gerrit_id != new_gerrit_id ) <EOL> if has_issue_tracker ( ) : <EOL> new_tracker_id = self . rm . get_user_id_by_username ( '<STR_LIT>' ) <EOL> self . assertTrue ( tracker_id != new_tracker_id ) </s>
