Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[arvados.git] / services / api / test / integration / permissions_test.rb
1 require 'test_helper'
2
3 class PermissionsTest < ActionDispatch::IntegrationTest
4   include CurrentApiClient  # for empty_collection
5   fixtures :users, :groups, :api_client_authorizations, :collections
6
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)
10     assert_response 404
11
12     # try to add permission as spectator
13     post "/arvados/v1/links", {
14       :format => :json,
15       :link => {
16         tail_uuid: users(:spectator).uuid,
17         link_class: 'permission',
18         name: 'can_read',
19         head_uuid: collections(:foo_file).uuid,
20         properties: {}
21       }
22     }, auth(:spectator)
23     assert_response 422
24
25     # add permission as admin
26     post "/arvados/v1/links", {
27       :format => :json,
28       :link => {
29         tail_uuid: users(:spectator).uuid,
30         link_class: 'permission',
31         name: 'can_read',
32         head_uuid: collections(:foo_file).uuid,
33         properties: {}
34       }
35     }, auth(:admin)
36     u = json_response['uuid']
37     assert_response :success
38
39     # read collection as spectator
40     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
41     assert_response :success
42
43     # try to delete permission as spectator
44     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:spectator)
45     assert_response 403
46
47     # delete permission as admin
48     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
49     assert_response :success
50
51     # try to read collection as spectator
52     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
53     assert_response 404
54   end
55
56
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)
60     assert_response 404
61
62     # add permission for spectator to read group
63     post "/arvados/v1/links", {
64       :format => :json,
65       :link => {
66         tail_uuid: users(:spectator).uuid,
67         link_class: 'permission',
68         name: 'can_read',
69         head_uuid: groups(:private).uuid,
70         properties: {}
71       }
72     }, auth(:admin)
73     assert_response :success
74
75     # try to read collection as spectator
76     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
77     assert_response 404
78
79     # add permission for group to read collection
80     post "/arvados/v1/links", {
81       :format => :json,
82       :link => {
83         tail_uuid: groups(:private).uuid,
84         link_class: 'permission',
85         name: 'can_read',
86         head_uuid: collections(:foo_file).uuid,
87         properties: {}
88       }
89     }, auth(:admin)
90     u = json_response['uuid']
91     assert_response :success
92
93     # try to read collection as spectator
94     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
95     assert_response :success
96
97     # delete permission for group to read collection
98     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
99     assert_response :success
100
101     # try to read collection as spectator
102     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
103     assert_response 404
104
105   end
106
107
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)
111     assert_response 404
112
113     # add permission for group to read collection
114     post "/arvados/v1/links", {
115       :format => :json,
116       :link => {
117         tail_uuid: groups(:private).uuid,
118         link_class: 'permission',
119         name: 'can_read',
120         head_uuid: collections(:foo_file).uuid,
121         properties: {}
122       }
123     }, auth(:admin)
124     assert_response :success
125
126     # try to read collection as spectator
127     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
128     assert_response 404
129
130     # add permission for spectator to read group
131     post "/arvados/v1/links", {
132       :format => :json,
133       :link => {
134         tail_uuid: users(:spectator).uuid,
135         link_class: 'permission',
136         name: 'can_read',
137         head_uuid: groups(:private).uuid,
138         properties: {}
139       }
140     }, auth(:admin)
141     u = json_response['uuid']
142     assert_response :success
143
144     # try to read collection as spectator
145     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
146     assert_response :success
147
148     # delete permission for spectator to read group
149     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
150     assert_response :success
151
152     # try to read collection as spectator
153     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
154     assert_response 404
155
156   end
157
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)
161     assert_response 404
162
163     # add permission for user to read group
164     post "/arvados/v1/links", {
165       :format => :json,
166       :link => {
167         tail_uuid: users(:spectator).uuid,
168         link_class: 'permission',
169         name: 'can_read',
170         head_uuid: groups(:private).uuid,
171         properties: {}
172       }
173     }, auth(:admin)
174     assert_response :success
175
176     # add permission for group to read group
177     post "/arvados/v1/links", {
178       :format => :json,
179       :link => {
180         tail_uuid: groups(:private).uuid,
181         link_class: 'permission',
182         name: 'can_read',
183         head_uuid: groups(:empty_lonely_group).uuid,
184         properties: {}
185       }
186     }, auth(:admin)
187     assert_response :success
188
189     # add permission for group to read collection
190     post "/arvados/v1/links", {
191       :format => :json,
192       :link => {
193         tail_uuid: groups(:empty_lonely_group).uuid,
194         link_class: 'permission',
195         name: 'can_read',
196         head_uuid: collections(:foo_file).uuid,
197         properties: {}
198       }
199     }, auth(:admin)
200     u = json_response['uuid']
201     assert_response :success
202
203     # try to read collection as spectator
204     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
205     assert_response :success
206
207     # delete permission for group to read collection
208     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
209     assert_response :success
210
211     # try to read collection as spectator
212     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
213     assert_response 404
214   end
215
216   test "read-only group-admin cannot modify administered user" do
217     put "/arvados/v1/users/#{users(:active).uuid}", {
218       :user => {
219         first_name: 'KilroyWasHere'
220       },
221       :format => :json
222     }, auth(:rominiadmin)
223     assert_response 403
224   end
225
226   test "read-only group-admin cannot read or update non-administered user" do
227     get "/arvados/v1/users/#{users(:spectator).uuid}", {
228       :format => :json
229     }, auth(:rominiadmin)
230     assert_response 404
231
232     put "/arvados/v1/users/#{users(:spectator).uuid}", {
233       :user => {
234         first_name: 'KilroyWasHere'
235       },
236       :format => :json
237     }, auth(:rominiadmin)
238     assert_response 404
239   end
240
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" %
253                      [which_user.to_s,
254                       should_find ? '' : 'not ',
255                       uuid])
256         put "/arvados/v1/specimens/#{uuid}", {
257           :specimen => {
258             properties: {
259               miniadmin_was_here: true
260             }
261           },
262           :format => :json
263         }, auth(which_user)
264         if !should_find
265           assert_response 404
266         elsif !update_should_succeed
267           assert_response 403
268         else
269           assert_response :success
270         end
271       end
272     end
273   end
274
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)
278     assert_response 404
279
280     # add some permissions, including can_manage
281     # permission for user :active
282     post "/arvados/v1/links", {
283       :format => :json,
284       :link => {
285         tail_uuid: users(:spectator).uuid,
286         link_class: 'permission',
287         name: 'can_read',
288         head_uuid: groups(:public).uuid,
289         properties: {}
290       }
291     }, auth(:admin)
292     assert_response :success
293     can_read_uuid = json_response['uuid']
294
295     post "/arvados/v1/links", {
296       :format => :json,
297       :link => {
298         tail_uuid: users(:inactive).uuid,
299         link_class: 'permission',
300         name: 'can_write',
301         head_uuid: groups(:public).uuid,
302         properties: {}
303       }
304     }, auth(:admin)
305     assert_response :success
306     can_write_uuid = json_response['uuid']
307
308     post "/arvados/v1/links", {
309       :format => :json,
310       :link => {
311         tail_uuid: users(:active).uuid,
312         link_class: 'permission',
313         name: 'can_manage',
314         head_uuid: groups(:public).uuid,
315         properties: {}
316       }
317     }, auth(:admin)
318     assert_response :success
319     can_manage_uuid = json_response['uuid']
320
321     # Now user :active should be able to retrieve permissions
322     # on group :public.
323     get("/arvados/v1/permissions/#{groups(:public).uuid}",
324         { :format => :json },
325         auth(:active))
326     assert_response :success
327
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"
332   end
333
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)
338     assert_response 404
339
340     get "/arvados/v1/permissions/#{nonexistent}", nil, auth(:active)
341     assert_response 404
342   end
343
344   test "get_permissions returns 404 for unreadable uuid" do
345     get "/arvados/v1/permissions/#{groups(:public).uuid}", nil, auth(:active)
346     assert_response 404
347   end
348
349   test "get_permissions returns 403 if user can read but not manage" do
350     post "/arvados/v1/links", {
351       :link => {
352         tail_uuid: users(:active).uuid,
353         link_class: 'permission',
354         name: 'can_read',
355         head_uuid: groups(:public).uuid,
356         properties: {}
357       }
358     }, auth(:admin)
359     assert_response :success
360
361     get "/arvados/v1/permissions/#{groups(:public).uuid}", nil, auth(:active)
362     assert_response 403
363   end
364
365   test "active user can read the empty collection" do
366     # The active user should be able to read the empty collection.
367
368     get("/arvados/v1/collections/#{empty_collection_uuid}",
369         { :format => :json },
370         auth(:active))
371     assert_response :success
372     assert_empty json_response['manifest_text'], "empty collection manifest_text is not empty"
373   end
374 end