3 class ApplicationControllerTest < ActionController::TestCase
4 # These tests don't do state-changing API calls. Save some time by
5 # skipping the database reset.
6 reset_api_fixtures :after_each_test, false
7 reset_api_fixtures :after_suite, true
10 @user_dataclass = ArvadosBase.resource_class_for_uuid(api_fixture('users')['active']['uuid'])
13 test "links for object" do
16 ac = ApplicationController.new
18 link_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
20 links = ac.send :links_for_object, link_head_uuid
22 assert links, 'Expected links'
23 assert links.is_a?(Array), 'Expected an array'
24 assert links.size > 0, 'Expected at least one link'
25 assert links[0][:uuid], 'Expected uuid for the head_link'
28 test "preload links for objects and uuids" do
31 ac = ApplicationController.new
33 link1_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
34 link2_uuid = api_fixture('links')['bar_file_readable_by_active']['uuid']
35 link3_head_uuid = api_fixture('links')['bar_file_readable_by_active']['head_uuid']
37 link2_object = User.find(api_fixture('users')['active']['uuid'])
38 link2_object_uuid = link2_object['uuid']
40 uuids = [link1_head_uuid, link2_object, link3_head_uuid]
41 links = ac.send :preload_links_for_objects, uuids
43 assert links, 'Expected links'
44 assert links.is_a?(Hash), 'Expected a hash'
45 assert links.size == 3, 'Expected two objects in the preloaded links hash'
46 assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
47 assert links[link2_object_uuid], 'Expected links for the passed in object uuid'
48 assert links[link3_head_uuid], 'Expected links for the passed in link head_uuid'
50 # invoke again for this same input. this time, the preloaded data will be returned
51 links = ac.send :preload_links_for_objects, uuids
52 assert links, 'Expected links'
53 assert links.is_a?(Hash), 'Expected a hash'
54 assert links.size == 3, 'Expected two objects in the preloaded links hash'
55 assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
58 [ [:preload_links_for_objects, [] ],
59 [:preload_collections_for_objects, [] ],
60 [:preload_log_collections_for_objects, [] ],
61 [:preload_objects_for_dataclass, [] ],
63 test "preload data for empty array input #{input}" do
66 ac = ApplicationController.new
68 if input[0] == :preload_objects_for_dataclass
69 objects = ac.send input[0], @user_dataclass, input[1]
71 objects = ac.send input[0], input[1]
74 assert objects, 'Expected objects'
75 assert objects.is_a?(Hash), 'Expected a hash'
76 assert objects.size == 0, 'Expected no objects in the preloaded hash'
80 [ [:preload_links_for_objects, 'input not an array'],
81 [:preload_links_for_objects, nil],
82 [:links_for_object, nil],
83 [:preload_collections_for_objects, 'input not an array'],
84 [:preload_collections_for_objects, nil],
85 [:collections_for_object, nil],
86 [:preload_log_collections_for_objects, 'input not an array'],
87 [:preload_log_collections_for_objects, nil],
88 [:log_collections_for_object, nil],
89 [:preload_objects_for_dataclass, 'input not an array'],
90 [:preload_objects_for_dataclass, nil],
91 [:object_for_dataclass, 'some_dataclass', nil],
92 [:object_for_dataclass, nil, 'some_uuid'],
94 test "preload data for wrong type input #{input}" do
97 ac = ApplicationController.new
99 if input[0] == :object_for_dataclass
100 assert_raise ArgumentError do
101 ac.send input[0], input[1], input[2]
104 assert_raise ArgumentError do
105 ac.send input[0], input[1]
111 [ [:links_for_object, 'no-such-uuid' ],
112 [:collections_for_object, 'no-such-uuid' ],
113 [:log_collections_for_object, 'no-such-uuid' ],
114 [:object_for_dataclass, 'no-such-uuid' ],
116 test "get data for no such uuid #{input}" do
119 ac = ApplicationController.new
121 if input[0] == :object_for_dataclass
122 object = ac.send input[0], @user_dataclass, input[1]
123 assert_not object, 'Expected no object'
125 objects = ac.send input[0], input[1]
126 assert objects, 'Expected objects'
127 assert objects.is_a?(Array), 'Expected a array'
132 test "get 10 objects of data class user" do
135 ac = ApplicationController.new
137 objects = ac.send :get_n_objects_of_class, @user_dataclass, 10
139 assert objects, 'Expected objects'
140 assert objects.is_a?(ArvadosResourceList), 'Expected an ArvadosResourceList'
142 first_object = objects.first
143 assert first_object, 'Expected at least one object'
144 assert_equal 'User', first_object.class.name, 'Expected user object'
146 # invoke it again. this time, the preloaded info will be returned
147 objects = ac.send :get_n_objects_of_class, @user_dataclass, 10
148 assert objects, 'Expected objects'
149 assert_equal 'User', objects.first.class.name, 'Expected user object'
154 [@user_dataclass, 0],
155 [@user_dataclass, -1],
156 [@user_dataclass, nil] ].each do |input|
157 test "get_n_objects for incorrect input #{input}" do
160 ac = ApplicationController.new
162 assert_raise ArgumentError do
163 ac.send :get_n_objects_of_class, input[0], input[1]
168 test "collections for object" do
171 ac = ApplicationController.new
173 uuid = api_fixture('collections')['foo_file']['uuid']
175 collections = ac.send :collections_for_object, uuid
177 assert collections, 'Expected collections'
178 assert collections.is_a?(Array), 'Expected an array'
179 assert collections.size == 1, 'Expected one collection object'
180 assert_equal collections[0][:uuid], uuid, 'Expected uuid not found in collections'
183 test "preload collections for given uuids" do
186 ac = ApplicationController.new
188 uuid1 = api_fixture('collections')['foo_file']['uuid']
189 uuid2 = api_fixture('collections')['bar_file']['uuid']
191 uuids = [uuid1, uuid2]
192 collections = ac.send :preload_collections_for_objects, uuids
194 assert collections, 'Expected collection'
195 assert collections.is_a?(Hash), 'Expected a hash'
196 assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
197 assert collections[uuid1], 'Expected collections for the passed in uuid'
198 assert_equal collections[uuid1].size, 1, 'Expected one collection for the passed in uuid'
199 assert collections[uuid2], 'Expected collections for the passed in uuid'
200 assert_equal collections[uuid2].size, 1, 'Expected one collection for the passed in uuid'
202 # invoke again for this same input. this time, the preloaded data will be returned
203 collections = ac.send :preload_collections_for_objects, uuids
204 assert collections, 'Expected collection'
205 assert collections.is_a?(Hash), 'Expected a hash'
206 assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
207 assert collections[uuid1], 'Expected collections for the passed in uuid'
210 test "log collections for object" do
213 ac = ApplicationController.new
215 uuid = api_fixture('logs')['log4']['object_uuid']
217 collections = ac.send :log_collections_for_object, uuid
219 assert collections, 'Expected collections'
220 assert collections.is_a?(Array), 'Expected an array'
221 assert collections.size == 1, 'Expected one collection object'
222 assert_equal collections[0][:uuid], uuid, 'Expected uuid not found in collections'
225 test "preload log collections for given uuids" do
228 ac = ApplicationController.new
230 uuid1 = api_fixture('logs')['log4']['object_uuid']
231 uuid2 = api_fixture('collections')['bar_file']['uuid']
233 uuids = [uuid1, uuid2]
234 collections = ac.send :preload_log_collections_for_objects, uuids
236 assert collections, 'Expected collection'
237 assert collections.is_a?(Hash), 'Expected a hash'
238 assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
239 assert collections[uuid1], 'Expected collections for the passed in uuid'
240 assert_equal collections[uuid1].size, 1, 'Expected one collection for the passed in uuid'
241 assert collections[uuid2], 'Expected collections for the passed in uuid'
242 assert_equal collections[uuid2].size, 1, 'Expected one collection for the passed in uuid'
244 # invoke again for this same input. this time, the preloaded data will be returned
245 collections = ac.send :preload_log_collections_for_objects, uuids
246 assert collections, 'Expected collection'
247 assert collections.is_a?(Hash), 'Expected a hash'
248 assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
249 assert collections[uuid1], 'Expected collections for the passed in uuid'
252 test "object for dataclass" do
255 ac = ApplicationController.new
257 dataclass = ArvadosBase.resource_class_for_uuid(api_fixture('jobs')['running']['uuid'])
258 uuid = api_fixture('jobs')['running']['uuid']
260 obj = ac.send :object_for_dataclass, dataclass, uuid
262 assert obj, 'Expected object'
263 assert 'Job', obj.class
264 assert_equal uuid, obj['uuid'], 'Expected uuid not found'
265 assert_equal api_fixture('jobs')['running']['script_version'], obj['script_version'],
266 'Expected script_version not found'
269 test "preload objects for dataclass" do
272 ac = ApplicationController.new
274 dataclass = ArvadosBase.resource_class_for_uuid(api_fixture('jobs')['running']['uuid'])
276 uuid1 = api_fixture('jobs')['running']['uuid']
277 uuid2 = api_fixture('jobs')['running_cancelled']['uuid']
279 uuids = [uuid1, uuid2]
280 users = ac.send :preload_objects_for_dataclass, dataclass, uuids
282 assert users, 'Expected objects'
283 assert users.is_a?(Hash), 'Expected a hash'
285 assert users.size == 2, 'Expected two objects in the preloaded hash'
286 assert users[uuid1], 'Expected user object for the passed in uuid'
287 assert users[uuid2], 'Expected user object for the passed in uuid'
289 # invoke again for this same input. this time, the preloaded data will be returned
290 users = ac.send :preload_objects_for_dataclass, dataclass, uuids
291 assert users, 'Expected objects'
292 assert users.is_a?(Hash), 'Expected a hash'
293 assert users.size == 2, 'Expected two objects in the preloaded hash'
295 # invoke again for this with one more uuid
296 uuids << api_fixture('jobs')['foobar']['uuid']
297 users = ac.send :preload_objects_for_dataclass, dataclass, uuids
298 assert users, 'Expected objects'
299 assert users.is_a?(Hash), 'Expected a hash'
300 assert users.size == 3, 'Expected two objects in the preloaded hash'
303 test "requesting a nonexistent object returns 404" do
304 # We're really testing ApplicationController's find_object_by_uuid.
305 # It's easiest to do that by instantiating a concrete controller.
306 @controller = NodesController.new
307 get(:show, {id: "zzzzz-zzzzz-zzzzzzzzzzzzzzz"}, session_for(:admin))
311 test "Workbench returns 4xx when API server is unreachable" do
312 # We're really testing ApplicationController's render_exception.
313 # Our primary concern is that it doesn't raise an error and
315 orig_api_server = Rails.configuration.arvados_v1_base
317 # The URL should look valid in all respects, and avoid talking over a
318 # network. 100::/64 is the IPv6 discard prefix, so it's perfect.
319 Rails.configuration.arvados_v1_base = "https://[100::f]:1/"
320 @controller = NodesController.new
321 get(:index, {}, session_for(:active))
322 assert_includes(405..422, @response.code.to_i,
323 "bad response code when API server is unreachable")
325 Rails.configuration.arvados_v1_base = orig_api_server