PageRenderTime 31ms CodeModel.GetById 8ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/functional/test_user_info.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 250 lines | 241 code | 2 blank | 7 comment | 7 complexity | 48b8233288bbf256b04855a535cbdb2d MD5 | raw file
  1from base.twilltestcase import *
  2from base.test_db_util import *
  3
  4
  5# TODO: Functional tests start failing at 020, fix or eliminate rest of tests.
  6class TestUserInfo( TwillTestCase ):
  7    def test_000_initiate_users( self ):
  8        """Ensuring all required user accounts exist"""
  9        self.logout()
 10        self.login( email='test1@bx.psu.edu', username='regular-user1' )
 11        global regular_user1
 12        regular_user1 = get_user( 'test1@bx.psu.edu' )
 13        assert regular_user1 is not None, 'Problem retrieving user with email "test1@bx.psu.edu" from the database'
 14        global regular_user1_private_role
 15        regular_user1_private_role = get_private_role( regular_user1 )
 16        self.logout()
 17        self.login( email='test2@bx.psu.edu', username='regular-user2' )
 18        global regular_user2
 19        regular_user2 = get_user( 'test2@bx.psu.edu' )
 20        assert regular_user2 is not None, 'Problem retrieving user with email "test2@bx.psu.edu" from the database'
 21        global regular_user2_private_role
 22        regular_user2_private_role = get_private_role( regular_user2 )
 23        self.logout()
 24        self.login( email='test3@bx.psu.edu', username='regular-user3' )
 25        global regular_user3
 26        regular_user3 = get_user( 'test3@bx.psu.edu' )
 27        assert regular_user3 is not None, 'Problem retrieving user with email "test3@bx.psu.edu" from the database'
 28        global regular_user3_private_role
 29        regular_user3_private_role = get_private_role( regular_user3 )
 30        self.logout()
 31        self.login( email='test@bx.psu.edu', username='admin-user' )
 32        global admin_user
 33        admin_user = get_user( 'test@bx.psu.edu' )
 34        assert admin_user is not None, 'Problem retrieving user with email "test@bx.psu.edu" from the database'
 35        global admin_user_private_role
 36        admin_user_private_role = get_private_role( admin_user )
 37    def test_005_create_user_info_forms( self ):
 38        """Testing creating a new user info form and editing it"""
 39        # Logged in as admin_user
 40        # Create a the first form
 41        name = "Student"
 42        desc = "This is Student user info form's description"
 43        form_type = get_user_info_form_definition()
 44        self.create_form( name=name,
 45                          description=desc,
 46                          form_type=form_type,
 47                          num_fields=0,
 48                          strings_displayed=[ 'Create a new form definition' ],
 49                          strings_displayed_after_submit=[ name, desc, form_type ] )
 50        tmp_form = get_form( name )
 51        # field names
 52        global affiliation_field_name
 53        affiliation_field_name = 'affiliation'
 54        global organization_field_name
 55        organization_field_name = 'name_of_organization'
 56        global feedback_field_name
 57        feedback_field_name = 'contact_for_feedback'
 58        # Add fields to the form
 59        field_dicts = [ dict( label='Affiliation',
 60                              desc='The type of  organization you are affiliated with',
 61                              type='SelectField',
 62                              required='optional',
 63                              selectlist=[ 'Educational', 'Research', 'Commercial' ],
 64                              name=affiliation_field_name ),
 65                        dict( label='Name of Organization',
 66                              desc='',
 67                              type='TextField',
 68                              required='optional',
 69                              name=organization_field_name ),
 70                        dict( label='Contact for feedback',
 71                              desc='',
 72                              type='CheckboxField',
 73                              required='optional',
 74                              name=feedback_field_name ) ]
 75        self.edit_form( id=self.security.encode_id( tmp_form.current.id ),
 76                        field_dicts=field_dicts,
 77                        field_index=len( tmp_form.fields ),
 78                        strings_displayed=[ 'Edit form definition "%s"' % name ],
 79                        strings_displayed_after_submit=[ "The form '%s' has been updated with the changes." % name ] )
 80        # Get the form_definition object for later tests
 81        global form_one
 82        form_one = get_form( name )
 83        assert form_one is not None, 'Problem retrieving form named "%s" from the database' % name
 84        assert len( form_one.fields ) == len( tmp_form.fields ) + len( field_dicts )
 85        # Create the second form
 86        name = "Researcher"
 87        desc = "This is Researcher user info form's description"
 88        self.create_form( name=name,
 89                          description=desc,
 90                          form_type=form_type,
 91                          num_fields=0,
 92                          strings_displayed=[ 'Create a new form definition' ],
 93                          strings_displayed_after_submit=[ name, desc, form_type ] )
 94        tmp_form = get_form( name )
 95        # Add fields to the form
 96        field_dicts = [ dict( label='Affiliation',
 97                              desc='The type of  organization you are affiliated with',
 98                              type='SelectField',
 99                              required='optional',
100                              selectlist=[ 'Educational', 'Research', 'Commercial' ],
101                              name=affiliation_field_name ),
102                        dict( label='Name of Organization',
103                              desc='',
104                              type='TextField',
105                              required='optional',
106                              name=organization_field_name ),
107                        dict( label='Contact for feedback',
108                              desc='',
109                              type='CheckboxField',
110                              required='optional',
111                              name=feedback_field_name ) ]
112        self.edit_form( id=self.security.encode_id( tmp_form.current.id ),
113                        field_dicts=field_dicts,
114                        field_index=len( tmp_form.fields ),
115                        strings_displayed=[ 'Edit form definition "%s"' % name ],
116                        strings_displayed_after_submit=[ "The form '%s' has been updated with the changes." % name ] )
117        # Get the form_definition object for later tests
118        global form_two
119        form_two = get_form( name )
120        assert form_two is not None, 'Problem retrieving form named "%s" from the database' % name
121        assert len( form_two.fields ) == len( tmp_form.fields ) + len( field_dicts )
122    def test_010_user_reqistration_multiple_user_info_forms( self ):
123        """Testing user registration with multiple user info forms"""
124        # Logged in as admin_user
125        self.logout()
126        # Create a new user with 'Student' user info form.  The user_info_values will be the values
127        # filled into the fields defined in field_dicts above ( 'Educational' -> 'Affiliation,
128        # 'Penn State' -> 'Name of Organization', '1' -> 'Contact for feedback' )
129        email = 'test11@bx.psu.edu'
130        password = 'testuser'
131        username = 'test11'
132        user_info_values=[ ( affiliation_field_name, 'Educational' ), 
133                           ( organization_field_name, 'Penn State' ), 
134                           ( feedback_field_name, '1' ) ]
135        self.create_user_with_info( cntrller='admin',
136                                    email=email,
137                                    password=password,
138                                    username=username, 
139                                    user_type_fd_id=self.security.encode_id( form_one.id ), 
140                                    user_info_values=user_info_values,
141                                    strings_displayed=[ "Create account", "User type" ] )
142        global regular_user11
143        regular_user11 = get_user( email )
144        assert regular_user11 is not None, 'Problem retrieving user with email "%s" from the database' % email
145        global regular_user11_private_role
146        regular_user11_private_role = get_private_role( regular_user11 )
147        self.logout()
148        self.login( email=regular_user11.email, username=username )
149        global form_checkbox_field3_string
150        form_checkbox_field3_string = '<input type="checkbox" id="%s" name="%s" value="true" checked="checked">' % ( feedback_field_name, feedback_field_name )
151        self.edit_user_info( cntrller='user',
152                             strings_displayed=[ "Manage User Information",
153                                                 user_info_values[0][1],
154                                                 user_info_values[1][1],
155                                                 form_checkbox_field3_string ] )
156    def test_015_user_reqistration_single_user_info_forms( self ):
157        """Testing user registration with a single user info form"""
158        # Logged in as regular_user_11
159        self.logout()
160        self.login( email=admin_user.email )
161        # Delete the 'Researcher' user info form
162        self.mark_form_deleted( self.security.encode_id( form_two.current.id ) )
163        # Create a new user with 'Student' user info form.  The user_info_values will be the values
164        # filled into the fields defined in field_dicts above ( 'Educational' -> 'Affiliation,
165        # 'Penn State' -> 'Name of Organization', '1' -> 'Contact for feedback' )
166        email = 'test12@bx.psu.edu'
167        password = 'testuser'
168        username = 'test12'
169        user_info_values=[ ( affiliation_field_name, 'Educational' ), 
170                           ( organization_field_name, 'Penn State' ), 
171                           ( feedback_field_name, '1' ) ]
172        self.create_user_with_info( cntrller='admin',
173                                    email=email,
174                                    password=password,
175                                    username=username, 
176                                    user_type_fd_id=self.security.encode_id( form_one.id ), 
177                                    user_info_values=user_info_values,
178                                    strings_displayed=[ "Create account", "User type" ] )
179        global regular_user12
180        regular_user12 = get_user( email )
181        assert regular_user12 is not None, 'Problem retrieving user with email "%s" from the database' % email
182        global regular_user12_private_role
183        regular_user12_private_role = get_private_role( regular_user12 )
184        self.logout()
185        self.login( email=regular_user12.email, username=username )
186        self.edit_user_info( cntrller='user',
187                             strings_displayed=[ "Manage User Information",
188                                                 user_info_values[0][1],
189                                                 user_info_values[1][1],
190                                                 form_checkbox_field3_string ] )
191    def test_020_edit_user_info( self ):
192        """Testing editing user info as a regular user"""
193        # Logged in as regular_user_12
194        # Test changing email and user name - first try an invalid user name
195        self.edit_user_info( cntrller='user',
196                             new_email='test12_new@bx.psu.edu',
197                             new_username='test12_new',
198                             strings_displayed_after_submit=[ "Public names must be at least four characters" ] )
199        # Now try a valid user name
200        self.edit_user_info( cntrller='user',
201                             new_email='test12_new@bx.psu.edu',
202                             new_username='test12-new',
203                             strings_displayed_after_submit=[ 'The login information has been updated with the changes' ] )
204        # Since we changed the user's account. make sure the user's private role was changed accordingly
205        if not get_private_role( regular_user12 ):
206            raise AssertionError, "The private role for %s was not correctly set when their account (email) was changed" % regular_user12.email
207        # Test changing password
208        self.edit_user_info( cntrller='user',
209                             password='testuser',
210                             new_password='testuser#',\
211                             strings_displayed_after_submit=[ 'The password has been changed' ] )
212        self.logout()
213        refresh( regular_user12 )
214        # Test logging in with new email and password
215        self.login( email=regular_user12.email, password='testuser#' )
216        # Test editing the user info
217        new_user_info_values=[ ( affiliation_field_name, 'Educational' ), 
218                               ( organization_field_name, 'Penn State' ) ]
219        self.edit_user_info( cntrller='user',
220                             info_values=new_user_info_values,
221                             strings_displayed_after_submit=[ "The user information has been updated with the changes" ] )
222    def test_999_reset_data_for_later_test_runs( self ):
223        """Reseting data to enable later test runs to pass"""
224        # Logged in as regular_user_12
225        self.logout()
226        self.login( email=admin_user.email )
227        ##################
228        # Mark all forms deleted that have not yet been marked deleted ( form_two has )
229        ##################
230        for form in [ form_one ]:
231            self.mark_form_deleted( self.security.encode_id( form.current.id ) )
232        ###############
233        # Purge private roles
234        ###############
235        for role in [ regular_user11_private_role, regular_user12_private_role ]:
236            self.mark_role_deleted( self.security.encode_id( role.id ), role.name )
237            self.purge_role( self.security.encode_id( role.id ), role.name )
238            # Manually delete the role from the database
239            refresh( role )
240            delete_obj( role )
241        ###############
242        # Purge appropriate users
243        ###############
244        for user in [ regular_user11, regular_user12 ]:
245            self.mark_user_deleted( user_id=self.security.encode_id( user.id ), email=user.email )
246            refresh( user )
247            self.purge_user( self.security.encode_id( user.id ), user.email )
248            refresh( user )
249            delete_user_roles( user )
250            delete_obj( user )