3 class PermissionsTest < ActionDispatch::IntegrationTest
4 include CurrentApiClient # for empty_collection
5 fixtures :users, :groups, :api_client_authorizations, :collections
7 test "adding and removing direct can_read links" do
8 # try to read collection as spectator
9 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
12 # try to add permission as spectator
13 post "/arvados/v1/links", {
16 tail_uuid: users(:spectator).uuid,
17 link_class: 'permission',
19 head_uuid: collections(:foo_file).uuid,
25 # add permission as admin
26 post "/arvados/v1/links", {
29 tail_uuid: users(:spectator).uuid,
30 link_class: 'permission',
32 head_uuid: collections(:foo_file).uuid,
36 u = json_response['uuid']
37 assert_response :success
39 # read collection as spectator
40 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
41 assert_response :success
43 # try to delete permission as spectator
44 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:spectator)
47 # delete permission as admin
48 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
49 assert_response :success
51 # try to read collection as spectator
52 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
57 test "adding can_read links from user to group, group to collection" do
58 # try to read collection as spectator
59 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
62 # add permission for spectator to read group
63 post "/arvados/v1/links", {
66 tail_uuid: users(:spectator).uuid,
67 link_class: 'permission',
69 head_uuid: groups(:private).uuid,
73 assert_response :success
75 # try to read collection as spectator
76 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
79 # add permission for group to read collection
80 post "/arvados/v1/links", {
83 tail_uuid: groups(:private).uuid,
84 link_class: 'permission',
86 head_uuid: collections(:foo_file).uuid,
90 u = json_response['uuid']
91 assert_response :success
93 # try to read collection as spectator
94 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
95 assert_response :success
97 # delete permission for group to read collection
98 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
99 assert_response :success
101 # try to read collection as spectator
102 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
108 test "adding can_read links from group to collection, user to group" do
109 # try to read collection as spectator
110 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
113 # add permission for group to read collection
114 post "/arvados/v1/links", {
117 tail_uuid: groups(:private).uuid,
118 link_class: 'permission',
120 head_uuid: collections(:foo_file).uuid,
124 assert_response :success
126 # try to read collection as spectator
127 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
130 # add permission for spectator to read group
131 post "/arvados/v1/links", {
134 tail_uuid: users(:spectator).uuid,
135 link_class: 'permission',
137 head_uuid: groups(:private).uuid,
141 u = json_response['uuid']
142 assert_response :success
144 # try to read collection as spectator
145 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
146 assert_response :success
148 # delete permission for spectator to read group
149 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
150 assert_response :success
152 # try to read collection as spectator
153 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
158 test "adding can_read links from user to group, group to group, group to collection" do
159 # try to read collection as spectator
160 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
163 # add permission for user to read group
164 post "/arvados/v1/links", {
167 tail_uuid: users(:spectator).uuid,
168 link_class: 'permission',
170 head_uuid: groups(:private).uuid,
174 assert_response :success
176 # add permission for group to read group
177 post "/arvados/v1/links", {
180 tail_uuid: groups(:private).uuid,
181 link_class: 'permission',
183 head_uuid: groups(:empty_lonely_group).uuid,
187 assert_response :success
189 # add permission for group to read collection
190 post "/arvados/v1/links", {
193 tail_uuid: groups(:empty_lonely_group).uuid,
194 link_class: 'permission',
196 head_uuid: collections(:foo_file).uuid,
200 u = json_response['uuid']
201 assert_response :success
203 # try to read collection as spectator
204 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
205 assert_response :success
207 # delete permission for group to read collection
208 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
209 assert_response :success
211 # try to read collection as spectator
212 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
216 test "read-only group-admin cannot modify administered user" do
217 put "/arvados/v1/users/#{users(:active).uuid}", {
219 first_name: 'KilroyWasHere'
222 }, auth(:rominiadmin)
226 test "read-only group-admin cannot read or update non-administered user" do
227 get "/arvados/v1/users/#{users(:spectator).uuid}", {
229 }, auth(:rominiadmin)
232 put "/arvados/v1/users/#{users(:spectator).uuid}", {
234 first_name: 'KilroyWasHere'
237 }, auth(:rominiadmin)
241 test "RO group-admin finds user's specimens, RW group-admin can update" do
242 [[:rominiadmin, false],
243 [:miniadmin, true]].each do |which_user, update_should_succeed|
244 get "/arvados/v1/specimens", {:format => :json}, auth(which_user)
245 assert_response :success
246 resp_uuids = json_response['items'].collect { |i| i['uuid'] }
247 [[true, specimens(:owned_by_active_user).uuid],
248 [true, specimens(:owned_by_private_group).uuid],
249 [false, specimens(:owned_by_spectator).uuid],
250 ].each do |should_find, uuid|
251 assert_equal(should_find, !resp_uuids.index(uuid).nil?,
252 "%s should%s see %s in specimen list" %
254 should_find ? '' : 'not ',
256 put "/arvados/v1/specimens/#{uuid}", {
259 miniadmin_was_here: true
266 elsif !update_should_succeed
269 assert_response :success
275 test "get_permissions returns list" do
276 # First confirm that user :active cannot get permissions on group :public
277 get "/arvados/v1/permissions/#{groups(:public).uuid}", nil, auth(:active)
280 # add some permissions, including can_manage
281 # permission for user :active
282 post "/arvados/v1/links", {
285 tail_uuid: users(:spectator).uuid,
286 link_class: 'permission',
288 head_uuid: groups(:public).uuid,
292 assert_response :success
293 can_read_uuid = json_response['uuid']
295 post "/arvados/v1/links", {
298 tail_uuid: users(:inactive).uuid,
299 link_class: 'permission',
301 head_uuid: groups(:public).uuid,
305 assert_response :success
306 can_write_uuid = json_response['uuid']
308 post "/arvados/v1/links", {
311 tail_uuid: users(:active).uuid,
312 link_class: 'permission',
314 head_uuid: groups(:public).uuid,
318 assert_response :success
319 can_manage_uuid = json_response['uuid']
321 # Now user :active should be able to retrieve permissions
323 get("/arvados/v1/permissions/#{groups(:public).uuid}",
324 { :format => :json },
326 assert_response :success
328 perm_uuids = json_response['items'].map { |item| item['uuid'] }
329 assert_includes perm_uuids, can_read_uuid, "can_read_uuid not found"
330 assert_includes perm_uuids, can_write_uuid, "can_write_uuid not found"
331 assert_includes perm_uuids, can_manage_uuid, "can_manage_uuid not found"
334 test "get_permissions returns 404 for nonexistent uuid" do
335 nonexistent = Group.generate_uuid
336 # make sure it really doesn't exist
337 get "/arvados/v1/groups/#{nonexistent}", nil, auth(:admin)
340 get "/arvados/v1/permissions/#{nonexistent}", nil, auth(:active)
344 test "get_permissions returns 404 for unreadable uuid" do
345 get "/arvados/v1/permissions/#{groups(:public).uuid}", nil, auth(:active)
349 test "get_permissions returns 403 if user can read but not manage" do
350 post "/arvados/v1/links", {
352 tail_uuid: users(:active).uuid,
353 link_class: 'permission',
355 head_uuid: groups(:public).uuid,
359 assert_response :success
361 get "/arvados/v1/permissions/#{groups(:public).uuid}", nil, auth(:active)
365 test "active user can read the empty collection" do
366 # The active user should be able to read the empty collection.
368 get("/arvados/v1/collections/#{empty_collection_uuid}",
369 { :format => :json },
371 assert_response :success
372 assert_empty json_response['manifest_text'], "empty collection manifest_text is not empty"