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