PageRenderTime 86ms CodeModel.GetById 25ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/test/functional/test_sample_tracking.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 940 lines | 810 code | 3 blank | 127 comment | 64 complexity | ec79c219450da84e920eceab0d3d8203 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1import galaxy.model
  2from galaxy.model.orm import *
  3from base.twilltestcase import *
  4from base.test_db_util import *
  5
  6
  7# TODO: Functional tests start failing at 025, fix or eliminate rest of tests.
  8class TestFormsAndSampleTracking( TwillTestCase ):
  9    # ====== Setup Users, Groups & Roles required for this test suite ========= 
 10    def test_000_initiate_users( self ):
 11        """Ensuring all required user accounts exist"""
 12        self.logout()
 13        self.login( email='test1@bx.psu.edu', username='regular-user1' )
 14        global regular_user1
 15        regular_user1 = get_user( 'test1@bx.psu.edu' )
 16        assert regular_user1 is not None, 'Problem retrieving user with email "test1@bx.psu.edu" from the database'
 17        global regular_user1_private_role
 18        regular_user1_private_role = get_private_role( regular_user1 )
 19        self.logout()
 20        self.login( email='test2@bx.psu.edu', username='regular-user2' )
 21        global regular_user2
 22        regular_user2 = get_user( 'test2@bx.psu.edu' )
 23        assert regular_user2 is not None, 'Problem retrieving user with email "test2@bx.psu.edu" from the database'
 24        global regular_user2_private_role
 25        regular_user2_private_role = get_private_role( regular_user2 )
 26        self.logout()
 27        self.login( email='test3@bx.psu.edu', username='regular-user3' )
 28        global regular_user3
 29        regular_user3 = get_user( 'test3@bx.psu.edu' )
 30        assert regular_user3 is not None, 'Problem retrieving user with email "test3@bx.psu.edu" from the database'
 31        global regular_user3_private_role
 32        regular_user3_private_role = get_private_role( regular_user3 )
 33        self.logout()
 34        self.login( email='test@bx.psu.edu', username='admin-user' )
 35        global admin_user
 36        admin_user = get_user( 'test@bx.psu.edu' )
 37        assert admin_user is not None, 'Problem retrieving user with email "test@bx.psu.edu" from the database'
 38        global admin_user_private_role
 39        admin_user_private_role = get_private_role( admin_user )
 40    def test_005_create_required_groups_and_roles( self ):
 41        """Testing creating all required groups and roles for this script"""
 42        # Logged in as admin_user
 43        # Create role1
 44        name = 'Role1'
 45        description = "This is Role1's description"
 46        user_ids = [ str( admin_user.id ), str( regular_user1.id ), str( regular_user3.id ) ]
 47        self.create_role( name=name,
 48                          description=description,
 49                          in_user_ids=user_ids,
 50                          in_group_ids=[],
 51                          create_group_for_role='no',
 52                          private_role=admin_user.email )
 53        # Get the role object for later tests
 54        global role1
 55        role1 = get_role_by_name( name )
 56        # Create group1
 57        name = 'Group1'
 58        self.create_group( name=name, in_user_ids=[ str( regular_user1.id ) ], in_role_ids=[ str( role1.id ) ] )
 59        # Get the group object for later tests
 60        global group1
 61        group1 = get_group_by_name( name )
 62        assert group1 is not None, 'Problem retrieving group named "Group1" from the database'
 63        # NOTE: To get this to work with twill, all select lists on the ~/admin/role page must contain at least
 64        # 1 option value or twill throws an exception, which is: ParseError: OPTION outside of SELECT
 65        # Due to this bug in twill, we create the role, we bypass the page and visit the URL in the
 66        # associate_users_and_groups_with_role() method.
 67        #
 68        #create role2
 69        name = 'Role2'
 70        description = 'This is Role2'
 71        user_ids = [ str( admin_user.id ) ]
 72        group_ids = [ str( group1.id ) ]
 73        private_role = admin_user.email
 74        self.create_role( name=name,
 75                          description=description,
 76                          in_user_ids=user_ids,
 77                          in_group_ids=group_ids,
 78                          private_role=private_role )
 79        # Get the role object for later tests
 80        global role2
 81        role2 = get_role_by_name( name )
 82        assert role2 is not None, 'Problem retrieving role named "Role2" from the database'
 83    def test_006_create_library_and_folder( self ):
 84        """Testing creating the target data library and folder"""
 85        # Logged in as admin_user
 86        for index in range( 0, 2 ):
 87            name = 'library%s' % str( index + 1 )
 88            description = '%s description' % name
 89            synopsis = '%s synopsis' % name
 90            self.create_library( name=name, description=description, synopsis=synopsis )
 91        # Get the libraries for later use
 92        global library1
 93        library1 = get_library( 'library1', 'library1 description', 'library1 synopsis' )
 94        assert library1 is not None, 'Problem retrieving library (library1) from the database'
 95        global library2
 96        library2 = get_library( 'library2', 'library2 description', 'library2 synopsis' )
 97        assert library2 is not None, 'Problem retrieving library (library2) from the database'
 98        # setup add_library_item permission to regular_user1
 99        # Set permissions on the library, sort for later testing.
