Merge branch 'origin/3443-read-collections-from-name-links' refs #3443
[arvados.git] / services / api / test / functional / arvados / v1 / groups_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::GroupsControllerTest < ActionController::TestCase
4
5   test "attempt to delete group without read or write access" do
6     authorize_with :active
7     post :destroy, id: groups(:empty_lonely_group).uuid
8     assert_response 404
9   end
10
11   test "attempt to delete group without write access" do
12     authorize_with :active
13     post :destroy, id: groups(:all_users).uuid
14     assert_response 403
15   end
16
17   test "get list of projects" do
18     authorize_with :active
19     get :index, filters: [['group_class', 'in', ['project', 'folder']]], format: :json
20     assert_response :success
21     group_uuids = []
22     json_response['items'].each do |group|
23       assert_includes ['folder', 'project'], group['group_class']
24       group_uuids << group['uuid']
25     end
26     assert_includes group_uuids, groups(:aproject).uuid
27     assert_includes group_uuids, groups(:asubproject).uuid
28     assert_not_includes group_uuids, groups(:system_group).uuid
29     assert_not_includes group_uuids, groups(:private).uuid
30   end
31
32   test "get list of groups that are not projects" do
33     authorize_with :active
34     get :index, filters: [['group_class', '=', nil]], format: :json
35     assert_response :success
36     group_uuids = []
37     json_response['items'].each do |group|
38       assert_equal nil, group['group_class']
39       group_uuids << group['uuid']
40     end
41     assert_not_includes group_uuids, groups(:aproject).uuid
42     assert_not_includes group_uuids, groups(:asubproject).uuid
43     assert_includes group_uuids, groups(:private).uuid
44   end
45
46   test "get list of groups with bogus group_class" do
47     authorize_with :active
48     get :index, {
49       filters: [['group_class', '=', 'nogrouphasthislittleclass']],
50       format: :json,
51     }
52     assert_response :success
53     assert_equal [], json_response['items']
54     assert_equal 0, json_response['items_available']
55   end
56
57   def check_project_contents_response
58     assert_response :success
59     assert_operator 2, :<=, json_response['items_available']
60     assert_operator 2, :<=, json_response['items'].count
61     kinds = json_response['items'].collect { |i| i['kind'] }.uniq
62     expect_kinds = %w'arvados#group arvados#specimen arvados#pipelineTemplate arvados#job'
63     assert_equal expect_kinds, (expect_kinds & kinds)
64
65     json_response['items'].each do |i|
66       if i['kind'] == 'arvados#group'
67         assert(i['group_class'] == 'project',
68                "group#contents returned a non-project group")
69       end
70     end
71   end
72
73   test 'get group-owned objects' do
74     authorize_with :active
75     get :contents, {
76       id: groups(:aproject).uuid,
77       format: :json,
78       include_linked: true,
79     }
80     check_project_contents_response
81   end
82
83   test "user with project read permission can see project objects" do
84     authorize_with :project_viewer
85     get :contents, {
86       id: groups(:aproject).uuid,
87       format: :json,
88       include_linked: true,
89     }
90     check_project_contents_response
91   end
92
93   [false, true].each do |include_linked|
94     test "list objects across projects, include_linked=#{include_linked}" do
95       authorize_with :project_viewer
96       get :contents, {
97         format: :json,
98         include_linked: include_linked,
99         filters: [['uuid', 'is_a', 'arvados#specimen']]
100       }
101       assert_response :success
102       found_uuids = json_response['items'].collect { |i| i['uuid'] }
103       [[:in_aproject, true],
104        [:in_asubproject, true],
105        [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
106         if should_find
107           assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
108         else
109           refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
110         end
111     end
112   end
113
114   test "user with project read permission can see project collections" do
115     authorize_with :project_viewer
116     get :contents, {
117       id: groups(:asubproject).uuid,
118       format: :json,
119       include_linked: true,
120     }
121     ids = json_response['items'].map { |item| item["uuid"] }
122     assert_includes ids, collections(:baz_file).uuid
123   end
124
125   test 'list objects across multiple projects' do
126     authorize_with :project_viewer
127     get :contents, {
128       format: :json,
129       include_linked: false,
130       filters: [['uuid', 'is_a', 'arvados#specimen']]
131     }
132     assert_response :success
133     found_uuids = json_response['items'].collect { |i| i['uuid'] }
134     [[:in_aproject, true],
135      [:in_asubproject, true],
136      [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
137       if should_find
138         assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
139       else
140         refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
141       end
142     end
143   end
144
145   # Even though the project_viewer tests go through other controllers,
146   # I'm putting them here so they're easy to find alongside the other
147   # project tests.
148   def check_new_project_link_fails(link_attrs)
149     @controller = Arvados::V1::LinksController.new
150     post :create, link: {
151       link_class: "permission",
152       name: "can_read",
153       head_uuid: groups(:aproject).uuid,
154     }.merge(link_attrs)
155     assert_includes(403..422, response.status)
156   end
157
158   test "user with project read permission can't add users to it" do
159     authorize_with :project_viewer
160     check_new_project_link_fails(tail_uuid: users(:spectator).uuid)
161   end
162
163   test "user with project read permission can't add items to it" do
164     authorize_with :project_viewer
165     check_new_project_link_fails(tail_uuid: collections(:baz_file).uuid)
166   end
167
168   test "user with project read permission can't rename items in it" do
169     authorize_with :project_viewer
170     @controller = Arvados::V1::LinksController.new
171     post :update, {
172       id: links(:job_name_in_aproject).uuid,
173       link: {name: "Denied test name"},
174     }
175     assert_includes(403..404, response.status)
176   end
177
178   test "user with project read permission can't remove items from it" do
179     @controller = Arvados::V1::PipelineTemplatesController.new
180     authorize_with :project_viewer
181     post :update, {
182       id: links(:template_name_in_aproject).head_uuid,
183       pipeline_template: {
184         owner_uuid: users(:project_viewer).uuid,
185       }
186     }
187     assert_response 403
188   end
189
190   test "user with project read permission can't delete it" do
191     authorize_with :project_viewer
192     post :destroy, {id: groups(:aproject).uuid}
193     assert_response 403
194   end
195
196   test 'get group-owned objects with limit' do
197     authorize_with :active
198     get :contents, {
199       id: groups(:aproject).uuid,
200       limit: 1,
201       format: :json,
202     }
203     assert_response :success
204     assert_operator 1, :<, json_response['items_available']
205     assert_equal 1, json_response['items'].count
206   end
207
208   test 'get group-owned objects with limit and offset' do
209     authorize_with :active
210     get :contents, {
211       id: groups(:aproject).uuid,
212       limit: 1,
213       offset: 12345,
214       format: :json,
215     }
216     assert_response :success
217     assert_operator 1, :<, json_response['items_available']
218     assert_equal 0, json_response['items'].count
219   end
220
221   test 'get group-owned objects with additional filter matching nothing' do
222     authorize_with :active
223     get :contents, {
224       id: groups(:aproject).uuid,
225       filters: [['uuid', 'in', ['foo_not_a_uuid','bar_not_a_uuid']]],
226       format: :json,
227     }
228     assert_response :success
229     assert_equal [], json_response['items']
230     assert_equal 0, json_response['items_available']
231   end
232
233   test 'get group-owned objects without include_linked' do
234     unexpected_uuid = specimens(:in_aproject_linked_from_asubproject).uuid
235     authorize_with :active
236     get :contents, {
237       id: groups(:asubproject).uuid,
238       format: :json,
239     }
240     assert_response :success
241     uuids = json_response['items'].collect { |i| i['uuid'] }
242     assert_equal nil, uuids.index(unexpected_uuid)
243   end
244
245   test 'get group-owned objects with include_linked' do
246     expected_uuid = specimens(:in_aproject_linked_from_asubproject).uuid
247     authorize_with :active
248     get :contents, {
249       id: groups(:asubproject).uuid,
250       include_linked: true,
251       format: :json,
252     }
253     assert_response :success
254     uuids = json_response['items'].collect { |i| i['uuid'] }
255     assert_includes uuids, expected_uuid, "Did not get #{expected_uuid}"
256
257     expected_name = links(:specimen_is_in_two_projects).name
258     found_specimen_name = false
259     assert(json_response['links'].any?,
260            "Expected a non-empty array of links in response")
261     json_response['links'].each do |link|
262       if link['head_uuid'] == expected_uuid
263         if link['name'] == expected_name
264           found_specimen_name = true
265         end
266       end
267     end
268     assert(found_specimen_name,
269            "Expected to find name '#{expected_name}' in response")
270   end
271
272   [false, true].each do |inc_ind|
273     test "get all pages of group-owned #{'and -linked ' if inc_ind}objects" do
274       authorize_with :active
275       limit = 5
276       offset = 0
277       items_available = nil
278       uuid_received = {}
279       owner_received = {}
280       while true
281         # Behaving badly here, using the same controller multiple
282         # times within a test.
283         @json_response = nil
284         get :contents, {
285           id: groups(:aproject).uuid,
286           include_linked: inc_ind,
287           limit: limit,
288           offset: offset,
289           format: :json,
290         }
291         assert_response :success
292         assert_operator(0, :<, json_response['items'].count,
293                         "items_available=#{items_available} but received 0 "\
294                         "items with offset=#{offset}")
295         items_available ||= json_response['items_available']
296         assert_equal(items_available, json_response['items_available'],
297                      "items_available changed between page #{offset/limit} "\
298                      "and page #{1+offset/limit}")
299         json_response['items'].each do |item|
300           uuid = item['uuid']
301           assert_equal(nil, uuid_received[uuid],
302                        "Received '#{uuid}' again on page #{1+offset/limit}")
303           uuid_received[uuid] = true
304           owner_received[item['owner_uuid']] = true
305           offset += 1
306           if not inc_ind
307             assert_equal groups(:aproject).uuid, item['owner_uuid']
308           end
309         end
310         break if offset >= items_available
311       end
312       if inc_ind
313         assert_operator 0, :<, (json_response.keys - [users(:active).uuid]).count,
314         "Set include_linked=true but did not receive any non-owned items"
315       end
316     end
317   end
318
319   %w(offset limit).each do |arg|
320     ['foo', '', '1234five', '0x10', '-8'].each do |val|
321       test "Raise error on bogus #{arg} parameter #{val.inspect}" do
322         authorize_with :active
323         get :contents, {
324           :id => groups(:aproject).uuid,
325           :format => :json,
326           arg => val,
327         }
328         assert_response 422
329       end
330     end
331   end
332
333   test 'get writable_by list for owned group' do
334     authorize_with :active
335     get :show, {
336       id: groups(:aproject).uuid,
337       format: :json
338     }
339     assert_response :success
340     assert_not_nil(json_response['writable_by'],
341                    "Should receive uuid list in 'writable_by' field")
342     assert_includes(json_response['writable_by'], users(:active).uuid,
343                     "owner should be included in writable_by list")
344   end
345
346   test 'no writable_by list for group with read-only access' do
347     authorize_with :rominiadmin
348     get :show, {
349       id: groups(:testusergroup_admins).uuid,
350       format: :json
351     }
352     assert_response :success
353     assert_nil(json_response['writable_by'],
354                "Should not receive uuid list in 'writable_by' field")
355   end
356
357   test 'get writable_by list by admin user' do
358     authorize_with :admin
359     get :show, {
360       id: groups(:testusergroup_admins).uuid,
361       format: :json
362     }
363     assert_response :success
364     assert_not_nil(json_response['writable_by'],
365                    "Should receive uuid list in 'writable_by' field")
366     assert_includes(json_response['writable_by'],
367                     users(:admin).uuid,
368                     "Current user should be included in 'writable_by' field")
369   end
370 end