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