[2] | 1 | import pkg_resources |
---|
| 2 | pkg_resources.require( "twill==0.9" ) |
---|
| 3 | |
---|
| 4 | import StringIO, os, sys, random, filecmp, time, unittest, urllib, logging, difflib, tarfile, zipfile, tempfile, re, shutil |
---|
| 5 | from itertools import * |
---|
| 6 | |
---|
| 7 | import twill |
---|
| 8 | import twill.commands as tc |
---|
| 9 | from twill.other_packages._mechanize_dist import ClientForm |
---|
| 10 | pkg_resources.require( "elementtree" ) |
---|
| 11 | from elementtree import ElementTree |
---|
| 12 | from galaxy.web import security |
---|
| 13 | from galaxy.web.framework.helpers import iff |
---|
| 14 | |
---|
| 15 | buffer = StringIO.StringIO() |
---|
| 16 | |
---|
| 17 | #Force twill to log to a buffer -- FIXME: Should this go to stdout and be captured by nose? |
---|
| 18 | twill.set_output(buffer) |
---|
| 19 | tc.config('use_tidy', 0) |
---|
| 20 | |
---|
| 21 | # Dial ClientCookie logging down (very noisy) |
---|
| 22 | logging.getLogger( "ClientCookie.cookies" ).setLevel( logging.WARNING ) |
---|
| 23 | log = logging.getLogger( __name__ ) |
---|
| 24 | |
---|
| 25 | class TwillTestCase( unittest.TestCase ): |
---|
| 26 | |
---|
| 27 | def setUp( self ): |
---|
| 28 | # Security helper |
---|
| 29 | self.security = security.SecurityHelper( id_secret='changethisinproductiontoo' ) |
---|
| 30 | self.history_id = os.environ.get( 'GALAXY_TEST_HISTORY_ID', None ) |
---|
| 31 | self.host = os.environ.get( 'GALAXY_TEST_HOST' ) |
---|
| 32 | self.port = os.environ.get( 'GALAXY_TEST_PORT' ) |
---|
| 33 | self.url = "http://%s:%s" % ( self.host, self.port ) |
---|
| 34 | self.file_dir = os.environ.get( 'GALAXY_TEST_FILE_DIR' ) |
---|
| 35 | self.home() |
---|
| 36 | #self.set_history() |
---|
| 37 | |
---|
| 38 | # Functions associated with files |
---|
| 39 | def files_diff( self, file1, file2, attributes=None ): |
---|
| 40 | """Checks the contents of 2 files for differences""" |
---|
| 41 | def get_lines_diff( diff ): |
---|
| 42 | count = 0 |
---|
| 43 | for line in diff: |
---|
| 44 | if ( line.startswith( '+' ) and not line.startswith( '+++' ) ) or ( line.startswith( '-' ) and not line.startswith( '---' ) ): |
---|
| 45 | count += 1 |
---|
| 46 | return count |
---|
| 47 | if not filecmp.cmp( file1, file2 ): |
---|
| 48 | files_differ = False |
---|
| 49 | local_file = open( file1, 'U' ).readlines() |
---|
| 50 | history_data = open( file2, 'U' ).readlines() |
---|
| 51 | if attributes is None: |
---|
| 52 | attributes = {} |
---|
| 53 | if attributes.get( 'sort', False ): |
---|
| 54 | history_data.sort() |
---|
| 55 | ##Why even bother with the check loop below, why not just use the diff output? This seems wasteful. |
---|
| 56 | if len( local_file ) == len( history_data ): |
---|
| 57 | for i in range( len( history_data ) ): |
---|
| 58 | if local_file[i].rstrip( '\r\n' ) != history_data[i].rstrip( '\r\n' ): |
---|
| 59 | files_differ = True |
---|
| 60 | break |
---|
| 61 | else: |
---|
| 62 | files_differ = True |
---|
| 63 | if files_differ: |
---|
| 64 | allowed_diff_count = int(attributes.get( 'lines_diff', 0 )) |
---|
| 65 | diff = list( difflib.unified_diff( local_file, history_data, "local_file", "history_data" ) ) |
---|
| 66 | diff_lines = get_lines_diff( diff ) |
---|
| 67 | log.debug('## files diff on %s and %s lines_diff=%d, found diff = %d' % (file1,file2,allowed_diff_count,diff_lines)) |
---|
| 68 | if diff_lines > allowed_diff_count: |
---|
| 69 | diff_slice = diff[0:40] |
---|
| 70 | #FIXME: This pdf stuff is rather special cased and has not been updated to consider lines_diff |
---|
| 71 | #due to unknown desired behavior when used in conjunction with a non-zero lines_diff |
---|
| 72 | #PDF forgiveness can probably be handled better by not special casing by __extension__ here |
---|
| 73 | #and instead using lines_diff or a regular expression matching |
---|
| 74 | #or by creating and using a specialized pdf comparison function |
---|
| 75 | if file1.endswith( '.pdf' ) or file2.endswith( '.pdf' ): |
---|
| 76 | # PDF files contain creation dates, modification dates, ids and descriptions that change with each |
---|
| 77 | # new file, so we need to handle these differences. As long as the rest of the PDF file does |
---|
| 78 | # not differ we're ok. |
---|
| 79 | valid_diff_strs = [ 'description', 'createdate', 'creationdate', 'moddate', 'id', 'producer', 'creator' ] |
---|
| 80 | valid_diff = False |
---|
| 81 | for line in diff_slice: |
---|
| 82 | # Make sure to lower case strings before checking. |
---|
| 83 | line = line.lower() |
---|
| 84 | # Diff lines will always start with a + or - character, but handle special cases: '--- local_file \n', '+++ history_data \n' |
---|
| 85 | if ( line.startswith( '+' ) or line.startswith( '-' ) ) and line.find( 'local_file' ) < 0 and line.find( 'history_data' ) < 0: |
---|
| 86 | for vdf in valid_diff_strs: |
---|
| 87 | if line.find( vdf ) < 0: |
---|
| 88 | valid_diff = False |
---|
| 89 | else: |
---|
| 90 | valid_diff = True |
---|
| 91 | # Stop checking as soon as we know we have a valid difference |
---|
| 92 | break |
---|
| 93 | if not valid_diff: |
---|
| 94 | # Print out diff_slice so we can see what failed |
---|
| 95 | print "###### diff_slice ######" |
---|
| 96 | raise AssertionError( "".join( diff_slice ) ) |
---|
| 97 | else: |
---|
| 98 | for line in diff_slice: |
---|
| 99 | for char in line: |
---|
| 100 | if ord( char ) > 128: |
---|
| 101 | raise AssertionError( "Binary data detected, not displaying diff" ) |
---|
| 102 | raise AssertionError( "".join( diff_slice ) ) |
---|
| 103 | |
---|
| 104 | def files_re_match( self, file1, file2, attributes=None ): |
---|
| 105 | """Checks the contents of 2 files for differences using re.match""" |
---|
| 106 | local_file = open( file1, 'U' ).readlines() #regex file |
---|
| 107 | history_data = open( file2, 'U' ).readlines() |
---|
| 108 | assert len( local_file ) == len( history_data ), 'Data File and Regular Expression File contain a different number of lines (%s != %s)' % ( len( local_file ), len( history_data ) ) |
---|
| 109 | if attributes is None: |
---|
| 110 | attributes = {} |
---|
| 111 | if attributes.get( 'sort', False ): |
---|
| 112 | history_data.sort() |
---|
| 113 | lines_diff = int(attributes.get( 'lines_diff', 0 )) |
---|
| 114 | line_diff_count = 0 |
---|
| 115 | diffs = [] |
---|
| 116 | for i in range( len( history_data ) ): |
---|
| 117 | if not re.match( local_file[i].rstrip( '\r\n' ), history_data[i].rstrip( '\r\n' ) ): |
---|
| 118 | line_diff_count += 1 |
---|
| 119 | diffs.append( 'Regular Expression: %s\nData file : %s' % ( local_file[i].rstrip( '\r\n' ), history_data[i].rstrip( '\r\n' ) ) ) |
---|
| 120 | if line_diff_count > lines_diff: |
---|
| 121 | raise AssertionError, "Regular expression did not match data file (allowed variants=%i):\n%s" % ( lines_diff, "".join( diffs ) ) |
---|
| 122 | |
---|
| 123 | def files_re_match_multiline( self, file1, file2, attributes=None ): |
---|
| 124 | """Checks the contents of 2 files for differences using re.match in multiline mode""" |
---|
| 125 | local_file = open( file1, 'U' ).read() #regex file |
---|
| 126 | if attributes is None: |
---|
| 127 | attributes = {} |
---|
| 128 | if attributes.get( 'sort', False ): |
---|
| 129 | history_data = open( file2, 'U' ).readlines() |
---|
| 130 | history_data.sort() |
---|
| 131 | history_data = ''.join( history_data ) |
---|
| 132 | else: |
---|
| 133 | history_data = open( file2, 'U' ).read() |
---|
| 134 | #lines_diff not applicable to multiline matching |
---|
| 135 | assert re.match( local_file, history_data, re.MULTILINE ), "Multiline Regular expression did not match data file" |
---|
| 136 | |
---|
| 137 | def get_filename( self, filename ): |
---|
| 138 | full = os.path.join( self.file_dir, filename) |
---|
| 139 | return os.path.abspath(full) |
---|
| 140 | |
---|
| 141 | def save_log( *path ): |
---|
| 142 | """Saves the log to a file""" |
---|
| 143 | filename = os.path.join( *path ) |
---|
| 144 | file(filename, 'wt').write(buffer.getvalue()) |
---|
| 145 | |
---|
| 146 | def upload_file( self, filename, ftype='auto', dbkey='unspecified (?)', space_to_tab = False, metadata = None, composite_data = None ): |
---|
| 147 | """Uploads a file""" |
---|
| 148 | self.visit_url( "%s/tool_runner?tool_id=upload1" % self.url ) |
---|
| 149 | try: |
---|
| 150 | self.refresh_form( "file_type", ftype ) #Refresh, to support composite files |
---|
| 151 | tc.fv("1","dbkey", dbkey) |
---|
| 152 | if metadata: |
---|
| 153 | for elem in metadata: |
---|
| 154 | tc.fv( "1", "files_metadata|%s" % elem.get( 'name' ), elem.get( 'value' ) ) |
---|
| 155 | if composite_data: |
---|
| 156 | for i, composite_file in enumerate( composite_data ): |
---|
| 157 | filename = self.get_filename( composite_file.get( 'value' ) ) |
---|
| 158 | tc.formfile( "1", "files_%i|file_data" % i, filename ) |
---|
| 159 | tc.fv( "1", "files_%i|space_to_tab" % i, composite_file.get( 'space_to_tab', False ) ) |
---|
| 160 | else: |
---|
| 161 | filename = self.get_filename( filename ) |
---|
| 162 | tc.formfile( "1", "file_data", filename ) |
---|
| 163 | tc.fv( "1", "space_to_tab", space_to_tab ) |
---|
| 164 | tc.submit("runtool_btn") |
---|
| 165 | self.home() |
---|
| 166 | except AssertionError, err: |
---|
| 167 | errmsg = "Uploading file resulted in the following exception. Make sure the file (%s) exists. " % filename |
---|
| 168 | errmsg += str( err ) |
---|
| 169 | raise AssertionError( errmsg ) |
---|
| 170 | # Make sure every history item has a valid hid |
---|
| 171 | hids = self.get_hids_in_history() |
---|
| 172 | for hid in hids: |
---|
| 173 | try: |
---|
| 174 | valid_hid = int( hid ) |
---|
| 175 | except: |
---|
| 176 | raise AssertionError, "Invalid hid (%s) created when uploading file %s" % ( hid, filename ) |
---|
| 177 | # Wait for upload processing to finish (TODO: this should be done in each test case instead) |
---|
| 178 | self.wait() |
---|
| 179 | def upload_url_paste( self, url_paste, ftype='auto', dbkey='unspecified (?)' ): |
---|
| 180 | """Pasted data in the upload utility""" |
---|
| 181 | self.visit_page( "tool_runner/index?tool_id=upload1" ) |
---|
| 182 | try: |
---|
| 183 | tc.fv( "1", "file_type", ftype ) |
---|
| 184 | tc.fv( "1", "dbkey", dbkey ) |
---|
| 185 | tc.fv( "1", "url_paste", url_paste ) |
---|
| 186 | tc.submit( "runtool_btn" ) |
---|
| 187 | self.home() |
---|
| 188 | except Exception, e: |
---|
| 189 | errmsg = "Problem executing upload utility using url_paste: %s" % str( e ) |
---|
| 190 | raise AssertionError( e ) |
---|
| 191 | # Make sure every history item has a valid hid |
---|
| 192 | hids = self.get_hids_in_history() |
---|
| 193 | for hid in hids: |
---|
| 194 | try: |
---|
| 195 | valid_hid = int( hid ) |
---|
| 196 | except: |
---|
| 197 | raise AssertionError, "Invalid hid (%s) created when pasting %s" % ( hid, url_paste ) |
---|
| 198 | # Wait for upload processing to finish (TODO: this should be done in each test case instead) |
---|
| 199 | self.wait() |
---|
| 200 | |
---|
| 201 | # Functions associated with histories |
---|
| 202 | def check_history_for_errors( self ): |
---|
| 203 | """Raises an exception if there are errors in a history""" |
---|
| 204 | self.home() |
---|
| 205 | self.visit_page( "history" ) |
---|
| 206 | page = self.last_page() |
---|
| 207 | if page.find( 'error' ) > -1: |
---|
| 208 | raise AssertionError('Errors in the history for user %s' % self.user ) |
---|
| 209 | def check_history_for_string( self, patt, show_deleted=False ): |
---|
| 210 | """Looks for 'string' in history page""" |
---|
| 211 | self.home() |
---|
| 212 | if show_deleted: |
---|
| 213 | self.visit_page( "history?show_deleted=True" ) |
---|
| 214 | else: |
---|
| 215 | self.visit_page( "history" ) |
---|
| 216 | for subpatt in patt.split(): |
---|
| 217 | tc.find(subpatt) |
---|
| 218 | self.home() |
---|
| 219 | def clear_history( self ): |
---|
| 220 | """Empties a history of all datasets""" |
---|
| 221 | self.visit_page( "clear_history" ) |
---|
| 222 | self.check_history_for_string( 'Your history is empty' ) |
---|
| 223 | self.home() |
---|
| 224 | def delete_history( self, id ): |
---|
| 225 | """Deletes one or more histories""" |
---|
| 226 | history_list = self.get_histories_as_data_list() |
---|
| 227 | self.assertTrue( history_list ) |
---|
| 228 | num_deleted = len( id.split( ',' ) ) |
---|
| 229 | self.home() |
---|
| 230 | self.visit_page( "history/list?operation=delete&id=%s" % ( id ) ) |
---|
| 231 | check_str = 'Deleted %d %s' % ( num_deleted, iff( num_deleted != 1, "histories", "history" ) ) |
---|
| 232 | self.check_page_for_string( check_str ) |
---|
| 233 | self.home() |
---|
| 234 | def delete_current_history( self, strings_displayed=[] ): |
---|
| 235 | """Deletes the current history""" |
---|
| 236 | self.home() |
---|
| 237 | self.visit_page( "history/delete_current" ) |
---|
| 238 | for check_str in strings_displayed: |
---|
| 239 | self.check_page_for_string( check_str ) |
---|
| 240 | self.home() |
---|
| 241 | def get_histories_as_data_list( self ): |
---|
| 242 | """Returns the data elements of all histories""" |
---|
| 243 | tree = self.histories_as_xml_tree() |
---|
| 244 | data_list = [ elem for elem in tree.findall("data") ] |
---|
| 245 | return data_list |
---|
| 246 | def get_history_as_data_list( self, show_deleted=False ): |
---|
| 247 | """Returns the data elements of a history""" |
---|
| 248 | tree = self.history_as_xml_tree( show_deleted=show_deleted ) |
---|
| 249 | data_list = [ elem for elem in tree.findall("data") ] |
---|
| 250 | return data_list |
---|
| 251 | def history_as_xml_tree( self, show_deleted=False ): |
---|
| 252 | """Returns a parsed xml object of a history""" |
---|
| 253 | self.home() |
---|
| 254 | self.visit_page( 'history?as_xml=True&show_deleted=%s' % show_deleted ) |
---|
| 255 | xml = self.last_page() |
---|
| 256 | tree = ElementTree.fromstring(xml) |
---|
| 257 | return tree |
---|
| 258 | def histories_as_xml_tree( self ): |
---|
| 259 | """Returns a parsed xml object of all histories""" |
---|
| 260 | self.home() |
---|
| 261 | self.visit_page( 'history/list_as_xml' ) |
---|
| 262 | xml = self.last_page() |
---|
| 263 | tree = ElementTree.fromstring(xml) |
---|
| 264 | return tree |
---|
| 265 | def history_options( self, user=False, active_datasets=False, activatable_datasets=False, histories_shared_by_others=False ): |
---|
| 266 | """Mimics user clicking on history options link""" |
---|
| 267 | self.home() |
---|
| 268 | self.visit_page( "root/history_options" ) |
---|
| 269 | if user: |
---|
| 270 | self.check_page_for_string( 'Previously</a> stored histories' ) |
---|
| 271 | if active_datasets: |
---|
| 272 | self.check_page_for_string( 'Create</a> a new empty history' ) |
---|
| 273 | self.check_page_for_string( 'Construct workflow</a> from current history' ) |
---|
| 274 | self.check_page_for_string( 'Clone</a> current history' ) |
---|
| 275 | self.check_page_for_string( 'Share</a> current history' ) |
---|
| 276 | self.check_page_for_string( 'Change default permissions</a> for current history' ) |
---|
| 277 | if histories_shared_by_others: |
---|
| 278 | self.check_page_for_string( 'Histories</a> shared with you by others' ) |
---|
| 279 | if activatable_datasets: |
---|
| 280 | self.check_page_for_string( 'Show deleted</a> datasets in current history' ) |
---|
| 281 | self.check_page_for_string( 'Rename</a> current history' ) |
---|
| 282 | self.check_page_for_string( 'Delete</a> current history' ) |
---|
| 283 | self.home() |
---|
| 284 | def new_history( self, name=None ): |
---|
| 285 | """Creates a new, empty history""" |
---|
| 286 | self.home() |
---|
| 287 | if name: |
---|
| 288 | self.visit_url( "%s/history_new?name=%s" % ( self.url, name ) ) |
---|
| 289 | else: |
---|
| 290 | self.visit_url( "%s/history_new" % self.url ) |
---|
| 291 | self.check_history_for_string('Your history is empty') |
---|
| 292 | self.home() |
---|
| 293 | def rename_history( self, id, old_name, new_name ): |
---|
| 294 | """Rename an existing history""" |
---|
| 295 | self.home() |
---|
| 296 | self.visit_page( "history/rename?id=%s&name=%s" %( id, new_name ) ) |
---|
| 297 | check_str = 'History: %s renamed to: %s' % ( old_name, urllib.unquote( new_name ) ) |
---|
| 298 | self.check_page_for_string( check_str ) |
---|
| 299 | self.home() |
---|
| 300 | def set_history( self ): |
---|
| 301 | """Sets the history (stores the cookies for this run)""" |
---|
| 302 | if self.history_id: |
---|
| 303 | self.home() |
---|
| 304 | self.visit_page( "history?id=%s" % self.history_id ) |
---|
| 305 | else: |
---|
| 306 | self.new_history() |
---|
| 307 | self.home() |
---|
| 308 | def share_current_history( self, email, strings_displayed=[], strings_displayed_after_submit=[], |
---|
| 309 | action='', action_strings_displayed=[], action_strings_displayed_after_submit=[] ): |
---|
| 310 | """Share the current history with different users""" |
---|
| 311 | self.visit_url( "%s/history/share" % self.url ) |
---|
| 312 | for check_str in strings_displayed: |
---|
| 313 | self.check_page_for_string( check_str ) |
---|
| 314 | tc.fv( 'share', 'email', email ) |
---|
| 315 | tc.submit( 'share_button' ) |
---|
| 316 | for check_str in strings_displayed_after_submit: |
---|
| 317 | self.check_page_for_string( check_str ) |
---|
| 318 | if action: |
---|
| 319 | # If we have an action, then we are sharing datasets with users that do not have access permissions on them |
---|
| 320 | for check_str in action_strings_displayed: |
---|
| 321 | self.check_page_for_string( check_str ) |
---|
| 322 | tc.fv( 'share_restricted', 'action', action ) |
---|
| 323 | tc.submit( "share_restricted_button" ) |
---|
| 324 | for check_str in action_strings_displayed_after_submit: |
---|
| 325 | self.check_page_for_string( check_str ) |
---|
| 326 | self.home() |
---|
| 327 | def share_histories_with_users( self, ids, emails, strings_displayed=[], strings_displayed_after_submit=[], |
---|
| 328 | action=None, action_strings_displayed=[] ): |
---|
| 329 | """Share one or more histories with one or more different users""" |
---|
| 330 | self.visit_url( "%s/history/list?id=%s&operation=Share" % ( self.url, ids ) ) |
---|
| 331 | for check_str in strings_displayed: |
---|
| 332 | self.check_page_for_string( check_str ) |
---|
| 333 | tc.fv( 'share', 'email', emails ) |
---|
| 334 | tc.submit( 'share_button' ) |
---|
| 335 | for check_str in strings_displayed_after_submit: |
---|
| 336 | self.check_page_for_string( check_str ) |
---|
| 337 | if action: |
---|
| 338 | # If we have an action, then we are sharing datasets with users that do not have access permissions on them |
---|
| 339 | tc.fv( 'share_restricted', 'action', action ) |
---|
| 340 | tc.submit( "share_restricted_button" ) |
---|
| 341 | for check_str in action_strings_displayed: |
---|
| 342 | self.check_page_for_string( check_str ) |
---|
| 343 | self.home() |
---|
| 344 | def unshare_history( self, history_id, user_id, strings_displayed=[] ): |
---|
| 345 | """Unshare a history that has been shared with another user""" |
---|
| 346 | self.visit_url( "%s/history/list?id=%s&operation=share+or+publish" % ( self.url, history_id ) ) |
---|
| 347 | for check_str in strings_displayed: |
---|
| 348 | self.check_page_for_string( check_str ) |
---|
| 349 | self.visit_url( "%s/history/sharing?unshare_user=%s&id=%s" % ( self.url, user_id, history_id ) ) |
---|
| 350 | self.home() |
---|
| 351 | def switch_history( self, id='', name='' ): |
---|
| 352 | """Switches to a history in the current list of histories""" |
---|
| 353 | self.visit_url( "%s/history/list?operation=switch&id=%s" % ( self.url, id ) ) |
---|
| 354 | if name: |
---|
| 355 | self.check_history_for_string( name ) |
---|
| 356 | self.home() |
---|
| 357 | def view_stored_active_histories( self, strings_displayed=[] ): |
---|
| 358 | self.home() |
---|
| 359 | self.visit_page( "history/list" ) |
---|
| 360 | self.check_page_for_string( 'Saved Histories' ) |
---|
| 361 | self.check_page_for_string( '<input type="checkbox" name="id" value=' ) |
---|
| 362 | self.check_page_for_string( 'operation=Rename' ) |
---|
| 363 | self.check_page_for_string( 'operation=Switch' ) |
---|
| 364 | self.check_page_for_string( 'operation=Delete' ) |
---|
| 365 | for check_str in strings_displayed: |
---|
| 366 | self.check_page_for_string( check_str ) |
---|
| 367 | self.home() |
---|
| 368 | def view_stored_deleted_histories( self, strings_displayed=[] ): |
---|
| 369 | self.home() |
---|
| 370 | self.visit_page( "history/list?f-deleted=True" ) |
---|
| 371 | self.check_page_for_string( 'Saved Histories' ) |
---|
| 372 | self.check_page_for_string( '<input type="checkbox" name="id" value=' ) |
---|
| 373 | self.check_page_for_string( 'operation=Undelete' ) |
---|
| 374 | for check_str in strings_displayed: |
---|
| 375 | self.check_page_for_string( check_str ) |
---|
| 376 | self.home() |
---|
| 377 | def view_shared_histories( self, strings_displayed=[] ): |
---|
| 378 | self.home() |
---|
| 379 | self.visit_page( "history/list_shared" ) |
---|
| 380 | for check_str in strings_displayed: |
---|
| 381 | self.check_page_for_string( check_str ) |
---|
| 382 | self.home() |
---|
| 383 | def clone_history( self, history_id, clone_choice, strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 384 | self.home() |
---|
| 385 | self.visit_page( "history/clone?id=%s" % history_id ) |
---|
| 386 | for check_str in strings_displayed: |
---|
| 387 | self.check_page_for_string( check_str ) |
---|
| 388 | tc.fv( '1', 'clone_choice', clone_choice ) |
---|
| 389 | tc.submit( 'clone_choice_button' ) |
---|
| 390 | for check_str in strings_displayed_after_submit: |
---|
| 391 | self.check_page_for_string( check_str ) |
---|
| 392 | self.home() |
---|
| 393 | def make_accessible_via_link( self, history_id, strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 394 | self.home() |
---|
| 395 | self.visit_page( "history/list?operation=share+or+publish&id=%s" % history_id ) |
---|
| 396 | for check_str in strings_displayed: |
---|
| 397 | self.check_page_for_string( check_str ) |
---|
| 398 | # twill barfs on this form, possibly because it contains no fields, but not sure. |
---|
| 399 | # In any case, we have to mimic the form submission |
---|
| 400 | self.home() |
---|
| 401 | self.visit_page( 'history/sharing?id=%s&make_accessible_via_link=True' % history_id ) |
---|
| 402 | for check_str in strings_displayed_after_submit: |
---|
| 403 | self.check_page_for_string( check_str ) |
---|
| 404 | self.home() |
---|
| 405 | def disable_access_via_link( self, history_id, strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 406 | self.home() |
---|
| 407 | self.visit_page( "history/list?operation=share+or+publish&id=%s" % history_id ) |
---|
| 408 | for check_str in strings_displayed: |
---|
| 409 | self.check_page_for_string( check_str ) |
---|
| 410 | # twill barfs on this form, possibly because it contains no fields, but not sure. |
---|
| 411 | # In any case, we have to mimic the form submission |
---|
| 412 | self.home() |
---|
| 413 | self.visit_page( 'history/sharing?id=%s&disable_link_access=True' % history_id ) |
---|
| 414 | for check_str in strings_displayed_after_submit: |
---|
| 415 | self.check_page_for_string( check_str ) |
---|
| 416 | self.home() |
---|
| 417 | def import_history_via_url( self, history_id, email, strings_displayed_after_submit=[] ): |
---|
| 418 | self.home() |
---|
| 419 | self.visit_page( "history/imp?&id=%s" % history_id ) |
---|
| 420 | for check_str in strings_displayed_after_submit: |
---|
| 421 | self.check_page_for_string( check_str ) |
---|
| 422 | self.home() |
---|
| 423 | |
---|
| 424 | # Functions associated with datasets (history items) and meta data |
---|
| 425 | def get_job_stderr( self, id ): |
---|
| 426 | self.visit_page( "dataset/stderr?id=%s" % id ) |
---|
| 427 | return self.last_page() |
---|
| 428 | |
---|
| 429 | def _assert_dataset_state( self, elem, state ): |
---|
| 430 | if elem.get( 'state' ) != state: |
---|
| 431 | errmsg = "Expecting dataset state '%s', but state is '%s'. Dataset blurb: %s\n\n" % ( state, elem.get('state'), elem.text.strip() ) |
---|
| 432 | errmsg += "---------------------- >> begin tool stderr << -----------------------\n" |
---|
| 433 | errmsg += self.get_job_stderr( elem.get( 'id' ) ) + "\n" |
---|
| 434 | errmsg += "----------------------- >> end tool stderr << ------------------------\n" |
---|
| 435 | raise AssertionError( errmsg ) |
---|
| 436 | |
---|
| 437 | def check_metadata_for_string( self, patt, hid=None ): |
---|
| 438 | """Looks for 'patt' in the edit page when editing a dataset""" |
---|
| 439 | data_list = self.get_history_as_data_list() |
---|
| 440 | self.assertTrue( data_list ) |
---|
| 441 | if hid is None: # take last hid |
---|
| 442 | elem = data_list[-1] |
---|
| 443 | hid = int( elem.get('hid') ) |
---|
| 444 | self.assertTrue( hid ) |
---|
| 445 | self.visit_page( "edit?hid=%s" % hid ) |
---|
| 446 | for subpatt in patt.split(): |
---|
| 447 | tc.find(subpatt) |
---|
| 448 | def delete_history_item( self, hda_id, strings_displayed=[] ): |
---|
| 449 | """Deletes an item from a history""" |
---|
| 450 | try: |
---|
| 451 | hda_id = int( hda_id ) |
---|
| 452 | except: |
---|
| 453 | raise AssertionError, "Invalid hda_id '%s' - must be int" % hda_id |
---|
| 454 | self.visit_url( "%s/root/delete?show_deleted_on_refresh=False&id=%s" % ( self.url, hda_id ) ) |
---|
| 455 | for check_str in strings_displayed: |
---|
| 456 | self.check_page_for_string( check_str ) |
---|
| 457 | def undelete_history_item( self, hda_id, strings_displayed=[] ): |
---|
| 458 | """Un-deletes a deleted item in a history""" |
---|
| 459 | try: |
---|
| 460 | hda_id = int( hda_id ) |
---|
| 461 | except: |
---|
| 462 | raise AssertionError, "Invalid hda_id '%s' - must be int" % hda_id |
---|
| 463 | self.visit_url( "%s/dataset/undelete?id=%s" % ( self.url, hda_id ) ) |
---|
| 464 | for check_str in strings_displayed: |
---|
| 465 | self.check_page_for_string( check_str ) |
---|
| 466 | def display_history_item( self, hda_id, strings_displayed=[] ): |
---|
| 467 | """Displays a history item - simulates eye icon click""" |
---|
| 468 | self.visit_url( '%s/datasets/%s/display/' % ( self.url, self.security.encode_id( hda_id ) ) ) |
---|
| 469 | for check_str in strings_displayed: |
---|
| 470 | self.check_page_for_string( check_str ) |
---|
| 471 | self.home() |
---|
| 472 | def view_history( self, history_id, strings_displayed=[] ): |
---|
| 473 | """Displays a history for viewing""" |
---|
| 474 | self.visit_url( '%s/history/view?id=%s' % ( self.url, self.security.encode_id( history_id ) ) ) |
---|
| 475 | for check_str in strings_displayed: |
---|
| 476 | self.check_page_for_string( check_str ) |
---|
| 477 | self.home() |
---|
| 478 | def edit_hda_attribute_info( self, hda_id, new_name='', new_info='', new_dbkey='', new_startcol='', |
---|
| 479 | strings_displayed=[], strings_not_displayed=[] ): |
---|
| 480 | """Edit history_dataset_association attribute information""" |
---|
| 481 | self.home() |
---|
| 482 | self.visit_url( "%s/root/edit?id=%s" % ( self.url, hda_id ) ) |
---|
| 483 | submit_required = False |
---|
| 484 | self.check_page_for_string( 'Edit Attributes' ) |
---|
| 485 | if new_name: |
---|
| 486 | tc.fv( 'edit_attributes', 'name', new_name ) |
---|
| 487 | submit_required = True |
---|
| 488 | if new_info: |
---|
| 489 | tc.fv( 'edit_attributes', 'info', new_info ) |
---|
| 490 | submit_required = True |
---|
| 491 | if new_dbkey: |
---|
| 492 | tc.fv( 'edit_attributes', 'dbkey', new_dbkey ) |
---|
| 493 | submit_required = True |
---|
| 494 | if new_startcol: |
---|
| 495 | tc.fv( 'edit_attributes', 'startCol', new_startcol ) |
---|
| 496 | submit_required = True |
---|
| 497 | if submit_required: |
---|
| 498 | tc.submit( 'save' ) |
---|
| 499 | self.check_page_for_string( 'Attributes updated' ) |
---|
| 500 | for check_str in strings_displayed: |
---|
| 501 | self.check_page_for_string( check_str ) |
---|
| 502 | for check_str in strings_not_displayed: |
---|
| 503 | try: |
---|
| 504 | self.check_page_for_string( check_str ) |
---|
| 505 | raise AssertionError, "String (%s) incorrectly displayed on Edit Attributes page." % check_str |
---|
| 506 | except: |
---|
| 507 | pass |
---|
| 508 | self.home() |
---|
| 509 | def check_hda_attribute_info( self, hda_id, strings_displayed=[] ): |
---|
| 510 | """Edit history_dataset_association attribute information""" |
---|
| 511 | for check_str in strings_displayed: |
---|
| 512 | self.check_page_for_string( check_str ) |
---|
| 513 | def auto_detect_metadata( self, hda_id ): |
---|
| 514 | """Auto-detect history_dataset_association metadata""" |
---|
| 515 | self.home() |
---|
| 516 | self.visit_url( "%s/root/edit?id=%s" % ( self.url, hda_id ) ) |
---|
| 517 | self.check_page_for_string( 'This will inspect the dataset and attempt' ) |
---|
| 518 | tc.fv( 'auto_detect', 'id', hda_id ) |
---|
| 519 | tc.submit( 'detect' ) |
---|
| 520 | try: |
---|
| 521 | self.check_page_for_string( 'Attributes have been queued to be updated' ) |
---|
| 522 | self.wait() |
---|
| 523 | except AssertionError: |
---|
| 524 | self.check_page_for_string( 'Attributes updated' ) |
---|
| 525 | #self.check_page_for_string( 'Attributes updated' ) |
---|
| 526 | self.home() |
---|
| 527 | def convert_format( self, hda_id, target_type ): |
---|
| 528 | """Convert format of history_dataset_association""" |
---|
| 529 | self.home() |
---|
| 530 | self.visit_url( "%s/root/edit?id=%s" % ( self.url, hda_id ) ) |
---|
| 531 | self.check_page_for_string( 'This will inspect the dataset and attempt' ) |
---|
| 532 | tc.fv( 'convert_data', 'target_type', target_type ) |
---|
| 533 | tc.submit( 'convert_data' ) |
---|
| 534 | self.check_page_for_string( 'The file conversion of Convert BED to GFF on data' ) |
---|
| 535 | self.wait() #wait for the format convert tool to finish before returning |
---|
| 536 | self.home() |
---|
| 537 | def change_datatype( self, hda_id, datatype ): |
---|
| 538 | """Change format of history_dataset_association""" |
---|
| 539 | self.home() |
---|
| 540 | self.visit_url( "%s/root/edit?id=%s" % ( self.url, hda_id ) ) |
---|
| 541 | self.check_page_for_string( 'This will change the datatype of the existing dataset but' ) |
---|
| 542 | tc.fv( 'change_datatype', 'datatype', datatype ) |
---|
| 543 | tc.submit( 'change' ) |
---|
| 544 | self.check_page_for_string( 'Changed the type of dataset' ) |
---|
| 545 | self.home() |
---|
| 546 | def copy_history_item( self, source_dataset_ids='', target_history_ids=[], all_target_history_ids=[], |
---|
| 547 | deleted_history_ids=[] ): |
---|
| 548 | """Copy 1 or more history_dataset_associations to 1 or more histories""" |
---|
| 549 | self.home() |
---|
| 550 | self.visit_url( "%s/dataset/copy_datasets?source_dataset_ids=%s" % ( self.url, source_dataset_ids ) ) |
---|
| 551 | self.check_page_for_string( 'Source History Items' ) |
---|
| 552 | # Make sure all of users active histories are displayed |
---|
| 553 | for id in all_target_history_ids: |
---|
| 554 | self.check_page_for_string( id ) |
---|
| 555 | # Make sure only active histories are displayed |
---|
| 556 | for id in deleted_history_ids: |
---|
| 557 | try: |
---|
| 558 | self.check_page_for_string( id ) |
---|
| 559 | raise AssertionError, "deleted history id %d displayed in list of target histories" % id |
---|
| 560 | except: |
---|
| 561 | pass |
---|
| 562 | # Check each history to which we want to copy the item |
---|
| 563 | for id in target_history_ids: |
---|
| 564 | tc.fv( '1', 'target_history_ids', id ) |
---|
| 565 | tc.submit( 'do_copy' ) |
---|
| 566 | no_source_ids = len( source_dataset_ids.split( ',' ) ) |
---|
| 567 | check_str = '%d datasets copied to %d histories.' % ( no_source_ids, len( target_history_ids ) ) |
---|
| 568 | self.check_page_for_string( check_str ) |
---|
| 569 | self.home() |
---|
| 570 | def get_hids_in_history( self ): |
---|
| 571 | """Returns the list of hid values for items in a history""" |
---|
| 572 | data_list = self.get_history_as_data_list() |
---|
| 573 | hids = [] |
---|
| 574 | for elem in data_list: |
---|
| 575 | hid = elem.get('hid') |
---|
| 576 | hids.append(hid) |
---|
| 577 | return hids |
---|
| 578 | def get_hids_in_histories( self ): |
---|
| 579 | """Returns the list of hids values for items in all histories""" |
---|
| 580 | data_list = self.get_histories_as_data_list() |
---|
| 581 | hids = [] |
---|
| 582 | for elem in data_list: |
---|
| 583 | hid = elem.get('hid') |
---|
| 584 | hids.append(hid) |
---|
| 585 | return hids |
---|
| 586 | |
---|
| 587 | def makeTfname(self, fname=None): |
---|
| 588 | """ |
---|
| 589 | safe temp name - preserve the file extension for tools that interpret it |
---|
| 590 | """ |
---|
| 591 | suffix = os.path.split(fname)[-1] # ignore full path |
---|
| 592 | fd,temp_prefix = tempfile.mkstemp(prefix='tmp',suffix=suffix) |
---|
| 593 | return temp_prefix |
---|
| 594 | |
---|
| 595 | |
---|
| 596 | def verify_dataset_correctness( self, filename, hid=None, wait=True, maxseconds=120, attributes=None ): |
---|
| 597 | """Verifies that the attributes and contents of a history item meet expectations""" |
---|
| 598 | if wait: |
---|
| 599 | self.wait( maxseconds=maxseconds ) #wait for job to finish |
---|
| 600 | data_list = self.get_history_as_data_list() |
---|
| 601 | self.assertTrue( data_list ) |
---|
| 602 | if hid is None: # take last hid |
---|
| 603 | elem = data_list[-1] |
---|
| 604 | hid = str( elem.get('hid') ) |
---|
| 605 | else: |
---|
| 606 | hid = str( hid ) |
---|
| 607 | elems = [ elem for elem in data_list if elem.get('hid') == hid ] |
---|
| 608 | self.assertTrue( len(elems) == 1 ) |
---|
| 609 | elem = elems[0] |
---|
| 610 | self.assertTrue( hid ) |
---|
| 611 | self._assert_dataset_state( elem, 'ok' ) |
---|
| 612 | if self.is_zipped( filename ): |
---|
| 613 | errmsg = 'History item %s is a zip archive which includes invalid files:\n' % hid |
---|
| 614 | zip_file = zipfile.ZipFile( filename, "r" ) |
---|
| 615 | name = zip_file.namelist()[0] |
---|
| 616 | test_ext = name.split( "." )[1].strip().lower() |
---|
| 617 | if not ( test_ext == 'scf' or test_ext == 'ab1' or test_ext == 'txt' ): |
---|
| 618 | raise AssertionError( errmsg ) |
---|
| 619 | for name in zip_file.namelist(): |
---|
| 620 | ext = name.split( "." )[1].strip().lower() |
---|
| 621 | if ext != test_ext: |
---|
| 622 | raise AssertionError( errmsg ) |
---|
| 623 | else: |
---|
| 624 | local_name = self.get_filename( filename ) |
---|
| 625 | temp_name = self.makeTfname(fname = filename) |
---|
| 626 | self.home() |
---|
| 627 | self.visit_page( "display?hid=" + hid ) |
---|
| 628 | data = self.last_page() |
---|
| 629 | file( temp_name, 'wb' ).write(data) |
---|
| 630 | try: |
---|
| 631 | # have to nest try-except in try-finally to handle 2.4 |
---|
| 632 | try: |
---|
| 633 | if attributes is None: |
---|
| 634 | attributes = {} |
---|
| 635 | compare = attributes.get( 'compare', 'diff' ) |
---|
| 636 | extra_files = attributes.get( 'extra_files', None ) |
---|
| 637 | if compare == 'diff': |
---|
| 638 | self.files_diff( local_name, temp_name, attributes=attributes ) |
---|
| 639 | elif compare == 're_match': |
---|
| 640 | self.files_re_match( local_name, temp_name, attributes=attributes ) |
---|
| 641 | elif compare == 're_match_multiline': |
---|
| 642 | self.files_re_match_multiline( local_name, temp_name, attributes=attributes ) |
---|
| 643 | elif compare == 'sim_size': |
---|
| 644 | delta = attributes.get('delta','100') |
---|
| 645 | s1 = len(data) |
---|
| 646 | s2 = os.path.getsize(local_name) |
---|
| 647 | if abs(s1-s2) > int(delta): |
---|
| 648 | raise Exception, 'Files %s=%db but %s=%db - compare (delta=%s) failed' % (temp_name,s1,local_name,s2,delta) |
---|
| 649 | else: |
---|
| 650 | raise Exception, 'Unimplemented Compare type: %s' % compare |
---|
| 651 | if extra_files: |
---|
| 652 | self.verify_extra_files_content( extra_files, elem.get( 'id' ) ) |
---|
| 653 | except AssertionError, err: |
---|
| 654 | errmsg = 'History item %s different than expected, difference (using %s):\n' % ( hid, compare ) |
---|
| 655 | errmsg += str( err ) |
---|
| 656 | raise AssertionError( errmsg ) |
---|
| 657 | finally: |
---|
| 658 | os.remove( temp_name ) |
---|
| 659 | |
---|
| 660 | def verify_extra_files_content( self, extra_files, hda_id ): |
---|
| 661 | files_list = [] |
---|
| 662 | for extra_type, extra_value, extra_name, extra_attributes in extra_files: |
---|
| 663 | if extra_type == 'file': |
---|
| 664 | files_list.append( ( extra_name, extra_value, extra_attributes ) ) |
---|
| 665 | elif extra_type == 'directory': |
---|
| 666 | for filename in os.listdir( self.get_filename( extra_value ) ): |
---|
| 667 | files_list.append( ( filename, os.path.join( extra_value, filename ), extra_attributes ) ) |
---|
| 668 | else: |
---|
| 669 | raise ValueError, 'unknown extra_files type: %s' % extra_type |
---|
| 670 | for filename, filepath, attributes in files_list: |
---|
| 671 | self.verify_composite_datatype_file_content( filepath, hda_id, base_name = filename, attributes = attributes ) |
---|
| 672 | |
---|
| 673 | def verify_composite_datatype_file_content( self, file_name, hda_id, base_name = None, attributes = None ): |
---|
| 674 | local_name = self.get_filename( file_name ) |
---|
| 675 | if base_name is None: |
---|
| 676 | base_name = os.path.split(file_name)[-1] |
---|
| 677 | temp_name = self.makeTfname(fname = base_name) |
---|
| 678 | self.visit_url( "%s/datasets/%s/display/%s" % ( self.url, self.security.encode_id( hda_id ), base_name ) ) |
---|
| 679 | data = self.last_page() |
---|
| 680 | file( temp_name, 'wb' ).write( data ) |
---|
| 681 | try: |
---|
| 682 | # have to nest try-except in try-finally to handle 2.4 |
---|
| 683 | try: |
---|
| 684 | if attributes is None: |
---|
| 685 | attributes = {} |
---|
| 686 | compare = attributes.get( 'compare', 'diff' ) |
---|
| 687 | if compare == 'diff': |
---|
| 688 | self.files_diff( local_name, temp_name, attributes=attributes ) |
---|
| 689 | elif compare == 're_match': |
---|
| 690 | self.files_re_match( local_name, temp_name, attributes=attributes ) |
---|
| 691 | elif compare == 're_match_multiline': |
---|
| 692 | self.files_re_match_multiline( local_name, temp_name, attributes=attributes ) |
---|
| 693 | elif compare == 'sim_size': |
---|
| 694 | delta = attributes.get('delta','100') |
---|
| 695 | s1 = len(data) |
---|
| 696 | s2 = os.path.getsize(local_name) |
---|
| 697 | if abs(s1-s2) > int(delta): |
---|
| 698 | raise Exception, 'Files %s=%db but %s=%db - compare (delta=%s) failed' % (temp_name,s1,local_name,s2,delta) |
---|
| 699 | else: |
---|
| 700 | raise Exception, 'Unimplemented Compare type: %s' % compare |
---|
| 701 | except AssertionError, err: |
---|
| 702 | errmsg = 'Composite file (%s) of History item %s different than expected, difference (using %s):\n' % ( base_name, hda_id, compare ) |
---|
| 703 | errmsg += str( err ) |
---|
| 704 | raise AssertionError( errmsg ) |
---|
| 705 | finally: |
---|
| 706 | os.remove( temp_name ) |
---|
| 707 | |
---|
| 708 | def is_zipped( self, filename ): |
---|
| 709 | if not zipfile.is_zipfile( filename ): |
---|
| 710 | return False |
---|
| 711 | return True |
---|
| 712 | |
---|
| 713 | def is_binary( self, filename ): |
---|
| 714 | temp = open( filename, "U" ) # why is this not filename? Where did temp_name come from |
---|
| 715 | lineno = 0 |
---|
| 716 | for line in temp: |
---|
| 717 | lineno += 1 |
---|
| 718 | line = line.strip() |
---|
| 719 | if line: |
---|
| 720 | for char in line: |
---|
| 721 | if ord( char ) > 128: |
---|
| 722 | return True |
---|
| 723 | if lineno > 10: |
---|
| 724 | break |
---|
| 725 | return False |
---|
| 726 | |
---|
| 727 | def verify_genome_build( self, dbkey='hg17' ): |
---|
| 728 | """Verifies that the last used genome_build at history id 'hid' is as expected""" |
---|
| 729 | data_list = self.get_history_as_data_list() |
---|
| 730 | self.assertTrue( data_list ) |
---|
| 731 | elems = [ elem for elem in data_list ] |
---|
| 732 | elem = elems[-1] |
---|
| 733 | genome_build = elem.get('dbkey') |
---|
| 734 | self.assertTrue( genome_build == dbkey ) |
---|
| 735 | |
---|
| 736 | # Functions associated with user accounts |
---|
| 737 | def create( self, email='test@bx.psu.edu', password='testuser', username='admin-user', webapp='galaxy', referer='' ): |
---|
| 738 | # HACK: don't use panels because late_javascripts() messes up the twill browser and it |
---|
| 739 | # can't find form fields (and hence user can't be logged in). |
---|
| 740 | self.visit_url( "%s/user/create?use_panels=False" % self.url ) |
---|
| 741 | tc.fv( '1', 'email', email ) |
---|
| 742 | tc.fv( '1', 'webapp', webapp ) |
---|
| 743 | tc.fv( '1', 'referer', referer ) |
---|
| 744 | tc.fv( '1', 'password', password ) |
---|
| 745 | tc.fv( '1', 'confirm', password ) |
---|
| 746 | tc.fv( '1', 'username', username ) |
---|
| 747 | tc.submit( 'create_user_button' ) |
---|
| 748 | previously_created = False |
---|
| 749 | username_taken = False |
---|
| 750 | invalid_username = False |
---|
| 751 | try: |
---|
| 752 | self.check_page_for_string( "Created new user account" ) |
---|
| 753 | except: |
---|
| 754 | try: |
---|
| 755 | # May have created the account in a previous test run... |
---|
| 756 | self.check_page_for_string( "User with that email already exists" ) |
---|
| 757 | previously_created = True |
---|
| 758 | except: |
---|
| 759 | try: |
---|
| 760 | self.check_page_for_string( 'This user name is not available' ) |
---|
| 761 | username_taken = True |
---|
| 762 | except: |
---|
| 763 | try: |
---|
| 764 | # Note that we're only checking if the usr name is >< 4 chars here... |
---|
| 765 | self.check_page_for_string( 'User name must be at least 4 characters in length' ) |
---|
| 766 | invalid_username = True |
---|
| 767 | except: |
---|
| 768 | pass |
---|
| 769 | return previously_created, username_taken, invalid_username |
---|
| 770 | def create_user_with_info( self, email, password, username, user_info_values, user_info_select='', admin_view='False', |
---|
| 771 | strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 772 | # This method creates a new user with associated info |
---|
| 773 | self.visit_url( "%s/user/create?admin_view=%s&use_panels=False" % ( self.url, admin_view ) ) |
---|
| 774 | for check_str in strings_displayed: |
---|
| 775 | self.check_page_for_string( check_str) |
---|
| 776 | tc.fv( "1", "email", email ) |
---|
| 777 | tc.fv( "1", "password", password ) |
---|
| 778 | tc.fv( "1", "confirm", password ) |
---|
| 779 | tc.fv( "1", "username", username ) |
---|
| 780 | if user_info_select: |
---|
| 781 | # The user_info_select SelectField requires a refresh_on_change |
---|
| 782 | self.refresh_form( 'user_info_select', user_info_select ) |
---|
| 783 | for index, info_value in enumerate( user_info_values ): |
---|
| 784 | tc.fv( "1", "field_%i" % index, info_value ) |
---|
| 785 | tc.submit( "create_user_button" ) |
---|
| 786 | def edit_user_info( self, new_email='', new_username='', password='', new_password='', |
---|
| 787 | info_values=[], strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 788 | self.visit_url( "%s/user/show_info" % self.url ) |
---|
| 789 | for check_str in strings_displayed: |
---|
| 790 | self.check_page_for_string( check_str ) |
---|
| 791 | if new_email or new_username: |
---|
| 792 | if new_email: |
---|
| 793 | tc.fv( "login_info", "email", new_email ) |
---|
| 794 | if new_username: |
---|
| 795 | tc.fv( "login_info", "username", new_username ) |
---|
| 796 | tc.submit( "login_info_button" ) |
---|
| 797 | if password and new_password: |
---|
| 798 | tc.fv( "change_password", "current", password ) |
---|
| 799 | tc.fv( "change_password", "password", new_password ) |
---|
| 800 | tc.fv( "change_password", "confirm", new_password ) |
---|
| 801 | tc.submit( "change_password_button" ) |
---|
| 802 | if info_values: |
---|
| 803 | for index, info_value in enumerate( info_values ): |
---|
| 804 | tc.fv( "user_info", "field_%i" % index, info_value ) |
---|
| 805 | tc.submit( "edit_user_info_button" ) |
---|
| 806 | for check_str in strings_displayed_after_submit: |
---|
| 807 | self.check_page_for_string( check_str ) |
---|
| 808 | self.home() |
---|
| 809 | def user_set_default_permissions( self, permissions_out=[], permissions_in=[], role_id='2' ): |
---|
| 810 | # role.id = 2 is Private Role for test2@bx.psu.edu |
---|
| 811 | # NOTE: Twill has a bug that requires the ~/user/permissions page to contain at least 1 option value |
---|
| 812 | # in each select list or twill throws an exception, which is: ParseError: OPTION outside of SELECT |
---|
| 813 | # Due to this bug, we'll bypass visiting the page, and simply pass the permissions on to the |
---|
| 814 | # /user/set_default_permissions method. |
---|
| 815 | url = "user/set_default_permissions?update_roles_button=Save&id=None" |
---|
| 816 | for po in permissions_out: |
---|
| 817 | key = '%s_out' % po |
---|
| 818 | url ="%s&%s=%s" % ( url, key, str( role_id ) ) |
---|
| 819 | for pi in permissions_in: |
---|
| 820 | key = '%s_in' % pi |
---|
| 821 | url ="%s&%s=%s" % ( url, key, str( role_id ) ) |
---|
| 822 | self.home() |
---|
| 823 | self.visit_url( "%s/%s" % ( self.url, url ) ) |
---|
| 824 | self.last_page() |
---|
| 825 | self.check_page_for_string( 'Default new history permissions have been changed.' ) |
---|
| 826 | self.home() |
---|
| 827 | def history_set_default_permissions( self, permissions_out=[], permissions_in=[], role_id=3 ): # role.id = 3 is Private Role for test3@bx.psu.edu |
---|
| 828 | # NOTE: Twill has a bug that requires the ~/user/permissions page to contain at least 1 option value |
---|
| 829 | # in each select list or twill throws an exception, which is: ParseError: OPTION outside of SELECT |
---|
| 830 | # Due to this bug, we'll bypass visiting the page, and simply pass the permissions on to the |
---|
| 831 | # /user/set_default_permissions method. |
---|
| 832 | url = "root/history_set_default_permissions?update_roles_button=Save&id=None&dataset=True" |
---|
| 833 | for po in permissions_out: |
---|
| 834 | key = '%s_out' % po |
---|
| 835 | url ="%s&%s=%s" % ( url, key, str( role_id ) ) |
---|
| 836 | for pi in permissions_in: |
---|
| 837 | key = '%s_in' % pi |
---|
| 838 | url ="%s&%s=%s" % ( url, key, str( role_id ) ) |
---|
| 839 | self.home() |
---|
| 840 | self.visit_url( "%s/%s" % ( self.url, url ) ) |
---|
| 841 | self.check_page_for_string( 'Default history permissions have been changed.' ) |
---|
| 842 | self.home() |
---|
| 843 | def login( self, email='test@bx.psu.edu', password='testuser', username='admin-user', webapp='galaxy', referer='' ): |
---|
| 844 | # test@bx.psu.edu is configured as an admin user |
---|
| 845 | previously_created, username_taken, invalid_username = \ |
---|
| 846 | self.create( email=email, password=password, username=username, webapp=webapp, referer=referer ) |
---|
| 847 | if previously_created: |
---|
| 848 | # The acount has previously been created, so just login. |
---|
| 849 | # HACK: don't use panels because late_javascripts() messes up the twill browser and it |
---|
| 850 | # can't find form fields (and hence user can't be logged in). |
---|
| 851 | self.visit_url( "%s/user/login?use_panels=False" % self.url ) |
---|
| 852 | tc.fv( '1', 'email', email ) |
---|
| 853 | tc.fv( '1', 'webapp', webapp ) |
---|
| 854 | tc.fv( '1', 'referer', referer ) |
---|
| 855 | tc.fv( '1', 'password', password ) |
---|
| 856 | tc.submit( 'login_button' ) |
---|
| 857 | def logout( self ): |
---|
| 858 | self.home() |
---|
| 859 | self.visit_page( "user/logout" ) |
---|
| 860 | self.check_page_for_string( "You have been logged out" ) |
---|
| 861 | self.home() |
---|
| 862 | |
---|
| 863 | # Functions associated with browsers, cookies, HTML forms and page visits |
---|
| 864 | |
---|
| 865 | def check_page_for_string( self, patt ): |
---|
| 866 | """Looks for 'patt' in the current browser page""" |
---|
| 867 | page = self.last_page() |
---|
| 868 | for subpatt in patt.split(): |
---|
| 869 | if page.find( patt ) == -1: |
---|
| 870 | fname = self.write_temp_file( page ) |
---|
| 871 | errmsg = "no match to '%s'\npage content written to '%s'" % ( patt, fname ) |
---|
| 872 | raise AssertionError( errmsg ) |
---|
| 873 | |
---|
| 874 | def write_temp_file( self, content, suffix='.html' ): |
---|
| 875 | fd, fname = tempfile.mkstemp( suffix=suffix, prefix='twilltestcase-' ) |
---|
| 876 | f = os.fdopen( fd, "w" ) |
---|
| 877 | f.write( content ) |
---|
| 878 | f.close() |
---|
| 879 | return fname |
---|
| 880 | |
---|
| 881 | def clear_cookies( self ): |
---|
| 882 | tc.clear_cookies() |
---|
| 883 | |
---|
| 884 | def clear_form( self, form=0 ): |
---|
| 885 | """Clears a form""" |
---|
| 886 | tc.formclear(str(form)) |
---|
| 887 | |
---|
| 888 | def home( self ): |
---|
| 889 | self.visit_url( self.url ) |
---|
| 890 | |
---|
| 891 | def last_page( self ): |
---|
| 892 | return tc.browser.get_html() |
---|
| 893 | |
---|
| 894 | def load_cookies( self, file ): |
---|
| 895 | filename = self.get_filename(file) |
---|
| 896 | tc.load_cookies(filename) |
---|
| 897 | |
---|
| 898 | def reload_page( self ): |
---|
| 899 | tc.reload() |
---|
| 900 | tc.code(200) |
---|
| 901 | |
---|
| 902 | def show_cookies( self ): |
---|
| 903 | return tc.show_cookies() |
---|
| 904 | |
---|
| 905 | def showforms( self ): |
---|
| 906 | """Shows form, helpful for debugging new tests""" |
---|
| 907 | return tc.showforms() |
---|
| 908 | |
---|
| 909 | def submit_form( self, form_no=0, button="runtool_btn", **kwd ): |
---|
| 910 | """Populates and submits a form from the keyword arguments.""" |
---|
| 911 | # An HTMLForm contains a sequence of Controls. Supported control classes are: |
---|
| 912 | # TextControl, FileControl, ListControl, RadioControl, CheckboxControl, SelectControl, |
---|
| 913 | # SubmitControl, ImageControl |
---|
| 914 | for i, f in enumerate( self.showforms() ): |
---|
| 915 | if i == form_no: |
---|
| 916 | break |
---|
| 917 | # To help with debugging a tool, print out the form controls when the test fails |
---|
| 918 | print "form '%s' contains the following controls ( note the values )" % f.name |
---|
| 919 | controls = {} |
---|
| 920 | hc_prefix = '<HiddenControl(' |
---|
| 921 | for i, control in enumerate( f.controls ): |
---|
| 922 | print "control %d: %s" % ( i, str( control ) ) |
---|
| 923 | if not hc_prefix in str( control ): |
---|
| 924 | try: |
---|
| 925 | #check if a repeat element needs to be added |
---|
| 926 | if control.name not in kwd and control.name.endswith( '_add' ): |
---|
| 927 | #control name doesn't exist, could be repeat |
---|
| 928 | repeat_startswith = control.name[0:-4] |
---|
| 929 | if repeat_startswith and not [ c_name for c_name in controls.keys() if c_name.startswith( repeat_startswith ) ] and [ c_name for c_name in kwd.keys() if c_name.startswith( repeat_startswith ) ]: |
---|
| 930 | tc.submit( control.name ) |
---|
| 931 | return self.submit_form( form_no=form_no, button=button, **kwd ) |
---|
| 932 | # Check for refresh_on_change attribute, submit a change if required |
---|
| 933 | if hasattr( control, 'attrs' ) and 'refresh_on_change' in control.attrs.keys(): |
---|
| 934 | changed = False |
---|
| 935 | item_labels = [ item.attrs[ 'label' ] for item in control.get_items() if item.selected ] #For DataToolParameter, control.value is the HDA id, but kwd contains the filename. This loop gets the filename/label for the selected values. |
---|
| 936 | for value in kwd[ control.name ]: |
---|
| 937 | if value not in control.value and True not in [ value in item_label for item_label in item_labels ]: |
---|
| 938 | changed = True |
---|
| 939 | break |
---|
| 940 | if changed: |
---|
| 941 | # Clear Control and set to proper value |
---|
| 942 | control.clear() |
---|
| 943 | # kwd[control.name] should be a singlelist |
---|
| 944 | for elem in kwd[ control.name ]: |
---|
| 945 | tc.fv( f.name, control.name, str( elem ) ) |
---|
| 946 | # Create a new submit control, allows form to refresh, instead of going to next page |
---|
| 947 | control = ClientForm.SubmitControl( 'SubmitControl', '___refresh_grouping___', {'name':'refresh_grouping'} ) |
---|
| 948 | control.add_to_form( f ) |
---|
| 949 | control.fixup() |
---|
| 950 | # Submit for refresh |
---|
| 951 | tc.submit( '___refresh_grouping___' ) |
---|
| 952 | return self.submit_form( form_no=form_no, button=button, **kwd ) |
---|
| 953 | except Exception, e: |
---|
| 954 | log.debug( "In submit_form, continuing, but caught exception: %s" % str( e ) ) |
---|
| 955 | continue |
---|
| 956 | controls[ control.name ] = control |
---|
| 957 | # No refresh_on_change attribute found in current form, so process as usual |
---|
| 958 | for control_name, control_value in kwd.items(): |
---|
| 959 | if control_name not in controls: |
---|
| 960 | continue # these cannot be handled safely - cause the test to barf out |
---|
| 961 | if not isinstance( control_value, list ): |
---|
| 962 | control_value = [ control_value ] |
---|
| 963 | control = controls[ control_name ] |
---|
| 964 | control.clear() |
---|
| 965 | if control.is_of_kind( "text" ): |
---|
| 966 | tc.fv( f.name, control.name, ",".join( control_value ) ) |
---|
| 967 | elif control.is_of_kind( "list" ): |
---|
| 968 | try: |
---|
| 969 | if control.is_of_kind( "multilist" ): |
---|
| 970 | if control.type == "checkbox": |
---|
| 971 | def is_checked( value ): |
---|
| 972 | # Copied from form_builder.CheckboxField |
---|
| 973 | if value == True: |
---|
| 974 | return True |
---|
| 975 | if isinstance( value, list ): |
---|
| 976 | value = value[0] |
---|
| 977 | return isinstance( value, basestring ) and value.lower() in ( "yes", "true", "on" ) |
---|
| 978 | try: |
---|
| 979 | checkbox = control.get() |
---|
| 980 | checkbox.selected = is_checked( control_value ) |
---|
| 981 | except Exception, e1: |
---|
| 982 | print "Attempting to set checkbox selected value threw exception: ", e1 |
---|
| 983 | # if there's more than one checkbox, probably should use the behaviour for |
---|
| 984 | # ClientForm.ListControl ( see twill code ), but this works for now... |
---|
| 985 | for elem in control_value: |
---|
| 986 | control.get( name=elem ).selected = True |
---|
| 987 | else: |
---|
| 988 | for elem in control_value: |
---|
| 989 | control.get( name=elem ).selected = True |
---|
| 990 | else: # control.is_of_kind( "singlelist" ) |
---|
| 991 | for elem in control_value: |
---|
| 992 | try: |
---|
| 993 | tc.fv( f.name, control.name, str( elem ) ) |
---|
| 994 | except Exception, e2: |
---|
| 995 | print "Attempting to set control '", control.name, "' to value '", elem, "' threw exception: ", e2 |
---|
| 996 | # Galaxy truncates long file names in the dataset_collector in ~/parameters/basic.py |
---|
| 997 | if len( elem ) > 30: |
---|
| 998 | elem_name = '%s..%s' % ( elem[:17], elem[-11:] ) |
---|
| 999 | else: |
---|
| 1000 | elem_name = elem |
---|
| 1001 | tc.fv( f.name, control.name, str( elem_name ) ) |
---|
| 1002 | except Exception, exc: |
---|
| 1003 | errmsg = "Attempting to set field '%s' to value '%s' in form '%s' threw exception: %s\n" % ( control_name, str( control_value ), f.name, str( exc ) ) |
---|
| 1004 | errmsg += "control: %s\n" % str( control ) |
---|
| 1005 | errmsg += "If the above control is a DataToolparameter whose data type class does not include a sniff() method,\n" |
---|
| 1006 | errmsg += "make sure to include a proper 'ftype' attribute to the tag for the control within the <test> tag set.\n" |
---|
| 1007 | raise AssertionError( errmsg ) |
---|
| 1008 | else: |
---|
| 1009 | # Add conditions for other control types here when necessary. |
---|
| 1010 | pass |
---|
| 1011 | tc.submit( button ) |
---|
| 1012 | def refresh_form( self, control_name, value, form_no=0, **kwd ): |
---|
| 1013 | """Handles Galaxy's refresh_on_change for forms without ultimately submitting the form""" |
---|
| 1014 | # control_name is the name of the form field that requires refresh_on_change, and value is |
---|
| 1015 | # the value to which that field is being set. |
---|
| 1016 | for i, f in enumerate( self.showforms() ): |
---|
| 1017 | if i == form_no: |
---|
| 1018 | break |
---|
| 1019 | try: |
---|
| 1020 | control = f.find_control( name=control_name ) |
---|
| 1021 | except: |
---|
| 1022 | # This assumes we always want the first control of the given name, which may not be ideal... |
---|
| 1023 | control = f.find_control( name=control_name, nr=0 ) |
---|
| 1024 | # Check for refresh_on_change attribute, submit a change if required |
---|
| 1025 | if 'refresh_on_change' in control.attrs.keys(): |
---|
| 1026 | # Clear Control and set to proper value |
---|
| 1027 | control.clear() |
---|
| 1028 | tc.fv( f.name, control.name, value ) |
---|
| 1029 | # Create a new submit control, allows form to refresh, instead of going to next page |
---|
| 1030 | control = ClientForm.SubmitControl( 'SubmitControl', '___refresh_grouping___', {'name':'refresh_grouping'} ) |
---|
| 1031 | control.add_to_form( f ) |
---|
| 1032 | control.fixup() |
---|
| 1033 | # Submit for refresh |
---|
| 1034 | tc.submit( '___refresh_grouping___' ) |
---|
| 1035 | def visit_page( self, page ): |
---|
| 1036 | # tc.go("./%s" % page) |
---|
| 1037 | if not page.startswith( "/" ): |
---|
| 1038 | page = "/" + page |
---|
| 1039 | tc.go( self.url + page ) |
---|
| 1040 | tc.code( 200 ) |
---|
| 1041 | |
---|
| 1042 | def visit_url( self, url ): |
---|
| 1043 | tc.go("%s" % url) |
---|
| 1044 | tc.code( 200 ) |
---|
| 1045 | |
---|
| 1046 | """Functions associated with Galaxy tools""" |
---|
| 1047 | def run_tool( self, tool_id, repeat_name=None, **kwd ): |
---|
| 1048 | tool_id = tool_id.replace(" ", "+") |
---|
| 1049 | """Runs the tool 'tool_id' and passes it the key/values from the *kwd""" |
---|
| 1050 | self.visit_url( "%s/tool_runner/index?tool_id=%s" % (self.url, tool_id) ) |
---|
| 1051 | if repeat_name is not None: |
---|
| 1052 | repeat_button = '%s_add' % repeat_name |
---|
| 1053 | # Submit the "repeat" form button to add an input) |
---|
| 1054 | tc.submit( repeat_button ) |
---|
| 1055 | print "button '%s' clicked" % repeat_button |
---|
| 1056 | tc.find( 'runtool_btn' ) |
---|
| 1057 | self.submit_form( **kwd ) |
---|
| 1058 | |
---|
| 1059 | def run_ucsc_main( self, track_params, output_params ): |
---|
| 1060 | """Gets Data From UCSC""" |
---|
| 1061 | tool_id = "ucsc_table_direct1" |
---|
| 1062 | track_string = urllib.urlencode( track_params ) |
---|
| 1063 | galaxy_url = urllib.quote_plus( "%s/tool_runner/index?" % self.url ) |
---|
| 1064 | self.visit_url( "http://genome.ucsc.edu/cgi-bin/hgTables?GALAXY_URL=%s&hgta_compressType=none&tool_id=%s&%s" % ( galaxy_url, tool_id, track_string ) ) |
---|
| 1065 | tc.fv( "mainForm", "hgta_doTopSubmit", "get output" ) |
---|
| 1066 | self.submit_form( button="get output" )#, **track_params ) |
---|
| 1067 | tc.fv( 2, "hgta_doGalaxyQuery", "Send query to Galaxy" ) |
---|
| 1068 | self.submit_form( button="Send query to Galaxy" )#, **output_params ) #AssertionError: Attempting to set field 'fbQual' to value '['whole']' in form 'None' threw exception: no matching forms! control: <RadioControl(fbQual=[whole, upstreamAll, endAll])> |
---|
| 1069 | |
---|
| 1070 | def wait( self, maxseconds=120 ): |
---|
| 1071 | """Waits for the tools to finish""" |
---|
| 1072 | sleep_amount = 0.1 |
---|
| 1073 | slept = 0 |
---|
| 1074 | self.home() |
---|
| 1075 | while slept <= maxseconds: |
---|
| 1076 | self.visit_page( "history" ) |
---|
| 1077 | page = tc.browser.get_html() |
---|
| 1078 | if page.find( '<!-- running: do not change this comment, used by TwillTestCase.wait -->' ) > -1: |
---|
| 1079 | time.sleep( sleep_amount ) |
---|
| 1080 | slept += sleep_amount |
---|
| 1081 | sleep_amount *= 2 |
---|
| 1082 | if slept + sleep_amount > maxseconds: |
---|
| 1083 | sleep_amount = maxseconds - slept # don't overshoot maxseconds |
---|
| 1084 | else: |
---|
| 1085 | break |
---|
| 1086 | assert slept < maxseconds |
---|
| 1087 | |
---|
| 1088 | # Dataset Security stuff |
---|
| 1089 | # Tests associated with users |
---|
| 1090 | def create_new_account_as_admin( self, email='test4@bx.psu.edu', password='testuser', |
---|
| 1091 | username='regular-user4', webapp='galaxy', referer='' ): |
---|
| 1092 | """Create a new account for another user""" |
---|
| 1093 | # HACK: don't use panels because late_javascripts() messes up the twill browser and it |
---|
| 1094 | # can't find form fields (and hence user can't be logged in). |
---|
| 1095 | self.visit_url( "%s/user/create?admin_view=True" % self.url ) |
---|
| 1096 | tc.fv( '1', 'email', email ) |
---|
| 1097 | tc.fv( '1', 'webapp', webapp ) |
---|
| 1098 | tc.fv( '1', 'referer', referer ) |
---|
| 1099 | tc.fv( '1', 'password', password ) |
---|
| 1100 | tc.fv( '1', 'confirm', password ) |
---|
| 1101 | tc.fv( '1', 'username', username ) |
---|
| 1102 | tc.submit( 'create_user_button' ) |
---|
| 1103 | previously_created = False |
---|
| 1104 | username_taken = False |
---|
| 1105 | invalid_username = False |
---|
| 1106 | try: |
---|
| 1107 | self.check_page_for_string( "Created new user account" ) |
---|
| 1108 | except: |
---|
| 1109 | try: |
---|
| 1110 | # May have created the account in a previous test run... |
---|
| 1111 | self.check_page_for_string( "User with that email already exists" ) |
---|
| 1112 | previously_created = True |
---|
| 1113 | except: |
---|
| 1114 | try: |
---|
| 1115 | self.check_page_for_string( 'This user name is not available' ) |
---|
| 1116 | username_taken = True |
---|
| 1117 | except: |
---|
| 1118 | try: |
---|
| 1119 | # Note that we're only checking if the usr name is >< 4 chars here... |
---|
| 1120 | self.check_page_for_string( 'User name must be at least 4 characters in length' ) |
---|
| 1121 | invalid_username = True |
---|
| 1122 | except: |
---|
| 1123 | pass |
---|
| 1124 | return previously_created, username_taken, invalid_username |
---|
| 1125 | def reset_password_as_admin( self, user_id, password='testreset' ): |
---|
| 1126 | """Reset a user password""" |
---|
| 1127 | self.home() |
---|
| 1128 | self.visit_url( "%s/admin/reset_user_password?id=%s" % ( self.url, user_id ) ) |
---|
| 1129 | tc.fv( "1", "password", password ) |
---|
| 1130 | tc.fv( "1", "confirm", password ) |
---|
| 1131 | tc.submit( "reset_user_password_button" ) |
---|
| 1132 | self.check_page_for_string( "Passwords reset for 1 users" ) |
---|
| 1133 | self.home() |
---|
| 1134 | def mark_user_deleted( self, user_id, email='' ): |
---|
| 1135 | """Mark a user as deleted""" |
---|
| 1136 | self.home() |
---|
| 1137 | self.visit_url( "%s/admin/users?operation=delete&id=%s" % ( self.url, user_id ) ) |
---|
| 1138 | check_str = "Deleted 1 users" |
---|
| 1139 | self.check_page_for_string( check_str ) |
---|
| 1140 | self.home() |
---|
| 1141 | def undelete_user( self, user_id, email='' ): |
---|
| 1142 | """Undelete a user""" |
---|
| 1143 | self.home() |
---|
| 1144 | self.visit_url( "%s/admin/users?operation=undelete&id=%s" % ( self.url, user_id ) ) |
---|
| 1145 | check_str = "Undeleted 1 users" |
---|
| 1146 | self.check_page_for_string( check_str ) |
---|
| 1147 | self.home() |
---|
| 1148 | def purge_user( self, user_id, email ): |
---|
| 1149 | """Purge a user account""" |
---|
| 1150 | self.home() |
---|
| 1151 | self.visit_url( "%s/admin/users?operation=purge&id=%s" % ( self.url, user_id ) ) |
---|
| 1152 | check_str = "Purged 1 users" |
---|
| 1153 | self.check_page_for_string( check_str ) |
---|
| 1154 | self.home() |
---|
| 1155 | def manage_roles_and_groups_for_user( self, user_id, in_role_ids=[], out_role_ids=[], |
---|
| 1156 | in_group_ids=[], out_group_ids=[], strings_displayed=[] ): |
---|
| 1157 | self.home() |
---|
| 1158 | url = "%s/admin/manage_roles_and_groups_for_user?id=%s" % ( self.url, user_id ) |
---|
| 1159 | if in_role_ids: |
---|
| 1160 | url += "&in_roles=%s" % ','.join( in_role_ids ) |
---|
| 1161 | if out_role_ids: |
---|
| 1162 | url += "&out_roles=%s" % ','.join( out_role_ids ) |
---|
| 1163 | if in_group_ids: |
---|
| 1164 | url += "&in_groups=%s" % ','.join( in_group_ids ) |
---|
| 1165 | if out_group_ids: |
---|
| 1166 | url += "&out_groups=%s" % ','.join( out_group_ids ) |
---|
| 1167 | if in_role_ids or out_role_ids or in_group_ids or out_group_ids: |
---|
| 1168 | url += "&user_roles_groups_edit_button=Save" |
---|
| 1169 | self.visit_url( url ) |
---|
| 1170 | for check_str in strings_displayed: |
---|
| 1171 | self.check_page_for_string( check_str ) |
---|
| 1172 | self.home() |
---|
| 1173 | |
---|
| 1174 | # Tests associated with roles |
---|
| 1175 | def browse_roles( self, strings_displayed=[] ): |
---|
| 1176 | self.visit_url( '%s/admin/roles' % self.url ) |
---|
| 1177 | for check_str in strings_displayed: |
---|
| 1178 | self.check_page_for_string( check_str ) |
---|
| 1179 | def create_role( self, |
---|
| 1180 | name='Role One', |
---|
| 1181 | description="This is Role One", |
---|
| 1182 | in_user_ids=[], |
---|
| 1183 | in_group_ids=[], |
---|
| 1184 | create_group_for_role='no', |
---|
| 1185 | private_role='' ): |
---|
| 1186 | """Create a new role""" |
---|
| 1187 | url = "%s/admin/roles?operation=create&create_role_button=Save&name=%s&description=%s" % ( self.url, name.replace( ' ', '+' ), description.replace( ' ', '+' ) ) |
---|
| 1188 | if in_user_ids: |
---|
| 1189 | url += "&in_users=%s" % ','.join( in_user_ids ) |
---|
| 1190 | if in_group_ids: |
---|
| 1191 | url += "&in_groups=%s" % ','.join( in_group_ids ) |
---|
| 1192 | if create_group_for_role == 'yes': |
---|
| 1193 | url += '&create_group_for_role=yes' |
---|
| 1194 | self.home() |
---|
| 1195 | self.visit_url( url ) |
---|
| 1196 | if create_group_for_role == 'yes': |
---|
| 1197 | check_str = "Group '%s' has been created, and role '%s' has been created with %d associated users and %d associated groups" % \ |
---|
| 1198 | ( name, name, len( in_user_ids ), len( in_group_ids ) ) |
---|
| 1199 | else: |
---|
| 1200 | check_str = "Role '%s' has been created with %d associated users and %d associated groups" % \ |
---|
| 1201 | ( name, len( in_user_ids ), len( in_group_ids ) ) |
---|
| 1202 | self.check_page_for_string( check_str ) |
---|
| 1203 | if private_role: |
---|
| 1204 | # Make sure no private roles are displayed |
---|
| 1205 | try: |
---|
| 1206 | self.check_page_for_string( private_role ) |
---|
| 1207 | errmsg = 'Private role %s displayed on Roles page' % private_role |
---|
| 1208 | raise AssertionError( errmsg ) |
---|
| 1209 | except AssertionError: |
---|
| 1210 | # Reaching here is the behavior we want since no private roles should be displayed |
---|
| 1211 | pass |
---|
| 1212 | self.home() |
---|
| 1213 | self.visit_url( "%s/admin/roles" % self.url ) |
---|
| 1214 | self.check_page_for_string( name ) |
---|
| 1215 | self.home() |
---|
| 1216 | def rename_role( self, role_id, name='Role One Renamed', description='This is Role One Re-described' ): |
---|
| 1217 | """Rename a role""" |
---|
| 1218 | self.home() |
---|
| 1219 | self.visit_url( "%s/admin/roles?operation=rename&id=%s" % ( self.url, role_id ) ) |
---|
| 1220 | self.check_page_for_string( 'Change role name and description' ) |
---|
| 1221 | tc.fv( "1", "name", name ) |
---|
| 1222 | tc.fv( "1", "description", description ) |
---|
| 1223 | tc.submit( "rename_role_button" ) |
---|
| 1224 | self.home() |
---|
| 1225 | def mark_role_deleted( self, role_id, role_name ): |
---|
| 1226 | """Mark a role as deleted""" |
---|
| 1227 | self.home() |
---|
| 1228 | self.visit_url( "%s/admin/roles?operation=delete&id=%s" % ( self.url, role_id ) ) |
---|
| 1229 | check_str = "Deleted 1 roles: %s" % role_name |
---|
| 1230 | self.check_page_for_string( check_str ) |
---|
| 1231 | self.home() |
---|
| 1232 | def undelete_role( self, role_id, role_name ): |
---|
| 1233 | """Undelete an existing role""" |
---|
| 1234 | self.home() |
---|
| 1235 | self.visit_url( "%s/admin/roles?operation=undelete&id=%s" % ( self.url, role_id ) ) |
---|
| 1236 | check_str = "Undeleted 1 roles: %s" % role_name |
---|
| 1237 | self.check_page_for_string( check_str ) |
---|
| 1238 | self.home() |
---|
| 1239 | def purge_role( self, role_id, role_name ): |
---|
| 1240 | """Purge an existing role""" |
---|
| 1241 | self.home() |
---|
| 1242 | self.visit_url( "%s/admin/roles?operation=purge&id=%s" % ( self.url, role_id ) ) |
---|
| 1243 | check_str = "Purged 1 roles: %s" % role_name |
---|
| 1244 | self.check_page_for_string( check_str ) |
---|
| 1245 | self.home() |
---|
| 1246 | def associate_users_and_groups_with_role( self, role_id, role_name, user_ids=[], group_ids=[] ): |
---|
| 1247 | self.home() |
---|
| 1248 | url = "%s/admin/role?id=%s&role_members_edit_button=Save" % ( self.url, role_id ) |
---|
| 1249 | if user_ids: |
---|
| 1250 | url += "&in_users=%s" % ','.join( user_ids ) |
---|
| 1251 | if group_ids: |
---|
| 1252 | url += "&in_groups=%s" % ','.join( group_ids ) |
---|
| 1253 | self.visit_url( url ) |
---|
| 1254 | check_str = "Role '%s' has been updated with %d associated users and %d associated groups" % ( role_name, len( user_ids ), len( group_ids ) ) |
---|
| 1255 | self.check_page_for_string( check_str ) |
---|
| 1256 | self.home() |
---|
| 1257 | |
---|
| 1258 | # Tests associated with groups |
---|
| 1259 | def create_group( self, name='Group One', in_user_ids=[], in_role_ids=[] ): |
---|
| 1260 | """Create a new group""" |
---|
| 1261 | url = "%s/admin/groups?operation=create&create_group_button=Save&name=%s" % ( self.url, name.replace( ' ', '+' ) ) |
---|
| 1262 | if in_user_ids: |
---|
| 1263 | url += "&in_users=%s" % ','.join( in_user_ids ) |
---|
| 1264 | if in_role_ids: |
---|
| 1265 | url += "&in_roles=%s" % ','.join( in_role_ids ) |
---|
| 1266 | self.home() |
---|
| 1267 | self.visit_url( url ) |
---|
| 1268 | check_str = "Group '%s' has been created with %d associated users and %d associated roles" % ( name, len( in_user_ids ), len( in_role_ids ) ) |
---|
| 1269 | self.check_page_for_string( check_str ) |
---|
| 1270 | self.home() |
---|
| 1271 | self.visit_url( "%s/admin/groups" % self.url ) |
---|
| 1272 | self.check_page_for_string( name ) |
---|
| 1273 | self.home() |
---|
| 1274 | def browse_groups( self, strings_displayed=[] ): |
---|
| 1275 | self.visit_url( '%s/admin/groups' % self.url ) |
---|
| 1276 | for check_str in strings_displayed: |
---|
| 1277 | self.check_page_for_string( check_str ) |
---|
| 1278 | def rename_group( self, group_id, name='Group One Renamed' ): |
---|
| 1279 | """Rename a group""" |
---|
| 1280 | self.home() |
---|
| 1281 | self.visit_url( "%s/admin/groups?operation=rename&id=%s" % ( self.url, group_id ) ) |
---|
| 1282 | self.check_page_for_string( 'Change group name' ) |
---|
| 1283 | tc.fv( "1", "name", name ) |
---|
| 1284 | tc.submit( "rename_group_button" ) |
---|
| 1285 | self.home() |
---|
| 1286 | def associate_users_and_roles_with_group( self, group_id, group_name, user_ids=[], role_ids=[] ): |
---|
| 1287 | self.home() |
---|
| 1288 | url = "%s/admin/manage_users_and_roles_for_group?id=%s&group_roles_users_edit_button=Save" % ( self.url, group_id ) |
---|
| 1289 | if user_ids: |
---|
| 1290 | url += "&in_users=%s" % ','.join( user_ids ) |
---|
| 1291 | if role_ids: |
---|
| 1292 | url += "&in_roles=%s" % ','.join( role_ids ) |
---|
| 1293 | self.visit_url( url ) |
---|
| 1294 | check_str = "Group '%s' has been updated with %d associated roles and %d associated users" % ( group_name, len( role_ids ), len( user_ids ) ) |
---|
| 1295 | self.check_page_for_string( check_str ) |
---|
| 1296 | self.home() |
---|
| 1297 | def mark_group_deleted( self, group_id, group_name ): |
---|
| 1298 | """Mark a group as deleted""" |
---|
| 1299 | self.home() |
---|
| 1300 | self.visit_url( "%s/admin/groups?operation=delete&id=%s" % ( self.url, group_id ) ) |
---|
| 1301 | check_str = "Deleted 1 groups: %s" % group_name |
---|
| 1302 | self.check_page_for_string( check_str ) |
---|
| 1303 | self.home() |
---|
| 1304 | def undelete_group( self, group_id, group_name ): |
---|
| 1305 | """Undelete an existing group""" |
---|
| 1306 | self.home() |
---|
| 1307 | self.visit_url( "%s/admin/groups?operation=undelete&id=%s" % ( self.url, group_id ) ) |
---|
| 1308 | check_str = "Undeleted 1 groups: %s" % group_name |
---|
| 1309 | self.check_page_for_string( check_str ) |
---|
| 1310 | self.home() |
---|
| 1311 | def purge_group( self, group_id, group_name ): |
---|
| 1312 | """Purge an existing group""" |
---|
| 1313 | self.home() |
---|
| 1314 | self.visit_url( "%s/admin/groups?operation=purge&id=%s" % ( self.url, group_id ) ) |
---|
| 1315 | check_str = "Purged 1 groups: %s" % group_name |
---|
| 1316 | self.check_page_for_string( check_str ) |
---|
| 1317 | self.home() |
---|
| 1318 | |
---|
| 1319 | # Form stuff |
---|
| 1320 | def create_form( self, name, desc, form_type, field_type='TextField', form_layout_name='', |
---|
| 1321 | num_fields=1, num_options=0, strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1322 | """Create a new form definition.""" |
---|
| 1323 | self.visit_url( "%s/forms/create_form" % self.url ) |
---|
| 1324 | for check_str in strings_displayed: |
---|
| 1325 | self.check_page_for_string( check_str ) |
---|
| 1326 | tc.fv( "1", "name", name ) |
---|
| 1327 | tc.fv( "1", "description", desc ) |
---|
| 1328 | tc.fv( "1", "form_type_selectbox", form_type ) |
---|
| 1329 | tc.submit( "create_form_button" ) |
---|
| 1330 | if form_type == "Sequencing Sample Form": |
---|
| 1331 | tc.submit( "add_layout_grid" ) |
---|
| 1332 | tc.fv( "1", "grid_layout0", form_layout_name ) |
---|
| 1333 | # Add fields to the new form definition |
---|
| 1334 | for index1 in range( num_fields ): |
---|
| 1335 | field_name = 'field_name_%i' % index1 |
---|
| 1336 | field_contents = field_type |
---|
| 1337 | field_help_name = 'field_helptext_%i' % index1 |
---|
| 1338 | field_help_contents = 'Field %i help' % index1 |
---|
| 1339 | field_default = 'field_default_0' |
---|
| 1340 | field_default_contents = '%s default contents' % form_type |
---|
| 1341 | tc.fv( "1", field_name, field_contents ) |
---|
| 1342 | tc.fv( "1", field_help_name, field_help_contents ) |
---|
| 1343 | if field_type == 'SelectField': |
---|
| 1344 | # SelectField field_type requires a refresh_on_change |
---|
| 1345 | self.refresh_form( 'field_type_0', field_type ) |
---|
| 1346 | # Add options so our select list is functional |
---|
| 1347 | if num_options == 0: |
---|
| 1348 | # Default to 2 options |
---|
| 1349 | num_options = 2 |
---|
| 1350 | for index2 in range( 1, num_options+1 ): |
---|
| 1351 | tc.submit( "addoption_0" ) |
---|
| 1352 | # Add contents to the new options fields |
---|
| 1353 | for index2 in range( num_options ): |
---|
| 1354 | option_field_name = 'field_0_option_%i' % index2 |
---|
| 1355 | option_field_value = 'Option%i' % index2 |
---|
| 1356 | tc.fv( "1", option_field_name, option_field_value ) |
---|
| 1357 | else: |
---|
| 1358 | tc.fv( "1", "field_type_0", field_type ) |
---|
| 1359 | tc.fv( "1", field_default, field_default_contents ) |
---|
| 1360 | tc.submit( "save_changes_button" ) |
---|
| 1361 | if num_fields == 0: |
---|
| 1362 | self.visit_url( "%s/forms/manage" % self.url ) |
---|
| 1363 | for check_str in strings_displayed_after_submit: |
---|
| 1364 | self.check_page_for_string( check_str ) |
---|
| 1365 | self.home() |
---|
| 1366 | def edit_form( self, id, form_type='', new_form_name='', new_form_desc='', field_dicts=[], field_index=0, |
---|
| 1367 | strings_displayed=[], strings_not_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1368 | """Edit form details; name and description""" |
---|
| 1369 | self.home() |
---|
| 1370 | self.visit_url( "%s/forms/manage?operation=Edit&id=%s" % ( self.url, id ) ) |
---|
| 1371 | for check_str in strings_displayed: |
---|
| 1372 | self.check_page_for_string( check_str ) |
---|
| 1373 | if new_form_name: |
---|
| 1374 | tc.fv( "1", "name", new_form_name ) |
---|
| 1375 | if new_form_desc: |
---|
| 1376 | tc.fv( "1", "description", new_form_desc ) |
---|
| 1377 | for i, field_dict in enumerate( field_dicts ): |
---|
| 1378 | index = i + field_index |
---|
| 1379 | tc.submit( "add_field_button" ) |
---|
| 1380 | field_name = "field_name_%i" % index |
---|
| 1381 | field_value = field_dict[ 'name' ] |
---|
| 1382 | field_help = "field_helptext_%i" % index |
---|
| 1383 | field_help_value = field_dict[ 'desc' ] |
---|
| 1384 | field_type = "field_type_%i" % index |
---|
| 1385 | field_type_value = field_dict[ 'type' ] |
---|
| 1386 | field_required = "field_required_%i" % index |
---|
| 1387 | field_required_value = field_dict[ 'required' ] |
---|
| 1388 | tc.fv( "1", field_name, field_value ) |
---|
| 1389 | tc.fv( "1", field_help, field_help_value ) |
---|
| 1390 | tc.fv( "1", field_required, field_required_value ) |
---|
| 1391 | if field_type_value.lower() == 'selectfield': |
---|
| 1392 | # SelectFields require a refresh_on_change |
---|
| 1393 | self.refresh_form( field_type, field_type_value ) |
---|
| 1394 | for option_index, option in enumerate( field_dict[ 'selectlist' ] ): |
---|
| 1395 | tc.submit( "addoption_0" ) |
---|
| 1396 | tc.fv( "1", "field_%i_option_%i" % ( index, option_index ), option ) |
---|
| 1397 | else: |
---|
| 1398 | tc.fv( "1", field_type, field_type_value ) |
---|
| 1399 | tc.submit( "save_changes_button" ) |
---|
| 1400 | for check_str in strings_displayed_after_submit: |
---|
| 1401 | self.check_page_for_string( check_str ) |
---|
| 1402 | self.home() |
---|
| 1403 | def mark_form_deleted( self, form_id ): |
---|
| 1404 | """Mark a form_definition as deleted""" |
---|
| 1405 | self.home() |
---|
| 1406 | url = "%s/forms/manage?operation=delete&id=%s" % ( self.url, form_id ) |
---|
| 1407 | self.visit_url( url ) |
---|
| 1408 | check_str = "1 forms have been deleted." |
---|
| 1409 | self.check_page_for_string( check_str ) |
---|
| 1410 | self.home() |
---|
| 1411 | |
---|
| 1412 | # Requests stuff |
---|
| 1413 | def check_request_grid( self, cntrller, state, deleted=False, strings_displayed=[] ): |
---|
| 1414 | self.visit_url( '%s/%s/browse_requests?sort=create_time&f-state=%s&f-deleted=%s' % \ |
---|
| 1415 | ( self.url, cntrller, state.replace( ' ', '+' ), str( deleted ) ) ) |
---|
| 1416 | for check_str in strings_displayed: |
---|
| 1417 | self.check_page_for_string( check_str ) |
---|
| 1418 | def create_request_type( self, name, desc, request_form_id, sample_form_id, states, strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1419 | self.home() |
---|
| 1420 | self.visit_url( "%s/requests_admin/create_request_type" % self.url ) |
---|
| 1421 | for check_str in strings_displayed: |
---|
| 1422 | self.check_page_for_string( check_str ) |
---|
| 1423 | tc.fv( "1", "name", name ) |
---|
| 1424 | tc.fv( "1", "desc", desc ) |
---|
| 1425 | tc.fv( "1", "request_form_id", request_form_id ) |
---|
| 1426 | tc.fv( "1", "sample_form_id", sample_form_id ) |
---|
| 1427 | for index, state in enumerate(states): |
---|
| 1428 | tc.submit( "add_state_button" ) |
---|
| 1429 | tc.fv("1", "state_name_%i" % index, state[0]) |
---|
| 1430 | tc.fv("1", "state_desc_%i" % index, state[1]) |
---|
| 1431 | tc.submit( "save_request_type" ) |
---|
| 1432 | for check_str in strings_displayed_after_submit: |
---|
| 1433 | self.check_page_for_string( check_str ) |
---|
| 1434 | def request_type_permissions( self, request_type_id, request_type_name, role_ids_str, permissions_in, permissions_out ): |
---|
| 1435 | # role_ids_str must be a comma-separated string of role ids |
---|
| 1436 | url = "requests_admin/request_type_permissions?id=%s&update_roles_button=Save" % ( request_type_id ) |
---|
| 1437 | for po in permissions_out: |
---|
| 1438 | key = '%s_out' % po |
---|
| 1439 | url ="%s&%s=%s" % ( url, key, role_ids_str ) |
---|
| 1440 | for pi in permissions_in: |
---|
| 1441 | key = '%s_in' % pi |
---|
| 1442 | url ="%s&%s=%s" % ( url, key, role_ids_str ) |
---|
| 1443 | self.home() |
---|
| 1444 | self.visit_url( "%s/%s" % ( self.url, url ) ) |
---|
| 1445 | check_str = "Permissions updated for sequencer configuration '%s'" % request_type_name |
---|
| 1446 | self.check_page_for_string( check_str ) |
---|
| 1447 | self.home() |
---|
| 1448 | def create_request( self, cntrller, request_type_id, name, desc, field_value_tuples, other_users_id='', |
---|
| 1449 | strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1450 | self.visit_url( "%s/requests_common/create_request?cntrller=%s" % ( self.url, cntrller ) ) |
---|
| 1451 | # The request_type SelectList requires a refresh_on_change |
---|
| 1452 | self.refresh_form( 'request_type_id', request_type_id ) |
---|
| 1453 | if cntrller == 'requests_admin' and other_users_id: |
---|
| 1454 | # The admin is creating a request on behalf of another user |
---|
| 1455 | # The user_id SelectField requires a refresh_on_change so that the selected |
---|
| 1456 | # user's addresses will be populated in the AddressField widget |
---|
| 1457 | self.refresh_form( "user_id", other_users_id ) |
---|
| 1458 | for check_str in strings_displayed: |
---|
| 1459 | self.check_page_for_string( check_str ) |
---|
| 1460 | tc.fv( "1", "name", name ) |
---|
| 1461 | tc.fv( "1", "desc", desc ) |
---|
| 1462 | for index, field_value_tuple in enumerate( field_value_tuples ): |
---|
| 1463 | field_name = "field_%i" % index |
---|
| 1464 | field_value, refresh_on_change = field_value_tuple |
---|
| 1465 | if refresh_on_change: |
---|
| 1466 | # TODO: If the field is an AddressField, we should test for adding a new address |
---|
| 1467 | # which would need to be handled here. This currently only allows an existing |
---|
| 1468 | # user_address to be selected. |
---|
| 1469 | self.refresh_form( field_name, field_value ) |
---|
| 1470 | else: |
---|
| 1471 | tc.fv( "1", field_name, field_value ) |
---|
| 1472 | tc.submit( "create_request_button" ) |
---|
| 1473 | for check_str in strings_displayed_after_submit: |
---|
| 1474 | self.check_page_for_string( check_str ) |
---|
| 1475 | self.home() |
---|
| 1476 | def edit_basic_request_info( self, cntrller, request_id, name, new_name='', new_desc='', new_fields=[], |
---|
| 1477 | strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1478 | self.visit_url( "%s/requests_common/edit_basic_request_info?cntrller=%s&id=%s" % ( self.url, cntrller, request_id ) ) |
---|
| 1479 | for check_str in strings_displayed: |
---|
| 1480 | self.check_page_for_string( check_str ) |
---|
| 1481 | self.check_page_for_string( 'Edit sequencing request "%s"' % name ) |
---|
| 1482 | if new_name: |
---|
| 1483 | tc.fv( "1", "name", new_name ) |
---|
| 1484 | if new_desc: |
---|
| 1485 | tc.fv( "1", "desc", new_desc ) |
---|
| 1486 | for index, field_value in enumerate( new_fields ): |
---|
| 1487 | tc.fv( "1", "field_%i" % index, field_value ) |
---|
| 1488 | tc.submit( "edit_basic_request_info_button" ) |
---|
| 1489 | for check_str in strings_displayed_after_submit: |
---|
| 1490 | self.check_page_for_string( check_str ) |
---|
| 1491 | def add_samples( self, cntrller, request_id, request_name, sample_value_tuples, strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1492 | self.visit_url( "%s/requests_common/manage_request?cntrller=%s&id=%s" % ( self.url, cntrller, request_id ) ) |
---|
| 1493 | for check_str in strings_displayed: |
---|
| 1494 | self.check_page_for_string( check_str ) |
---|
| 1495 | # Simulate clicking the add-sample_button on the form. (gvk: 9/21/10 - TODO : There must be a bug in the mako template |
---|
| 1496 | # because twill cannot find any forms on the page, but I cannot find it although I've spent time cleaning up the |
---|
| 1497 | # template code and looking for any problems. |
---|
| 1498 | url = "%s/requests_common/manage_request?cntrller=%s&id=%s" % ( self.url, cntrller, request_id ) |
---|
| 1499 | # This should work, but although twill does not thorw any exceptions, the button click never occurs |
---|
| 1500 | # There are multiple forms on this page, and we'll only be using the form named manage_request. |
---|
| 1501 | # for sample_index, sample_value_tuple in enumerate( sample_value_tuples ): |
---|
| 1502 | # # Add the following form value to the already populated hidden field so that the manage_request |
---|
| 1503 | # # form is the current form |
---|
| 1504 | # tc.fv( "1", "id", request_id ) |
---|
| 1505 | # tc.submit( 'add_sample_button' ) |
---|
| 1506 | for sample_index, sample_value_tuple in enumerate( sample_value_tuples ): |
---|
| 1507 | sample_name, field_values = sample_value_tuple |
---|
| 1508 | sample_name = sample_name.replace( ' ', '+' ) |
---|
| 1509 | field_name = "sample_%i_name" % sample_index |
---|
| 1510 | # The following form_value setting should work but since twill barfed on submitting the add_sample_button |
---|
| 1511 | # above, we have to simulate it by appending to the url. |
---|
| 1512 | # tc.fv( "1", field_name, sample_name ) |
---|
| 1513 | url += "&%s=%s" % ( field_name, sample_name ) |
---|
| 1514 | for field_index, field_value in enumerate( field_values ): |
---|
| 1515 | field_name = "sample_%i_field_%i" % ( sample_index, field_index ) |
---|
| 1516 | field_value = field_value.replace( ' ', '+' ) |
---|
| 1517 | # The following form_value setting should work but since twill barfed on submitting the add_sample_button |
---|
| 1518 | # above, we have to simulate it by appending to the url. |
---|
| 1519 | # tc.fv( "1", field_name, field_value ) |
---|
| 1520 | url += "&%s=%s" % ( field_name , field_value ) |
---|
| 1521 | # The following button submit should work but since twill barfed on submitting the add_sample_button |
---|
| 1522 | # above, we have to simulate it by appending to the url. |
---|
| 1523 | # tc.submit( "save_samples_button" ) |
---|
| 1524 | url += "&save_samples_button=Save" |
---|
| 1525 | self.visit_url( url ) |
---|
| 1526 | for check_str in strings_displayed_after_submit: |
---|
| 1527 | self.check_page_for_string( check_str ) |
---|
| 1528 | def submit_request( self, cntrller, request_id, request_name, strings_displayed_after_submit=[] ): |
---|
| 1529 | self.visit_url( "%s/requests_common/submit_request?cntrller=%s&id=%s" % ( self.url, cntrller, request_id ) ) |
---|
| 1530 | for check_str in strings_displayed_after_submit: |
---|
| 1531 | self.check_page_for_string( check_str ) |
---|
| 1532 | def reject_request( self, request_id, request_name, comment, strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1533 | self.visit_url( "%s/requests_admin/reject?id=%s" % ( self.url, request_id ) ) |
---|
| 1534 | for check_str in strings_displayed: |
---|
| 1535 | self.check_page_for_string( check_str ) |
---|
| 1536 | tc.fv( "1", "comment", comment ) |
---|
| 1537 | tc.submit( "reject_button" ) |
---|
| 1538 | for check_str in strings_displayed_after_submit: |
---|
| 1539 | self.check_page_for_string( check_str ) |
---|
| 1540 | def add_bar_codes( self, request_id, request_name, bar_codes, samples, strings_displayed_after_submit=[] ): |
---|
| 1541 | # We have to simulate the form submission here since twill barfs on the page |
---|
| 1542 | # gvk - 9/22/10 - TODO: make sure the mako template produces valid html |
---|
| 1543 | url = "%s/requests_common/manage_request?cntrller=requests_admin&id=%s&managing_samples=True" % ( self.url, request_id ) |
---|
| 1544 | for index, field_value in enumerate( bar_codes ): |
---|
| 1545 | sample_field_name = "sample_%i_name" % index |
---|
| 1546 | sample_field_value = samples[ index ].name.replace( ' ', '+' ) |
---|
| 1547 | field_name = "sample_%i_barcode" % index |
---|
| 1548 | url += "&%s=%s" % ( field_name, field_value ) |
---|
| 1549 | url += "&%s=%s" % ( sample_field_name, sample_field_value ) |
---|
| 1550 | url += "&save_samples_button=Save" |
---|
| 1551 | self.visit_url( url ) |
---|
| 1552 | for check_str in strings_displayed_after_submit: |
---|
| 1553 | self.check_page_for_string( check_str ) |
---|
| 1554 | def change_sample_state( self, request_id, request_name, sample_names, sample_ids, new_sample_state_id, new_state_name, comment='', |
---|
| 1555 | strings_displayed=[], strings_displayed_after_submit=[] ): |
---|
| 1556 | # We have to simulate the form submission here since twill barfs on the page |
---|
| 1557 | # gvk - 9/22/10 - TODO: make sure the mako template produces valid html |
---|
| 1558 | url = "%s/requests_common/manage_request?cntrller=requests_admin&id=%s" % ( self.url, request_id ) |
---|
| 1559 | url += "&comment=%s&sample_state_id=%s" % ( comment, self.security.encode_id( new_sample_state_id ) ) |
---|
| 1560 | # select_sample_%i=true must be included twice for each sample to simulate a CheckboxField checked setting. |
---|
| 1561 | for sample_id in sample_ids: |
---|
| 1562 | url += "&select_sample_%i=true&select_sample_%i=true" % ( sample_id, sample_id ) |
---|
| 1563 | url += "&sample_operation=Change%20state&refresh=true" |
---|
| 1564 | url += "&change_state_button=Save" |
---|
| 1565 | self.visit_url( url ) |
---|
| 1566 | self.check_page_for_string( 'Sequencing Request "%s"' % request_name ) |
---|
| 1567 | for sample_id, sample_name in zip( sample_ids, sample_names ): |
---|
| 1568 | self.visit_url( "%s/requests_common/sample_events?cntrller=requests_admin&sample_id=%s" % ( self.url, self.security.encode_id( sample_id ) ) ) |
---|
| 1569 | self.check_page_for_string( 'Events for Sample "%s"' % sample_name ) |
---|
| 1570 | self.check_page_for_string( new_state_name ) |
---|
| 1571 | def add_user_address( self, user_id, address_dict ): |
---|
| 1572 | self.home() |
---|
| 1573 | self.visit_url( "%s/user/new_address?admin_view=False&user_id=%i" % ( self.url, user_id ) ) |
---|
| 1574 | self.check_page_for_string( 'Add new address' ) |
---|
| 1575 | for field_name, value in address_dict.items(): |
---|
| 1576 | tc.fv( "1", field_name, value ) |
---|
| 1577 | tc.submit( "new_address_button" ) |
---|
| 1578 | self.check_page_for_string( 'Address (%s) has been added' % address_dict[ 'short_desc' ] ) |
---|
| 1579 | |
---|
| 1580 | # Library stuff |
---|
| 1581 | def add_library_template( self, cntrller, item_type, library_id, form_id, form_name, folder_id=None, ldda_id=None ): |
---|
| 1582 | """ |
---|
| 1583 | Add a new info template to a library item - the template will ALWAYS BE SET TO INHERITABLE here. If you want to |
---|
| 1584 | dis-inherit your template, call the manage_library_template_inheritance() below immediately after you call this |
---|
| 1585 | method in your test code. |
---|
| 1586 | """ |
---|
| 1587 | self.home() |
---|
| 1588 | if item_type == 'library': |
---|
| 1589 | url = "%s/library_common/add_template?cntrller=%s&item_type=%s&library_id=%s" % \ |
---|
| 1590 | ( self.url, cntrller, item_type, library_id ) |
---|
| 1591 | elif item_type == 'folder': |
---|
| 1592 | url = "%s/library_common/add_template?cntrller=%s&item_type=%s&library_id=%s&folder_id=%s" % \ |
---|
| 1593 | ( self.url, cntrller, item_type, library_id, folder_id ) |
---|
| 1594 | elif item_type == 'ldda': |
---|
| 1595 | url = "%s/library_common/add_template?cntrller=%s&item_type=%s&library_id=%s&folder_id=%s&ldda_id=%s" % \ |
---|
| 1596 | ( self.url, cntrller, item_type, library_id, folder_id, ldda_id ) |
---|
| 1597 | self.visit_url( url ) |
---|
| 1598 | self.check_page_for_string ( "Select a template for the" ) |
---|
| 1599 | self.refresh_form( "form_id", form_id ) |
---|
| 1600 | # For some unknown reason, twill barfs if the form number ( 1 ) is used in the following |
---|
| 1601 | # rather than the form anme ( select_template ), so we have to use the form name. |
---|
| 1602 | tc.fv( "select_template", "inheritable", '1' ) |
---|
| 1603 | tc.submit( "add_template_button" ) |
---|
| 1604 | self.check_page_for_string = 'A template based on the form "%s" has been added to this' % form_name |
---|
| 1605 | self.home() |
---|
| 1606 | def manage_library_template_inheritance( self, cntrller, item_type, library_id, folder_id=None, ldda_id=None, inheritable=True ): |
---|
| 1607 | # If inheritable is True, the item is currently inheritable. |
---|
| 1608 | self.home() |
---|
| 1609 | if item_type == 'library': |
---|
| 1610 | url = "%s/library_common/manage_template_inheritance?cntrller=%s&item_type=%s&library_id=%s" % \ |
---|
| 1611 | ( self.url, cntrller, item_type, library_id ) |
---|
| 1612 | elif item_type == 'folder': |
---|
| 1613 | url = "%s/library_common/manage_template_inheritance?cntrller=%s&item_type=%s&library_id=%s&folder_id=%s" % \ |
---|
| 1614 | ( self.url, cntrller, item_type, library_id, folder_id ) |
---|
| 1615 | elif item_type == 'ldda': |
---|
| 1616 | url = "%s/library_common/manage_template_inheritance?cntrller=%s&item_type=%s&library_id=%s&folder_id=%s&ldda_id=%s" % \ |
---|
| 1617 | ( self.url, cntrller, item_type, library_id, folder_id, ldda_id ) |
---|
| 1618 | self.visit_url( url ) |
---|
| 1619 | if inheritable: |
---|
| 1620 | self.check_page_for_string = 'will no longer be inherited to contained folders and datasets' |
---|
| 1621 | else: |
---|
| 1622 | self.check_page_for_string = 'will now be inherited to contained folders and datasets' |
---|
| 1623 | self.home() |
---|
| 1624 | def browse_libraries_admin( self, deleted=False, strings_displayed=[], strings_not_displayed=[] ): |
---|
| 1625 | self.visit_url( '%s/library_admin/browse_libraries?sort=name&f-description=All&f-name=All&f-deleted=%s' % ( self.url, str( deleted ) ) ) |
---|
| 1626 | for check_str in strings_displayed: |
---|
| 1627 | self.check_page_for_string( check_str ) |
---|
| 1628 | for check_str in strings_not_displayed: |
---|
| 1629 | try: |
---|
| 1630 | self.check_page_for_string( check_str ) |
---|
| 1631 | raise AssertionError, "String (%s) incorrectly displayed when browing library." % check_str |
---|
| 1632 | except: |
---|
| 1633 | pass |
---|
| 1634 | def browse_libraries_regular_user( self, strings_displayed=[], strings_not_displayed=[] ): |
---|
| 1635 | self.visit_url( '%s/library/browse_libraries' % self.url ) |
---|
| 1636 | for check_str in strings_displayed: |
---|
| 1637 | self.check_page_for_string( check_str ) |
---|
| 1638 | for check_str in strings_not_displayed: |
---|
| 1639 | try: |
---|
| 1640 | self.check_page_for_string( check_str ) |
---|
| 1641 | raise AssertionError, "String (%s) incorrectly displayed when browing library." % check_str |
---|
| 1642 | except: |
---|
| 1643 | pass |
---|
| 1644 | def browse_library( self, cntrller, id, show_deleted=False, strings_displayed=[], strings_not_displayed=[] ): |
---|
| 1645 | self.visit_url( '%s/library_common/browse_library?cntrller=%s&id=%s&show_deleted=%s' % ( self.url, cntrller, id, str( show_deleted ) ) ) |
---|
| 1646 | data=self.last_page() |
---|
| 1647 | file( 'greg.html', 'wb' ).write( data ) |
---|
| 1648 | for check_str in strings_displayed: |
---|
| 1649 | self.check_page_for_string( check_str ) |
---|
| 1650 | for check_str in strings_not_displayed: |
---|
| 1651 | try: |
---|
| 1652 | self.check_page_for_string( check_str ) |
---|
| 1653 | raise AssertionError, "String (%s) incorrectly displayed when browing library." % check_str |
---|
| 1654 | except: |
---|
| 1655 | pass |
---|
| 1656 | def create_library( self, name='Library One', description='This is Library One', synopsis='Synopsis for Library One' ): |
---|
| 1657 | """Create a new library""" |
---|
| 1658 | self.visit_url( "%s/library_admin/create_library" % self.url ) |
---|
| 1659 | self.check_page_for_string( 'Create a new data library' ) |
---|
| 1660 | tc.fv( "1", "name", name ) |
---|
| 1661 | tc.fv( "1", "description", description ) |
---|
| 1662 | tc.fv( "1", "synopsis", synopsis ) |
---|
| 1663 | tc.submit( "create_library_button" ) |
---|
| 1664 | check_str = "The new library named '%s' has been created" % name |
---|
| 1665 | self.check_page_for_string( check_str ) |
---|
| 1666 | self.home() |
---|
| 1667 | def edit_template( self, cntrller, item_type, library_id, field_type, field_name_1, field_helptext_1, field_default_1, |
---|
| 1668 | folder_id='', ldda_id='', action='add_field' ): |
---|
| 1669 | """Edit the form fields defining a library template""" |
---|
| 1670 | self.visit_url( "%s/library_common/edit_template?cntrller=%s&item_type=%s&library_id=%s" % \ |
---|
| 1671 | ( self.url, cntrller, item_type, library_id ) ) |
---|
| 1672 | self.check_page_for_string( "Edit form definition" ) |
---|
| 1673 | if action == 'add_field': |
---|
| 1674 | tc.submit( "add_field_button" ) |
---|
| 1675 | tc.fv( "edit_form", "field_name_1", field_name_1 ) |
---|
| 1676 | tc.fv( "edit_form", "field_helptext_1", field_helptext_1 ) |
---|
| 1677 | if field_type == 'SelectField': |
---|
| 1678 | # Performs a refresh_on_change in this case |
---|
| 1679 | self.refresh_form( "field_type_1", field_type ) |
---|
| 1680 | else: |
---|
| 1681 | tc.fv( "edit_form", "field_type_1", field_type ) |
---|
| 1682 | tc.fv( "edit_form", "field_default_1", field_default_1 ) |
---|
| 1683 | tc.submit( 'save_changes_button' ) |
---|
| 1684 | self.check_page_for_string( "The template for this data library has been updated with your changes." ) |
---|
| 1685 | def library_info( self, cntrller, library_id, library_name='', new_name='', new_description='', new_synopsis='', |
---|
| 1686 | template_fields=[], strings_displayed=[] ): |
---|
| 1687 | """Edit information about a library, optionally using an existing template with up to 2 elements""" |
---|
| 1688 | self.visit_url( "%s/library_common/library_info?cntrller=%s&id=%s" % ( self.url, cntrller, library_id ) ) |
---|
| 1689 | for check_str in strings_displayed: |
---|
| 1690 | self.check_page_for_string( check_str ) |
---|
| 1691 | if new_name and new_description and new_synopsis: |
---|
| 1692 | tc.fv( '1', 'name', new_name ) |
---|
| 1693 | tc.fv( '1', 'description', new_description ) |
---|
| 1694 | tc.fv( '1', 'synopsis', new_synopsis ) |
---|
| 1695 | tc.submit( 'library_info_button' ) |
---|
| 1696 | self.check_page_for_string( "Information updated for library" ) |
---|
| 1697 | if template_fields: |
---|
| 1698 | for field_name, field_value in template_fields: |
---|
| 1699 | # The 2nd form on the page contains the template, and the form is named edit_info. |
---|
| 1700 | # Set the template field value |
---|
| 1701 | tc.fv( "edit_info", field_name, field_value ) |
---|
| 1702 | tc.submit( 'edit_info_button' ) |
---|
| 1703 | self.home() |
---|
| 1704 | def library_permissions( self, library_id, library_name, role_ids_str, permissions_in, permissions_out, cntrller='library_admin' ): |
---|
| 1705 | # role_ids_str must be a comma-separated string of role ids |
---|
| 1706 | url = "library_common/library_permissions?id=%s&cntrller=%s&update_roles_button=Save" % ( library_id, cntrller ) |
---|
| 1707 | for po in permissions_out: |
---|
| 1708 | key = '%s_out' % po |
---|
| 1709 | url ="%s&%s=%s" % ( url, key, role_ids_str ) |
---|
| 1710 | for pi in permissions_in: |
---|
| 1711 | key = '%s_in' % pi |
---|
| 1712 | url ="%s&%s=%s" % ( url, key, role_ids_str ) |
---|
| 1713 | self.home() |
---|
| 1714 | self.visit_url( "%s/%s" % ( self.url, url ) ) |
---|
| 1715 | check_str = "Permissions updated for library '%s'." % library_name |
---|
| 1716 | self.check_page_for_string( check_str ) |
---|
| 1717 | self.home() |
---|
| 1718 | def make_library_item_public( self, library_id, id, cntrller='library_admin', item_type='library', |
---|
| 1719 | contents=False, library_name='', folder_name='', ldda_name='' ): |
---|
| 1720 | url = "%s/library_common/make_library_item_public?cntrller=%s&library_id=%s&item_type=%s&id=%s&contents=%s" % \ |
---|
| 1721 | ( self.url, cntrller, library_id, item_type, id, str( contents ) ) |
---|
| 1722 | self.visit_url( url ) |
---|
| 1723 | if item_type == 'library': |
---|
| 1724 | if contents: |
---|
| 1725 | check_str = "The data library (%s) and all it's contents have been made publicly accessible." % library_name |
---|
| 1726 | else: |
---|
| 1727 | check_str = "The data library (%s) has been made publicly accessible, but access to it's contents has been left unchanged." % library_name |
---|
| 1728 | elif item_type == 'folder': |
---|
| 1729 | check_str = "All of the contents of folder (%s) have been made publicly accessible." % folder_name |
---|
| 1730 | elif item_type == 'ldda': |
---|
| 1731 | check_str = "The libary dataset (%s) has been made publicly accessible." % ldda_name |
---|
| 1732 | self.check_page_for_string( check_str ) |
---|
| 1733 | |
---|
| 1734 | # Library folder stuff |
---|
| 1735 | def add_folder( self, cntrller, library_id, folder_id, name='Folder One', description='This is Folder One' ): |
---|
| 1736 | """Create a new folder""" |
---|
| 1737 | url = "%s/library_common/create_folder?cntrller=%s&library_id=%s&parent_id=%s" % ( self.url, cntrller, library_id, folder_id ) |
---|
| 1738 | self.visit_url( url ) |
---|
| 1739 | self.check_page_for_string( 'Create a new folder' ) |
---|
| 1740 | tc.fv( "1", "name", name ) |
---|
| 1741 | tc.fv( "1", "description", description ) |
---|
| 1742 | tc.submit( "new_folder_button" ) |
---|
| 1743 | check_str = "The new folder named '%s' has been added to the data library." % name |
---|
| 1744 | self.check_page_for_string( check_str ) |
---|
| 1745 | self.home() |
---|
| 1746 | def folder_info( self, cntrller, folder_id, library_id, name='', new_name='', description='', |
---|
| 1747 | template_refresh_field_contents='', template_fields=[], strings_displayed=[], strings_not_displayed=[], |
---|
| 1748 | strings_displayed_after_submit=[], strings_not_displayed_after_submit=[] ): |
---|
| 1749 | """Add information to a library using an existing template with 2 elements""" |
---|
| 1750 | self.visit_url( "%s/library_common/folder_info?cntrller=%s&id=%s&library_id=%s" % \ |
---|
| 1751 | ( self.url, cntrller, folder_id, library_id ) ) |
---|
| 1752 | if name and new_name and description: |
---|
| 1753 | tc.fv( '1', "name", new_name ) |
---|
| 1754 | tc.fv( '1', "description", description ) |
---|
| 1755 | tc.submit( 'rename_folder_button' ) |
---|
| 1756 | for check_str in strings_displayed: |
---|
| 1757 | self.check_page_for_string( check_str ) |
---|
| 1758 | for check_str in strings_not_displayed: |
---|
| 1759 | try: |
---|
| 1760 | self.check_page_for_string( check_str ) |
---|
| 1761 | raise AssertionError, "String (%s) incorrectly displayed." % check_str |
---|
| 1762 | except: |
---|
| 1763 | pass |
---|
| 1764 | if template_refresh_field_contents: |
---|
| 1765 | # A template containing an AddressField is displayed on the form, so we need to refresh the form |
---|
| 1766 | # with the received template_refresh_field_contents. There are 2 forms on the folder_info page |
---|
| 1767 | # when in edit mode, and the 2nd one is the one we want. |
---|
| 1768 | self.refresh_form( "field_0", template_refresh_field_contents, form_no=2 ) |
---|
| 1769 | if template_fields: |
---|
| 1770 | # We have an information template associated with the folder, so |
---|
| 1771 | # there are 2 forms on this page and the template is the 2nd form |
---|
| 1772 | for field_name, field_value in template_fields: |
---|
| 1773 | tc.fv( "edit_info", field_name, field_value ) |
---|
| 1774 | tc.submit( 'edit_info_button' ) |
---|
| 1775 | for check_str in strings_displayed_after_submit: |
---|
| 1776 | self.check_page_for_string( check_str ) |
---|
| 1777 | for check_str in strings_not_displayed_after_submit: |
---|
| 1778 | try: |
---|
| 1779 | self.check_page_for_string( check_str ) |
---|
| 1780 | raise AssertionError, "String (%s) incorrectly displayed." % check_str |
---|
| 1781 | except: |
---|
| 1782 | pass |
---|
| 1783 | self.home() |
---|
| 1784 | |
---|
| 1785 | # Library dataset stuff |
---|
| 1786 | def upload_library_dataset( self, cntrller, library_id, folder_id, filename='', server_dir='', replace_id='', |
---|
| 1787 | upload_option='upload_file', file_type='auto', dbkey='hg18', space_to_tab='', |
---|
| 1788 | link_data_only='', dont_preserve_dirs='', roles=[], ldda_message='', hda_ids='', |
---|
| 1789 | template_refresh_field_contents='', template_fields=[], show_deleted='False', strings_displayed=[] ): |
---|
| 1790 | """Add datasets to library using any upload_option""" |
---|
| 1791 | # NOTE: due to the library_wait() method call at the end of this method, no tests should be done |
---|
| 1792 | # for strings_displayed_after_submit. |
---|
| 1793 | url = "%s/library_common/upload_library_dataset?cntrller=%s&library_id=%s&folder_id=%s" % \ |
---|
| 1794 | ( self.url, cntrller, library_id, folder_id ) |
---|
| 1795 | if replace_id: |
---|
| 1796 | # If we're uploading a new version of a library dataset, we have to include the replace_id param in the |
---|
| 1797 | # request because the form field named replace_id will not be displayed on the upload form if we dont. |
---|
| 1798 | url += "&replace_id=%s" % replace_id |
---|
| 1799 | self.visit_url( url ) |
---|
| 1800 | if template_refresh_field_contents: |
---|
| 1801 | # A template containing an AddressField is displayed on the upload form, so we need to refresh the form |
---|
| 1802 | # with the received template_refresh_field_contents. |
---|
| 1803 | self.refresh_form( "field_0", template_refresh_field_contents ) |
---|
| 1804 | for tup in template_fields: |
---|
| 1805 | tc.fv( "1", tup[0], tup[1] ) |
---|
| 1806 | tc.fv( "1", "library_id", library_id ) |
---|
| 1807 | tc.fv( "1", "folder_id", folder_id ) |
---|
| 1808 | tc.fv( "1", "show_deleted", show_deleted ) |
---|
| 1809 | tc.fv( "1", "ldda_message", ldda_message ) |
---|
| 1810 | tc.fv( "1", "file_type", file_type ) |
---|
| 1811 | tc.fv( "1", "dbkey", dbkey ) |
---|
| 1812 | if space_to_tab: |
---|
| 1813 | tc.fv( "1", "space_to_tab", space_to_tab ) |
---|
| 1814 | if link_data_only: |
---|
| 1815 | tc.fv( "1", "link_data_only", link_data_only ) |
---|
| 1816 | if dont_preserve_dirs: |
---|
| 1817 | tc.fv( "1", "dont_preserve_dirs", dont_preserve_dirs ) |
---|
| 1818 | for role_id in roles: |
---|
| 1819 | tc.fv( "1", "roles", role_id ) |
---|
| 1820 | # Refresh the form by selecting the upload_option - we do this here to ensure |
---|
| 1821 | # all previously entered form contents are retained. |
---|
| 1822 | self.refresh_form( 'upload_option', upload_option ) |
---|
| 1823 | if upload_option == 'import_from_history': |
---|
| 1824 | for check_str in strings_displayed: |
---|
| 1825 | self.check_page_for_string( check_str ) |
---|
| 1826 | if hda_ids: |
---|
| 1827 | # Twill cannot handle multi-checkboxes, so the form can only have 1 hda_ids checkbox |
---|
| 1828 | try: |
---|
| 1829 | tc.fv( "add_history_datasets_to_library", "hda_ids", hda_ids ) |
---|
| 1830 | except: |
---|
| 1831 | tc.fv( "add_history_datasets_to_library", "hda_ids", '1' ) |
---|
| 1832 | tc.submit( 'add_history_datasets_to_library_button' ) |
---|
| 1833 | else: |
---|
| 1834 | if filename: |
---|
| 1835 | filename = self.get_filename( filename ) |
---|
| 1836 | tc.formfile( "1", "files_0|file_data", filename ) |
---|
| 1837 | elif server_dir: |
---|
| 1838 | tc.fv( "1", "server_dir", server_dir ) |
---|
| 1839 | for check_str in strings_displayed: |
---|
| 1840 | self.check_page_for_string( check_str ) |
---|
| 1841 | tc.submit( "runtool_btn" ) |
---|
| 1842 | # Give the files some time to finish uploading |
---|
| 1843 | self.library_wait( library_id ) |
---|
| 1844 | data = self.last_page() |
---|
| 1845 | file( 'greg1.html', 'wb' ).write( data ) |
---|
| 1846 | self.home() |
---|
| 1847 | def ldda_permissions( self, cntrller, library_id, folder_id, id, role_ids_str, |
---|
| 1848 | permissions_in=[], permissions_out=[], strings_displayed=[], ldda_name='' ): |
---|
| 1849 | # role_ids_str must be a comma-separated string of role ids |
---|
| 1850 | url = "%s/library_common/ldda_permissions?cntrller=%s&library_id=%s&folder_id=%s&id=%s" % \ |
---|
| 1851 | ( self.url, cntrller, library_id, folder_id, id ) |
---|
| 1852 | for po in permissions_out: |
---|
| 1853 | key = '%s_out' % po |
---|
| 1854 | url ="%s&%s=%s" % ( url, key, role_ids_str ) |
---|
| 1855 | for pi in permissions_in: |
---|
| 1856 | key = '%s_in' % pi |
---|
| 1857 | url ="%s&%s=%s" % ( url, key, role_ids_str ) |
---|
| 1858 | if permissions_in or permissions_out: |
---|
| 1859 | url += "&update_roles_button=Save" |
---|
| 1860 | self.visit_url( url ) |
---|
| 1861 | if not strings_displayed: |
---|
| 1862 | strings_displayed = [ "Permissions updated for dataset '%s'." % ldda_name ] |
---|
| 1863 | for check_str in strings_displayed: |
---|
| 1864 | self.check_page_for_string( check_str ) |
---|
| 1865 | self.home() |
---|
| 1866 | def ldda_edit_info( self, cntrller, library_id, folder_id, ldda_id, ldda_name, new_ldda_name='', |
---|
| 1867 | template_refresh_field_contents='', template_fields=[], strings_displayed=[], strings_not_displayed=[] ): |
---|
| 1868 | """Edit library_dataset_dataset_association information, optionally template element information""" |
---|
| 1869 | self.visit_url( "%s/library_common/ldda_edit_info?cntrller=%s&library_id=%s&folder_id=%s&id=%s" % \ |
---|
| 1870 | ( self.url, cntrller, library_id, folder_id, ldda_id ) ) |
---|
| 1871 | check_str = 'Edit attributes of %s' % ldda_name |
---|
| 1872 | self.check_page_for_string( check_str ) |
---|
| 1873 | if new_ldda_name: |
---|
| 1874 | tc.fv( '1', 'name', new_ldda_name ) |
---|
| 1875 | tc.submit( 'save' ) |
---|
| 1876 | check_str = "Attributes updated for library dataset '%s'." % new_ldda_name |
---|
| 1877 | self.check_page_for_string( check_str ) |
---|
| 1878 | if template_refresh_field_contents: |
---|
| 1879 | # A template containing an AddressField is displayed on the upload form, so we need to refresh the form |
---|
| 1880 | # with the received template_refresh_field_contents. There are 4 forms on this page, and the template is |
---|
| 1881 | # contained in the 4th form named "edit_info". |
---|
| 1882 | self.refresh_form( "field_0", template_refresh_field_contents, form_no=4 ) |
---|
| 1883 | if template_fields: |
---|
| 1884 | # We have an information template associated with the folder, so |
---|
| 1885 | # there are 2 forms on this page and the template is the 2nd form |
---|
| 1886 | for field_name, field_value in template_fields: |
---|
| 1887 | tc.fv( "edit_info", field_name, field_value ) |
---|
| 1888 | tc.submit( 'edit_info_button' ) |
---|
| 1889 | for check_str in strings_displayed: |
---|
| 1890 | self.check_page_for_string( check_str ) |
---|
| 1891 | for check_str in strings_not_displayed: |
---|
| 1892 | try: |
---|
| 1893 | self.check_page_for_string( check_str ) |
---|
| 1894 | raise AssertionError, "String (%s) should not have been displayed on ldda Edit Attributes page." % check_str |
---|
| 1895 | except: |
---|
| 1896 | pass |
---|
| 1897 | self.home() |
---|
| 1898 | def act_on_multiple_datasets( self, cntrller, library_id, do_action, ldda_ids='', strings_displayed=[] ): |
---|
| 1899 | # Can't use the ~/library_admin/libraries form as twill barfs on it so we'll simulate the form submission |
---|
| 1900 | # by going directly to the form action |
---|
| 1901 | self.visit_url( '%s/library_common/act_on_multiple_datasets?cntrller=%s&library_id=%s&ldda_ids=%s&do_action=%s' \ |
---|
| 1902 | % ( self.url, cntrller, library_id, ldda_ids, do_action ) ) |
---|
| 1903 | for check_str in strings_displayed: |
---|
| 1904 | self.check_page_for_string( check_str ) |
---|
| 1905 | def download_archive_of_library_files( self, cntrller, library_id, ldda_ids, format ): |
---|
| 1906 | self.home() |
---|
| 1907 | # Here it would be ideal to have twill set form values and submit the form, but |
---|
| 1908 | # twill barfs on that due to the recently introduced page wrappers around the contents |
---|
| 1909 | # of the browse_library.mako template which enable panel layout when visiting the |
---|
| 1910 | # page from an external URL. By "barfs", I mean that twill somehow loses hod on the |
---|
| 1911 | # cntrller param. We'll just simulate the form submission by building the URL manually. |
---|
| 1912 | # Here's the old, better approach... |
---|
| 1913 | #self.visit_url( "%s/library_common/browse_library?cntrller=%s&id=%s" % ( self.url, cntrller, library_id ) ) |
---|
| 1914 | #for ldda_id in ldda_ids: |
---|
| 1915 | # tc.fv( "1", "ldda_ids", ldda_id ) |
---|
| 1916 | #tc.fv( "1", "do_action", format ) |
---|
| 1917 | #tc.submit( "action_on_datasets_button" ) |
---|
| 1918 | # Here's the new approach... |
---|
| 1919 | url = "%s/library_common/act_on_multiple_datasets?cntrller=%s&library_id=%s&do_action=%s" % ( self.url, cntrller, library_id, format ) |
---|
| 1920 | for ldda_id in ldda_ids: |
---|
| 1921 | url += "&ldda_ids=%s" % ldda_id |
---|
| 1922 | self.visit_url( url ) |
---|
| 1923 | tc.code( 200 ) |
---|
| 1924 | archive = self.write_temp_file( self.last_page(), suffix='.' + format ) |
---|
| 1925 | self.home() |
---|
| 1926 | return archive |
---|
| 1927 | def check_archive_contents( self, archive, lddas ): |
---|
| 1928 | def get_ldda_path( ldda ): |
---|
| 1929 | path = "" |
---|
| 1930 | parent_folder = ldda.library_dataset.folder |
---|
| 1931 | while parent_folder is not None: |
---|
| 1932 | if parent_folder.parent is None: |
---|
| 1933 | path = os.path.join( parent_folder.library_root[0].name, path ) |
---|
| 1934 | break |
---|
| 1935 | path = os.path.join( parent_folder.name, path ) |
---|
| 1936 | parent_folder = parent_folder.parent |
---|
| 1937 | path += ldda.name |
---|
| 1938 | return path |
---|
| 1939 | def mkdir( file ): |
---|
| 1940 | dir = os.path.join( tmpd, os.path.dirname( file ) ) |
---|
| 1941 | if not os.path.exists( dir ): |
---|
| 1942 | os.makedirs( dir ) |
---|
| 1943 | tmpd = tempfile.mkdtemp() |
---|
| 1944 | if tarfile.is_tarfile( archive ): |
---|
| 1945 | t = tarfile.open( archive ) |
---|
| 1946 | for n in t.getnames(): |
---|
| 1947 | mkdir( n ) |
---|
| 1948 | t.extract( n, tmpd ) |
---|
| 1949 | t.close() |
---|
| 1950 | elif zipfile.is_zipfile( archive ): |
---|
| 1951 | z = zipfile.ZipFile( archive, 'r' ) |
---|
| 1952 | for n in z.namelist(): |
---|
| 1953 | mkdir( n ) |
---|
| 1954 | open( os.path.join( tmpd, n ), 'wb' ).write( z.read( n ) ) |
---|
| 1955 | z.close() |
---|
| 1956 | else: |
---|
| 1957 | raise Exception( 'Unable to read archive: %s' % archive ) |
---|
| 1958 | for ldda in lddas: |
---|
| 1959 | orig_file = self.get_filename( ldda.name ) |
---|
| 1960 | downloaded_file = os.path.join( tmpd, get_ldda_path( ldda ) ) |
---|
| 1961 | assert os.path.exists( downloaded_file ) |
---|
| 1962 | try: |
---|
| 1963 | self.files_diff( orig_file, downloaded_file ) |
---|
| 1964 | except AssertionError, err: |
---|
| 1965 | errmsg = 'Library item %s different than expected, difference:\n' % ldda.name |
---|
| 1966 | errmsg += str( err ) |
---|
| 1967 | errmsg += 'Unpacked archive remains in: %s\n' % tmpd |
---|
| 1968 | raise AssertionError( errmsg ) |
---|
| 1969 | shutil.rmtree( tmpd ) |
---|
| 1970 | def delete_library_item( self, cntrller, library_id, item_id, item_name, item_type='library_dataset' ): |
---|
| 1971 | """Mark a library item as deleted""" |
---|
| 1972 | self.home() |
---|
| 1973 | self.visit_url( "%s/library_common/delete_library_item?cntrller=%s&library_id=%s&item_id=%s&item_type=%s" \ |
---|
| 1974 | % ( self.url, cntrller, library_id, item_id, item_type ) ) |
---|
| 1975 | if item_type == 'library_dataset': |
---|
| 1976 | item_desc = 'Dataset' |
---|
| 1977 | else: |
---|
| 1978 | item_desc = item_type.capitalize() |
---|
| 1979 | check_str = "%s '%s' has been marked deleted" % ( item_desc, item_name ) |
---|
| 1980 | self.check_page_for_string( check_str ) |
---|
| 1981 | self.home() |
---|
| 1982 | def undelete_library_item( self, cntrller, library_id, item_id, item_name, item_type='library_dataset' ): |
---|
| 1983 | """Mark a library item as deleted""" |
---|
| 1984 | self.home() |
---|
| 1985 | self.visit_url( "%s/library_common/undelete_library_item?cntrller=%s&library_id=%s&item_id=%s&item_type=%s" \ |
---|
| 1986 | % ( self.url, cntrller, library_id, item_id, item_type ) ) |
---|
| 1987 | if item_type == 'library_dataset': |
---|
| 1988 | item_desc = 'Dataset' |
---|
| 1989 | else: |
---|
| 1990 | item_desc = item_type.capitalize() |
---|
| 1991 | check_str = "%s '%s' has been marked undeleted" % ( item_desc, item_name ) |
---|
| 1992 | self.check_page_for_string( check_str ) |
---|
| 1993 | self.home() |
---|
| 1994 | def purge_library( self, library_id, library_name ): |
---|
| 1995 | """Purge a library""" |
---|
| 1996 | self.home() |
---|
| 1997 | self.visit_url( "%s/library_admin/purge_library?id=%s" % ( self.url, library_id ) ) |
---|
| 1998 | check_str = "Library '%s' and all of its contents have been purged" % library_name |
---|
| 1999 | self.check_page_for_string( check_str ) |
---|
| 2000 | self.home() |
---|
| 2001 | def library_wait( self, library_id, cntrller='library_admin', maxiter=90 ): |
---|
| 2002 | """Waits for the tools to finish""" |
---|
| 2003 | count = 0 |
---|
| 2004 | sleep_amount = 1 |
---|
| 2005 | while count < maxiter: |
---|
| 2006 | count += 1 |
---|
| 2007 | self.visit_url( "%s/library_common/browse_library?cntrller=%s&id=%s" % ( self.url, cntrller, library_id ) ) |
---|
| 2008 | page = tc.browser.get_html() |
---|
| 2009 | if page.find( '<!-- running: do not change this comment, used by TwillTestCase.library_wait -->' ) > -1: |
---|
| 2010 | time.sleep( sleep_amount ) |
---|
| 2011 | sleep_amount += 1 |
---|
| 2012 | else: |
---|
| 2013 | break |
---|
| 2014 | self.assertNotEqual(count, maxiter) |
---|
| 2015 | |
---|
| 2016 | # Tests associated with tags |
---|
| 2017 | def add_tag( self, item_id, item_class, context, new_tag ): |
---|
| 2018 | self.visit_url( "%s/tag/add_tag_async?item_id=%s&item_class=%s&context=%s&new_tag=%s" % \ |
---|
| 2019 | ( self.url, item_id, item_class, context, new_tag ) ) |
---|