1 | from base.twilltestcase import * |
---|
2 | from base.test_db_util import * |
---|
3 | |
---|
4 | class TestDataSecurity( 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_default_permissions( self ): |
---|
36 | """Testing initial settings for DefaultUserPermissions and DefaultHistoryPermissions""" |
---|
37 | # Logged in as admin_user |
---|
38 | # Make sure DefaultUserPermissions are correct |
---|
39 | dups = get_default_user_permissions_by_user( admin_user ) |
---|
40 | if len( dups ) > 1: |
---|
41 | raise AssertionError( '%d DefaultUserPermissions associated with user %s ( should be 1 )' \ |
---|
42 | % ( len( admin_user.default_permissions ), admin_user.email ) ) |
---|
43 | dup = dups[0] |
---|
44 | if not dup.action == galaxy.model.Dataset.permitted_actions.DATASET_MANAGE_PERMISSIONS.action: |
---|
45 | raise AssertionError( 'The DefaultUserPermission.action for user "%s" is "%s", but it should be "%s"' \ |
---|
46 | % ( admin_user.email, dup.action, galaxy.model.Dataset.permitted_actions.DATASET_MANAGE_PERMISSIONS.action ) ) |
---|
47 | # Make sure DefaultHistoryPermissions are correct |
---|
48 | latest_history = get_latest_history_for_user( admin_user ) |
---|
49 | dhps = get_default_history_permissions_by_history( latest_history ) |
---|
50 | if len( dhps ) > 1: |
---|
51 | raise AssertionError( '%d DefaultHistoryPermissions were created for history id %d when it was created ( should have been 1 )' \ |
---|
52 | % ( len( latest_history.default_permissions ), latest_history.id ) ) |
---|
53 | dhp = dhps[0] |
---|
54 | if not dhp.action == galaxy.model.Dataset.permitted_actions.DATASET_MANAGE_PERMISSIONS.action: |
---|
55 | raise AssertionError( 'The DefaultHistoryPermission.action for history id %d is "%s", but it should be "%s"' \ |
---|
56 | % ( latest_history.id, dhp.action, galaxy.model.Dataset.permitted_actions.DATASET_MANAGE_PERMISSIONS.action ) ) |
---|
57 | self.manage_roles_and_groups_for_user( self.security.encode_id( admin_user.id ), |
---|
58 | strings_displayed=[ admin_user.email ] ) |
---|
59 | # Try deleting the admin_user's private role |
---|
60 | self.manage_roles_and_groups_for_user( self.security.encode_id( admin_user.id ), |
---|
61 | out_role_ids=str( admin_user_private_role.id ), |
---|
62 | strings_displayed = [ "You cannot eliminate a user's private role association." ] ) |
---|
63 | def test_010_private_role_creation_and_default_history_permissions( self ): |
---|
64 | """Testing private role creation and changing DefaultHistoryPermissions for new histories""" |
---|
65 | # Logged in as admin_user |
---|
66 | self.logout() |
---|
67 | # Some of the history related tests here are similar to some tests in the |
---|
68 | # test_history_functions.py script, so we could potentially eliminate 1 or 2 of them. |
---|
69 | self.login( email='test1@bx.psu.edu' ) |
---|
70 | global regular_user1 |
---|
71 | regular_user1 = get_user( 'test1@bx.psu.edu' ) |
---|
72 | assert regular_user1 is not None, 'Problem retrieving user with email "test1@bx.psu.edu" from the database' |
---|
73 | # Add a dataset to the history |
---|
74 | self.upload_file( '1.bed' ) |
---|
75 | latest_dataset = get_latest_dataset() |
---|
76 | # Make sure DatasetPermissions are correct - default is 'manage permissions' |
---|
77 | dps = get_dataset_permissions_by_dataset( latest_dataset ) |
---|
78 | if len( dps ) > 1: |
---|
79 | raise AssertionError( '%d DatasetPermissions were created for dataset id %d when it was created ( should have been 1 )' \ |
---|
80 | % ( len( dps ), latest_dataset.id ) ) |
---|
81 | dp = dps[0] |
---|
82 | if not dp.action == galaxy.model.Dataset.permitted_actions.DATASET_MANAGE_PERMISSIONS.action: |
---|
83 | raise AssertionError( 'The DatasetPermissions.action for dataset id %d is "%s", but it should be "manage permissions"' \ |
---|
84 | % ( latest_dataset.id, dp.action ) ) |
---|
85 | # Change DefaultHistoryPermissions for regular_user1 |
---|
86 | permissions_in = [] |
---|
87 | actions_in = [] |
---|
88 | for key, value in galaxy.model.Dataset.permitted_actions.items(): |
---|
89 | # Setting the 'access' permission with the private role makes this dataset private |
---|
90 | permissions_in.append( key ) |
---|
91 | actions_in.append( value.action ) |
---|
92 | # Sort actions for later comparison |
---|
93 | actions_in.sort() |
---|
94 | self.user_set_default_permissions( permissions_in=permissions_in, role_id=str( regular_user1_private_role.id ) ) |
---|
95 | # Make sure the default permissions are changed for new histories |
---|
96 | self.new_history() |
---|
97 | # logged in as regular_user1 |
---|
98 | latest_history = get_latest_history_for_user( regular_user1 ) |
---|
99 | if len( latest_history.default_permissions ) != len( actions_in ): |
---|
100 | raise AssertionError( '%d DefaultHistoryPermissions were created for history id %d, should have been %d' % \ |
---|
101 | ( len( latest_history.default_permissions ), latest_history.id, len( actions_in ) ) ) |
---|
102 | dhps = [] |
---|
103 | for dhp in latest_history.default_permissions: |
---|
104 | dhps.append( dhp.action ) |
---|
105 | # Sort permissions for later comparison |
---|
106 | dhps.sort() |
---|
107 | for key, value in galaxy.model.Dataset.permitted_actions.items(): |
---|
108 | if value.action not in dhps: |
---|
109 | raise AssertionError( '%s not in history id %d default_permissions after they were changed' % ( value.action, latest_history.id ) ) |
---|
110 | # Add a dataset to the history |
---|
111 | self.upload_file( '1.bed' ) |
---|
112 | latest_dataset = get_latest_dataset() |
---|
113 | # Make sure DatasetPermissions are correct |
---|
114 | if len( latest_dataset.actions ) != len( latest_history.default_permissions ): |
---|
115 | raise AssertionError( '%d DatasetPermissions were created for dataset id %d when it was created ( should have been %d )' % \ |
---|
116 | ( len( latest_dataset.actions ), latest_dataset.id, len( latest_history.default_permissions ) ) ) |
---|
117 | dps = [] |
---|
118 | for dp in latest_dataset.actions: |
---|
119 | dps.append( dp.action ) |
---|
120 | # Sort actions for later comparison |
---|
121 | dps.sort() |
---|
122 | # Compare DatasetPermissions with permissions_in - should be the same |
---|
123 | if dps != actions_in: |
---|
124 | raise AssertionError( 'DatasetPermissions "%s" for dataset id %d differ from changed default permissions "%s"' \ |
---|
125 | % ( str( dps ), latest_dataset.id, str( actions_in ) ) ) |
---|
126 | # Compare DefaultHistoryPermissions and DatasetPermissions - should be the same |
---|
127 | if dps != dhps: |
---|
128 | raise AssertionError( 'DatasetPermissions "%s" for dataset id %d differ from DefaultHistoryPermissions "%s" for history id %d' \ |
---|
129 | % ( str( dps ), latest_dataset.id, str( dhps ), latest_history.id ) ) |
---|
130 | def test_015_change_default_permissions_for_current_history( self ): |
---|
131 | """Testing changing DefaultHistoryPermissions for the current history""" |
---|
132 | # logged in a regular_user1 |
---|
133 | self.logout() |
---|
134 | self.login( email=regular_user2.email ) |
---|
135 | latest_history = get_latest_history_for_user( regular_user2 ) |
---|
136 | self.upload_file( '1.bed' ) |
---|
137 | latest_dataset = get_latest_dataset() |
---|
138 | permissions_in = [ 'DATASET_MANAGE_PERMISSIONS' ] |
---|
139 | # Make sure these are in sorted order for later comparison |
---|
140 | actions_in = [ 'manage permissions' ] |
---|
141 | permissions_out = [ 'DATASET_ACCESS' ] |
---|
142 | actions_out = [ 'access' ] |
---|
143 | # Change DefaultHistoryPermissions for the current history |
---|
144 | self.history_set_default_permissions( permissions_out=permissions_out, permissions_in=permissions_in, role_id=str( regular_user2_private_role.id ) ) |
---|
145 | if len( latest_history.default_permissions ) != len( actions_in ): |
---|
146 | raise AssertionError( '%d DefaultHistoryPermissions were created for history id %d, should have been %d' \ |
---|
147 | % ( len( latest_history.default_permissions ), latest_history.id, len( permissions_in ) ) ) |
---|
148 | # Make sure DefaultHistoryPermissions were correctly changed for the current history |
---|
149 | dhps = [] |
---|
150 | for dhp in latest_history.default_permissions: |
---|
151 | dhps.append( dhp.action ) |
---|
152 | # Sort permissions for later comparison |
---|
153 | dhps.sort() |
---|
154 | # Compare DefaultHistoryPermissions and actions_in - should be the same |
---|
155 | if dhps != actions_in: |
---|
156 | raise AssertionError( 'DefaultHistoryPermissions "%s" for history id %d differ from actions "%s" passed for changing' \ |
---|
157 | % ( str( dhps ), latest_history.id, str( actions_in ) ) ) |
---|
158 | # Make sure DatasetPermissionss are correct |
---|
159 | if len( latest_dataset.actions ) != len( latest_history.default_permissions ): |
---|
160 | raise AssertionError( '%d DatasetPermissionss were created for dataset id %d when it was created ( should have been %d )' \ |
---|
161 | % ( len( latest_dataset.actions ), latest_dataset.id, len( latest_history.default_permissions ) ) ) |
---|
162 | dps = [] |
---|
163 | for dp in latest_dataset.actions: |
---|
164 | dps.append( dp.action ) |
---|
165 | # Sort actions for comparison |
---|
166 | dps.sort() |
---|
167 | # Compare DatasetPermissionss and DefaultHistoryPermissions - should be the same |
---|
168 | if dps != dhps: |
---|
169 | raise AssertionError( 'DatasetPermissionss "%s" for dataset id %d differ from DefaultHistoryPermissions "%s"' \ |
---|
170 | % ( str( dps ), latest_dataset.id, str( dhps ) ) ) |
---|
171 | def test_999_reset_data_for_later_test_runs( self ): |
---|
172 | """Reseting data to enable later test runs to pass""" |
---|
173 | # Logged in as regular_user2 |
---|
174 | self.logout() |
---|
175 | self.login( email=admin_user.email ) |
---|
176 | ################## |
---|
177 | # Make sure all users are associated only with their private roles |
---|
178 | ################## |
---|
179 | for user in [ admin_user, regular_user1, regular_user2, regular_user3 ]: |
---|
180 | refresh( user ) |
---|
181 | if len( user.roles) != 1: |
---|
182 | raise AssertionError( '%d UserRoleAssociations are associated with %s ( should be 1 )' % ( len( user.roles ), user.email ) ) |
---|
183 | ##################### |
---|
184 | # Reset DefaultHistoryPermissions for regular_user1 |
---|
185 | ##################### |
---|
186 | self.logout() |
---|
187 | self.login( email=regular_user1.email ) |
---|
188 | # Change DefaultHistoryPermissions for regular_user1 back to the default |
---|
189 | permissions_in = [ 'DATASET_MANAGE_PERMISSIONS' ] |
---|
190 | permissions_out = [ 'DATASET_ACCESS' ] |
---|
191 | self.user_set_default_permissions( permissions_in=permissions_in, |
---|
192 | permissions_out=permissions_out, |
---|
193 | role_id=str( regular_user1_private_role.id ) ) |
---|
194 | self.logout() |
---|
195 | self.login( email=admin_user.email ) |
---|