/test/functional/test_user_info.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 250 lines · 226 code · 2 blank · 22 comment · 0 complexity · 48b8233288bbf256b04855a535cbdb2d MD5 · raw file

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