Merge branch 'master' into 3296-user-profile
[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', '=', 'project']], format: :json
20     assert_response :success
21     group_uuids = []
22     json_response['items'].each do |group|
23       assert_equal '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   end
114
115   [false, true].each do |include_linked|
116     test "list objects in home project, include_linked=#{include_linked}" do
117       authorize_with :active
118       get :contents, {
119         format: :json,
120         id: users(:active).uuid,
121         include_linked: include_linked,
122       }
123       assert_response :success
124       found_uuids = json_response['items'].collect { |i| i['uuid'] }
125       if include_linked
126         assert_includes found_uuids, collections(:empty).uuid, "empty collection did not appear in home project"
127       end
128       assert_includes found_uuids, specimens(:owned_by_active_user).uuid, "specimen did not appear in home project"
129       refute_includes found_uuids, specimens(:in_asubproject).uuid, "specimen appeared unexpectedly in home project"
130     end
131   end
132
133   test "user with project read permission can see project collections" do
134     authorize_with :project_viewer
135     get :contents, {
136       id: groups(:asubproject).uuid,
137       format: :json,
138       include_linked: true,
139     }
140     ids = json_response['items'].map { |item| item["uuid"] }
141     assert_includes ids, collections(:baz_file).uuid
142   end
143
144   test 'list objects across multiple projects' do
145     authorize_with :project_viewer
146     get :contents, {
147       format: :json,
148       include_linked: false,
149       filters: [['uuid', 'is_a', 'arvados#specimen']]
150     }
151     assert_response :success
152     found_uuids = json_response['items'].collect { |i| i['uuid'] }
153     [[:in_aproject, true],
154      [:in_asubproject, true],
155      [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
156       if should_find
157         assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
158       else
159         refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
160       end
161     end
162   end
163
164   # Even though the project_viewer tests go through other controllers,
165   # I'm putting them here so they're easy to find alongside the other
166   # project tests.
167   def check_new_project_link_fails(link_attrs)
168     @controller = Arvados::V1::LinksController.new
169     post :create, link: {
170       link_class: "permission",
171       name: "can_read",
172       head_uuid: groups(:aproject).uuid,
173     }.merge(link_attrs)
174     assert_includes(403..422, response.status)
175   end
176
177   test "user with project read permission can't add users to it" do
178     authorize_with :project_viewer
179     check_new_project_link_fails(tail_uuid: users(:spectator).uuid)
180   end
181
182   test "user with project read permission can't add items to it" do
183     authorize_with :project_viewer
184     check_new_project_link_fails(tail_uuid: collections(:baz_file).uuid)
185   end
186
187   test "user with project read permission can't rename items in it" do
188     authorize_with :project_viewer
189     @controller = Arvados::V1::LinksController.new
190     post :update, {
191       id: links(:job_name_in_aproject).uuid,
192       link: {name: "Denied test name"},
193     }
194     assert_includes(403..404, response.status)
195   end
196
197   test "user with project read permission can't remove items from it" do
198     @controller = Arvados::V1::PipelineTemplatesController.new
199     authorize_with :project_viewer
200     post :update, {
201       id: links(:template_name_in_aproject).head_uuid,
202       pipeline_template: {
203         owner_uuid: users(:project_viewer).uuid,
204       }
205     }
206     assert_response 403
207   end
208
209   test "user with project read permission can't delete it" do
210     authorize_with :project_viewer
211     post :destroy, {id: groups(:aproject).uuid}
212     assert_response 403
213   end
214
215   test 'get group-owned objects with limit' do
216     authorize_with :active
217     get :contents, {
218       id: groups(:aproject).uuid,
219       limit: 1,
220       format: :json,
221     }
222     assert_response :success
223     assert_operator 1, :<, json_response['items_available']
224     assert_equal 1, json_response['items'].count
225   end
226
227   test 'get group-owned objects with limit and offset' do
228     authorize_with :active
229     get :contents, {
230       id: groups(:aproject).uuid,
231       limit: 1,
232       offset: 12345,
233       format: :json,
234     }
235     assert_response :success
236     assert_operator 1, :<, json_response['items_available']
237     assert_equal 0, json_response['items'].count
238   end
239
240   test 'get group-owned objects with additional filter matching nothing' do
241     authorize_with :active
242     get :contents, {
243       id: groups(:aproject).uuid,
244       filters: [['uuid', 'in', ['foo_not_a_uuid','bar_not_a_uuid']]],
245       format: :json,
246     }
247     assert_response :success
248     assert_equal [], json_response['items']
249     assert_equal 0, json_response['items_available']
250   end
251
252   test 'get group-owned objects without include_linked' do
253     unexpected_uuid = specimens(:in_aproject_linked_from_asubproject).uuid
254     authorize_with :active
255     get :contents, {
256       id: groups(:asubproject).uuid,
257       format: :json,
258     }
259     assert_response :success
260     uuids = json_response['items'].collect { |i| i['uuid'] }
261     assert_equal nil, uuids.index(unexpected_uuid)
262   end
263
264   test 'get group-owned objects with include_linked' do
265     expected_uuid = specimens(:in_aproject_linked_from_asubproject).uuid
266     authorize_with :active
267     get :contents, {
268       id: groups(:asubproject).uuid,
269       include_linked: true,
270       format: :json,
271     }
272     assert_response :success
273     uuids = json_response['items'].collect { |i| i['uuid'] }
274     assert_includes uuids, expected_uuid, "Did not get #{expected_uuid}"
275
276     expected_name = links(:specimen_is_in_two_projects).name
277     found_specimen_name = false
278     assert(json_response['links'].any?,
279            "Expected a non-empty array of links in response")
280     json_response['links'].each do |link|
281       if link['head_uuid'] == expected_uuid
282         if link['name'] == expected_name
283           found_specimen_name = true
284         end
285       end
286     end
287     assert(found_specimen_name,
288            "Expected to find name '#{expected_name}' in response")
289   end
290
291   [false, true].each do |inc_ind|
292     test "get all pages of group-owned #{'and -linked ' if inc_ind}objects" do
293       authorize_with :active
294       limit = 5
295       offset = 0
296       items_available = nil
297       uuid_received = {}
298       owner_received = {}
299       while true
300         # Behaving badly here, using the same controller multiple
301         # times within a test.
302         @json_response = nil
303         get :contents, {
304           id: groups(:aproject).uuid,
305           include_linked: inc_ind,
306           limit: limit,
307           offset: offset,
308           format: :json,
309         }
310         assert_response :success
311         assert_operator(0, :<, json_response['items'].count,
312                         "items_available=#{items_available} but received 0 "\
313                         "items with offset=#{offset}")
314         items_available ||= json_response['items_available']
315         assert_equal(items_available, json_response['items_available'],
316                      "items_available changed between page #{offset/limit} "\
317                      "and page #{1+offset/limit}")
318         json_response['items'].each do |item|
319           uuid = item['uuid']
320           assert_equal(nil, uuid_received[uuid],
321                        "Received '#{uuid}' again on page #{1+offset/limit}")
322           uuid_received[uuid] = true
323           owner_received[item['owner_uuid']] = true
324           offset += 1
325           if not inc_ind
326             assert_equal groups(:aproject).uuid, item['owner_uuid']
327           end
328         end
329         break if offset >= items_available
330       end
331       if inc_ind
332         assert_operator 0, :<, (json_response.keys - [users(:active).uuid]).count,
333         "Set include_linked=true but did not receive any non-owned items"
334       end
335     end
336   end
337
338   %w(offset limit).each do |arg|
339     ['foo', '', '1234five', '0x10', '-8'].each do |val|
340       test "Raise error on bogus #{arg} parameter #{val.inspect}" do
341         authorize_with :active
342         get :contents, {
343           :id => groups(:aproject).uuid,
344           :format => :json,
345           arg => val,
346         }
347         assert_response 422
348       end
349     end
350   end
351
352   test 'get writable_by list for owned group' do
353     authorize_with :active
354     get :show, {
355       id: groups(:aproject).uuid,
356       format: :json
357     }
358     assert_response :success
359     assert_not_nil(json_response['writable_by'],
360                    "Should receive uuid list in 'writable_by' field")
361     assert_includes(json_response['writable_by'], users(:active).uuid,
362                     "owner should be included in writable_by list")
363   end
364
365   test 'no writable_by list for group with read-only access' do
366     authorize_with :rominiadmin
367     get :show, {
368       id: groups(:testusergroup_admins).uuid,
369       format: :json
370     }
371     assert_response :success
372     assert_nil(json_response['writable_by'],
373                "Should not receive uuid list in 'writable_by' field")
374   end
375
376   test 'get writable_by list by admin user' do
377     authorize_with :admin
378     get :show, {
379       id: groups(:testusergroup_admins).uuid,
380       format: :json
381     }
382     assert_response :success
383     assert_not_nil(json_response['writable_by'],
384                    "Should receive uuid list in 'writable_by' field")
385     assert_includes(json_response['writable_by'],
386                     users(:admin).uuid,
387                     "Current user should be included in 'writable_by' field")
388   end
389 end