Merge branch 'patch-1' of https://github.com/mr-c/arvados into mr-c-patch-1
[arvados.git] / services / api / test / functional / arvados / v1 / groups_controller_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class Arvados::V1::GroupsControllerTest < ActionController::TestCase
8
9   test "attempt to delete group without read or write access" do
10     authorize_with :active
11     post :destroy, params: {id: groups(:empty_lonely_group).uuid}
12     assert_response 404
13   end
14
15   test "attempt to delete group without write access" do
16     authorize_with :active
17     post :destroy, params: {id: groups(:all_users).uuid}
18     assert_response 403
19   end
20
21   test "get list of projects" do
22     authorize_with :active
23     get :index, params: {filters: [['group_class', '=', 'project']], format: :json}
24     assert_response :success
25     group_uuids = []
26     json_response['items'].each do |group|
27       assert_equal 'project', group['group_class']
28       group_uuids << group['uuid']
29     end
30     assert_includes group_uuids, groups(:aproject).uuid
31     assert_includes group_uuids, groups(:asubproject).uuid
32     assert_includes group_uuids, groups(:private).uuid
33     assert_not_includes group_uuids, groups(:system_group).uuid
34     assert_not_includes group_uuids, groups(:private_and_can_read_foofile).uuid
35   end
36
37   test "get list of groups that are not projects" do
38     authorize_with :active
39     get :index, params: {filters: [['group_class', '!=', 'project']], format: :json}
40     assert_response :success
41     group_uuids = []
42     json_response['items'].each do |group|
43       assert_not_equal 'project', group['group_class']
44       group_uuids << group['uuid']
45     end
46     assert_not_includes group_uuids, groups(:aproject).uuid
47     assert_not_includes group_uuids, groups(:asubproject).uuid
48   end
49
50   test "get list of groups with bogus group_class" do
51     authorize_with :active
52     get :index, params: {
53       filters: [['group_class', '=', 'nogrouphasthislittleclass']],
54       format: :json,
55     }
56     assert_response :success
57     assert_equal [], json_response['items']
58     assert_equal 0, json_response['items_available']
59   end
60
61   def check_project_contents_response disabled_kinds=[]
62     assert_response :success
63     assert_operator 2, :<=, json_response['items_available']
64     assert_operator 2, :<=, json_response['items'].count
65     kinds = json_response['items'].collect { |i| i['kind'] }.uniq
66     expect_kinds = %w'arvados#group arvados#specimen arvados#pipelineTemplate arvados#job' - disabled_kinds
67     assert_equal expect_kinds, (expect_kinds & kinds)
68
69     json_response['items'].each do |i|
70       if i['kind'] == 'arvados#group'
71         assert(i['group_class'] == 'project',
72                "group#contents returned a non-project group")
73       end
74     end
75
76     disabled_kinds.each do |d|
77       assert_equal true, !kinds.include?(d)
78     end
79   end
80
81   test 'get group-owned objects' do
82     authorize_with :active
83     get :contents, params: {
84       id: groups(:aproject).uuid,
85       format: :json,
86     }
87     check_project_contents_response
88   end
89
90   test "user with project read permission can see project objects" do
91     authorize_with :project_viewer
92     get :contents, params: {
93       id: groups(:aproject).uuid,
94       format: :json,
95     }
96     check_project_contents_response
97   end
98
99   test "list objects across projects" do
100     authorize_with :project_viewer
101     get :contents, params: {
102       format: :json,
103       filters: [['uuid', 'is_a', 'arvados#specimen']]
104     }
105     assert_response :success
106     found_uuids = json_response['items'].collect { |i| i['uuid'] }
107     [[:in_aproject, true],
108      [:in_asubproject, true],
109      [:owned_by_private_group, false]].each do |specimen_fixture, should_find|
110       if should_find
111         assert_includes found_uuids, specimens(specimen_fixture).uuid, "did not find specimen fixture '#{specimen_fixture}'"
112       else
113         refute_includes found_uuids, specimens(specimen_fixture).uuid, "found specimen fixture '#{specimen_fixture}'"
114       end
115     end
116   end
117
118   test "list trashed collections and projects" do
119     authorize_with :active
120     get(:contents, params: {
121           format: :json,
122           include_trash: true,
123           filters: [
124             ['uuid', 'is_a', ['arvados#collection', 'arvados#group']],
125             ['is_trashed', '=', true],
126           ],
127           limit: 10000,
128         })
129     assert_response :success
130     found_uuids = json_response['items'].collect { |i| i['uuid'] }
131     assert_includes found_uuids, groups(:trashed_project).uuid
132     refute_includes found_uuids, groups(:aproject).uuid
133     assert_includes found_uuids, collections(:expired_collection).uuid
134     refute_includes found_uuids, collections(:w_a_z_file).uuid
135   end
136
137   test "list objects in home project" do
138     authorize_with :active
139     get :contents, params: {
140       format: :json,
141       limit: 200,
142       id: users(:active).uuid
143     }
144     assert_response :success
145     found_uuids = json_response['items'].collect { |i| i['uuid'] }
146     assert_includes found_uuids, specimens(:owned_by_active_user).uuid, "specimen did not appear in home project"
147     refute_includes found_uuids, specimens(:in_asubproject).uuid, "specimen appeared unexpectedly in home project"
148   end
149
150   test "user with project read permission can see project collections" do
151     authorize_with :project_viewer
152     get :contents, params: {
153       id: groups(:asubproject).uuid,
154       format: :json,
155     }
156     ids = json_response['items'].map { |item| item["uuid"] }
157     assert_includes ids, collections(:baz_file_in_asubproject).uuid
158   end
159
160   [
161     ['collections.name', 'asc', :<=, "name"],
162     ['collections.name', 'desc', :>=, "name"],
163     ['name', 'asc', :<=, "name"],
164     ['name', 'desc', :>=, "name"],
165     ['collections.created_at', 'asc', :<=, "created_at"],
166     ['collections.created_at', 'desc', :>=, "created_at"],
167     ['created_at', 'asc', :<=, "created_at"],
168     ['created_at', 'desc', :>=, "created_at"],
169   ].each do |column, order, operator, field|
170     test "user with project read permission can sort projects on #{column} #{order}" do
171       authorize_with :project_viewer
172       get :contents, params: {
173         id: groups(:asubproject).uuid,
174         format: :json,
175         filters: [['uuid', 'is_a', "arvados#collection"]],
176         order: "#{column} #{order}"
177       }
178       sorted_values = json_response['items'].collect { |item| item[field] }
179       if field == "name"
180         # Here we avoid assuming too much about the database
181         # collation. Both "alice"<"Bob" and "alice">"Bob" can be
182         # correct. Hopefully it _is_ safe to assume that if "a" comes
183         # before "b" in the ascii alphabet, "aX">"bY" is never true for
184         # any strings X and Y.
185         reliably_sortable_names = sorted_values.select do |name|
186           name[0] >= 'a' && name[0] <= 'z'
187         end.uniq do |name|
188           name[0]
189         end
190         # Preserve order of sorted_values. But do not use &=. If
191         # sorted_values has out-of-order duplicates, we want to preserve
192         # them here, so we can detect them and fail the test below.
193         sorted_values.select! do |name|
194           reliably_sortable_names.include? name
195         end
196       end
197       assert_sorted(operator, sorted_values)
198     end
199   end
200
201   def assert_sorted(operator, sorted_items)
202     actually_checked_anything = false
203     previous = nil
204     sorted_items.each do |entry|
205       if !previous.nil?
206         assert_operator(previous, operator, entry,
207                         "Entries sorted incorrectly.")
208         actually_checked_anything = true
209       end
210       previous = entry
211     end
212     assert actually_checked_anything, "Didn't even find two items to compare."
213   end
214
215   # Even though the project_viewer tests go through other controllers,
216   # I'm putting them here so they're easy to find alongside the other
217   # project tests.
218   def check_new_project_link_fails(link_attrs)
219     @controller = Arvados::V1::LinksController.new
220     post :create, params: {
221       link: {
222         link_class: "permission",
223         name: "can_read",
224         head_uuid: groups(:aproject).uuid,
225       }.merge(link_attrs)
226     }
227     assert_includes(403..422, response.status)
228   end
229
230   test "user with project read permission can't add users to it" do
231     authorize_with :project_viewer
232     check_new_project_link_fails(tail_uuid: users(:spectator).uuid)
233   end
234
235   test "user with project read permission can't add items to it" do
236     authorize_with :project_viewer
237     check_new_project_link_fails(tail_uuid: collections(:baz_file).uuid)
238   end
239
240   test "user with project read permission can't rename items in it" do
241     authorize_with :project_viewer
242     @controller = Arvados::V1::LinksController.new
243     post :update, params: {
244       id: jobs(:running).uuid,
245       name: "Denied test name",
246     }
247     assert_includes(403..404, response.status)
248   end
249
250   test "user with project read permission can't remove items from it" do
251     @controller = Arvados::V1::PipelineTemplatesController.new
252     authorize_with :project_viewer
253     post :update, params: {
254       id: pipeline_templates(:two_part).uuid,
255       pipeline_template: {
256         owner_uuid: users(:project_viewer).uuid,
257       }
258     }
259     assert_response 403
260   end
261
262   test "user with project read permission can't delete it" do
263     authorize_with :project_viewer
264     post :destroy, params: {id: groups(:aproject).uuid}
265     assert_response 403
266   end
267
268   test 'get group-owned objects with limit' do
269     authorize_with :active
270     get :contents, params: {
271       id: groups(:aproject).uuid,
272       limit: 1,
273       format: :json,
274     }
275     assert_response :success
276     assert_operator 1, :<, json_response['items_available']
277     assert_equal 1, json_response['items'].count
278   end
279
280   test 'get group-owned objects with limit and offset' do
281     authorize_with :active
282     get :contents, params: {
283       id: groups(:aproject).uuid,
284       limit: 1,
285       offset: 12345,
286       format: :json,
287     }
288     assert_response :success
289     assert_operator 1, :<, json_response['items_available']
290     assert_equal 0, json_response['items'].count
291   end
292
293   test 'get group-owned objects with additional filter matching nothing' do
294     authorize_with :active
295     get :contents, params: {
296       id: groups(:aproject).uuid,
297       filters: [['uuid', 'in', ['foo_not_a_uuid','bar_not_a_uuid']]],
298       format: :json,
299     }
300     assert_response :success
301     assert_equal [], json_response['items']
302     assert_equal 0, json_response['items_available']
303   end
304
305   %w(offset limit).each do |arg|
306     ['foo', '', '1234five', '0x10', '-8'].each do |val|
307       test "Raise error on bogus #{arg} parameter #{val.inspect}" do
308         authorize_with :active
309         get :contents, params: {
310           :id => groups(:aproject).uuid,
311           :format => :json,
312           arg => val,
313         }
314         assert_response 422
315       end
316     end
317   end
318
319   test "Collection contents don't include manifest_text" do
320     authorize_with :active
321     get :contents, params: {
322       id: groups(:aproject).uuid,
323       filters: [["uuid", "is_a", "arvados#collection"]],
324       format: :json,
325     }
326     assert_response :success
327     refute(json_response["items"].any? { |c| not c["portable_data_hash"] },
328            "response included an item without a portable data hash")
329     refute(json_response["items"].any? { |c| c.include?("manifest_text") },
330            "response included an item with a manifest text")
331   end
332
333   test 'get writable_by list for owned group' do
334     authorize_with :active
335     get :show, params: {
336       id: groups(:aproject).uuid,
337       format: :json
338     }
339     assert_response :success
340     assert_not_nil(json_response['writable_by'],
341                    "Should receive uuid list in 'writable_by' field")
342     assert_includes(json_response['writable_by'], users(:active).uuid,
343                     "owner should be included in writable_by list")
344   end
345
346   test 'no writable_by list for group with read-only access' do
347     authorize_with :rominiadmin
348     get :show, params: {
349       id: groups(:testusergroup_admins).uuid,
350       format: :json
351     }
352     assert_response :success
353     assert_equal([json_response['owner_uuid']],
354                  json_response['writable_by'],
355                  "Should only see owner_uuid in 'writable_by' field")
356   end
357
358   test 'get writable_by list by admin user' do
359     authorize_with :admin
360     get :show, params: {
361       id: groups(:testusergroup_admins).uuid,
362       format: :json
363     }
364     assert_response :success
365     assert_not_nil(json_response['writable_by'],
366                    "Should receive uuid list in 'writable_by' field")
367     assert_includes(json_response['writable_by'],
368                     users(:admin).uuid,
369                     "Current user should be included in 'writable_by' field")
370   end
371
372   test 'creating subproject with duplicate name fails' do
373     authorize_with :active
374     post :create, params: {
375       group: {
376         name: 'A Project',
377         owner_uuid: users(:active).uuid,
378         group_class: 'project',
379       },
380     }
381     assert_response 422
382     response_errors = json_response['errors']
383     assert_not_nil response_errors, 'Expected error in response'
384     assert(response_errors.first.include?('duplicate key'),
385            "Expected 'duplicate key' error in #{response_errors.first}")
386   end
387
388   test 'creating duplicate named subproject succeeds with ensure_unique_name' do
389     authorize_with :active
390     post :create, params: {
391       group: {
392         name: 'A Project',
393         owner_uuid: users(:active).uuid,
394         group_class: 'project',
395       },
396       ensure_unique_name: true
397     }
398     assert_response :success
399     new_project = json_response
400     assert_not_equal(new_project['uuid'],
401                      groups(:aproject).uuid,
402                      "create returned same uuid as existing project")
403     assert_match(/^A Project \(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z\)$/,
404                  new_project['name'])
405   end
406
407   [
408     [['owner_uuid', '!=', 'zzzzz-tpzed-xurymjxw79nv3jz'], 200,
409         'zzzzz-d1hrv-subprojpipeline', 'zzzzz-d1hrv-1xfj6xkicf2muk2'],
410     [["pipeline_instances.state", "not in", ["Complete", "Failed"]], 200,
411         'zzzzz-d1hrv-1xfj6xkicf2muk2', 'zzzzz-d1hrv-i3e77t9z5y8j9cc'],
412     [['container_requests.requesting_container_uuid', '=', nil], 200,
413         'zzzzz-xvhdp-cr4queuedcontnr', 'zzzzz-xvhdp-cr4requestercn2'],
414     [['container_requests.no_such_column', '=', nil], 422],
415     [['container_requests.', '=', nil], 422],
416     [['.requesting_container_uuid', '=', nil], 422],
417     [['no_such_table.uuid', '!=', 'zzzzz-tpzed-xurymjxw79nv3jz'], 422],
418   ].each do |filter, expect_code, expect_uuid, not_expect_uuid|
419     test "get contents with '#{filter}' filter" do
420       authorize_with :active
421       get :contents, params: {filters: [filter], format: :json}
422       assert_response expect_code
423       if expect_code == 200
424         assert_not_empty json_response['items']
425         item_uuids = json_response['items'].collect {|item| item['uuid']}
426         assert_includes(item_uuids, expect_uuid)
427         assert_not_includes(item_uuids, not_expect_uuid)
428       end
429     end
430   end
431
432   test 'get contents with jobs and pipeline instances disabled' do
433     Rails.configuration.API.DisabledAPIs = {'jobs.index'=>{}, 'pipeline_instances.index'=>{}}
434
435     authorize_with :active
436     get :contents, params: {
437       id: groups(:aproject).uuid,
438       format: :json,
439     }
440     check_project_contents_response %w'arvados#pipelineInstance arvados#job'
441   end
442
443   test 'get contents with low max_index_database_read' do
444     # Some result will certainly have at least 12 bytes in a
445     # restricted column
446     Rails.configuration.API.MaxIndexDatabaseRead = 12
447     authorize_with :active
448     get :contents, params: {
449           id: groups(:aproject).uuid,
450           format: :json,
451         }
452     assert_response :success
453     assert_not_empty(json_response['items'])
454     assert_operator(json_response['items'].count,
455                     :<, json_response['items_available'])
456   end
457
458   test 'get contents, recursive=true' do
459     authorize_with :active
460     params = {
461       id: groups(:aproject).uuid,
462       recursive: true,
463       format: :json,
464     }
465     get :contents, params: params
466     owners = json_response['items'].map do |item|
467       item['owner_uuid']
468     end
469     assert_includes(owners, groups(:aproject).uuid)
470     assert_includes(owners, groups(:asubproject).uuid)
471   end
472
473   [false, nil].each do |recursive|
474     test "get contents, recursive=#{recursive.inspect}" do
475       authorize_with :active
476       params = {
477         id: groups(:aproject).uuid,
478         format: :json,
479       }
480       params[:recursive] = false if recursive == false
481       get :contents, params: params
482       owners = json_response['items'].map do |item|
483         item['owner_uuid']
484       end
485       assert_includes(owners, groups(:aproject).uuid)
486       refute_includes(owners, groups(:asubproject).uuid)
487     end
488   end
489
490   test 'get home project contents, recursive=true' do
491     authorize_with :active
492     get :contents, params: {
493           id: users(:active).uuid,
494           recursive: true,
495           format: :json,
496         }
497     owners = json_response['items'].map do |item|
498       item['owner_uuid']
499     end
500     assert_includes(owners, users(:active).uuid)
501     assert_includes(owners, groups(:aproject).uuid)
502     assert_includes(owners, groups(:asubproject).uuid)
503   end
504
505   ### trashed project tests ###
506
507   #
508   # The structure is
509   #
510   # trashed_project         (zzzzz-j7d0g-trashedproject1)
511   #   trashed_subproject    (zzzzz-j7d0g-trashedproject2)
512   #   trashed_subproject3   (zzzzz-j7d0g-trashedproject3)
513   #   zzzzz-xvhdp-cr5trashedcontr
514
515   [:active,
516    :admin].each do |auth|
517     # project: to query,    to untrash,    is visible, parent contents listing success
518     [
519      [:trashed_project,     [],                 false, true],
520      [:trashed_project,     [:trashed_project], true,  true],
521      [:trashed_subproject,  [],                 false, false],
522      [:trashed_subproject,  [:trashed_project], true,  true],
523      [:trashed_subproject3, [:trashed_project], false, true],
524      [:trashed_subproject3, [:trashed_subproject3], false, false],
525      [:trashed_subproject3, [:trashed_project, :trashed_subproject3], true, true],
526     ].each do |project, untrash, visible, success|
527
528       test "contents listing #{project} #{untrash} as #{auth}" do
529         authorize_with auth
530         untrash.each do |pr|
531           Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
532         end
533         get :contents, params: {
534               id: groups(project).owner_uuid,
535               format: :json
536             }
537         if success
538           assert_response :success
539           item_uuids = json_response['items'].map do |item|
540             item['uuid']
541           end
542           if visible
543             assert_includes(item_uuids, groups(project).uuid)
544           else
545             assert_not_includes(item_uuids, groups(project).uuid)
546           end
547         else
548           assert_response 404
549         end
550       end
551
552       test "contents of #{project} #{untrash} as #{auth}" do
553         authorize_with auth
554         untrash.each do |pr|
555           Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
556         end
557         get :contents, params: {
558               id: groups(project).uuid,
559               format: :json
560             }
561         if visible
562           assert_response :success
563         else
564           assert_response 404
565         end
566       end
567
568       test "index #{project} #{untrash} as #{auth}" do
569         authorize_with auth
570         untrash.each do |pr|
571           Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
572         end
573         get :index, params: {
574               format: :json,
575             }
576         assert_response :success
577         item_uuids = json_response['items'].map do |item|
578           item['uuid']
579         end
580         if visible
581           assert_includes(item_uuids, groups(project).uuid)
582         else
583           assert_not_includes(item_uuids, groups(project).uuid)
584         end
585       end
586
587       test "show #{project} #{untrash} as #{auth}" do
588         authorize_with auth
589         untrash.each do |pr|
590           Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
591         end
592         get :show, params: {
593               id: groups(project).uuid,
594               format: :json
595             }
596         if visible
597           assert_response :success
598         else
599           assert_response 404
600         end
601       end
602
603       test "show include_trash=false #{project} #{untrash} as #{auth}" do
604         authorize_with auth
605         untrash.each do |pr|
606           Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
607         end
608         get :show, params: {
609               id: groups(project).uuid,
610               format: :json,
611               include_trash: false
612             }
613         if visible
614           assert_response :success
615         else
616           assert_response 404
617         end
618       end
619
620       test "show include_trash #{project} #{untrash} as #{auth}" do
621         authorize_with auth
622         untrash.each do |pr|
623           Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
624         end
625         get :show, params: {
626               id: groups(project).uuid,
627               format: :json,
628               include_trash: true
629             }
630         assert_response :success
631       end
632
633       test "index include_trash #{project} #{untrash} as #{auth}" do
634         authorize_with auth
635         untrash.each do |pr|
636           Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
637         end
638         get :index, params: {
639               format: :json,
640               include_trash: true
641             }
642         assert_response :success
643         item_uuids = json_response['items'].map do |item|
644           item['uuid']
645         end
646         assert_includes(item_uuids, groups(project).uuid)
647       end
648     end
649
650     test "delete project #{auth}" do
651       authorize_with auth
652       [:trashed_project].each do |pr|
653         Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
654       end
655       assert !Group.find_by_uuid(groups(:trashed_project).uuid).is_trashed
656       post :destroy, params: {
657             id: groups(:trashed_project).uuid,
658             format: :json,
659           }
660       assert_response :success
661       assert Group.find_by_uuid(groups(:trashed_project).uuid).is_trashed
662     end
663
664     test "untrash project #{auth}" do
665       authorize_with auth
666       assert Group.find_by_uuid(groups(:trashed_project).uuid).is_trashed
667       post :untrash, params: {
668             id: groups(:trashed_project).uuid,
669             format: :json,
670           }
671       assert_response :success
672       assert !Group.find_by_uuid(groups(:trashed_project).uuid).is_trashed
673     end
674
675     test "untrash project with name conflict #{auth}" do
676       authorize_with auth
677       [:trashed_project].each do |pr|
678         Group.find_by_uuid(groups(pr).uuid).update! is_trashed: false
679       end
680       gc = Group.create!({owner_uuid: "zzzzz-j7d0g-trashedproject1",
681                          name: "trashed subproject 3",
682                          group_class: "project"})
683       post :untrash, params: {
684             id: groups(:trashed_subproject3).uuid,
685             format: :json,
686             ensure_unique_name: true
687            }
688       assert_response :success
689       assert_match /^trashed subproject 3 \(\d{4}-\d\d-\d\d.*?Z\)$/, json_response['name']
690     end
691
692     test "move trashed subproject to new owner #{auth}" do
693       authorize_with auth
694       assert_nil Group.readable_by(users(auth)).where(uuid: groups(:trashed_subproject).uuid).first
695       put :update, params: {
696             id: groups(:trashed_subproject).uuid,
697             group: {
698               owner_uuid: users(:active).uuid
699             },
700             include_trash: true,
701             format: :json,
702           }
703       assert_response :success
704       assert_not_nil Group.readable_by(users(auth)).where(uuid: groups(:trashed_subproject).uuid).first
705     end
706   end
707
708   test 'get shared owned by another user' do
709     authorize_with :user_bar_in_sharing_group
710
711     act_as_system_user do
712       Link.create!(
713         tail_uuid: users(:user_bar_in_sharing_group).uuid,
714         link_class: 'permission',
715         name: 'can_read',
716         head_uuid: groups(:project_owned_by_foo).uuid)
717     end
718
719     get :shared, params: {:filters => [["group_class", "=", "project"]], :include => "owner_uuid"}
720
721     assert_equal 1, json_response['items'].length
722     assert_equal json_response['items'][0]["uuid"], groups(:project_owned_by_foo).uuid
723
724     assert_equal 1, json_response['included'].length
725     assert_equal json_response['included'][0]["uuid"], users(:user_foo_in_sharing_group).uuid
726   end
727
728   test 'get shared, owned by unreadable project' do
729     authorize_with :user_bar_in_sharing_group
730
731     act_as_system_user do
732       Group.find_by_uuid(groups(:project_owned_by_foo).uuid).update!(owner_uuid: groups(:aproject).uuid)
733       Link.create!(
734         tail_uuid: users(:user_bar_in_sharing_group).uuid,
735         link_class: 'permission',
736         name: 'can_read',
737         head_uuid: groups(:project_owned_by_foo).uuid)
738     end
739
740     get :shared, params: {:filters => [["group_class", "=", "project"]], :include => "owner_uuid"}
741
742     assert_equal 1, json_response['items'].length
743     assert_equal json_response['items'][0]["uuid"], groups(:project_owned_by_foo).uuid
744
745     assert_equal 0, json_response['included'].length
746   end
747
748   test 'get shared, add permission link' do
749     authorize_with :user_bar_in_sharing_group
750
751     act_as_system_user do
752       Link.create!(tail_uuid: groups(:group_for_sharing_tests).uuid,
753                    head_uuid: groups(:project_owned_by_foo).uuid,
754                    link_class: 'permission',
755                    name: 'can_manage')
756     end
757
758     get :shared, params: {:filters => [["group_class", "=", "project"]], :include => "owner_uuid"}
759
760     assert_equal 1, json_response['items'].length
761     assert_equal groups(:project_owned_by_foo).uuid, json_response['items'][0]["uuid"]
762
763     assert_equal 1, json_response['included'].length
764     assert_equal users(:user_foo_in_sharing_group).uuid, json_response['included'][0]["uuid"]
765   end
766
767   ### contents with exclude_home_project
768
769   test 'contents, exclude home owned by another user' do
770     authorize_with :user_bar_in_sharing_group
771
772     act_as_system_user do
773       Link.create!(
774         tail_uuid: users(:user_bar_in_sharing_group).uuid,
775         link_class: 'permission',
776         name: 'can_read',
777         head_uuid: groups(:project_owned_by_foo).uuid)
778       Link.create!(
779         tail_uuid: users(:user_bar_in_sharing_group).uuid,
780         link_class: 'permission',
781         name: 'can_read',
782         head_uuid: collections(:collection_owned_by_foo).uuid)
783     end
784
785     get :contents, params: {:include => "owner_uuid", :exclude_home_project => true}
786
787     assert_equal 2, json_response['items'].length
788     assert_equal json_response['items'][0]["uuid"], groups(:project_owned_by_foo).uuid
789     assert_equal json_response['items'][1]["uuid"], collections(:collection_owned_by_foo).uuid
790
791     assert_equal 1, json_response['included'].length
792     assert_equal json_response['included'][0]["uuid"], users(:user_foo_in_sharing_group).uuid
793   end
794
795   test 'contents, exclude home, owned by unreadable project' do
796     authorize_with :user_bar_in_sharing_group
797
798     act_as_system_user do
799       Group.find_by_uuid(groups(:project_owned_by_foo).uuid).update!(owner_uuid: groups(:aproject).uuid)
800       Link.create!(
801         tail_uuid: users(:user_bar_in_sharing_group).uuid,
802         link_class: 'permission',
803         name: 'can_read',
804         head_uuid: groups(:project_owned_by_foo).uuid)
805     end
806
807     get :contents, params: {:include => "owner_uuid", :exclude_home_project => true}
808
809     assert_equal 1, json_response['items'].length
810     assert_equal json_response['items'][0]["uuid"], groups(:project_owned_by_foo).uuid
811
812     assert_equal 0, json_response['included'].length
813   end
814
815   test 'contents, exclude home, add permission link' do
816     authorize_with :user_bar_in_sharing_group
817
818     act_as_system_user do
819       Link.create!(tail_uuid: groups(:group_for_sharing_tests).uuid,
820                    head_uuid: groups(:project_owned_by_foo).uuid,
821                    link_class: 'permission',
822                    name: 'can_manage')
823     end
824
825     get :contents, params: {:include => "owner_uuid", :exclude_home_project => true}
826
827     assert_equal 1, json_response['items'].length
828     assert_equal groups(:project_owned_by_foo).uuid, json_response['items'][0]["uuid"]
829
830     assert_equal 1, json_response['included'].length
831     assert_equal users(:user_foo_in_sharing_group).uuid, json_response['included'][0]["uuid"]
832   end
833
834   test 'contents, exclude home, with parent specified' do
835     authorize_with :active
836
837     get :contents, params: {id: groups(:aproject).uuid, :include => "owner_uuid", :exclude_home_project => true}
838
839     assert_response 422
840   end
841 end