1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class PermissionsTest < ActionDispatch::IntegrationTest
9 include CurrentApiClient # for empty_collection
10 fixtures :users, :groups, :api_client_authorizations, :collections
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)
17 # try to add permission as spectator
18 post "/arvados/v1/links", {
21 tail_uuid: users(:spectator).uuid,
22 link_class: 'permission',
24 head_uuid: collections(:foo_file).uuid,
30 # add permission as admin
31 post "/arvados/v1/links", {
34 tail_uuid: users(:spectator).uuid,
35 link_class: 'permission',
37 head_uuid: collections(:foo_file).uuid,
41 u = json_response['uuid']
42 assert_response :success
44 # read collection as spectator
45 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
46 assert_response :success
48 # try to delete permission as spectator
49 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:spectator)
52 # delete permission as admin
53 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
54 assert_response :success
56 # try to read collection as spectator
57 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
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)
67 # add permission for spectator to read group
68 post "/arvados/v1/links", {
71 tail_uuid: users(:spectator).uuid,
72 link_class: 'permission',
74 head_uuid: groups(:private).uuid,
78 assert_response :success
80 # try to read collection as spectator
81 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
84 # add permission for group to read collection
85 post "/arvados/v1/links", {
88 tail_uuid: groups(:private).uuid,
89 link_class: 'permission',
91 head_uuid: collections(:foo_file).uuid,
95 u = json_response['uuid']
96 assert_response :success
98 # try to read collection as spectator
99 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
100 assert_response :success
102 # delete permission for group to read collection
103 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
104 assert_response :success
106 # try to read collection as spectator
107 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
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)
118 # add permission for group to read collection
119 post "/arvados/v1/links", {
122 tail_uuid: groups(:private).uuid,
123 link_class: 'permission',
125 head_uuid: collections(:foo_file).uuid,
129 assert_response :success
131 # try to read collection as spectator
132 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
135 # add permission for spectator to read group
136 post "/arvados/v1/links", {
139 tail_uuid: users(:spectator).uuid,
140 link_class: 'permission',
142 head_uuid: groups(:private).uuid,
146 u = json_response['uuid']
147 assert_response :success
149 # try to read collection as spectator
150 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
151 assert_response :success
153 # delete permission for spectator to read group
154 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
155 assert_response :success
157 # try to read collection as spectator
158 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
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)
168 # add permission for user to read group
169 post "/arvados/v1/links", {
172 tail_uuid: users(:spectator).uuid,
173 link_class: 'permission',
175 head_uuid: groups(:private).uuid,
179 assert_response :success
181 # add permission for group to read group
182 post "/arvados/v1/links", {
185 tail_uuid: groups(:private).uuid,
186 link_class: 'permission',
188 head_uuid: groups(:empty_lonely_group).uuid,
192 assert_response :success
194 # add permission for group to read collection
195 post "/arvados/v1/links", {
198 tail_uuid: groups(:empty_lonely_group).uuid,
199 link_class: 'permission',
201 head_uuid: collections(:foo_file).uuid,
205 u = json_response['uuid']
206 assert_response :success
208 # try to read collection as spectator
209 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
210 assert_response :success
212 # delete permission for group to read collection
213 delete "/arvados/v1/links/#{u}", {:format => :json}, auth(:admin)
214 assert_response :success
216 # try to read collection as spectator
217 get "/arvados/v1/collections/#{collections(:foo_file).uuid}", {:format => :json}, auth(:spectator)
221 test "read-only group-admin cannot modify administered user" do
222 put "/arvados/v1/users/#{users(:active).uuid}", {
224 first_name: 'KilroyWasHere'
227 }, auth(:rominiadmin)
231 test "read-only group-admin cannot read or update non-administered user" do
232 get "/arvados/v1/users/#{users(:spectator).uuid}", {
234 }, auth(:rominiadmin)
237 put "/arvados/v1/users/#{users(:spectator).uuid}", {
239 first_name: 'KilroyWasHere'
242 }, auth(:rominiadmin)
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" %
259 should_find ? '' : 'not ',
261 put "/arvados/v1/specimens/#{uuid}", {
264 miniadmin_was_here: true
271 elsif !update_should_succeed
274 assert_response :success
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)
285 # add some permissions, including can_manage
286 # permission for user :active
287 post "/arvados/v1/links", {
290 tail_uuid: users(:spectator).uuid,
291 link_class: 'permission',
293 head_uuid: groups(:public).uuid,
297 assert_response :success
298 can_read_uuid = json_response['uuid']
300 post "/arvados/v1/links", {
303 tail_uuid: users(:inactive).uuid,
304 link_class: 'permission',
306 head_uuid: groups(:public).uuid,
310 assert_response :success
311 can_write_uuid = json_response['uuid']
313 post "/arvados/v1/links", {
316 tail_uuid: users(:active).uuid,
317 link_class: 'permission',
319 head_uuid: groups(:public).uuid,
323 assert_response :success
324 can_manage_uuid = json_response['uuid']
326 # Now user :active should be able to retrieve permissions
328 get("/arvados/v1/permissions/#{groups(:public).uuid}",
329 { :format => :json },
331 assert_response :success
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"
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)
345 get "/arvados/v1/permissions/#{nonexistent}", 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"