Add 'sdk/java-v2/' from commit '55f103e336ca9fb8bf1720d2ef4ee8dd4e221118'
[arvados.git] / services / api / test / integration / groups_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 GroupsTest < ActionDispatch::IntegrationTest
8   [[], ['replication_confirmed']].each do |orders|
9     test "results are consistent when provided orders #{orders} is incomplete" do
10       last = nil
11       (0..20).each do
12         get '/arvados/v1/groups/contents', {
13           id: groups(:aproject).uuid,
14           filters: [["uuid", "is_a", "arvados#collection"]].to_json,
15           orders: orders.to_json,
16           format: :json,
17         }, auth(:active)
18         assert_response :success
19         if last.nil?
20           last = json_response['items']
21         else
22           assert_equal last, json_response['items']
23         end
24       end
25     end
26   end
27
28   test "get all pages of group-owned objects" do
29     limit = 5
30     offset = 0
31     items_available = nil
32     uuid_received = {}
33     owner_received = {}
34     while true
35       get "/arvados/v1/groups/contents", {
36         id: groups(:aproject).uuid,
37         limit: limit,
38         offset: offset,
39         format: :json,
40       }, auth(:active)
41
42       assert_response :success
43       assert_operator(0, :<, json_response['items'].count,
44                       "items_available=#{items_available} but received 0 "\
45                       "items with offset=#{offset}")
46       items_available ||= json_response['items_available']
47       assert_equal(items_available, json_response['items_available'],
48                    "items_available changed between page #{offset/limit} "\
49                    "and page #{1+offset/limit}")
50       json_response['items'].each do |item|
51         uuid = item['uuid']
52         assert_equal(nil, uuid_received[uuid],
53                      "Received '#{uuid}' again on page #{1+offset/limit}")
54         uuid_received[uuid] = true
55         owner_received[item['owner_uuid']] = true
56         offset += 1
57         assert_equal groups(:aproject).uuid, item['owner_uuid']
58       end
59       break if offset >= items_available
60     end
61   end
62
63   [
64     ['Collection_', true],            # collections and pipelines templates
65     ['hash', true],                   # pipeline templates
66     ['fa7aeb5140e2848d39b', false],   # script_parameter of pipeline instances
67     ['fa7aeb5140e2848d39b:*', true],  # script_parameter of pipeline instances
68     ['project pipeline', true],       # finds "Completed pipeline in A Project"
69     ['project pipeli:*', true],       # finds "Completed pipeline in A Project"
70     ['proje pipeli:*', false],        # first word is incomplete, so no prefix match
71     ['no-such-thing', false],         # script_parameter of pipeline instances
72   ].each do |search_filter, expect_results|
73     test "full text search of group-owned objects for #{search_filter}" do
74       get "/arvados/v1/groups/contents", {
75         id: groups(:aproject).uuid,
76         limit: 5,
77         :filters => [['any', '@@', search_filter]].to_json
78       }, auth(:active)
79       assert_response :success
80       if expect_results
81         refute_empty json_response['items']
82         json_response['items'].each do |item|
83           assert item['uuid']
84           assert_equal groups(:aproject).uuid, item['owner_uuid']
85         end
86       else
87         assert_empty json_response['items']
88       end
89     end
90   end
91
92   test "full text search is not supported for individual columns" do
93     get "/arvados/v1/groups/contents", {
94       :filters => [['name', '@@', 'Private']].to_json
95     }, auth(:active)
96     assert_response 422
97   end
98
99   test "group contents with include trash collections" do
100     get "/arvados/v1/groups/contents", {
101       include_trash: "true",
102       filters: [["uuid", "is_a", "arvados#collection"]].to_json,
103       limit: 1000
104     }, auth(:active)
105     assert_response 200
106
107     coll_uuids = []
108     json_response['items'].each { |c| coll_uuids << c['uuid'] }
109     assert_includes coll_uuids, collections(:foo_collection_in_aproject).uuid
110     assert_includes coll_uuids, collections(:expired_collection).uuid
111   end
112
113   test "group contents without trash collections" do
114     get "/arvados/v1/groups/contents", {
115       filters: [["uuid", "is_a", "arvados#collection"]].to_json,
116       limit: 1000
117     }, auth(:active)
118     assert_response 200
119
120     coll_uuids = []
121     json_response['items'].each { |c| coll_uuids << c['uuid'] }
122     assert_includes coll_uuids, collections(:foo_collection_in_aproject).uuid
123     assert_not_includes coll_uuids, collections(:expired_collection).uuid
124   end
125
126   test "create request with async=true defers permissions update" do
127     Rails.configuration.async_permissions_update_interval = 1 # seconds
128     name = "Random group #{rand(1000)}"
129     assert_equal nil, Group.find_by_name(name)
130     post "/arvados/v1/groups", {
131       group: {
132         name: name
133       },
134       async: true
135     }, auth(:active)
136     assert_response 202
137     g = Group.find_by_name(name)
138     assert_not_nil g
139     get "/arvados/v1/groups", {
140       filters: [["name", "=", name]].to_json,
141       limit: 10
142     }, auth(:active)
143     assert_response 200
144     assert_equal 0, json_response['items_available']
145
146     # Unblock the thread doing the permissions update
147     ActiveRecord::Base.clear_active_connections!
148
149     sleep(3)
150     get "/arvados/v1/groups", {
151       filters: [["name", "=", name]].to_json,
152       limit: 10
153     }, auth(:active)
154     assert_response 200
155     assert_equal 1, json_response['items_available']
156   end
157 end