Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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', '!=', 'project']], format: :json
35     assert_response :success
36     group_uuids = []
37     json_response['items'].each do |group|
38       assert_not_equal 'project', 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   test "list objects across projects" do
94     authorize_with :project_viewer
95     get :contents, {
96       format: :json,
97       filters: [['uuid', 'is_a', 'arvados#specimen']]
98     }
99     assert_response :success
100     found_uuids = json_response['items'].collect { |i| i['uuid'] }
101     [[:in_aproject, true],
102      [:in_asubproject, true],
103      [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
104       if should_find
105         assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
106       else
107         refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
108       end
109     end
110   end
111
112   test "list objects in home project" do
113     authorize_with :active
114     get :contents, {
115       format: :json,
116       id: users(:active).uuid
117     }
118     assert_response :success
119     found_uuids = json_response['items'].collect { |i| i['uuid'] }
120     assert_includes found_uuids, specimens(:owned_by_active_user).uuid, "specimen did not appear in home project"
121     refute_includes found_uuids, specimens(:in_asubproject).uuid, "specimen appeared unexpectedly in home project"
122   end
123
124   test "user with project read permission can see project collections" do
125     authorize_with :project_viewer
126     get :contents, {
127       id: groups(:asubproject).uuid,
128       format: :json,
129     }
130     ids = json_response['items'].map { |item| item["uuid"] }
131     assert_includes ids, collections(:baz_file_in_asubproject).uuid
132   end
133
134   [['asc', :<=],
135    ['desc', :>=]].each do |order, operator|
136     test "user with project read permission can sort project collections #{order}" do
137       authorize_with :project_viewer
138       get :contents, {
139         id: groups(:asubproject).uuid,
140         format: :json,
141         filters: [['uuid', 'is_a', "arvados#collection"]],
142         order: "collections.name #{order}"
143       }
144       sorted_names = json_response['items'].collect { |item| item["name"] }
145       # Here we avoid assuming too much about the database
146       # collation. Both "alice"<"Bob" and "alice">"Bob" can be
147       # correct. Hopefully it _is_ safe to assume that if "a" comes
148       # before "b" in the ascii alphabet, "aX">"bY" is never true for
149       # any strings X and Y.
150       reliably_sortable_names = sorted_names.select do |name|
151         name[0] >= 'a' and name[0] <= 'z'
152       end.uniq do |name|
153         name[0]
154       end
155       # Preserve order of sorted_names. But do not use &=. If
156       # sorted_names has out-of-order duplicates, we want to preserve
157       # them here, so we can detect them and fail the test below.
158       sorted_names.select! do |name|
159         reliably_sortable_names.include? name
160       end
161       actually_checked_anything = false
162       previous = nil
163       sorted_names.each do |entry|
164         if previous
165           assert_operator(previous, operator, entry,
166                           "Entries sorted incorrectly.")
167           actually_checked_anything = true
168         end
169         previous = entry
170       end
171       assert actually_checked_anything, "Didn't even find two names to compare."
172     end
173   end
174
175   test 'list objects across multiple projects' do
176     authorize_with :project_viewer
177     get :contents, {
178       format: :json,
179       include_linked: false,
180       filters: [['uuid', 'is_a', 'arvados#specimen']]
181     }
182     assert_response :success
183     found_uuids = json_response['items'].collect { |i| i['uuid'] }
184     [[:in_aproject, true],
185      [:in_asubproject, true],
186      [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
187       if should_find
188         assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
189       else
190         refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
191       end
192     end
193   end
194
195   # Even though the project_viewer tests go through other controllers,
196   # I'm putting them here so they're easy to find alongside the other
197   # project tests.
198   def check_new_project_link_fails(link_attrs)
199     @controller = Arvados::V1::LinksController.new
200     post :create, link: {
201       link_class: "permission",
202       name: "can_read",
203       head_uuid: groups(:aproject).uuid,
204     }.merge(link_attrs)
205     assert_includes(403..422, response.status)
206   end
207
208   test "user with project read permission can't add users to it" do
209     authorize_with :project_viewer
210     check_new_project_link_fails(tail_uuid: users(:spectator).uuid)
211   end
212
213   test "user with project read permission can't add items to it" do
214     authorize_with :project_viewer
215     check_new_project_link_fails(tail_uuid: collections(:baz_file).uuid)
216   end
217
218   test "user with project read permission can't rename items in it" do
219     authorize_with :project_viewer
220     @controller = Arvados::V1::LinksController.new
221     post :update, {
222       id: jobs(:running).uuid,
223       name: "Denied test name",
224     }
225     assert_includes(403..404, response.status)
226   end
227
228   test "user with project read permission can't remove items from it" do
229     @controller = Arvados::V1::PipelineTemplatesController.new
230     authorize_with :project_viewer
231     post :update, {
232       id: pipeline_templates(:two_part).uuid,
233       pipeline_template: {
234         owner_uuid: users(:project_viewer).uuid,
235       }
236     }
237     assert_response 403
238   end
239
240   test "user with project read permission can't delete it" do
241     authorize_with :project_viewer
242     post :destroy, {id: groups(:aproject).uuid}
243     assert_response 403
244   end
245
246   test 'get group-owned objects with limit' do
247     authorize_with :active
248     get :contents, {
249       id: groups(:aproject).uuid,
250       limit: 1,
251       format: :json,
252     }
253     assert_response :success
254     assert_operator 1, :<, json_response['items_available']
255     assert_equal 1, json_response['items'].count
256   end
257
258   test 'get group-owned objects with limit and offset' do
259     authorize_with :active
260     get :contents, {
261       id: groups(:aproject).uuid,
262       limit: 1,
263       offset: 12345,
264       format: :json,
265     }
266     assert_response :success
267     assert_operator 1, :<, json_response['items_available']
268     assert_equal 0, json_response['items'].count
269   end
270
271   test 'get group-owned objects with additional filter matching nothing' do
272     authorize_with :active
273     get :contents, {
274       id: groups(:aproject).uuid,
275       filters: [['uuid', 'in', ['foo_not_a_uuid','bar_not_a_uuid']]],
276       format: :json,
277     }
278     assert_response :success
279     assert_equal [], json_response['items']
280     assert_equal 0, json_response['items_available']
281   end
282
283   %w(offset limit).each do |arg|
284     ['foo', '', '1234five', '0x10', '-8'].each do |val|
285       test "Raise error on bogus #{arg} parameter #{val.inspect}" do
286         authorize_with :active
287         get :contents, {
288           :id => groups(:aproject).uuid,
289           :format => :json,
290           arg => val,
291         }
292         assert_response 422
293       end
294     end
295   end
296
297   test 'get writable_by list for owned group' do
298     authorize_with :active
299     get :show, {
300       id: groups(:aproject).uuid,
301       format: :json
302     }
303     assert_response :success
304     assert_not_nil(json_response['writable_by'],
305                    "Should receive uuid list in 'writable_by' field")
306     assert_includes(json_response['writable_by'], users(:active).uuid,
307                     "owner should be included in writable_by list")
308   end
309
310   test 'no writable_by list for group with read-only access' do
311     authorize_with :rominiadmin
312     get :show, {
313       id: groups(:testusergroup_admins).uuid,
314       format: :json
315     }
316     assert_response :success
317     assert_equal([json_response['owner_uuid']],
318                  json_response['writable_by'],
319                  "Should only see owner_uuid in 'writable_by' field")
320   end
321
322   test 'get writable_by list by admin user' do
323     authorize_with :admin
324     get :show, {
325       id: groups(:testusergroup_admins).uuid,
326       format: :json
327     }
328     assert_response :success
329     assert_not_nil(json_response['writable_by'],
330                    "Should receive uuid list in 'writable_by' field")
331     assert_includes(json_response['writable_by'],
332                     users(:admin).uuid,
333                     "Current user should be included in 'writable_by' field")
334   end
335
336   test 'creating subproject with duplicate name fails' do
337     authorize_with :active
338     post :create, {
339       group: {
340         name: 'A Project',
341         owner_uuid: users(:active).uuid,
342         group_class: 'project',
343       },
344     }
345     assert_response 422
346     response_errors = json_response['errors']
347     assert_not_nil response_errors, 'Expected error in response'
348     assert(response_errors.first.include?('duplicate key'),
349            "Expected 'duplicate key' error in #{response_errors.first}")
350   end
351
352   test 'creating duplicate named subproject succeeds with ensure_unique_name' do
353     authorize_with :active
354     post :create, {
355       group: {
356         name: 'A Project',
357         owner_uuid: users(:active).uuid,
358         group_class: 'project',
359       },
360       ensure_unique_name: true
361     }
362     assert_response :success
363     new_project = json_response
364     assert_not_equal(new_project['uuid'],
365                      groups(:aproject).uuid,
366                      "create returned same uuid as existing project")
367     assert_equal(new_project['name'],
368                  'A Project (2)',
369                  "new project name '#{new_project['name']}' was expected to be 'A Project (2)'")
370   end
371 end