8784: Fix test for latest firefox.
[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     assert_includes group_uuids, groups(:group_with_no_class).uuid
45   end
46
47   test "get list of groups with bogus group_class" do
48     authorize_with :active
49     get :index, {
50       filters: [['group_class', '=', 'nogrouphasthislittleclass']],
51       format: :json,
52     }
53     assert_response :success
54     assert_equal [], json_response['items']
55     assert_equal 0, json_response['items_available']
56   end
57
58   def check_project_contents_response disabled_kinds=[]
59     assert_response :success
60     assert_operator 2, :<=, json_response['items_available']
61     assert_operator 2, :<=, json_response['items'].count
62     kinds = json_response['items'].collect { |i| i['kind'] }.uniq
63     expect_kinds = %w'arvados#group arvados#specimen arvados#pipelineTemplate arvados#job' - disabled_kinds
64     assert_equal expect_kinds, (expect_kinds & kinds)
65
66     json_response['items'].each do |i|
67       if i['kind'] == 'arvados#group'
68         assert(i['group_class'] == 'project',
69                "group#contents returned a non-project group")
70       end
71     end
72
73     disabled_kinds.each do |d|
74       assert_equal true, !kinds.include?(d)
75     end
76   end
77
78   test 'get group-owned objects' do
79     authorize_with :active
80     get :contents, {
81       id: groups(:aproject).uuid,
82       format: :json,
83     }
84     check_project_contents_response
85   end
86
87   test "user with project read permission can see project objects" do
88     authorize_with :project_viewer
89     get :contents, {
90       id: groups(:aproject).uuid,
91       format: :json,
92     }
93     check_project_contents_response
94   end
95
96   test "list objects across projects" do
97     authorize_with :project_viewer
98     get :contents, {
99       format: :json,
100       filters: [['uuid', 'is_a', 'arvados#specimen']]
101     }
102     assert_response :success
103     found_uuids = json_response['items'].collect { |i| i['uuid'] }
104     [[:in_aproject, true],
105      [:in_asubproject, true],
106      [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
107       if should_find
108         assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
109       else
110         refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
111       end
112     end
113   end
114
115   test "list objects in home project" do
116     authorize_with :active
117     get :contents, {
118       format: :json,
119       id: users(:active).uuid
120     }
121     assert_response :success
122     found_uuids = json_response['items'].collect { |i| i['uuid'] }
123     assert_includes found_uuids, specimens(:owned_by_active_user).uuid, "specimen did not appear in home project"
124     refute_includes found_uuids, specimens(:in_asubproject).uuid, "specimen appeared unexpectedly in home project"
125   end
126
127   test "user with project read permission can see project collections" do
128     authorize_with :project_viewer
129     get :contents, {
130       id: groups(:asubproject).uuid,
131       format: :json,
132     }
133     ids = json_response['items'].map { |item| item["uuid"] }
134     assert_includes ids, collections(:baz_file_in_asubproject).uuid
135   end
136
137   [['asc', :<=],
138    ['desc', :>=]].each do |order, operator|
139     test "user with project read permission can sort project collections #{order}" do
140       authorize_with :project_viewer
141       get :contents, {
142         id: groups(:asubproject).uuid,
143         format: :json,
144         filters: [['uuid', 'is_a', "arvados#collection"]],
145         order: "collections.name #{order}"
146       }
147       sorted_names = json_response['items'].collect { |item| item["name"] }
148       # Here we avoid assuming too much about the database
149       # collation. Both "alice"<"Bob" and "alice">"Bob" can be
150       # correct. Hopefully it _is_ safe to assume that if "a" comes
151       # before "b" in the ascii alphabet, "aX">"bY" is never true for
152       # any strings X and Y.
153       reliably_sortable_names = sorted_names.select do |name|
154         name[0] >= 'a' and name[0] <= 'z'
155       end.uniq do |name|
156         name[0]
157       end
158       # Preserve order of sorted_names. But do not use &=. If
159       # sorted_names has out-of-order duplicates, we want to preserve
160       # them here, so we can detect them and fail the test below.
161       sorted_names.select! do |name|
162         reliably_sortable_names.include? name
163       end
164       actually_checked_anything = false
165       previous = nil
166       sorted_names.each do |entry|
167         if previous
168           assert_operator(previous, operator, entry,
169                           "Entries sorted incorrectly.")
170           actually_checked_anything = true
171         end
172         previous = entry
173       end
174       assert actually_checked_anything, "Didn't even find two names to compare."
175     end
176   end
177
178   test 'list objects across multiple projects' do
179     authorize_with :project_viewer
180     get :contents, {
181       format: :json,
182       filters: [['uuid', 'is_a', 'arvados#specimen']]
183     }
184     assert_response :success
185     found_uuids = json_response['items'].collect { |i| i['uuid'] }
186     [[:in_aproject, true],
187      [:in_asubproject, true],
188      [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
189       if should_find
190         assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
191       else
192         refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
193       end
194     end
195   end
196
197   # Even though the project_viewer tests go through other controllers,
198   # I'm putting them here so they're easy to find alongside the other
199   # project tests.
200   def check_new_project_link_fails(link_attrs)
201     @controller = Arvados::V1::LinksController.new
202     post :create, link: {
203       link_class: "permission",
204       name: "can_read",
205       head_uuid: groups(:aproject).uuid,
206     }.merge(link_attrs)
207     assert_includes(403..422, response.status)
208   end
209
210   test "user with project read permission can't add users to it" do
211     authorize_with :project_viewer
212     check_new_project_link_fails(tail_uuid: users(:spectator).uuid)
213   end
214
215   test "user with project read permission can't add items to it" do
216     authorize_with :project_viewer
217     check_new_project_link_fails(tail_uuid: collections(:baz_file).uuid)
218   end
219
220   test "user with project read permission can't rename items in it" do
221     authorize_with :project_viewer
222     @controller = Arvados::V1::LinksController.new
223     post :update, {
224       id: jobs(:running).uuid,
225       name: "Denied test name",
226     }
227     assert_includes(403..404, response.status)
228   end
229
230   test "user with project read permission can't remove items from it" do
231     @controller = Arvados::V1::PipelineTemplatesController.new
232     authorize_with :project_viewer
233     post :update, {
234       id: pipeline_templates(:two_part).uuid,
235       pipeline_template: {
236         owner_uuid: users(:project_viewer).uuid,
237       }
238     }
239     assert_response 403
240   end
241
242   test "user with project read permission can't delete it" do
243     authorize_with :project_viewer
244     post :destroy, {id: groups(:aproject).uuid}
245     assert_response 403
246   end
247
248   test 'get group-owned objects with limit' do
249     authorize_with :active
250     get :contents, {
251       id: groups(:aproject).uuid,
252       limit: 1,
253       format: :json,
254     }
255     assert_response :success
256     assert_operator 1, :<, json_response['items_available']
257     assert_equal 1, json_response['items'].count
258   end
259
260   test 'get group-owned objects with limit and offset' do
261     authorize_with :active
262     get :contents, {
263       id: groups(:aproject).uuid,
264       limit: 1,
265       offset: 12345,
266       format: :json,
267     }
268     assert_response :success
269     assert_operator 1, :<, json_response['items_available']
270     assert_equal 0, json_response['items'].count
271   end
272
273   test 'get group-owned objects with additional filter matching nothing' do
274     authorize_with :active
275     get :contents, {
276       id: groups(:aproject).uuid,
277       filters: [['uuid', 'in', ['foo_not_a_uuid','bar_not_a_uuid']]],
278       format: :json,
279     }
280     assert_response :success
281     assert_equal [], json_response['items']
282     assert_equal 0, json_response['items_available']
283   end
284
285   %w(offset limit).each do |arg|
286     ['foo', '', '1234five', '0x10', '-8'].each do |val|
287       test "Raise error on bogus #{arg} parameter #{val.inspect}" do
288         authorize_with :active
289         get :contents, {
290           :id => groups(:aproject).uuid,
291           :format => :json,
292           arg => val,
293         }
294         assert_response 422
295       end
296     end
297   end
298
299   test "Collection contents don't include manifest_text" do
300     authorize_with :active
301     get :contents, {
302       id: groups(:aproject).uuid,
303       filters: [["uuid", "is_a", "arvados#collection"]],
304       format: :json,
305     }
306     assert_response :success
307     refute(json_response["items"].any? { |c| not c["portable_data_hash"] },
308            "response included an item without a portable data hash")
309     refute(json_response["items"].any? { |c| c.include?("manifest_text") },
310            "response included an item with a manifest text")
311   end
312
313   test 'get writable_by list for owned group' do
314     authorize_with :active
315     get :show, {
316       id: groups(:aproject).uuid,
317       format: :json
318     }
319     assert_response :success
320     assert_not_nil(json_response['writable_by'],
321                    "Should receive uuid list in 'writable_by' field")
322     assert_includes(json_response['writable_by'], users(:active).uuid,
323                     "owner should be included in writable_by list")
324   end
325
326   test 'no writable_by list for group with read-only access' do
327     authorize_with :rominiadmin
328     get :show, {
329       id: groups(:testusergroup_admins).uuid,
330       format: :json
331     }
332     assert_response :success
333     assert_equal([json_response['owner_uuid']],
334                  json_response['writable_by'],
335                  "Should only see owner_uuid in 'writable_by' field")
336   end
337
338   test 'get writable_by list by admin user' do
339     authorize_with :admin
340     get :show, {
341       id: groups(:testusergroup_admins).uuid,
342       format: :json
343     }
344     assert_response :success
345     assert_not_nil(json_response['writable_by'],
346                    "Should receive uuid list in 'writable_by' field")
347     assert_includes(json_response['writable_by'],
348                     users(:admin).uuid,
349                     "Current user should be included in 'writable_by' field")
350   end
351
352   test 'creating subproject with duplicate name fails' 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     }
361     assert_response 422
362     response_errors = json_response['errors']
363     assert_not_nil response_errors, 'Expected error in response'
364     assert(response_errors.first.include?('duplicate key'),
365            "Expected 'duplicate key' error in #{response_errors.first}")
366   end
367
368   test 'creating duplicate named subproject succeeds with ensure_unique_name' do
369     authorize_with :active
370     post :create, {
371       group: {
372         name: 'A Project',
373         owner_uuid: users(:active).uuid,
374         group_class: 'project',
375       },
376       ensure_unique_name: true
377     }
378     assert_response :success
379     new_project = json_response
380     assert_not_equal(new_project['uuid'],
381                      groups(:aproject).uuid,
382                      "create returned same uuid as existing project")
383     assert_match(/^A Project \(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z\)$/,
384                  new_project['name'])
385   end
386
387   test "unsharing a project results in hiding it from previously shared user" do
388     # remove sharing link for project
389     @controller = Arvados::V1::LinksController.new
390     authorize_with :admin
391     post :destroy, id: links(:share_starred_project_with_project_viewer).uuid
392     assert_response :success
393
394     # verify that the user can no longer see the project
395     @test_counter = 0  # Reset executed action counter
396     @controller = Arvados::V1::GroupsController.new
397     authorize_with :project_viewer
398     get :index, filters: [['group_class', '=', 'project']], format: :json
399     assert_response :success
400     found_projects = {}
401     json_response['items'].each do |g|
402       found_projects[g['uuid']] = g
403     end
404     assert_equal false, found_projects.include?(groups(:starred_and_shared_active_user_project).uuid)
405
406     # share the project
407     @test_counter = 0
408     @controller = Arvados::V1::LinksController.new
409     authorize_with :system_user
410     post :create, link: {
411       link_class: "permission",
412       name: "can_read",
413       head_uuid: groups(:starred_and_shared_active_user_project).uuid,
414       tail_uuid: users(:project_viewer).uuid,
415     }
416
417     # verify that project_viewer user can now see shared project again
418     @test_counter = 0
419     @controller = Arvados::V1::GroupsController.new
420     authorize_with :project_viewer
421     get :index, filters: [['group_class', '=', 'project']], format: :json
422     assert_response :success
423     found_projects = {}
424     json_response['items'].each do |g|
425       found_projects[g['uuid']] = g
426     end
427     assert_equal true, found_projects.include?(groups(:starred_and_shared_active_user_project).uuid)
428   end
429
430   [
431     [['owner_uuid', '!=', 'zzzzz-tpzed-xurymjxw79nv3jz'], 200,
432         'zzzzz-d1hrv-subprojpipeline', 'zzzzz-d1hrv-1xfj6xkicf2muk2'],
433     [["pipeline_instances.state", "not in", ["Complete", "Failed"]], 200,
434         'zzzzz-d1hrv-1xfj6xkicf2muk2', 'zzzzz-d1hrv-i3e77t9z5y8j9cc'],
435     [['container_requests.requesting_container_uuid', '=', nil], 200,
436         'zzzzz-xvhdp-cr4queuedcontnr', 'zzzzz-xvhdp-cr4requestercn2'],
437     [['container_requests.no_such_column', '=', nil], 422],
438     [['container_requests.', '=', nil], 422],
439     [['.requesting_container_uuid', '=', nil], 422],
440     [['no_such_table.uuid', '!=', 'zzzzz-tpzed-xurymjxw79nv3jz'], 422],
441   ].each do |filter, expect_code, expect_uuid, not_expect_uuid|
442     test "get contents with '#{filter}' filter" do
443       authorize_with :active
444       get :contents, filters: [filter], format: :json
445       assert_response expect_code
446       if expect_code == 200
447         assert_not_empty json_response['items']
448         item_uuids = json_response['items'].collect {|item| item['uuid']}
449         assert_includes(item_uuids, expect_uuid)
450         assert_not_includes(item_uuids, not_expect_uuid)
451       end
452     end
453   end
454
455   test 'get contents with jobs and pipeline instances disabled' do
456     Rails.configuration.disable_api_methods = ['jobs.index', 'pipeline_instances.index']
457
458     authorize_with :active
459     get :contents, {
460       id: groups(:aproject).uuid,
461       format: :json,
462     }
463     check_project_contents_response %w'arvados#pipelineInstance arvados#job'
464   end
465
466   test 'get contents with low max_index_database_read' do
467     # Some result will certainly have at least 12 bytes in a
468     # restricted column
469     Rails.configuration.max_index_database_read = 12
470     authorize_with :active
471     get :contents, {
472           id: groups(:aproject).uuid,
473           format: :json,
474         }
475     assert_response :success
476     assert_not_empty(json_response['items'])
477     assert_operator(json_response['items'].count,
478                     :<, json_response['items_available'])
479   end
480
481   test 'get contents, recursive=true' do
482     authorize_with :active
483     params = {
484       id: groups(:aproject).uuid,
485       recursive: true,
486       format: :json,
487     }
488     get :contents, params
489     owners = json_response['items'].map do |item|
490       item['owner_uuid']
491     end
492     assert_includes(owners, groups(:aproject).uuid)
493     assert_includes(owners, groups(:asubproject).uuid)
494   end
495
496   [false, nil].each do |recursive|
497     test "get contents, recursive=#{recursive.inspect}" do
498       authorize_with :active
499       params = {
500         id: groups(:aproject).uuid,
501         format: :json,
502       }
503       params[:recursive] = false if recursive == false
504       get :contents, params
505       owners = json_response['items'].map do |item|
506         item['owner_uuid']
507       end
508       assert_includes(owners, groups(:aproject).uuid)
509       refute_includes(owners, groups(:asubproject).uuid)
510     end
511   end
512
513   test 'get home project contents, recursive=true' do
514     authorize_with :active
515     get :contents, {
516           id: users(:active).uuid,
517           recursive: true,
518           format: :json,
519         }
520     owners = json_response['items'].map do |item|
521       item['owner_uuid']
522     end
523     assert_includes(owners, users(:active).uuid)
524     assert_includes(owners, groups(:aproject).uuid)
525     assert_includes(owners, groups(:asubproject).uuid)
526   end
527 end