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