100        permissions_in = [ 'LIBRARY_ACCESS' ]
101        permissions_out = []
102        # Role1 members are: admin_user, regular_user1, regular_user3.  
103        # Each of these users will be permitted for LIBRARY_ACCESS, LIBRARY_ADD on 
104        # library1 and library2.
105        for library in [ library1, library2 ]:
106            self.library_permissions( self.security.encode_id( library.id ),
107                                      library.name,
108                                      str( role1.id ),
109                                      permissions_in,
110                                      permissions_out )
111        # adding a folder
112        for library in [ library1, library2 ]:
113            name = "%s_folder1" % library.name
114            description = "%s description" % name
115            self.add_folder( 'library_admin',
116                             self.security.encode_id( library.id ),
117                             self.security.encode_id( library.root_folder.id ),
118                             name=name,
119                             description=description )
120        global library1_folder1
121        library1_folder1 = get_folder( library1.root_folder.id, 'library1_folder1', 'library1_folder1 description' )
122        assert library1_folder1 is not None, 'Problem retrieving library folder named "library1_folder1" from the database'
123        global library2_folder1
124        library2_folder1 = get_folder( library2.root_folder.id, 'library2_folder1', 'library2_folder1 description' )
125        assert library2_folder1 is not None, 'Problem retrieving library folder named "library2_folder1" from the database'
126        # add folders 4 levels deep to library1_folder1
127        # level 2
128        name = "%s_folder2" % library2.name
129        description = "%s description" % name
130        self.add_folder( 'library_admin',
131                         self.security.encode_id( library2.id ),
132                         self.security.encode_id( library2_folder1.id ),
133                         name=name,
134                         description=description )
135        global library2_folder2
136        library2_folder2 = get_folder( library2_folder1.id, name, description )
137        assert library2_folder2 is not None, 'Problem retrieving library folder named "%s" from the database' % name
138        # level 3
139        name = "%s_folder3" % library2.name
140        description = "%s description" % name
141        self.add_folder( 'library_admin',
142                         self.security.encode_id( library2.id ),
143                         self.security.encode_id( library2_folder2.id ),
144                         name=name,
145                         description=description )
146        global library2_folder3
147        library2_folder3 = get_folder( library2_folder2.id, name, description )
148        assert library2_folder3 is not None, 'Problem retrieving library folder named "%s" from the database' % name
149        # level 4
150        name = "%s_folder4" % library2.name
151        description = "%s description" % name
152        self.add_folder( 'library_admin',
153                         self.security.encode_id( library2.id ),
154                         self.security.encode_id( library2_folder3.id ),
155                         name=name,
156                         description=description )
157        global library2_folder4
158        library2_folder4 = get_folder( library2_folder3.id, name, description )
159        assert library2_folder4 is not None, 'Problem retrieving library folder named "%s" from the database' % name
160        
161    #
162    # ====== Form definition test methods ================================================ 
163    #
164    def test_010_create_request_form_definition( self ):
165        """Testing creating a sequencing request form definition, editing the name and description and adding fields"""
166        # Logged in as admin_user
167        # Create a form definition
168        tmp_name = "Temp form"
169        tmp_desc = "Temp form description"
170        form_type = galaxy.model.FormDefinition.types.REQUEST
171        self.create_form( name=tmp_name,
172                          description=tmp_desc,
173                          form_type=form_type,
174                          num_fields=0,
175                          field_name='1_field_name',
176                          strings_displayed=[ 'Create a new form definition' ],
177                          strings_displayed_after_submit=[ tmp_name, tmp_desc, form_type ] )
178        tmp_form = get_form( tmp_name )
179        # Edit the name and description of the form definition, and add 3 fields.
180        new_name = "Request Form"
181        new_desc = "Request Form description"
182        # labels
183        global request_field_label1
184        request_field_label1 = 'Request form field1'
185        global request_field_label2
186        request_field_label2 = 'Request form field2'
187        global request_field_label3
188        request_field_label3 = 'Request form field3'
189        # field names
190        global request_form_field_name1
191        request_form_field_name1 = 'request_form_field1'
192        global request_form_field_name2
193        request_form_field_name2 = 'request_form_field2'
194        global request_form_field_name3
195        request_form_field_name3 = 'request_form_field3'
196        field_dicts = [ dict( label=request_field_label1,
197                              desc='Description of '+request_field_label1,
198                              type='SelectField',
199                              required='optional',
200                              selectlist=[ 'option1', 'option2' ],
201                              name=request_form_field_name1 ),
202                        dict( label=request_field_label2,
203                              desc='Description of '+request_field_label2,
204                              type='AddressField',
205                              required='optional',
206                              name=request_form_field_name2 ),
207                        dict( label=request_field_label3,
208                              desc='Description of '+request_field_label3,
209                              type='TextField',
210                              required='required',
211                              name=request_form_field_name3 ) ]
212        self.edit_form( id=self.security.encode_id( tmp_form.current.id ),
213                        new_form_name=new_name,
214                        new_form_desc=new_desc,
215                        field_dicts=field_dicts,
216                        field_index=len( tmp_form.fields ),
217                        strings_displayed=[ 'Edit form definition "%s"' % tmp_name ],
218                        strings_displayed_after_submit=[ "The form '%s' has been updated with the changes." % new_name ] )
219        # Get the form_definition object for later tests
220        global request_form_definition1
221        request_form_definition1 = get_form( new_name )
222        assert request_form_definition1 is not None, 'Problem retrieving form named "%s" from the database' % new_name
223        assert len( request_form_definition1.fields ) == len( tmp_form.fields ) + len( field_dicts )
224        # check form view
225        self.view_form( id=self.security.encode_id( request_form_definition1.current.id ),
226                        form_name=new_name,
227                        form_desc=new_desc,
228                        form_type=form_type,
229                        field_dicts=field_dicts )
230    def test_015_create_sample_form_definition( self ):
231        """Testing creating sequencing sample form definition and adding fields"""
232        name = "Sample Form"
233        desc = "This is Sample Form's description"
234        form_type = galaxy.model.FormDefinition.types.SAMPLE
235        global sample_form_layout_grid_name
236        sample_form_layout_grid_name = 'Layout Grid1'
237        self.create_form( name=name,
238                          description=desc,
239                          form_type=form_type,
240                          form_layout_name=sample_form_layout_grid_name,
241                          num_fields=0,
242                          field_name='1_field_name',
243                          strings_displayed=[ 'Create a new form definition' ],
244                          strings_displayed_after_submit=[ "The form '%s' has been updated with the changes." % name ] )
245        tmp_form = get_form( name )
246        # now add fields to the sample form definition
247        global sample_field_label1
248        sample_field_label1 = 'Sample form field1'
249        global sample_field_label2
250        sample_field_label2 = 'Sample form field2'
251        global sample_field_label3
252        sample_field_label3 = 'Sample form field3'
253        field_dicts = [ dict( label=sample_field_label1,
254                              desc='Description of '+sample_field_label1,
255                              type='SelectField',
256                              required='optional',
257                              selectlist=[ 'option1', 'option2' ],
258                              name='sample_form_field1' ),
259                        dict( label=sample_field_label2,
260                              desc='Description of '+sample_field_label2,
261                              type='TextField',
262                              required='optional',
263                              name='sample_form_field2' ),
264                        dict( label=sample_field_label3,
265                              desc='Description of '+sample_field_label3,
266                              type='TextField',
267                              required='required',
268                              name='sample_form_field3' ) ]
269        self.edit_form( id=self.security.encode_id( tmp_form.current.id ),
270                        new_form_name=name,
271                        new_form_desc=desc,
272                        field_dicts=field_dicts,
273                        field_index=len( tmp_form.fields ),
274                        strings_displayed=[ 'Edit form definition "%s"' % name ],
275                        strings_displayed_after_submit=[ "The form '%s' has been updated with the changes." % name ] )
276        global sample_form_definition1
277        sample_form_definition1 = get_form( name )
278        assert sample_form_definition1 is not None, "Error retrieving form %s from db" % name
279        assert len( sample_form_definition1.fields ) == len( field_dicts )
280        # check form view
281        self.view_form( id=self.security.encode_id( sample_form_definition1.current.id ),
282                        form_name=name,
283                        form_desc=desc,
284                        form_type=form_type,
285                        form_layout_name=sample_form_layout_grid_name,
286                        field_dicts=field_dicts )
287    def test_020_create_request_type( self ):
288        """Testing creating a request_type"""
289        name = 'Request type1'
290        sample_states = [  ( 'New', 'Sample entered into the system' ), 
291                           ( 'Received', 'Sample tube received' ),
292                           ( 'Library Started', 'Sample library preparation' ), 
293                           ( 'Run Started', 'Sequence run in progress' ), 
294                           ( 'Done', 'Sequence run complete' ) ]
295        self.create_request_type( name,
296                                  name+" description",
297                                  self.security.encode_id( request_form_definition1.id ),
298                                  self.security.encode_id( sample_form_definition1.id ),
299                                  sample_states,
300                                  strings_displayed=[ 'Create a new request type' ],
301                                  strings_displayed_after_submit=[ "The request type has been created." ] )
302        global request_type1
303        request_type1 = get_request_type_by_name( name )
304        assert request_type1 is not None, 'Problem retrieving request type named "%s" from the database' % name
305        # check view
306        self.view_request_type( self.security.encode_id( request_type1.id ),
307                                request_type1.name,
308                                strings_displayed=[ request_form_definition1.name,
309                                                    sample_form_definition1.name ],
310                                sample_states=sample_states)
311        # Set permissions
312        permissions_in = [ k for k, v in galaxy.model.RequestType.permitted_actions.items() ]
313        permissions_out = []
314        # Role1 members are: admin_user, regular_user1, regular_user3.  Each of these users will be permitted for
315        # REQUEST_TYPE_ACCESS on this request_type
316        self.request_type_permissions( self.security.encode_id( request_type1.id ),
317                                       request_type1.name,
318                                       str( role1.id ),
319                                       permissions_in,
320                                       permissions_out )
321        # Make sure the request_type1 is not accessible by regular_user2 since regular_user2 does not have Role1.
322        self.logout()
323        self.login( email=regular_user2.email )
324        self.visit_url( '%s/requests_common/create_request?cntrller=requests&request_type=True' % self.url )
325        try:
326            self.check_page_for_string( 'There are no request types created for a new request.' )
327            raise AssertionError, 'The request_type %s is accessible by %s when it should be restricted' % ( request_type1.name, regular_user2.email )
328        except:
329            pass
330        self.logout()
331        self.login( email=admin_user.email )
332    # ====== Sequencing request test methods - regular user perspective ================ 
333    def test_025_create_request( self ):
334        """Testing creating a sequencing request as a regular user"""
335        # logged in as admin_user
336        # Create a user_address
337        self.logout()
338        self.login( email=regular_user1.email )
339        # add new address for this user
340        address_dict = dict( short_desc="Office",
341                             name="James Bond",
342                             institution="MI6" ,
343                             address="MI6 Headquarters",
344                             city="London",
345                             state="London",
346                             postal_code="007",
347                             country="United Kingdom",
348                             phone="007-007-0007" )
349        self.add_user_address( self.security.encode_id( regular_user1.id ), address_dict )
350        global user_address1
351        user_address1 = get_user_address( regular_user1, address_dict[ 'short_desc' ] )
352        # Set field values - the tuples in the field_values list include the field_value, and True if refresh_on_change
353        # is required for that field.
354        field_value_tuples = [ ( request_form_field_name1, 'option1', False ), 
355                               ( request_form_field_name2, ( str( user_address1.id ), str( user_address1.id ) ), True ), 
356                               ( request_form_field_name3, 'field3 value', False ) ] 
357        # Create the request
358        name = 'Request1'
359        desc = 'Request1 Description'
360        self.create_request( cntrller='requests',
361                             request_type_id=self.security.encode_id( request_type1.id ),
362                             name=name,
363                             desc=desc,
364                             field_value_tuples=field_value_tuples,
365                             strings_displayed=[ 'Create a new sequencing request',
366                                                 request_field_label1,
367                                                 request_field_label2,
368                                                 request_field_label3 ],
369                             strings_displayed_after_submit=[ 'The sequencing request has been created.',
370                                                              name, desc ] )
371        global request1
372        request1 = get_request_by_name( name )        
373        # Make sure the request's state is now set to NEW
374        assert request1.state is not request1.states.NEW, "The state of the request '%s' should be set to '%s'" \
375            % ( request1.name, request1.states.NEW )
376        # check request page
377        self.view_request( cntrller='requests',
378                           request_id=self.security.encode_id( request1.id ),
379                           strings_displayed=[ 'Sequencing request "%s"' % request1.name,
380                                               'There are no samples.' ],
381                           strings_not_displayed=[ request1.states.SUBMITTED,
382                                                   request1.states.COMPLETE,
383                                                   request1.states.REJECTED,
384                                                   'Submit request' ] ) # this button should NOT show up as there are no samples yet
385        # check if the request is showing in the 'new' filter
386        self.check_request_grid( cntrller='requests',
387                                 state=request1.states.NEW,
388                                 strings_displayed=[ request1.name ] )
389        self.view_request_history( cntrller='requests',
390                                   request_id=self.security.encode_id( request1.id ),
391                                   strings_displayed=[ 'History of sequencing request "%s"' % request1.name,
392                                                       request1.states.NEW,
393                                                       'Sequencing request created' ],
394                                   strings_not_displayed=[ request1.states.SUBMITTED,
395                                                           request1.states.COMPLETE,
396                                                           request1.states.REJECTED ] )
397    def test_030_edit_basic_request_info( self ):
398        """Testing editing the basic information and email settings of a sequencing request"""
399        # logged in as regular_user1
400        fields = [ ( request_form_field_name1, 'option2' ), 
401                   ( request_form_field_name2, str( user_address1.id ) ), 
402                   ( request_form_field_name3, 'field3 value (edited)' ) ]
403        new_name=request1.name + ' (Renamed)'
404        new_desc=request1.desc + ' (Re-described)'
405        self.edit_basic_request_info( request_id=self.security.encode_id( request1.id ),
406                                      cntrller='requests',
407                                      name=request1.name,
408                                      new_name=new_name, 
409                                      new_desc=new_desc,
410                                      new_fields=fields,
411                                      strings_displayed=[ 'Edit sequencing request "%s"' % request1.name ],
412                                      strings_displayed_after_submit=[ new_name, new_desc ] )
413        refresh( request1 )
414        # define the sample states when we want an email notification
415        global email_notification_sample_states
416        email_notification_sample_states = [ request1.type.states[2], request1.type.states[4] ] 
417        # check email notification settings
418        check_sample_states = []
419        for state in email_notification_sample_states:
420            check_sample_states.append( ( state.name, state.id, True ) )
421        strings_displayed = [ 'Edit sequencing request "%s"' % request1.name,
422                              'Email notification settings' ]
423        additional_emails = [ 'test@.bx.psu.edu', 'test2@.bx.psu.edu' ]
424        strings_displayed_after_submit = [ "The changes made to the email notification settings have been saved",
425                                           '\r\n'.join( additional_emails ) ]
426        self.edit_request_email_settings( cntrller='requests', 
427                                          request_id=self.security.encode_id( request1.id ), 
428                                          check_request_owner=True, 
429                                          additional_emails='\r\n'.join( additional_emails ), 
430                                          check_sample_states=check_sample_states, 
431                                          strings_displayed=strings_displayed, 
432                                          strings_displayed_after_submit=strings_displayed_after_submit )
433        # lastly check the details in the request page
434        strings_displayed = [ 'Sequencing request "%s"' % new_name,
435                              new_desc ]
436        for field in fields:
437            strings_displayed.append( field[1] )        
438        for state_name, id, is_checked in check_sample_states:
439            strings_displayed.append( state_name )
440        for email in additional_emails:
441            strings_displayed.append( email )
442        self.view_request( cntrller='requests',
443                           request_id=self.security.encode_id( request1.id ),
444                           strings_displayed=strings_displayed,
445                           strings_not_displayed=[] )
446    def test_035_add_samples_to_request( self ):
447        """Testing adding samples to request"""
448        # logged in as regular_user1
449        # Sample fields - the tuple represents a sample name and a list of sample form field values
450        target_library_info = dict(library=self.security.encode_id(library2.id), 
451                                   folder=self.security.encode_id(library2_folder1.id) )
452        sample_value_tuples = \
453        [ ( 'Sample1', target_library_info, [ 'option1', 'sample1 field2 value', 'sample1 field3 value' ] ),
454          ( 'Sample2', target_library_info, [ 'option2', 'sample2 field2 value', 'sample2 field3 value' ] ),
455          ( 'Sample3', target_library_info, [ 'option1', 'sample3 field2 value', 'sample3 field3 value' ] ) ]
456        strings_displayed_after_submit = [ 'Unsubmitted' ]
457        for sample_name, lib_info, field_values in sample_value_tuples:
458            strings_displayed_after_submit.append( sample_name )
459            strings_displayed_after_submit.append( library2.name )
460            strings_displayed_after_submit.append( library2_folder1.name )
461            # add the sample values too
462            for values in field_values:
463                strings_displayed_after_submit.append( values )
464        # list folders that populates folder selectfield when a data library is selected
465        folder_options = [ library2_folder1.name, library2_folder2.name, library2_folder3.name, library2_folder4.name ]        # Add samples to the request
466        self.add_samples( cntrller='requests',
467                          request_id=self.security.encode_id( request1.id ),
468                          sample_value_tuples=sample_value_tuples,
469                          folder_options=folder_options,
470                          strings_displayed=[ 'Add samples to sequencing request "%s"' % request1.name,
471                                              '<input type="text" name="sample_0_name" value="Sample_1" size="10"/>' ], # sample name input field
472                          strings_displayed_after_submit=strings_displayed_after_submit )
473        # check the new sample field values on the request page
474        strings_displayed = [ 'Sequencing request "%s"' % request1.name,
475                              'Submit request' ] # this button should appear now
476        strings_displayed.extend( strings_displayed_after_submit )
477        strings_displayed_count = []
478        strings_displayed_count.append( ( library2.name, len( sample_value_tuples ) ) )
479        strings_displayed_count.append( ( library2_folder1.name, len( sample_value_tuples ) ) )
480        self.view_request( cntrller='requests',
481                           request_id=self.security.encode_id( request1.id ),
482                           strings_displayed=strings_displayed,
483                           strings_displayed_count=strings_displayed_count )
484    def test_040_edit_samples_of_new_request( self ):
485        """Testing editing the sample information of new request1"""
486        # logged in as regular_user1
487        # target data library - change it to library1
488        target_library_info = dict(library=self.security.encode_id( library1.id ), 
489                                   folder=self.security.encode_id( library1_folder1.id ) )
490        new_sample_value_tuples = \
491        [ ( 'Sample1_renamed', target_library_info, [ 'option2', 'sample1 field2 value edited', 'sample1 field3 value edited' ] ),
492          ( 'Sample2_renamed', target_library_info, [ 'option1', 'sample2 field2 value edited', 'sample2 field3 value edited' ] ),
493          ( 'Sample3_renamed', target_library_info, [ 'option2', 'sample3 field2 value edited', 'sample3 field3 value edited' ] ) ]
494        strings_displayed_after_submit = [ 'Unsubmitted' ]
495        for sample_name, lib_info, field_values in new_sample_value_tuples:
496            strings_displayed_after_submit.append( sample_name )
497            # add the sample values too
498            for values in field_values:
499                strings_displayed_after_submit.append( values )
500        strings_displayed = [ 'Edit Current Samples of Sequencing Request "%s"' % request1.name,
501                              '<input type="text" name="sample_0_name" value="Sample1" size="10"/>', # sample name input field
502                              library2_folder1.name, # all the folders in library2 should show up in the folder selectlist 
503                              library2_folder2.name, 
504                              library2_folder3.name, 
505                              library2_folder4.name ]
506        # Add samples to the request
507        self.edit_samples( cntrller='requests',
508                           request_id=self.security.encode_id( request1.id ),
509                           sample_value_tuples=new_sample_value_tuples,
510                           strings_displayed=strings_displayed,
511                           strings_displayed_after_submit=strings_displayed_after_submit )
512        # check the changed sample field values on the request page
513        strings_displayed = [ 'Sequencing request "%s"' % request1.name ]
514        strings_displayed.extend( strings_displayed_after_submit )
515        strings_displayed_count = []
516        strings_displayed_count.append( ( library1.name, len( new_sample_value_tuples ) ) )
517        strings_displayed_count.append( ( library1_folder1.name, len( new_sample_value_tuples ) ) )
518        self.view_request( cntrller='requests',
519                           request_id=self.security.encode_id( request1.id ),
520                           strings_displayed=strings_displayed,
521                           strings_displayed_count=strings_displayed_count )
522    def test_045_submit_request( self ):
523        """Testing submitting a sequencing request"""
524        # logged in as regular_user1
525        self.submit_request( cntrller='requests',
526                             request_id=self.security.encode_id( request1.id ),
527                             request_name=request1.name,
528                             strings_displayed_after_submit=[ 'The sequencing request has been submitted.' ] )
529        refresh( request1 )
530        # Make sure the request is showing in the 'submitted' filter
531        self.check_request_grid( cntrller='requests',
532                                 state=request1.states.SUBMITTED,
533                                 strings_displayed=[ request1.name ] )
534        # Make sure the request's state is now set to 'submitted'
535        assert request1.state is not request1.states.SUBMITTED, "The state of the sequencing request '%s' should be set to '%s'" \
536            % ( request1.name, request1.states.SUBMITTED )
537        # the sample state should appear once for each sample
538        strings_displayed_count = [ ( request1.type.states[0].name, len( request1.samples ) ) ]
539        # after submission, these buttons should not appear 
540        strings_not_displayed = [ 'Add sample', 'Submit request' ]
541        # check the request page
542        self.view_request( cntrller='requests',
543                           request_id=self.security.encode_id( request1.id ),
544                           strings_displayed=[ request1.states.SUBMITTED ],
545                           strings_displayed_count=strings_displayed_count,
546                           strings_not_displayed=strings_not_displayed )
547        strings_displayed=[ 'History of sequencing request "%s"' % request1.name,
548                            'Sequencing request submitted by %s' % regular_user1.email,
549                            'Sequencing request created' ]
550        strings_displayed_count = [ ( request1.states.SUBMITTED, 1 ) ]
551        self.view_request_history( cntrller='requests',
552                                   request_id=self.security.encode_id( request1.id ),
553                                   strings_displayed=strings_displayed,
554                                   strings_displayed_count=strings_displayed_count,
555                                   strings_not_displayed=[ request1.states.COMPLETE,
556                                                           request1.states.REJECTED ] )
557    # ====== Sequencing request test methods - Admin perspective ================ 
558    def test_050_receive_request_as_admin( self ):
559        """Testing receiving a sequencing request and assigning it barcodes"""
560        # logged in as regular_user1
561        self.logout()
562        # login as a admin_user to assign bar codes to samples
563        self.login( email=admin_user.email )
564        self.check_request_grid( cntrller='requests_admin',
565                                 state=request1.states.SUBMITTED,
566                                 strings_displayed=[ request1.name ] )
567        strings_displayed = [ request1.states.SUBMITTED,
568                              'Reject this request',
569                              'Add sample',
570                              'Edit samples' ]
571        self.view_request( cntrller='requests_admin',
572                           request_id=self.security.encode_id( request1.id ),
573                           strings_displayed=strings_displayed )
574        # Set bar codes for the samples
575        bar_codes = [ '10001', '10002', '10003' ]
576        strings_displayed_after_submit = [ 'Changes made to the samples have been saved.' ]
577        strings_displayed_after_submit.extend( bar_codes )
578        self.add_bar_codes( cntrller='requests_admin',
579                            request_id=self.security.encode_id( request1.id ),
580                            bar_codes=bar_codes,
581                            strings_displayed=[ 'Edit Current Samples of Sequencing Request "%s"' % request1.name ],
582                            strings_displayed_after_submit=strings_displayed_after_submit )
583        # the second sample state should appear once for each sample
584        strings_displayed_count = [ ( request1.type.states[1].name, len( request1.samples ) ),
585                                    ( request1.type.states[0].name, 0 ) ]        
586        # check the request page
587        self.view_request( cntrller='requests_admin',
588                           request_id=self.security.encode_id( request1.id ),
589                           strings_displayed=bar_codes,
590                           strings_displayed_count=strings_displayed_count )
591        # the sample state descriptions of the future states should not appear
592        # here the state names are not checked as all of them appear at the top of
593        # the page like: state1 > state2 > state3
594        strings_not_displayed=[ request1.type.states[2].desc,
595                                request1.type.states[3].desc,
596                                request1.type.states[4].desc ]
597        # check history of each sample
598        for sample in request1.samples:
599            strings_displayed = [ 'History of sample "%s"' % sample.name,
600                                  'Sequencing request submitted and sample state set to %s' % request1.type.states[0].name,
601                                   request1.type.states[0].name,
602                                   request1.type.states[1].name ]
603            self.view_sample_history( cntrller='requests_admin',
604                                      sample_id=self.security.encode_id( sample.id ),
605                                      strings_displayed=strings_displayed,
606                                      strings_not_displayed=strings_not_displayed )
607    def test_055_request_lifecycle( self ):
608        """Testing request life-cycle as it goes through all the states"""
609        # logged in as admin_user
610        self.check_request_grid( cntrller='requests_admin',
611                                 state=request1.states.SUBMITTED,
612                                 strings_displayed=[ request1.name ] )
613        strings_displayed=[ 'History of sequencing request "%s"' % request1.name ]
614        # Change the states of all the samples of this request to ultimately be COMPLETE
615        for index, state in enumerate( request_type1.states ):
616            # start from the second state onwards
617            if index > 1:
618                # status message
619                if index == len( request_type1.states ) - 1:
620                    status_msg = 'All samples of this sequencing request are in the final sample state (%s).' % state.name
621                else:
622                    status_msg = 'All samples of this sequencing request are in the (%s) sample state. ' % state.name 
623                # check email notification message
624                email_msg = ''
625                if state.id in [ email_state.id for email_state in email_notification_sample_states ]:
626                    email_msg = 'Email notification failed as SMTP server not set in config file'
627                self.change_sample_state( request_id=self.security.encode_id( request1.id ),
628                                          sample_ids=[ sample.id for sample in request1.samples ],
629                                          new_sample_state_id=self.security.encode_id( state.id ),
630                                          strings_displayed=[ 'Edit Current Samples of Sequencing Request "%s"' % request1.name ],
631                                          strings_displayed_after_submit = [ status_msg, email_msg ] )
632                # check request history page
633                if index == len( request_type1.states ) - 1:
634                    strings_displayed.append( status_msg )
635                else:
636                    strings_displayed.append( status_msg )
637                self.view_request_history( cntrller='requests_admin',
638                                           request_id=self.security.encode_id( request1.id ),
639                                           strings_displayed=strings_displayed,
640                                           strings_not_displayed=[ request1.states.REJECTED ] )
641        refresh( request1 )
642        # check if the request's state is now set to 'complete'
643        self.check_request_grid( cntrller='requests_admin',
644                                 state='Complete',
645                                 strings_displayed=[ request1.name ] )
646        assert request1.state is not request1.states.COMPLETE, "The state of the sequencing request '%s' should be set to '%s'" \
647            % ( request1.name, request1.states.COMPLETE )
648    def test_060_admin_create_request_on_behalf_of_regular_user( self ):
649        """Testing creating and submitting a request as an admin on behalf of a regular user"""
650        # Logged in as regular_user1
651        self.logout()
652        self.login( email=admin_user.email )
653        # Create the request
654        name = "Request2"
655        desc = 'Request2 Description'
656        # new address
657        new_address_dict = dict( short_desc="Home",
658                                 name="Sherlock Holmes",
659                                 institution="None" ,
660                                 address="221B Baker Street",
661                                 city="London",
662                                 state="London",
663                                 postal_code="34534",
664                                 country="United Kingdom",
665                                 phone="007-007-0007" )
666        # Set field values - the tuples in the field_values list include the field_value, and True if refresh_on_change
667        # is required for that field.
668        field_value_tuples = [ ( request_form_field_name1, 'option2', False ), 
669                               ( request_form_field_name2, ( 'new', new_address_dict ) , True ), 
670                               ( request_form_field_name3, 'field_2_value', False ) ] 
671        self.create_request( cntrller='requests_admin',
672                             request_type_id=self.security.encode_id( request_type1.id ),
673                             other_users_id=self.security.encode_id( regular_user1.id ),
674                             name=name,
675                             desc=desc,
676                             field_value_tuples=field_value_tuples,
677                             strings_displayed=[ 'Create a new sequencing request',
678                                                 request_field_label1,
679                                                 request_field_label2,
680                                                 request_field_label3 ],
681                             strings_displayed_after_submit=[ "The sequencing request has been created.",
682                                                              name, desc ] )
683        global request2
684        request2 = get_request_by_name( name )
685        global user_address2
686        user_address2 = get_user_address( regular_user1, new_address_dict[ 'short_desc' ] )
687        # Make sure the request is showing in the 'new' filter
688        self.check_request_grid( cntrller='requests_admin',
689                                 state=request2.states.NEW,
690                                 strings_displayed=[ request2.name ] )
691        # Make sure the request's state is now set to 'new'
692        assert request2.state is not request2.states.NEW, "The state of the request '%s' should be set to '%s'" \
693            % ( request2.name, request2.states.NEW )
694        target_library_info = dict(library='none', folder='none' )
695        # Sample fields - the tuple represents a sample name and a list of sample form field values
696        sample_value_tuples = \
697        [ ( 'Sample1', target_library_info, [ 'option1', 'sample1 field2 value', 'sample1 field3 value' ] ),
698          ( 'Sample2', target_library_info, [ 'option2', 'sample2 field2 value', 'sample2 field3 value' ] ),
699          ( 'Sample3', target_library_info, [ 'option1', 'sample3 field2 value', 'sample3 field3 value' ] ) ]
700        strings_displayed_after_submit = [ 'Unsubmitted' ]
701        for sample_name, lib_info, field_values in sample_value_tuples:
702            strings_displayed_after_submit.append( sample_name )
703            for values in field_values:
704                strings_displayed_after_submit.append( values )
705        # Add samples to the request
706        self.add_samples( cntrller='requests_admin',
707                          request_id=self.security.encode_id( request2.id ),
708                          sample_value_tuples=sample_value_tuples,
709                          strings_displayed=[ 'Add samples to sequencing request "%s"' % request2.name,
710                                              '<input type="text" name="sample_0_name" value="Sample_1" size="10"/>' ], # sample name input field
711                          strings_displayed_after_submit=strings_displayed_after_submit )
712        # Submit the request
713        select_target_library_message = "Select a target data library and folder for a sample before selecting it's datasets to transfer from the external service"
714        self.submit_request( cntrller='requests_admin',
715                             request_id=self.security.encode_id( request2.id ),
716                             request_name=request2.name,
717                             strings_displayed_after_submit=[ select_target_library_message,
718                                                              'The sequencing request has been submitted.' ] )
719        refresh( request2 )
720        # Make sure the request is showing in the 'submitted' filter
721        self.check_request_grid( cntrller='requests_admin',
722                                 state=request2.states.SUBMITTED,
723                                 strings_displayed=[ request2.name ] )
724        # Make sure the request's state is now set to 'submitted'
725        assert request2.state is not request2.states.SUBMITTED, "The state of the sequencing request '%s' should be set to '%s'" \
726            % ( request2.name, request2.states.SUBMITTED )
727        # Make sure both requests are showing in the 'All' filter
728        self.check_request_grid( cntrller='requests_admin',
729                                 state='All',
730                                 strings_displayed=[ request1.name, request2.name ] )
731        # list folders that populates folder selectfield when a data library is selected
732        folder_options = [ library2_folder1.name, library2_folder2.name, library2_folder3.name, library2_folder4.name ]
733        # set the target data library to library2 using sample operation user interface
734        self.change_sample_target_data_library( cntrller='requests',
735                                                request_id=self.security.encode_id( request2.id ),
736                                                sample_ids=[ sample.id for sample in request2.samples ],
737                                                new_library_id=self.security.encode_id( library2.id ), 
738                                                new_folder_id=self.security.encode_id( library2_folder1.id ),
739                                                folder_options=folder_options,
740                                                strings_displayed=[ 'Edit Current Samples of Sequencing Request "%s"' % request2.name ],
741                                                strings_displayed_after_submit=[ 'Changes made to the samples have been saved.' ] )
742        # check the changed target data library & folder on the request page
743        strings_displayed_count = []
744        strings_displayed_count.append( ( library2.name, len( request1.samples ) ) )
745        strings_displayed_count.append( ( library2_folder1.name, len( request1.samples ) ) )
746        strings_displayed = [ 'Sequencing request "%s"' % name, regular_user1.email, request2.states.SUBMITTED ]
747        self.view_request( cntrller='requests',
748                           request_id=self.security.encode_id( request2.id ),
749                           strings_displayed=strings_displayed,
750                           strings_not_displayed=[ select_target_library_message ],
751                           strings_displayed_count=strings_displayed_count )
752    def test_065_reject_request( self ):
753        """Testing rejecting a request"""
754        # Logged in as admin_user
755        rejection_reason="This is why the sequencing request was rejected."
756        self.reject_request( request_id=self.security.encode_id( request2.id ),
757                             request_name=request2.name,
758                             comment=rejection_reason,
759                             strings_displayed=[ 'Reject sequencing request "%s"' % request2.name ],
760                             strings_displayed_after_submit=[ 'Sequencing request (%s) has been rejected.' % request2.name ] )
761        refresh( request2 )
762        # Make sure the request is showing in the 'rejected' filter
763        self.check_request_grid( cntrller='requests_admin',
764                                 state=request2.states.REJECTED,
765                                 strings_displayed=[ request2.name ] )
766        # Make sure the request's state is now set to REJECTED
767        assert request2.state is not request2.states.REJECTED, "The state of the request '%s' should be set to '%s'" \
768            % ( request2.name, request2.states.REJECTED )
769        # The rejection reason should show up in the request page and the request history page
770        rejection_message = 'Sequencing request marked rejected by %s. Reason: %s' % ( admin_user.email, rejection_reason )
771        strings_displayed = [ request2.states.REJECTED, 
772                              rejection_message ]
773        self.view_request( cntrller='requests',
774                           request_id=self.security.encode_id( request2.id ),
775                           strings_displayed=strings_displayed )
776        self.view_request_history( cntrller='requests',
777                                   request_id=self.security.encode_id( request2.id ),
778                                   strings_displayed=strings_displayed )
779        # login as the regular user to make sure that the request2 is fully editable
780        self.logout()
781        self.login( email=regular_user1.email )
782        strings_displayed=[ 'Sequencing request "%s"' % request2.name,
783                            request1.states.REJECTED,
784                            rejection_message ]
785        visible_buttons = 

Large files files are truncated, but you can click here to view the full file