14873: Fixes deprecation warnings on integration tests.
[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           params: {
14             id: groups(:aproject).uuid,
15             filters: [["uuid", "is_a", "arvados#collection"]].to_json,
16             orders: orders.to_json,
17             format: :json,
18           },
19           headers: auth(:active)
20         assert_response :success
21         if last.nil?
22           last = json_response['items']
23         else
24           assert_equal last, json_response['items']
25         end
26       end
27     end
28   end
29
30   test "get all pages of group-owned objects" do
31     limit = 5
32     offset = 0
33     items_available = nil
34     uuid_received = {}
35     owner_received = {}
36     while true
37       get "/arvados/v1/groups/contents",
38         params: {
39           id: groups(:aproject).uuid,
40           limit: limit,
41           offset: offset,
42           format: :json,
43         },
44         headers: auth(:active)
45
46       assert_response :success
47       assert_operator(0, :<, json_response['items'].count,
48                       "items_available=#{items_available} but received 0 "\
49                       "items with offset=#{offset}")
50       items_available ||= json_response['items_available']
51       assert_equal(items_available, json_response['items_available'],
52                    "items_available changed between page #{offset/limit} "\
53                    "and page #{1+offset/limit}")
54       json_response['items'].each do |item|
55         uuid = item['uuid']
56         assert_equal(nil, uuid_received[uuid],
57                      "Received '#{uuid}' again on page #{1+offset/limit}")
58         uuid_received[uuid] = true
59         owner_received[item['owner_uuid']] = true
60         offset += 1
61         assert_equal groups(:aproject).uuid, item['owner_uuid']
62       end
63       break if offset >= items_available
64     end
65   end
66
67   [
68     ['Collection_', true],            # collections and pipelines templates
69     ['hash', true],                   # pipeline templates
70     ['fa7aeb5140e2848d39b', false],   # script_parameter of pipeline instances
71     ['fa7aeb5140e2848d39b:*', true],  # script_parameter of pipeline instances
72     ['project pipeline', true],       # finds "Completed pipeline in A Project"
73     ['project pipeli:*', true],       # finds "Completed pipeline in A Project"
74     ['proje pipeli:*', false],        # first word is incomplete, so no prefix match
75     ['no-such-thing', false],         # script_parameter of pipeline instances
76   ].each do |search_filter, expect_results|
77     test "full text search of group-owned objects for #{search_filter}" do
78       get "/arvados/v1/groups/contents",
79         params: {
80           id: groups(:aproject).uuid,
81           limit: 5,
82           :filters => [['any', '@@', search_filter]].to_json
83         },
84         headers: auth(:active)
85       assert_response :success
86       if expect_results
87         refute_empty json_response['items']
88         json_response['items'].each do |item|
89           assert item['uuid']
90           assert_equal groups(:aproject).uuid, item['owner_uuid']
91         end
92       else
93         assert_empty json_response['items']
94       end
95     end
96   end
97
98   test "full text search is not supported for individual columns" do
99     get "/arvados/v1/groups/contents",
100       params: {
101         :filters => [['name', '@@', 'Private']].to_json
102       },
103       headers: auth(:active)
104     assert_response 422
105   end
106
107   test "group contents with include trash collections" do
108     get "/arvados/v1/groups/contents",
109       params: {
110         include_trash: "true",
111         filters: [["uuid", "is_a", "arvados#collection"]].to_json,
112         limit: 1000
113       },
114       headers: auth(:active)
115     assert_response 200
116
117     coll_uuids = []
118     json_response['items'].each { |c| coll_uuids << c['uuid'] }
119     assert_includes coll_uuids, collections(:foo_collection_in_aproject).uuid
120     assert_includes coll_uuids, collections(:expired_collection).uuid
121   end
122
123   test "group contents without trash collections" do
124     get "/arvados/v1/groups/contents",
125       params: {
126         filters: [["uuid", "is_a", "arvados#collection"]].to_json,
127         limit: 1000
128       },
129       headers: auth(:active)
130     assert_response 200
131
132     coll_uuids = []
133     json_response['items'].each { |c| coll_uuids << c['uuid'] }
134     assert_includes coll_uuids, collections(:foo_collection_in_aproject).uuid
135     assert_not_includes coll_uuids, collections(:expired_collection).uuid
136   end
137 end
138
139 class NonTransactionalGroupsTest < ActionDispatch::IntegrationTest
140   # Transactional tests are disabled to be able to test the concurrent
141   # asynchronous permissions update feature.
142   # This is needed because nested transactions share the connection pool, so
143   # one thread is locked while trying to talk to the database, until the other
144   # one finishes.
145   self.use_transactional_fixtures = false
146
147   teardown do
148     # Explicitly reset the database after each test.
149     post '/database/reset', params: {}, headers: auth(:admin)
150     assert_response :success
151   end
152
153   test "create request with async=true defers permissions update" do
154     Rails.configuration.async_permissions_update_interval = 1 # second
155     name = "Random group #{rand(1000)}"
156     assert_equal nil, Group.find_by_name(name)
157
158     # Trigger the asynchronous permission update by using async=true parameter.
159     post "/arvados/v1/groups",
160       params: {
161         group: {
162           name: name
163         },
164         async: true
165       },
166       headers: auth(:active)
167     assert_response 202
168
169     # The group exists on the database, but it's not accessible yet.
170     assert_not_nil Group.find_by_name(name)
171     get "/arvados/v1/groups",
172       params: {
173         filters: [["name", "=", name]].to_json,
174         limit: 10
175       },
176       headers: auth(:active)
177     assert_response 200
178     assert_equal 0, json_response['items_available']
179
180     # Wait a bit and try again.
181     sleep(1)
182     get "/arvados/v1/groups",
183       params: {
184         filters: [["name", "=", name]].to_json,
185         limit: 10
186       },
187       headers: auth(:active)
188     assert_response 200
189     assert_equal 1, json_response['items_available']
190   end
191 end