PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/functional/test_history_functions.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 900 lines | 810 code | 19 blank | 71 comment | 23 complexity | b3d6a5cd2e8f7352e74bd0d84f505814 MD5 | raw file
  1. import urllib
  2. import galaxy.model
  3. from galaxy.model.orm import *
  4. from base.test_db_util import sa_session
  5. from base.twilltestcase import *
  6. class TestHistory( TwillTestCase ):
  7. def test_000_history_behavior_between_logout_login( self ):
  8. """Testing history behavior between logout and login"""
  9. self.logout()
  10. self.history_options()
  11. # Create a new, empty history named anonymous
  12. name = 'anonymous'
  13. self.new_history( name=name )
  14. global anonymous_history
  15. anonymous_history = (
  16. sa_session.query( galaxy.model.History )
  17. .filter( and_( galaxy.model.History.table.c.deleted == False, galaxy.model.History.table.c.name == name ) )
  18. .order_by( desc( galaxy.model.History.table.c.create_time ) )
  19. .first()
  20. )
  21. assert anonymous_history is not None, "Problem retrieving anonymous_history from database"
  22. # Upload a dataset to anonymous_history so it will be set as the current history after login
  23. self.upload_file( '1.bed', dbkey='hg18' )
  24. self.login( email='test1@bx.psu.edu', username='regular-user1' )
  25. global regular_user1
  26. regular_user1 = sa_session.query( galaxy.model.User ) \
  27. .filter( galaxy.model.User.table.c.email == 'test1@bx.psu.edu' ) \
  28. .first()
  29. assert regular_user1 is not None, 'Problem retrieving user with email "test1@bx.psu.edu" from the database'
  30. # Current history should be anonymous_history
  31. self.check_history_for_string( name )
  32. self.logout()
  33. # Login as the same user again to ensure anonymous_history is still the current history
  34. self.login( email=regular_user1.email )
  35. self.check_history_for_string( name )
  36. self.logout()
  37. self.login( email='test2@bx.psu.edu', username='regular-user2' )
  38. global regular_user2
  39. regular_user2 = sa_session.query( galaxy.model.User ) \
  40. .filter( galaxy.model.User.table.c.email == 'test2@bx.psu.edu' ) \
  41. .first()
  42. assert regular_user2 is not None, 'Problem retrieving user with email "test2@bx.psu.edu" from the database'
  43. self.logout()
  44. self.login( email='test3@bx.psu.edu', username='regular-user3' )
  45. global regular_user3
  46. regular_user3 = sa_session.query( galaxy.model.User ) \
  47. .filter( galaxy.model.User.table.c.email == 'test3@bx.psu.edu' ) \
  48. .first()
  49. assert regular_user3 is not None, 'Problem retrieving user with email "test3@bx.psu.edu" from the database'
  50. self.logout()
  51. self.login( email='test@bx.psu.edu', username='admin-user' )
  52. global admin_user
  53. admin_user = sa_session.query( galaxy.model.User ) \
  54. .filter( galaxy.model.User.table.c.email == 'test@bx.psu.edu' ) \
  55. .one()
  56. assert admin_user is not None, 'Problem retrieving user with email "test@bx.psu.edu" from the database'
  57. # Get the admin_user private role for later use
  58. global admin_user_private_role
  59. admin_user_private_role = None
  60. for role in admin_user.all_roles():
  61. if role.name == admin_user.email and role.description == 'Private Role for %s' % admin_user.email:
  62. admin_user_private_role = role
  63. break
  64. if not admin_user_private_role:
  65. raise AssertionError( "Private role not found for user '%s'" % admin_user.email )
  66. historyA = sa_session.query( galaxy.model.History ) \
  67. .filter( and_( galaxy.model.History.table.c.deleted == False,
  68. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  69. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  70. .first()
  71. assert historyA is not None, "Problem retrieving historyA from database"
  72. assert not historyA.deleted, "After login, historyA is deleted"
  73. # Make sure the last used history is set for the next session after login
  74. self.logout()
  75. self.login( email=admin_user.email )
  76. historyB = sa_session.query( galaxy.model.History ) \
  77. .filter( and_( galaxy.model.History.table.c.deleted == False,
  78. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  79. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  80. .first()
  81. assert historyB is not None, "Problem retrieving historyB from database"
  82. assert historyA.id == historyB.id, "After the same user logged out and back in, their last used history was not associated with their new session"
  83. def test_005_deleting_histories( self ):
  84. """Testing deleting histories"""
  85. # Logged in as admin_user
  86. historyB = sa_session.query( galaxy.model.History ) \
  87. .filter( and_( galaxy.model.History.table.c.deleted == False,
  88. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  89. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  90. .first()
  91. assert historyB is not None, "Problem retrieving historyB from database"
  92. self.delete_history( self.security.encode_id( historyB.id ) )
  93. sa_session.refresh( historyB )
  94. if not historyB.deleted:
  95. raise AssertionError( "Problem deleting history id %d" % historyB.id )
  96. # Since we deleted the current history, make sure the history frame was refreshed
  97. self.check_history_for_string( 'Your history is empty.' )
  98. # We'll now test deleting a list of histories
  99. # After deleting the current history, a new one should have been created
  100. global history1
  101. history1 = sa_session.query( galaxy.model.History ) \
  102. .filter( and_( galaxy.model.History.table.c.deleted == False,
  103. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  104. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  105. .first()
  106. assert history1 is not None, "Problem retrieving history1 from database"
  107. self.upload_file( '1.bed', dbkey='hg18' )
  108. self.new_history( name=urllib.quote( 'history2' ) )
  109. global history2
  110. history2 = sa_session.query( galaxy.model.History ) \
  111. .filter( and_( galaxy.model.History.table.c.deleted == False,
  112. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  113. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  114. .first()
  115. assert history2 is not None, "Problem retrieving history2 from database"
  116. self.upload_file( '2.bed', dbkey='hg18' )
  117. ids = '%s,%s' % ( self.security.encode_id( history1.id ), self.security.encode_id( history2.id ) )
  118. self.delete_history( ids )
  119. # Since we deleted the current history, make sure the history frame was refreshed
  120. self.check_history_for_string( 'Your history is empty.' )
  121. try:
  122. self.view_stored_active_histories( strings_displayed=[ history1.name ] )
  123. raise AssertionError( "History %s is displayed in the active history list after it was deleted" % history1.name )
  124. except:
  125. pass
  126. self.view_stored_deleted_histories( strings_displayed=[ history1.name ] )
  127. try:
  128. self.view_stored_active_histories( strings_displayed=[ history2.name ] )
  129. raise AssertionError( "History %s is displayed in the active history list after it was deleted" % history2.name )
  130. except:
  131. pass
  132. self.view_stored_deleted_histories( strings_displayed=[ history2.name ] )
  133. sa_session.refresh( history1 )
  134. if not history1.deleted:
  135. raise AssertionError( "Problem deleting history id %d" % history1.id )
  136. if not history1.default_permissions:
  137. raise AssertionError( "Default permissions were incorrectly deleted from the db for history id %d when it was deleted" % history1.id )
  138. sa_session.refresh( history2 )
  139. if not history2.deleted:
  140. raise AssertionError( "Problem deleting history id %d" % history2.id )
  141. if not history2.default_permissions:
  142. raise AssertionError( "Default permissions were incorrectly deleted from the db for history id %d when it was deleted" % history2.id )
  143. # Current history is empty
  144. self.history_options( user=True )
  145. def test_010_history_rename( self ):
  146. """Testing renaming a history"""
  147. # Logged in as admin_user
  148. global history3
  149. history3 = sa_session.query( galaxy.model.History ) \
  150. .filter( galaxy.model.History.table.c.deleted == False ) \
  151. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  152. .first()
  153. assert history3 is not None, "Problem retrieving history3 from database"
  154. if history3.deleted:
  155. raise AssertionError( "History id %d deleted when it should not be" % latest_history.id )
  156. self.rename_history( self.security.encode_id( history3.id ), history3.name, new_name=urllib.quote( 'history 3' ) )
  157. sa_session.refresh( history3 )
  158. def test_015_history_list( self ):
  159. """Testing viewing previously stored active histories"""
  160. # Logged in as admin_user
  161. self.view_stored_active_histories()
  162. def test_020_share_current_history( self ):
  163. """Testing sharing the current history which contains only public datasets"""
  164. # Logged in as admin_user
  165. # Test sharing an empty history - current history is history3
  166. self.share_current_history( regular_user1.email,
  167. strings_displayed=[ history3.name ],
  168. strings_displayed_after_submit=[ 'You cannot share an empty history.' ] )
  169. # Make history3 sharable by adding a dataset
  170. self.upload_file( '1.bed', dbkey='hg18' )
  171. # Current history is no longer empty
  172. self.history_options( user=True, active_datasets=True, activatable_datasets=True )
  173. # Test sharing history3 with yourself
  174. self.share_current_history( admin_user.email,
  175. strings_displayed=[ history3.name ],
  176. strings_displayed_after_submit=[ 'You cannot send histories to yourself.' ] )
  177. # Share history3 with 1 valid user
  178. self.share_current_history( regular_user1.email,
  179. strings_displayed=[ history3.name ] )
  180. # Check out list of histories to make sure history3 was shared
  181. self.view_stored_active_histories( strings_displayed=[ 'operation=share' ] )
  182. # Make history3 accessible via link.
  183. self.make_accessible_via_link( self.security.encode_id( history3.id ),
  184. strings_displayed=[ 'Make History Accessible via Link' ],
  185. strings_displayed_after_submit=[ 'Anyone can view and import this history' ] )
  186. # Make sure history3 is now accessible.
  187. sa_session.refresh( history3 )
  188. if not history3.importable:
  189. raise AssertionError( "History 3 is not marked as importable after make_accessible_via_link" )
  190. # Try importing history3
  191. #Importing your own history was enabled in 5248:dc9efb540f61.
  192. #self.import_history_via_url( self.security.encode_id( history3.id ),
  193. # admin_user.email,
  194. # strings_displayed_after_submit=[ 'You cannot import your own history.' ] )
  195. # Disable access via link for history3.
  196. self.disable_access_via_link( self.security.encode_id( history3.id ),
  197. strings_displayed=[ 'Anyone can view and import this history' ],
  198. strings_displayed_after_submit=[ 'Make History Accessible via Link' ] )
  199. # Try importing history3 after disabling access via link. To do this, need to login as regular user 2, who cannot access
  200. # history via sharing or via link.
  201. self.logout()
  202. self.login( email=regular_user2.email )
  203. self.import_history_via_url( self.security.encode_id( history3.id ),
  204. admin_user.email,
  205. strings_displayed_after_submit=[ 'History is not accessible to the current user' ] )
  206. self.logout()
  207. self.login( email=admin_user.email )
  208. # Test sharing history3 with an invalid user
  209. self.share_current_history( 'jack@jill.com',
  210. strings_displayed_after_submit=[ 'jack@jill.com is not a valid Galaxy user.' ] )
  211. def test_025_delete_shared_current_history( self ):
  212. """Testing deleting the current history after it was shared"""
  213. # Logged in as admin_user
  214. self.delete_current_history(
  215. strings_displayed=[ "History (%s) has been shared with others, unshare it before deleting it." % history3.name ] )
  216. def test_030_clone_shared_history( self ):
  217. """Testing copying a shared history"""
  218. # logged in as admin user
  219. self.logout()
  220. self.login( email=regular_user1.email )
  221. # Shared history3 affects history options
  222. self.history_options( user=True, histories_shared_by_others=True )
  223. # Shared history3 should be in regular_user1's list of shared histories
  224. self.view_shared_histories( strings_displayed=[ history3.name, admin_user.email ] )
  225. self.copy_history( self.security.encode_id( history3.id ),
  226. 'activatable',
  227. strings_displayed_after_submit=[ 'has been created.' ] )
  228. global history3_clone1
  229. history3_clone1 = sa_session.query( galaxy.model.History ) \
  230. .filter( and_( galaxy.model.History.table.c.deleted == False,
  231. galaxy.model.History.table.c.user_id == regular_user1.id ) ) \
  232. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  233. .first()
  234. assert history3_clone1 is not None, "Problem retrieving history3_clone1 from database"
  235. # Check list of histories to make sure shared history3 was cloned
  236. strings_displayed = [ "Copy of '%s' shared by '%s'" % ( history3.name, admin_user.email ) ]
  237. self.view_stored_active_histories( strings_displayed=strings_displayed )
  238. def test_035_clone_current_history( self ):
  239. """Testing copying the current history"""
  240. # logged in as regular_user1
  241. self.logout()
  242. self.login( email=admin_user.email )
  243. # Current history should be history3, add more datasets to history3, then delete them so we can
  244. # test cloning activatable datasets as well as only the active datasets
  245. self.upload_file( '2.bed', dbkey='hg18' )
  246. hda_2_bed = (
  247. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  248. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history3.id,
  249. galaxy.model.HistoryDatasetAssociation.table.c.name == '2.bed' ) )
  250. .first() )
  251. assert hda_2_bed is not None, "Problem retrieving hda_2_bed from database"
  252. self.delete_history_item( str( hda_2_bed.id ) )
  253. self.upload_file( '3.bed', dbkey='hg18' )
  254. hda_3_bed = (
  255. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  256. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history3.id,
  257. galaxy.model.HistoryDatasetAssociation.table.c.name == '3.bed' ) )
  258. .first() )
  259. assert hda_3_bed is not None, "Problem retrieving hda_3_bed from database"
  260. self.delete_history_item( str( hda_3_bed.id ) )
  261. # Test cloning activatable datasets
  262. self.copy_history( self.security.encode_id( history3.id ),
  263. 'activatable',
  264. strings_displayed_after_submit=['has been created.' ] )
  265. global history3_clone2
  266. history3_clone2 = sa_session.query( galaxy.model.History ) \
  267. .filter( and_( galaxy.model.History.table.c.deleted == False,
  268. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  269. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  270. .first()
  271. assert history3_clone2 is not None, "Problem retrieving history3_clone2 from database"
  272. # Check list of histories to make sure shared history3 was cloned
  273. self.view_stored_active_histories( strings_displayed=[ "Copy of '%s'" % history3.name ] )
  274. # Switch to the cloned history to make sure activatable datasets were cloned
  275. self.switch_history( id=self.security.encode_id( history3_clone2.id ), name=history3_clone2.name )
  276. hda_2_bed = (
  277. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  278. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history3_clone2.id,
  279. galaxy.model.HistoryDatasetAssociation.table.c.name == '2.bed' ) )
  280. .first() )
  281. assert hda_2_bed is not None, "Problem retrieving hda_2_bed from database"
  282. hda_3_bed = (
  283. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  284. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history3_clone2.id,
  285. galaxy.model.HistoryDatasetAssociation.table.c.name == '3.bed' ) )
  286. .first() )
  287. assert hda_3_bed is not None, "Problem retrieving hda_3_bed from database"
  288. # Make sure the deleted datasets are included in the cloned history
  289. # check for encoded ids
  290. # - these will be available bc the refreshed page will have bootstrapped json for the hdas
  291. #NOTE: that these WON'T be available when refreshes become less common
  292. # (when the backbone.js is fully integrated and refreshes aren't used after every history function)
  293. self.check_history_for_exact_string( self.security.encode_id( hda_2_bed.id ), show_deleted=True )
  294. self.check_history_for_exact_string( self.security.encode_id( hda_3_bed.id ), show_deleted=True )
  295. # Test cloning only active datasets
  296. self.copy_history(
  297. self.security.encode_id( history3.id ),
  298. 'active',
  299. strings_displayed_after_submit=[ 'has been created.' ] )
  300. global history3_clone3
  301. history3_clone3 = (
  302. sa_session.query( galaxy.model.History )
  303. .filter( and_( galaxy.model.History.table.c.deleted == False,
  304. galaxy.model.History.table.c.user_id == admin_user.id ) )
  305. .order_by( desc( galaxy.model.History.table.c.create_time ) )
  306. .first()
  307. )
  308. assert history3_clone3 is not None, "Problem retrieving history3_clone3 from database"
  309. # Check list of histories to make sure shared history3 was cloned
  310. self.view_stored_active_histories( strings_displayed=[ "Copy of '%s'" % history3.name ] )
  311. # Switch to the cloned history to make sure ONLY activatable datasets were cloned
  312. self.switch_history( id=self.security.encode_id( history3_clone3.id ) )
  313. # Make sure the deleted datasets are NOT included in the cloned history
  314. # - again using the bootstrapped json for the hdas
  315. try:
  316. self.check_history_for_exact_string( '"deleted": true', show_deleted=True )
  317. #self.check_history_for_string( 'This dataset has been deleted.', show_deleted=True )
  318. raise AssertionError( "Deleted datasets incorrectly included in cloned history history3_clone3" )
  319. except:
  320. pass
  321. def test_040_sharing_mulitple_histories_with_multiple_users( self ):
  322. """Testing sharing multiple histories containing only public datasets with multiple users"""
  323. # Logged in as admin_user
  324. self.new_history()
  325. global history4
  326. history4 = sa_session.query( galaxy.model.History ) \
  327. .filter( and_( galaxy.model.History.table.c.deleted == False,
  328. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  329. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  330. .first()
  331. assert history4 is not None, "Problem retrieving history4 from database"
  332. self.rename_history( self.security.encode_id( history4.id ), history4.name, new_name=urllib.quote( 'history 4' ) )
  333. sa_session.refresh( history4 )
  334. # Galaxy's new history sharing code does not yet support sharing multiple histories; when support for sharing multiple histories is added,
  335. # this test will be uncommented and updated.
  336. """
  337. self.upload_file( '2.bed', dbkey='hg18' )
  338. ids = '%s,%s' % ( self.security.encode_id( history3.id ), self.security.encode_id( history4.id ) )
  339. emails = '%s,%s' % ( regular_user2.email, regular_user3.email )
  340. self.share_histories_with_users( ids,
  341. emails,
  342. strings_displayed=[ 'Share 2 histories', history4.name ] )
  343. self.logout()
  344. self.login( email=regular_user2.email )
  345. # Shared history3 should be in regular_user2's list of shared histories
  346. self.view_shared_histories( strings_displayed=[ history3.name, admin_user.email ] )
  347. self.logout()
  348. self.login( email=regular_user3.email )
  349. # Shared history3 should be in regular_user3's list of shared histories
  350. self.view_shared_histories( cstrings_displayed=[ history3.name, admin_user.email ] )
  351. """
  352. def test_045_change_permissions_on_current_history( self ):
  353. """Testing changing permissions on the current history"""
  354. # Logged in as regular_user3
  355. self.logout()
  356. self.login( email=admin_user.email )
  357. # Current history is history4
  358. self.new_history()
  359. global history5
  360. history5 = sa_session.query( galaxy.model.History ) \
  361. .filter( and_( galaxy.model.History.table.c.deleted == False,
  362. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  363. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  364. .first()
  365. assert history5 is not None, "Problem retrieving history5 from database"
  366. self.rename_history( self.security.encode_id( history5.id ), history5.name, new_name=urllib.quote( 'history 5' ) )
  367. # Current history is history5
  368. sa_session.refresh( history5 )
  369. # Due to the limitations of twill ( not functional with the permissions forms ), we're forced
  370. # to do this manually. At this point, we just want to restrict the access permission on history5
  371. # to the admin_user
  372. global access_action
  373. access_action = galaxy.model.Dataset.permitted_actions.DATASET_ACCESS.action
  374. dhp = galaxy.model.DefaultHistoryPermissions( history5, access_action, admin_user_private_role )
  375. sa_session.add( dhp )
  376. sa_session.flush()
  377. sa_session.refresh( history5 )
  378. global history5_default_permissions
  379. history5_default_permissions = [ dhp.action for dhp in history5.default_permissions ]
  380. # Sort for later comparison
  381. history5_default_permissions.sort()
  382. self.upload_file( '1.bed', dbkey='hg18' )
  383. history5_dataset1 = None
  384. for hda in history5.datasets:
  385. if hda.name == '1.bed':
  386. history5_dataset1 = hda.dataset
  387. break
  388. assert history5_dataset1 is not None, "Problem retrieving history5_dataset1 from the database"
  389. # The permissions on the dataset should be restricted from sharing with anyone due to the
  390. # inherited history permissions
  391. dataset_permissions = [ a.action for a in history5_dataset1.actions ]
  392. dataset_permissions.sort()
  393. if dataset_permissions != history5_default_permissions:
  394. err_msg = "Dataset permissions for history5_dataset1 (%s) were not correctly inherited from history permissions (%s)" \
  395. % ( str( dataset_permissions ), str( history5_default_permissions ) )
  396. raise AssertionError( err_msg )
  397. # Make sure when we logout and login, the history default permissions are preserved
  398. self.logout()
  399. self.login( email=admin_user.email )
  400. sa_session.refresh( history5 )
  401. current_history_permissions = [ dhp.action for dhp in history5.default_permissions ]
  402. current_history_permissions.sort()
  403. if current_history_permissions != history5_default_permissions:
  404. raise AssertionError( "With logout and login, the history default permissions are not preserved" )
  405. def test_050_sharing_restricted_history_by_making_datasets_public( self ):
  406. """Testing sharing a restricted history by making the datasets public"""
  407. # Logged in as admin_user
  408. action_strings_displayed = [ 'The following datasets can be shared with %s by updating their permissions' % regular_user1.email ]
  409. # Current history is history5
  410. self.share_current_history( regular_user1.email,
  411. action='public',
  412. action_strings_displayed=action_strings_displayed )
  413. self.logout()
  414. self.login( email=regular_user1.email )
  415. # Shared history5 should be in regular_user1's list of shared histories
  416. self.view_shared_histories( strings_displayed=[ history5.name, admin_user.email ] )
  417. # Clone restricted history5
  418. self.copy_history( self.security.encode_id( history5.id ),
  419. 'activatable',
  420. strings_displayed_after_submit=[ 'has been created.' ] )
  421. global history5_clone1
  422. history5_clone1 = sa_session.query( galaxy.model.History ) \
  423. .filter( and_( galaxy.model.History.table.c.deleted == False,
  424. galaxy.model.History.table.c.user_id == regular_user1.id ) ) \
  425. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  426. .first()
  427. assert history5_clone1 is not None, "Problem retrieving history5_clone1 from database"
  428. # Check list of histories to make sure shared history5 was cloned
  429. self.view_stored_active_histories( strings_displayed=[ "Copy of '%s'" % history5.name ] )
  430. # Make sure the dataset is accessible
  431. self.switch_history( id=self.security.encode_id( history5_clone1.id ), name=history5_clone1.name )
  432. self.check_history_for_string( 'chr1' )
  433. self.logout()
  434. self.login( email=admin_user.email )
  435. def test_055_sharing_restricted_history_by_making_new_sharing_role( self ):
  436. """Testing sharing a restricted history by associating a new sharing role with protected datasets"""
  437. # At this point, history5 should have 1 item, 1.bed, which is public. We'll add another
  438. # item which will be private to admin_user due to the permissions on history5
  439. self.upload_file( '2.bed', dbkey='hg18' )
  440. strings_displayed_after_submit = [ 'The following datasets can be shared with %s with no changes' % regular_user2.email,
  441. 'The following datasets can be shared with %s by updating their permissions' % regular_user2.email ]
  442. self.share_current_history( regular_user2.email,
  443. strings_displayed_after_submit=strings_displayed_after_submit,
  444. action='private' )
  445. # We should now have a new sharing role
  446. global sharing_role
  447. role_name = 'Sharing role for: %s, %s' % ( admin_user.email, regular_user2.email )
  448. sharing_role = sa_session.query( galaxy.model.Role ).filter( galaxy.model.Role.table.c.name == role_name ).first()
  449. if not sharing_role:
  450. # May have created a sharing role in a previous functional test suite from the opposite direction.
  451. role_name = 'Sharing role for: %s, %s' % ( regular_user2.email, admin_user.email )
  452. sharing_role = sa_session.query( galaxy.model.Role ) \
  453. .filter( and_( galaxy.model.Role.table.c.type == role_type,
  454. galaxy.model.Role.table.c.name == role_name ) ) \
  455. .first()
  456. if not sharing_role:
  457. raise AssertionError( "Privately sharing a dataset did not properly create a sharing role" )
  458. # The DATASET_ACCESS permission on 2.bed was originally associated with admin_user's private role.
  459. # Since we created a new sharing role for 2.bed, the original permission should have been eliminated,
  460. # replaced with the sharing role.
  461. history5_dataset2 = None
  462. for hda in history5.datasets:
  463. if hda.name == '2.bed':
  464. history5_dataset2 = hda.dataset
  465. break
  466. assert history5_dataset2 is not None, "Problem retrieving history5_dataset2 from the database"
  467. for dp in history5_dataset2.actions:
  468. if dp.action == 'access':
  469. assert dp.role == sharing_role, "Associating new sharing role with history5_dataset2 did not correctly eliminate original DATASET ACCESS permissions"
  470. self.logout()
  471. self.login( email=regular_user2.email )
  472. # Shared history5 should be in regular_user2's list of shared histories
  473. self.view_shared_histories( strings_displayed=[ history5.name, admin_user.email ] )
  474. # Clone restricted history5
  475. self.copy_history( self.security.encode_id( history5.id ),
  476. 'activatable',
  477. strings_displayed_after_submit=[ 'has been created.' ] )
  478. global history5_clone2
  479. history5_clone2 = sa_session.query( galaxy.model.History ) \
  480. .filter( and_( galaxy.model.History.table.c.deleted == False,
  481. galaxy.model.History.table.c.user_id == regular_user2.id ) ) \
  482. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  483. .first()
  484. assert history5_clone2 is not None, "Problem retrieving history5_clone2 from database"
  485. # Check list of histories to make sure shared history3 was cloned
  486. self.view_stored_active_histories( strings_displayed=[ "Copy of '%s'" % history5.name ] )
  487. # Make sure the dataset is accessible
  488. self.switch_history( id=self.security.encode_id( history5_clone2.id ), name=history5_clone2.name )
  489. # Make sure both datasets are in the history
  490. self.check_history_for_string( '1.bed' )
  491. self.check_history_for_string( '2.bed' )
  492. # Get both new hdas from the db that were created for the shared history
  493. hda_1_bed = sa_session.query( galaxy.model.HistoryDatasetAssociation ) \
  494. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history5_clone2.id,
  495. galaxy.model.HistoryDatasetAssociation.table.c.name == '1.bed' ) ) \
  496. .first()
  497. assert hda_1_bed is not None, "Problem retrieving hda_1_bed from database"
  498. hda_2_bed = sa_session.query( galaxy.model.HistoryDatasetAssociation ) \
  499. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history5_clone2.id,
  500. galaxy.model.HistoryDatasetAssociation.table.c.name == '2.bed' ) ) \
  501. .first()
  502. assert hda_2_bed is not None, "Problem retrieving hda_2_bed from database"
  503. # Make sure 1.bed is accessible since it is public
  504. self.display_history_item( str( hda_1_bed.id ), strings_displayed=[ 'chr1' ] )
  505. # Make sure 2.bed is accessible since it is associated with a sharing role
  506. self.display_history_item( str( hda_2_bed.id ), strings_displayed=[ 'chr1' ] )
  507. # Delete the clone so the next test will be valid
  508. self.delete_history( id=self.security.encode_id( history5_clone2.id ) )
  509. def test_060_sharing_restricted_history_with_multiple_users_by_changing_no_permissions( self ):
  510. """Testing sharing a restricted history with multiple users, making no permission changes"""
  511. # Logged in as regular_user2
  512. self.logout()
  513. self.login( email=admin_user.email )
  514. # History5 can be shared with any user, since it contains a public dataset ( 1.bed ). However, only
  515. # regular_user2 should be able to access history5's 2.bed dataset since it is associated with a
  516. # sharing role, and regular_user3 should be able to access history5's 1.bed, but not 2.bed even
  517. # though they can see it in their shared history.
  518. # We first need to unshare history5 from regular_user2 so that we can re-share it.
  519. self.unshare_history( self.security.encode_id( history5.id ),
  520. self.security.encode_id( regular_user2.id ),
  521. strings_displayed=[ regular_user1.email, regular_user2.email ] )
  522. # Make sure the history was unshared correctly
  523. self.logout()
  524. self.login( email=regular_user2.email )
  525. self.visit_page( "root/history_options" )
  526. try:
  527. self.check_page_for_string( 'List</a> histories shared with you by others' )
  528. raise AssertionError( "history5 still shared with regular_user2 after unsharing it with that user." )
  529. except:
  530. pass
  531. self.logout()
  532. self.login( admin_user.email )
  533. email = '%s,%s' % ( regular_user2.email, regular_user3.email )
  534. strings_displayed_after_submit = [
  535. 'The following datasets can be shared with %s with no changes' % email,
  536. 'The following datasets can be shared with %s by updating their permissions' % email
  537. ]
  538. # history5 will be shared with regular_user1, regular_user2 and regular_user3
  539. self.share_current_history( email,
  540. strings_displayed_after_submit=strings_displayed_after_submit,
  541. action='share_anyway' )
  542. # Check security on copy of history5 for regular_user2
  543. self.logout()
  544. self.login( email=regular_user2.email )
  545. # Shared history5 should be in regular_user2's list of shared histories
  546. self.view_shared_histories( strings_displayed=[ history5.name, admin_user.email ] )
  547. # Clone restricted history5
  548. self.copy_history( self.security.encode_id( history5.id ),
  549. 'activatable',
  550. strings_displayed_after_submit=[ 'has been created.' ] )
  551. global history5_clone3
  552. history5_clone3 = (
  553. sa_session.query( galaxy.model.History )
  554. .filter( and_( galaxy.model.History.table.c.deleted == False,
  555. galaxy.model.History.table.c.user_id == regular_user2.id ) )
  556. .order_by( desc( galaxy.model.History.table.c.create_time ) )
  557. .first() )
  558. assert history5_clone3 is not None, "Problem retrieving history5_clone3 from database"
  559. # Check list of histories to make sure shared history3 was cloned
  560. self.view_stored_active_histories( strings_displayed=[ "Copy of '%s'" % history5.name ] )
  561. # Make sure the dataset is accessible
  562. self.switch_history( id=self.security.encode_id( history5_clone3.id ), name=history5_clone3.name )
  563. # Make sure both datasets are in the history
  564. self.check_history_for_string( '1.bed' )
  565. self.check_history_for_string( '2.bed' )
  566. # Get both new hdas from the db that were created for the shared history
  567. hda_1_bed = (
  568. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  569. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history5_clone3.id,
  570. galaxy.model.HistoryDatasetAssociation.table.c.name == '1.bed' ) )
  571. .first() )
  572. assert hda_1_bed is not None, "Problem retrieving hda_1_bed from database"
  573. hda_2_bed = (
  574. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  575. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history5_clone3.id,
  576. galaxy.model.HistoryDatasetAssociation.table.c.name == '2.bed' ) )
  577. .first() )
  578. assert hda_2_bed is not None, "Problem retrieving hda_2_bed from database"
  579. # Make sure 1.bed is accessible since it is public
  580. self.display_history_item( str( hda_1_bed.id ), strings_displayed=[ 'chr1' ] )
  581. # Make sure 2.bed is accessible since it is associated with a sharing role
  582. self.display_history_item( str( hda_2_bed.id ), strings_displayed=[ 'chr1' ] )
  583. # Delete the clone so the next test will be valid
  584. self.delete_history( id=self.security.encode_id( history5_clone3.id ) )
  585. # Check security on copy of history5 for regular_user3
  586. self.logout()
  587. self.login( email=regular_user3.email )
  588. # Shared history5 should be in regular_user2's list of shared histories
  589. self.view_shared_histories( strings_displayed=[ history5.name, admin_user.email ] )
  590. # Clone restricted history5
  591. self.copy_history( self.security.encode_id( history5.id ),
  592. 'activatable',
  593. strings_displayed_after_submit=[ 'has been created.' ] )
  594. global history5_clone4
  595. history5_clone4 = (
  596. sa_session.query( galaxy.model.History )
  597. .filter( and_( galaxy.model.History.table.c.deleted == False,
  598. galaxy.model.History.table.c.user_id == regular_user3.id ) )
  599. .order_by( desc( galaxy.model.History.table.c.create_time ) )
  600. .first() )
  601. assert history5_clone4 is not None, "Problem retrieving history5_clone4 from database"
  602. # Check list of histories to make sure shared history3 was cloned
  603. self.view_stored_active_histories( strings_displayed=[ "Copy of '%s'" % history5.name ] )
  604. # Make sure the dataset is accessible
  605. self.switch_history( id=self.security.encode_id( history5_clone4.id ), name=history5_clone4.name )
  606. # Make sure both datasets are in the history
  607. self.check_history_for_string( '1.bed' )
  608. self.check_history_for_string( '2.bed' )
  609. # Get both new hdas from the db that were created for the shared history
  610. hda_1_bed = (
  611. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  612. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history5_clone4.id,
  613. galaxy.model.HistoryDatasetAssociation.table.c.name == '1.bed' ) )
  614. .first() )
  615. assert hda_1_bed is not None, "Problem retrieving hda_1_bed from database"
  616. hda_2_bed = (
  617. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  618. .filter( and_( galaxy.model.HistoryDatasetAssociation.table.c.history_id == history5_clone4.id,
  619. galaxy.model.HistoryDatasetAssociation.table.c.name == '2.bed' ) )
  620. .first() )
  621. assert hda_2_bed is not None, "Problem retrieving hda_2_bed from database"
  622. # Make sure 1.bed is accessible since it is public
  623. self.display_history_item( str( hda_1_bed.id ), strings_displayed=[ 'chr1' ] )
  624. # Make sure 2.bed is not accessible since it is protected
  625. try:
  626. self.display_history_item( str( hda_2_bed.id ), strings_displayed=[ 'chr1' ] )
  627. raise AssertionError( "History item 2.bed is accessible by user %s when is should not be" % regular_user3.email )
  628. except:
  629. pass
  630. # check the history page json for hda_2_bed and if it's accessible
  631. def hda_2_bed_is_inaccessible( hda_list ):
  632. for hda in hda_list:
  633. if hda[ 'id' ] == self.security.encode_id( hda_2_bed.id ):
  634. return ( not hda[ 'accessible' ] )
  635. return False
  636. self.check_history_json( r'\bhdaJSON\s*=\s*(.*);', hda_2_bed_is_inaccessible )
  637. # Admin users can view all datasets ( using the history/view feature ), so make sure 2.bed is accessible to the admin
  638. self.logout()
  639. self.login( email=admin_user.email )
  640. self.view_history( str( hda_2_bed.history_id ), strings_displayed=[ '<td>NM_005997_cds_0_0_chr1_147962193_r</td>' ] )
  641. self.logout()
  642. self.login( email=regular_user3.email )
  643. # Delete the clone so the next test will be valid
  644. self.delete_history( id=self.security.encode_id( history5_clone4.id ) )
  645. def test_065_sharing_private_history_by_choosing_to_not_share( self ):
  646. """Testing sharing a restricted history with multiple users by choosing not to share"""
  647. # Logged in as regular_user3 - login as admin
  648. self.logout()
  649. self.login( email=admin_user.email )
  650. # Unshare history5 from regular_user2
  651. self.unshare_history( self.security.encode_id( history5.id ),
  652. self.security.encode_id( regular_user2.id ),
  653. strings_displayed=[ regular_user1.email, regular_user2.email ] )
  654. # Unshare history5 from regular_user3
  655. self.unshare_history( self.security.encode_id( history5.id ),
  656. self.security.encode_id( regular_user3.id ),
  657. strings_displayed=[ regular_user1.email, regular_user3.email ] )
  658. # Make sure the histories were unshared correctly
  659. self.logout()
  660. self.login( email=regular_user2.email )
  661. self.visit_page( "root/history_options" )
  662. try:
  663. self.check_page_for_string( 'List</a> histories shared with you by others' )
  664. raise AssertionError( "history5 still shared with regular_user2 after unshaing it with that user." )
  665. except:
  666. pass
  667. self.logout()
  668. self.login( email=regular_user3.email )
  669. self.visit_page( "root/history_options" )
  670. try:
  671. self.check_page_for_string( 'List</a> histories shared with you by others' )
  672. raise AssertionError( "history5 still shared with regular_user3 after unshaing it with that user." )
  673. except:
  674. pass
  675. self.logout()
  676. self.login( email=admin_user.email )
  677. def test_070_history_show_and_hide_deleted_datasets( self ):
  678. """Testing displaying deleted history items"""
  679. #NOTE: due to the new client-side rendering of the history, this test isn't very apt
  680. # (a) searching for strings in the dom doesn't work (they won't be twill's html) and
  681. # (b) all datasets are included in the bootstrapped hda json regardless of the show_deleted setting
  682. #CE: for now, I'm changing this to simply check whether the show_deleted flag
  683. # is being properly passed to the history control
  684. #TODO: this test needs to be moved to client-side testing framework (selenium or other)
  685. # Logged in as admin_user
  686. # create a new history and upload a new hda (1.bed) into it
  687. self.new_history( name=urllib.quote( 'show hide deleted datasets' ) )
  688. latest_history = (
  689. sa_session.query( galaxy.model.History )
  690. .filter( and_( galaxy.model.History.table.c.deleted == False,
  691. galaxy.model.History.table.c.user_id == admin_user.id ) )
  692. .order_by( desc( galaxy.model.History.table.c.create_time ) )
  693. .first() )
  694. assert latest_history is not None, "Problem retrieving latest_history from database"
  695. self.upload_file('1.bed', dbkey='hg18')
  696. latest_hda = (
  697. sa_session.query( galaxy.model.HistoryDatasetAssociation )
  698. .order_by( desc( galaxy.model.HistoryDatasetAssociation.table.c.create_time ) )
  699. .first() )
  700. # delete that item and make sure the 'history empty' message shows
  701. self.home()
  702. log.info( 'deleting last hda' )
  703. self.delete_history_item( str( latest_hda.id ) )
  704. # check the historyPanel settings.show_deleted for a null json value (no show_deleted in query string)
  705. self.check_history_json( r'\bshow_deleted\s*:\s*(.*),', lambda x: x == None )
  706. # reload this history with the show_deleted flag set in the query string
  707. # the deleted dataset should be there with the proper 'deleted' text
  708. self.home()
  709. log.info( 'turning show_deleted on' )
  710. #self.visit_url( "%s/history/?show_deleted=True" % self.url )
  711. # check the historyPanel settings.show_deleted for a true json value
  712. self.check_history_json( r'\bshow_deleted\s*:\s*(.*),', lambda x: x == True, show_deleted=True )
  713. # reload this history again with the show_deleted flag set TO FALSE in the query string
  714. # make sure the 'history empty' message shows
  715. self.home()
  716. log.info( 'turning show_deleted off' )
  717. #self.visit_url( "%s/history/?show_deleted=False" % self.url )
  718. # check the historyPanel settings.show_deleted for a false json value
  719. self.check_history_json( r'\bshow_deleted\s*:\s*(.*),', lambda x: x == False, show_deleted=False )
  720. # delete this history
  721. self.delete_history( self.security.encode_id( latest_history.id ) )
  722. def test_075_deleting_and_undeleting_history_items( self ):
  723. """Testing deleting and un-deleting history items"""
  724. # logged in as admin_user
  725. # Deleting the current history in the last method created a new history
  726. latest_history = (
  727. sa_session.query( galaxy.model.History )
  728. .filter( and_( galaxy.model.History.table.c.deleted == False,
  729. galaxy.model.History.table.c.user_id == admin_user.id ) )
  730. .order_by( desc( galaxy.model.History.table.c.create_time ) )
  731. .first() )
  732. assert latest_history is not None, "Problem retrieving latest_history from database"
  733. self.rename_history( self.security.encode_id( latest_history.id ),
  734. latest_history.name, new_name=urllib.quote( 'delete undelete history items' ) )
  735. # Add a new history item
  736. self.upload_file( '1.bed', dbkey='hg15' )
  737. latest_hda = sa_session.query( galaxy.model.HistoryDatasetAssociation ) \
  738. .order_by( desc( galaxy.model.HistoryDatasetAssociation.table.c.create_time ) ) \
  739. .first()
  740. self.home()
  741. self.visit_url( "%s/history/?show_deleted=False" % self.url )
  742. self.check_page_for_string( '1.bed' )
  743. self.check_page_for_string( 'hg15' )
  744. self.assertEqual( len( self.get_history_as_data_list() ), 1 )
  745. # Delete the history item
  746. self.delete_history_item( str( latest_hda.id ), strings_displayed=[ "Your history is empty" ] )
  747. self.assertEqual( len( self.get_history_as_data_list() ), 0 )
  748. # Try deleting an invalid hid
  749. try:
  750. self.delete_history_item( 'XXX' )
  751. raise AssertionError( "Inproperly able to delete hda_id 'XXX' which is not an integer" )
  752. except:
  753. pass
  754. # Undelete the history item
  755. self.undelete_history_item( str( latest_hda.id ) )
  756. self.home()
  757. self.visit_url( "%s/history/?show_deleted=False" % self.url )
  758. self.check_page_for_string( '1.bed' )
  759. self.check_page_for_string( 'hg15' )
  760. self.delete_history( self.security.encode_id( latest_history.id ) )
  761. def test_080_copying_history_items_between_histories( self ):
  762. """Testing copying history items between histories"""
  763. # logged in as admin_user
  764. self.new_history( name=urllib.quote( 'copy history items' ) )
  765. history6 = sa_session.query( galaxy.model.History ) \
  766. .filter( and_( galaxy.model.History.table.c.deleted == False,
  767. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  768. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  769. .first()
  770. assert history6 is not None, "Problem retrieving history6 from database"
  771. self.upload_file( '1.bed', dbkey='hg18' )
  772. hda1 = sa_session.query( galaxy.model.HistoryDatasetAssociation ) \
  773. .order_by( desc( galaxy.model.HistoryDatasetAssociation.table.c.create_time ) ) \
  774. .first()
  775. assert hda1 is not None, "Problem retrieving hda1 from database"
  776. # We'll just test copying 1 hda
  777. source_dataset_ids = self.security.encode_id( hda1.id )
  778. # The valid list of target histories is only the user's active histories
  779. all_target_history_ids = [ self.security.encode_id( hda.id ) for hda in admin_user.active_histories ]
  780. # Since history1 and history2 have been deleted, they should not be displayed in the list of target histories
  781. # on the copy_view.mako form
  782. deleted_history_ids = [ self.security.encode_id( history1.id ), self.security.encode_id( history2.id ) ]
  783. # Test copying to the current history
  784. target_history_id = self.security.encode_id( history6.id )
  785. self.copy_history_item( source_dataset_id=source_dataset_ids,
  786. target_history_id=target_history_id,
  787. all_target_history_ids=all_target_history_ids,
  788. deleted_history_ids=deleted_history_ids )
  789. sa_session.refresh( history6 )
  790. if len( history6.datasets ) != 2:
  791. raise AssertionError( "Copying hda1 to the current history failed, history 6 has %d datasets, but should have 2" % len( history6.datasets ) )
  792. # Test copying 1 hda to another history
  793. self.new_history( name=urllib.quote( 'copy history items - 2' ) )
  794. history7 = sa_session.query( galaxy.model.History ) \
  795. .filter( and_( galaxy.model.History.table.c.deleted == False,
  796. galaxy.model.History.table.c.user_id == admin_user.id ) ) \
  797. .order_by( desc( galaxy.model.History.table.c.create_time ) ) \
  798. .first()
  799. assert history7 is not None, "Problem retrieving history7 from database"
  800. # Switch back to our history from which we want to copy
  801. self.switch_history( id=self.security.encode_id( history6.id ), name=history6.name )
  802. target_history_id = self.security.encode_id( history7.id )
  803. all_target_history_ids = [ self.security.encode_id( hda.id ) for hda in admin_user.active_histories ]
  804. # Test copying to the a history that is not the current history
  805. self.security.encode_id( history7.id )
  806. self.copy_history_item( source_dataset_id=source_dataset_ids,
  807. target_history_id=target_history_id,
  808. all_target_history_ids=all_target_history_ids,
  809. deleted_history_ids=deleted_history_ids )
  810. # Switch to the history to which we copied
  811. self.switch_history( id=self.security.encode_id( history7.id ), name=history7.name )
  812. self.check_history_for_string( hda1.name )
  813. self.delete_history( self.security.encode_id( history6.id ) )
  814. self.delete_history( self.security.encode_id( history7.id ) )
  815. def test_085_reset_data_for_later_test_runs( self ):
  816. """Reseting data to enable later test runs to to be valid"""
  817. # logged in as admin_user
  818. # Clean up admin_user
  819. # Unshare history3 - shared with regular_user1, regular_user2, regular_user3
  820. self.unshare_history( self.security.encode_id( history3.id ),
  821. self.security.encode_id( regular_user1.id ) )
  822. self.unshare_history( self.security.encode_id( history3.id ),
  823. self.security.encode_id( regular_user2.id ) )
  824. self.unshare_history( self.security.encode_id( history3.id ),
  825. self.security.encode_id( regular_user3.id ) )
  826. # Unshare history4 - shared with regular_user2, regular_user3
  827. self.unshare_history( self.security.encode_id( history4.id ),
  828. self.security.encode_id( regular_user2.id ) )
  829. self.unshare_history( self.security.encode_id( history4.id ),
  830. self.security.encode_id( regular_user3.id ) )
  831. # Unshare history5 - shared with regular_user1
  832. self.unshare_history( self.security.encode_id( history5.id ),
  833. self.security.encode_id( regular_user1.id ) )
  834. # Delete histories
  835. self.delete_history( id=self.security.encode_id( history3.id ) )
  836. self.delete_history( id=self.security.encode_id( history3_clone2.id ) )
  837. self.delete_history( id=self.security.encode_id( history3_clone3.id ) )
  838. self.delete_history( id=self.security.encode_id( history4.id ) )
  839. self.delete_history( id=self.security.encode_id( history5.id ) )
  840. # Eliminate Sharing role for: test@bx.psu.edu, test2@bx.psu.edu
  841. self.mark_role_deleted( self.security.encode_id( sharing_role.id ), sharing_role.name )
  842. self.purge_role( self.security.encode_id( sharing_role.id ), sharing_role.name )
  843. # Manually delete the sharing role from the database
  844. sa_session.refresh( sharing_role )
  845. sa_session.delete( sharing_role )
  846. sa_session.flush()
  847. # Clean up regular_user_1
  848. self.logout()
  849. self.login( email=regular_user1.email )
  850. self.delete_history( id=self.security.encode_id( history3_clone1.id ) )
  851. self.delete_history( id=self.security.encode_id( history5_clone1.id ) )