8784: Fix test for latest firefox.
[arvados.git] / services / api / test / integration / permissions_test.rb
1 require 'test_helper'
2
3 class PermissionsTest < ActionDispatch::IntegrationTest
4   include DbCurrentTime
5   include CurrentApiClient  # for empty_collection
6   fixtures :users, :groups, :api_client_authorizations, :collections
7
8   teardown do
9     User.invalidate_permissions_cache db_current_time.to_i
10   end
11
12   test "adding and removing direct can_read links" do
13     # try to read collection as spectator
14     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
15     assert_response 404
16
17     # try to add permission as spectator
18     post "/arvados/v1/links", {
19       :format => :json,
20       :link => {
21         tail_uuid: users(:spectator).uuid,
22         link_class: 'permission',
23         name: 'can_read',
24         head_uuid: collections(:foo_file).uuid,
25         properties: {}
26       }
27     }, auth(:spectator)
28     assert_response 422
29
30     # add permission as admin
31     post "/arvados/v1/links", {
32       :format => :json,
33       :link => {
34         tail_uuid: users(:spectator).uuid,
35         link_class: 'permission',
36         name: 'can_read',
37         head_uuid: collections(:foo_file).uuid,
38         properties: {}
39       }
40     }, auth(:admin)
41     u = json_response['uuid']
42     assert_response :success
43
44     # read collection as spectator
45     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
46     assert_response :success
47
48     # try to delete permission as spectator
49     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:spectator)
50     assert_response 403
51
52     # delete permission as admin
53     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
54     assert_response :success
55
56     # try to read collection as spectator
57     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
58     assert_response 404
59   end
60
61
62   test "adding can_read links from user to group, group to collection" do
63     # try to read collection as spectator
64     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
65     assert_response 404
66
67     # add permission for spectator to read group
68     post "/arvados/v1/links", {
69       :format => :json,
70       :link => {
71         tail_uuid: users(:spectator).uuid,
72         link_class: 'permission',
73         name: 'can_read',
74         head_uuid: groups(:private).uuid,
75         properties: {}
76       }
77     }, auth(:admin)
78     assert_response :success
79
80     # try to read collection as spectator
81     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
82     assert_response 404
83
84     # add permission for group to read collection
85     post "/arvados/v1/links", {
86       :format => :json,
87       :link => {
88         tail_uuid: groups(:private).uuid,
89         link_class: 'permission',
90         name: 'can_read',
91         head_uuid: collections(:foo_file).uuid,
92         properties: {}
93       }
94     }, auth(:admin)
95     u = json_response['uuid']
96     assert_response :success
97
98     # try to read collection as spectator
99     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
100     assert_response :success
101
102     # delete permission for group to read collection
103     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
104     assert_response :success
105
106     # try to read collection as spectator
107     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
108     assert_response 404
109
110   end
111
112
113   test "adding can_read links from group to collection, user to group" do
114     # try to read collection as spectator
115     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
116     assert_response 404
117
118     # add permission for group to read collection
119     post "/arvados/v1/links", {
120       :format => :json,
121       :link => {
122         tail_uuid: groups(:private).uuid,
123         link_class: 'permission',
124         name: 'can_read',
125         head_uuid: collections(:foo_file).uuid,
126         properties: {}
127       }
128     }, auth(:admin)
129     assert_response :success
130
131     # try to read collection as spectator
132     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
133     assert_response 404
134
135     # add permission for spectator to read group
136     post "/arvados/v1/links", {
137       :format => :json,
138       :link => {
139         tail_uuid: users(:spectator).uuid,
140         link_class: 'permission',
141         name: 'can_read',
142         head_uuid: groups(:private).uuid,
143         properties: {}
144       }
145     }, auth(:admin)
146     u = json_response['uuid']
147     assert_response :success
148
149     # try to read collection as spectator
150     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
151     assert_response :success
152
153     # delete permission for spectator to read group
154     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
155     assert_response :success
156
157     # try to read collection as spectator
158     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
159     assert_response 404
160
161   end
162
163   test "adding can_read links from user to group, group to group, group to collection" do
164     # try to read collection as spectator
165     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
166     assert_response 404
167
168     # add permission for user to read group
169     post "/arvados/v1/links", {
170       :format => :json,
171       :link => {
172         tail_uuid: users(:spectator).uuid,
173         link_class: 'permission',
174         name: 'can_read',
175         head_uuid: groups(:private).uuid,
176         properties: {}
177       }
178     }, auth(:admin)
179     assert_response :success
180
181     # add permission for group to read group
182     post "/arvados/v1/links", {
183       :format => :json,
184       :link => {
185         tail_uuid: groups(:private).uuid,
186         link_class: 'permission',
187         name: 'can_read',
188         head_uuid: groups(:empty_lonely_group).uuid,
189         properties: {}
190       }
191     }, auth(:admin)
192     assert_response :success
193
194     # add permission for group to read collection
195     post "/arvados/v1/links", {
196       :format => :json,
197       :link => {
198         tail_uuid: groups(:empty_lonely_group).uuid,
199         link_class: 'permission',
200         name: 'can_read',
201         head_uuid: collections(:foo_file).uuid,
202         properties: {}
203       }
204     }, auth(:admin)
205     u = json_response['uuid']
206     assert_response :success
207
208     # try to read collection as spectator
209     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
210     assert_response :success
211
212     # delete permission for group to read collection
213     delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
214     assert_response :success
215
216     # try to read collection as spectator
217     get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
218     assert_response 404
219   end
220
221   test "read-only group-admin cannot modify administered user" do
222     put "/arvados/v1/users/#{users(:active).uuid}", {
223       :user => {
224         first_name: 'KilroyWasHere'
225       },
226       :format => :json
227     }, auth(:rominiadmin)
228     assert_response 403
229   end
230
231   test "read-only group-admin cannot read or update non-administered user" do
232     get "/arvados/v1/users/#{users(:spectator).uuid}", {
233       :format => :json
234     }, auth(:rominiadmin)
235     assert_response 404
236
237     put "/arvados/v1/users/#{users(:spectator).uuid}", {
238       :user => {
239         first_name: 'KilroyWasHere'
240       },
241       :format => :json
242     }, auth(:rominiadmin)
243     assert_response 404
244   end
245
246   test "RO group-admin finds user's specimens, RW group-admin can update" do
247     [[:rominiadmin, false],
248      [:miniadmin, true]].each do |which_user, update_should_succeed|
249       get "/arvados/v1/specimens", {:format => :json}, auth(which_user)
250       assert_response :success
251       resp_uuids = json_response['items'].collect { |i| i['uuid'] }
252       [[true, specimens(:owned_by_active_user).uuid],
253        [true, specimens(:owned_by_private_group).uuid],
254        [false, specimens(:owned_by_spectator).uuid],
255       ].each do |should_find, uuid|
256         assert_equal(should_find, !resp_uuids.index(uuid).nil?,
257                      "%s should%s see %s in specimen list" %
258                      [which_user.to_s,
259                       should_find ? '' : 'not ',
260                       uuid])
261         put "/arvados/v1/specimens/#{uuid}", {
262           :specimen => {
263             properties: {
264               miniadmin_was_here: true
265             }
266           },
267           :format => :json
268         }, auth(which_user)
269         if !should_find
270           assert_response 404
271         elsif !update_should_succeed
272           assert_response 403
273         else
274           assert_response :success
275         end
276       end
277     end
278   end
279
280   test "get_permissions returns list" do
281     # First confirm that user :active cannot get permissions on group :public
282     get "/arvados/v1/permissions/#{groups(:public).uuid}", nil, auth(:active)
283     assert_response 404
284
285     # add some permissions, including can_manage
286     # permission for user :active
287     post "/arvados/v1/links", {
288       :format => :json,
289       :link => {
290         tail_uuid: users(:spectator).uuid,
291         link_class: 'permission',
292         name: 'can_read',
293         head_uuid: groups(:public).uuid,
294         properties: {}
295       }
296     }, auth(:admin)
297     assert_response :success
298     can_read_uuid = json_response['uuid']
299
300     post "/arvados/v1/links", {
301       :format => :json,
302       :link => {
303         tail_uuid: users(:inactive).uuid,
304         link_class: 'permission',
305         name: 'can_write',
306         head_uuid: groups(:public).uuid,
307         properties: {}
308       }
309     }, auth(:admin)
310     assert_response :success
311     can_write_uuid = json_response['uuid']
312
313     post "/arvados/v1/links", {
314       :format => :json,
315       :link => {
316         tail_uuid: users(:active).uuid,
317         link_class: 'permission',
318         name: 'can_manage',
319         head_uuid: groups(:public).uuid,
320         properties: {}
321       }
322     }, auth(:admin)
323     assert_response :success
324     can_manage_uuid = json_response['uuid']
325
326     # Now user :active should be able to retrieve permissions
327     # on group :public.
328     get("/arvados/v1/permissions/#{groups(:public).uuid}",
329         { :format => :json },
330         auth(:active))
331     assert_response :success
332
333     perm_uuids = json_response['items'].map { |item| item['uuid'] }
334     assert_includes perm_uuids, can_read_uuid, "can_read_uuid not found"
335     assert_includes perm_uuids, can_write_uuid, "can_write_uuid not found"
336     assert_includes perm_uuids, can_manage_uuid, "can_manage_uuid not found"
337   end
338
339   test "get_permissions returns 404 for nonexistent uuid" do
340     nonexistent = Group.generate_uuid
341     # make sure it really doesn't exist
342     get "/arvados/v1/groups/#{nonexistent}", nil, auth(:admin)
343     assert_response 404
344
345     get "/arvados/v1/permissions/#{nonexistent}", 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