Merge branch 'patch-1' of https://github.com/mr-c/arvados into mr-c-patch-1
[arvados.git] / services / api / test / integration / permissions_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class PermissionsTest < ActionDispatch::IntegrationTest
8   include DbCurrentTime
9   fixtures :users, :groups, :api_client_authorizations, :collections
10
11   test "adding and removing direct can_read links" do
12     # try to read collection as spectator
13     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
14       params: {:format => :json},
15       headers: auth(:spectator)
16     assert_response 404
17
18     # try to add permission as spectator
19     post "/arvados/v1/links",
20       params: {
21         :format => :json,
22         :link => {
23           tail_uuid: users(:spectator).uuid,
24           link_class: 'permission',
25           name: 'can_read',
26           head_uuid: collections(:foo_file).uuid,
27           properties: {}
28         }
29       },
30       headers: auth(:spectator)
31     assert_response 422
32
33     # add permission as admin
34     post "/arvados/v1/links",
35       params: {
36         :format => :json,
37         :link => {
38           tail_uuid: users(:spectator).uuid,
39           link_class: 'permission',
40           name: 'can_read',
41           head_uuid: collections(:foo_file).uuid,
42           properties: {}
43         }
44       },
45       headers: auth(:admin)
46     u = json_response['uuid']
47     assert_response :success
48
49     # read collection as spectator
50     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
51       params: {:format => :json},
52       headers: auth(:spectator)
53     assert_response :success
54
55     # try to delete permission as spectator
56     delete "/arvados/v1/links/#{u}",
57       params: {:format => :json},
58       headers: auth(:spectator)
59     assert_response 403
60
61     # delete permission as admin
62     delete "/arvados/v1/links/#{u}",
63       params: {:format => :json},
64       headers: auth(:admin)
65     assert_response :success
66
67     # try to read collection as spectator
68     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
69       params: {:format => :json},
70       headers: auth(:spectator)
71     assert_response 404
72   end
73
74
75   test "adding can_read links from user to group, group to collection" do
76     # try to read collection as spectator
77     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
78       params: {:format => :json},
79       headers: auth(:spectator)
80     assert_response 404
81
82     # add permission for spectator to read group
83     post "/arvados/v1/links",
84       params: {
85         :format => :json,
86         :link => {
87           tail_uuid: users(:spectator).uuid,
88           link_class: 'permission',
89           name: 'can_read',
90           head_uuid: groups(:private_role).uuid,
91           properties: {}
92         }
93       },
94       headers: auth(:admin)
95     assert_response :success
96
97     # try to read collection as spectator
98     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
99       params: {:format => :json},
100       headers: auth(:spectator)
101     assert_response 404
102
103     # add permission for group to read collection
104     post "/arvados/v1/links",
105       params: {
106         :format => :json,
107         :link => {
108           tail_uuid: groups(:private_role).uuid,
109           link_class: 'permission',
110           name: 'can_read',
111           head_uuid: collections(:foo_file).uuid,
112           properties: {}
113         }
114       },
115       headers: auth(:admin)
116     u = json_response['uuid']
117     assert_response :success
118
119     # try to read collection as spectator
120     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
121       params: {:format => :json},
122       headers: auth(:spectator)
123     assert_response :success
124
125     # delete permission for group to read collection
126     delete "/arvados/v1/links/#{u}",
127       params: {:format => :json},
128       headers: auth(:admin)
129     assert_response :success
130
131     # try to read collection as spectator
132     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
133       params: {:format => :json},
134       headers: auth(:spectator)
135     assert_response 404
136
137   end
138
139
140   test "adding can_read links from group to collection, user to group" do
141     # try to read collection as spectator
142     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
143       params: {:format => :json},
144       headers: auth(:spectator)
145     assert_response 404
146
147     # add permission for group to read collection
148     post "/arvados/v1/links",
149       params: {
150         :format => :json,
151         :link => {
152           tail_uuid: groups(:private_role).uuid,
153           link_class: 'permission',
154           name: 'can_read',
155           head_uuid: collections(:foo_file).uuid,
156           properties: {}
157         }
158       },
159       headers: auth(:admin)
160     assert_response :success
161
162     # try to read collection as spectator
163     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
164       params: {:format => :json},
165       headers: auth(:spectator)
166     assert_response 404
167
168     # add permission for spectator to read group
169     post "/arvados/v1/links",
170       params: {
171         :format => :json,
172         :link => {
173           tail_uuid: users(:spectator).uuid,
174           link_class: 'permission',
175           name: 'can_read',
176           head_uuid: groups(:private_role).uuid,
177           properties: {}
178         }
179       },
180       headers: auth(:admin)
181     u = json_response['uuid']
182     assert_response :success
183
184     # try to read collection as spectator
185     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
186       params: {:format => :json},
187       headers: auth(:spectator)
188     assert_response :success
189
190     # delete permission for spectator to read group
191     delete "/arvados/v1/links/#{u}",
192       params: {:format => :json},
193       headers: auth(:admin)
194     assert_response :success
195
196     # try to read collection as spectator
197     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
198       params: {:format => :json},
199       headers: auth(:spectator)
200     assert_response 404
201
202   end
203
204   test "adding can_read links from user to group, group to group, group to collection" do
205     # try to read collection as spectator
206     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
207       params: {:format => :json},
208       headers: auth(:spectator)
209     assert_response 404
210
211     # add permission for user to read group
212     post "/arvados/v1/links",
213       params: {
214         :format => :json,
215         :link => {
216           tail_uuid: users(:spectator).uuid,
217           link_class: 'permission',
218           name: 'can_read',
219           head_uuid: groups(:private_role).uuid,
220           properties: {}
221         }
222       },
223       headers: auth(:admin)
224     assert_response :success
225
226     # add permission for group to read group
227     post "/arvados/v1/links",
228       params: {
229         :format => :json,
230         :link => {
231           tail_uuid: groups(:private_role).uuid,
232           link_class: 'permission',
233           name: 'can_read',
234           head_uuid: groups(:empty_lonely_group).uuid,
235           properties: {}
236         }
237       },
238       headers: auth(:admin)
239     assert_response :success
240
241     # add permission for group to read collection
242     post "/arvados/v1/links",
243       params: {
244         :format => :json,
245         :link => {
246           tail_uuid: groups(:empty_lonely_group).uuid,
247           link_class: 'permission',
248           name: 'can_read',
249           head_uuid: collections(:foo_file).uuid,
250           properties: {}
251         }
252       },
253       headers: auth(:admin)
254     u = json_response['uuid']
255     assert_response :success
256
257     # try to read collection as spectator
258     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
259       params: {:format => :json},
260       headers: auth(:spectator)
261     assert_response :success
262
263     # delete permission for group to read collection
264     delete "/arvados/v1/links/#{u}",
265       params: {:format => :json},
266       headers: auth(:admin)
267     assert_response :success
268
269     # try to read collection as spectator
270     get "/arvados/v1/collections/#{collections(:foo_file).uuid}",
271       params: {:format => :json},
272       headers: auth(:spectator)
273     assert_response 404
274   end
275
276   test "read-only group-admin cannot modify administered user" do
277     put "/arvados/v1/users/#{users(:active).uuid}",
278       params: {
279         :user => {
280           first_name: 'KilroyWasHere'
281         },
282         :format => :json
283       },
284       headers: auth(:rominiadmin)
285     assert_response 403
286   end
287
288   test "read-only group-admin cannot read or update non-administered user" do
289     get "/arvados/v1/users/#{users(:spectator).uuid}",
290       params: {:format => :json},
291       headers: auth(:rominiadmin)
292     assert_response 404
293
294     put "/arvados/v1/users/#{users(:spectator).uuid}",
295       params: {
296         :user => {
297           first_name: 'KilroyWasHere'
298         },
299         :format => :json
300       },
301       headers: auth(:rominiadmin)
302     assert_response 404
303   end
304
305   test "RO group-admin finds user's specimens, RW group-admin can update" do
306     [[:rominiadmin, false],
307      [:miniadmin, true]].each do |which_user, update_should_succeed|
308       get "/arvados/v1/specimens",
309         params: {:format => :json},
310         headers: auth(which_user)
311       assert_response :success
312       resp_uuids = json_response['items'].collect { |i| i['uuid'] }
313       [[true, specimens(:owned_by_active_user).uuid],
314        [true, specimens(:owned_by_private_group).uuid],
315        [false, specimens(:owned_by_spectator).uuid],
316       ].each do |should_find, uuid|
317         assert_equal(should_find, !resp_uuids.index(uuid).nil?,
318                      "%s should%s see %s in specimen list" %
319                      [which_user.to_s,
320                       should_find ? '' : 'not ',
321                       uuid])
322         put "/arvados/v1/specimens/#{uuid}",
323           params: {
324             :specimen => {
325               properties: {
326                 miniadmin_was_here: true
327               }
328             },
329             :format => :json
330           },
331           headers: auth(which_user)
332         if !should_find
333           assert_response 404
334         elsif !update_should_succeed
335           assert_response 403
336         else
337           assert_response :success
338         end
339       end
340     end
341   end
342
343   test "get_permissions returns list" do
344     # First confirm that user :active cannot get permissions on group :public
345     get "/arvados/v1/permissions/#{groups(:public).uuid}",
346       params: nil,
347       headers: auth(:active)
348     assert_response 404
349
350     # add some permissions, including can_manage
351     # permission for user :active
352     post "/arvados/v1/links",
353       params: {
354         :format => :json,
355         :link => {
356           tail_uuid: users(:spectator).uuid,
357           link_class: 'permission',
358           name: 'can_read',
359           head_uuid: groups(:public).uuid,
360           properties: {}
361         }
362       },
363       headers: auth(:admin)
364     assert_response :success
365     can_read_uuid = json_response['uuid']
366
367     post "/arvados/v1/links",
368       params: {
369         :format => :json,
370         :link => {
371           tail_uuid: users(:inactive).uuid,
372           link_class: 'permission',
373           name: 'can_write',
374           head_uuid: groups(:public).uuid,
375           properties: {}
376         }
377       },
378       headers: auth(:admin)
379     assert_response :success
380     can_write_uuid = json_response['uuid']
381
382     post "/arvados/v1/links",
383       params: {
384         :format => :json,
385         :link => {
386           tail_uuid: users(:active).uuid,
387           link_class: 'permission',
388           name: 'can_manage',
389           head_uuid: groups(:public).uuid,
390           properties: {}
391         }
392       },
393       headers: auth(:admin)
394     assert_response :success
395     can_manage_uuid = json_response['uuid']
396
397     # Now user :active should be able to retrieve permissions
398     # on group :public.
399     get("/arvados/v1/permissions/#{groups(:public).uuid}",
400       params: { :format => :json },
401       headers: auth(:active))
402     assert_response :success
403
404     perm_uuids = json_response['items'].map { |item| item['uuid'] }
405     assert_includes perm_uuids, can_read_uuid, "can_read_uuid not found"
406     assert_includes perm_uuids, can_write_uuid, "can_write_uuid not found"
407     assert_includes perm_uuids, can_manage_uuid, "can_manage_uuid not found"
408   end
409
410   test "get_permissions returns 404 for nonexistent uuid" do
411     nonexistent = Group.generate_uuid
412     # make sure it really doesn't exist
413     get "/arvados/v1/groups/#{nonexistent}", params: nil, headers: auth(:admin)
414     assert_response 404
415
416     get "/arvados/v1/permissions/#{nonexistent}", params: nil, headers: auth(:active)
417     assert_response 404
418   end
419
420   test "get_permissions returns 403 if user can read but not manage" do
421     post "/arvados/v1/links",
422       params: {
423         :link => {
424           tail_uuid: users(:active).uuid,
425           link_class: 'permission',
426           name: 'can_read',
427           head_uuid: groups(:public).uuid,
428           properties: {}
429         }
430       },
431       headers: auth(:admin)
432     assert_response :success
433
434     get "/arvados/v1/permissions/#{groups(:public).uuid}",
435       params: nil,
436       headers: auth(:active)
437     assert_response 403
438   end
439
440   test "active user can read the empty collection" do
441     # The active user should be able to read the empty collection.
442
443     get("/arvados/v1/collections/#{empty_collection_pdh}",
444       params: {:format => :json},
445       headers: auth(:active))
446     assert_response :success
447     assert_empty json_response['manifest_text'], "empty collection manifest_text is not empty"
448   end
449 end