@@ -145,6 +145,26 @@ class BaseTestCases(WsgiSetup):
145145 wsgi server is started with various feature flags
146146 """
147147
148+ def create_login_session (self , username = "admin" , password = "sekrit" ,
149+ return_response = True , expect_login_ok = True ):
150+ # Set up session to manage cookies <insert blue monster here>
151+ session = requests .Session ()
152+ session .headers .update ({'Origin' : 'http://localhost:9001' })
153+
154+ # login using form to get cookie
155+ login = {"__login_name" : username , '__login_password' : password ,
156+ "@action" : "login" }
157+ response = session .post (self .url_base ()+ '/' , data = login )
158+
159+ if expect_login_ok :
160+ # verify we have a cookie
161+ self .assertIn ('roundup_session_Roundupissuetracker' ,
162+ session .cookies )
163+
164+ if not return_response :
165+ return session
166+ return session , response
167+
148168 def test_start_page (self ):
149169 """ simple test that verifies that the server can serve a start page.
150170 """
@@ -1039,14 +1059,9 @@ def test_missing_session_key(self):
10391059 '''Test case where we have an outdated session cookie. Make
10401060 sure cookie is removed.
10411061 '''
1042- session = requests .Session ()
1043- session .headers .update ({'Origin' : 'http://localhost:9001' })
10441062
1045- # login using form to get cookie
1046- login = {"__login_name" : 'admin' , '__login_password' : 'sekrit' ,
1047- "@action" : "login" }
1048- f = session .post (self .url_base ()+ '/' , data = login )
1049-
1063+ session , f = self .create_login_session ()
1064+
10501065 # verify cookie is present and we are logged in
10511066 self .assertIn ('<b>Hello, admin</b>' , f .text )
10521067 self .assertIn ('roundup_session_Roundupissuetracker' ,
@@ -1066,44 +1081,30 @@ def test_missing_session_key(self):
10661081 self .assertNotIn ('roundup_session_Roundupissuetracker' , session .cookies )
10671082
10681083 def test_login_fail_then_succeed (self ):
1069- # Set up session to manage cookies <insert blue monster here>
1070- session = requests .Session ()
1071- session .headers .update ({'Origin' : 'http://localhost:9001' })
10721084
1073- # login using form
1074- login = {"__login_name" : 'admin' , '__login_password' : 'bad_sekrit' ,
1075- "@action" : "login" }
1076- f = session .post (self .url_base ()+ '/' , data = login )
1085+ session , f = self .create_login_session (password = "bad_sekrit" ,
1086+ expect_login_ok = False )
1087+
10771088 # verify error message and no hello message in sidebar.
10781089 self .assertIn ('class="error-message">Invalid login <br/ >' , f .text )
10791090 self .assertNotIn ('<b>Hello, admin</b>' , f .text )
10801091
1081- # login using form
1082- login = {"__login_name" : 'admin' , '__login_password' : 'sekrit' ,
1083- "@action" : "login" }
1084- f = session .post (self .url_base ()+ '/' , data = login )
1085- # look for change in text in sidebar post login
1092+ session , f = self .create_login_session (return_response = True )
10861093 self .assertIn ('<b>Hello, admin</b>' , f .text )
10871094
10881095 def test__generic_item_template_editok (self , user = "admin" ):
1089- """Load /status1 object. Admin has edit rights so should see
1096+ """Load /status7 object. Admin has edit rights so should see
10901097 a submit button. fred doesn't have edit rights
10911098 so should not have a submit button.
10921099 """
1093- # Set up session to manage cookies <insert blue monster here>
1094- session = requests .Session ()
1095- session .headers .update ({'Origin' : self .url_base ()})
1100+ session , f = self .create_login_session (username = user )
10961101
1097- # login using form
1098- login = {"__login_name" : user , '__login_password' : 'sekrit' ,
1099- "@action" : "login" }
1100- f = session .post (self .url_base ()+ '/' , data = login )
11011102 # look for change in text in sidebar post login
11021103 self .assertIn ('Hello, %s' % user , f .text )
1103- f = session .post (self .url_base ()+ '/status7' , data = login )
1104+ f = session .get (self .url_base ()+ '/status7' )
11041105 print (f .content )
11051106
1106- # status1 's name is unread
1107+ # status7 's name is done-cbb
11071108 self .assertIn (b'done-cbb' , f .content )
11081109
11091110 if user == 'admin' :
@@ -1120,14 +1121,8 @@ def test__generic_item_template_editbad(self, user="fred"):
11201121 self .test__generic_item_template_editok (user = user )
11211122
11221123 def test_new_issue_with_file_upload (self ):
1123- # Set up session to manage cookies <insert blue monster here>
1124- session = requests .Session ()
1125- session .headers .update ({'Origin' : 'http://localhost:9001' })
1124+ session , f = self .create_login_session ()
11261125
1127- # login using form
1128- login = {"__login_name" : 'admin' , '__login_password' : 'sekrit' ,
1129- "@action" : "login" }
1130- f = session .post (self .url_base ()+ '/' , data = login )
11311126 # look for change in text in sidebar post login
11321127 self .assertIn ('Hello, admin' , f .text )
11331128
@@ -1198,12 +1193,7 @@ def test_new_file_via_rest(self):
11981193
11991194 # get session variable from web form login
12001195 # and use it to upload file
1201- # login using form
1202- login = {"__login_name" : 'admin' , '__login_password' : 'sekrit' ,
1203- "@action" : "login" }
1204- f = session .post (self .url_base ()+ '/' , data = login ,
1205- headers = {'Origin' : "http://localhost:9001" }
1206- )
1196+ session , f = self .create_login_session ()
12071197 # look for change in text in sidebar post login
12081198 self .assertIn ('Hello, admin' , f .text )
12091199
0 commit comments