Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / test / controllers / application_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 ApplicationControllerTest < ActionController::TestCase
8   # These tests don't do state-changing API calls. Save some time by
9   # skipping the database reset.
10   reset_api_fixtures :after_each_test, false
11   reset_api_fixtures :after_suite, true
12
13   setup do
14     @user_dataclass = ArvadosBase.resource_class_for_uuid(api_fixture('users')['active']['uuid'])
15   end
16
17   test "links for object" do
18     use_token :active
19
20     ac = ApplicationController.new
21
22     link_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
23
24     links = ac.send :links_for_object, link_head_uuid
25
26     assert links, 'Expected links'
27     assert links.is_a?(Array), 'Expected an array'
28     assert links.size > 0, 'Expected at least one link'
29     assert links[0][:uuid], 'Expected uuid for the head_link'
30   end
31
32   test "preload links for objects and uuids" do
33     use_token :active
34
35     ac = ApplicationController.new
36
37     link1_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
38     link2_uuid = api_fixture('links')['bar_file_readable_by_active']['uuid']
39     link3_head_uuid = api_fixture('links')['bar_file_readable_by_active']['head_uuid']
40
41     link2_object = User.find(api_fixture('users')['active']['uuid'])
42     link2_object_uuid = link2_object['uuid']
43
44     uuids = [link1_head_uuid, link2_object, link3_head_uuid]
45     links = ac.send :preload_links_for_objects, uuids
46
47     assert links, 'Expected links'
48     assert links.is_a?(Hash), 'Expected a hash'
49     assert links.size == 3, 'Expected two objects in the preloaded links hash'
50     assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
51     assert links[link2_object_uuid], 'Expected links for the passed in object uuid'
52     assert links[link3_head_uuid], 'Expected links for the passed in link head_uuid'
53
54     # invoke again for this same input. this time, the preloaded data will be returned
55     links = ac.send :preload_links_for_objects, uuids
56     assert links, 'Expected links'
57     assert links.is_a?(Hash), 'Expected a hash'
58     assert links.size == 3, 'Expected two objects in the preloaded links hash'
59     assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
60   end
61
62   [ [:preload_links_for_objects, [] ],
63     [:preload_collections_for_objects, [] ],
64     [:preload_log_collections_for_objects, [] ],
65     [:preload_objects_for_dataclass, [] ],
66     [:preload_for_pdhs, [] ],
67   ].each do |input|
68     test "preload data for empty array input #{input}" do
69       use_token :active
70
71       ac = ApplicationController.new
72
73       if input[0] == :preload_objects_for_dataclass
74         objects = ac.send input[0], @user_dataclass, input[1]
75       else
76         objects = ac.send input[0], input[1]
77       end
78
79       assert objects, 'Expected objects'
80       assert objects.is_a?(Hash), 'Expected a hash'
81       assert objects.size == 0, 'Expected no objects in the preloaded hash'
82     end
83   end
84
85   [ [:preload_links_for_objects, 'input not an array'],
86     [:preload_links_for_objects, nil],
87     [:links_for_object, nil],
88     [:preload_collections_for_objects, 'input not an array'],
89     [:preload_collections_for_objects, nil],
90     [:collections_for_object, nil],
91     [:preload_log_collections_for_objects, 'input not an array'],
92     [:preload_log_collections_for_objects, nil],
93     [:log_collections_for_object, nil],
94     [:preload_objects_for_dataclass, 'input not an array'],
95     [:preload_objects_for_dataclass, nil],
96     [:object_for_dataclass, 'some_dataclass', nil],
97     [:object_for_dataclass, nil, 'some_uuid'],
98     [:preload_for_pdhs, 'input not an array'],
99     [:preload_for_pdhs, nil],
100   ].each do |input|
101     test "preload data for wrong type input #{input}" do
102       use_token :active
103
104       ac = ApplicationController.new
105
106       if input[0] == :object_for_dataclass
107         assert_raise ArgumentError do
108           ac.send input[0], input[1], input[2]
109         end
110       else
111         assert_raise ArgumentError do
112           ac.send input[0], input[1]
113         end
114       end
115     end
116   end
117
118   [ [:links_for_object, 'no-such-uuid' ],
119     [:collections_for_object, 'no-such-uuid' ],
120     [:log_collections_for_object, 'no-such-uuid' ],
121     [:object_for_dataclass, 'no-such-uuid' ],
122     [:collection_for_pdh, 'no-such-pdh' ],
123   ].each do |input|
124     test "get data for no such uuid #{input}" do
125       use_token :active
126
127       ac = ApplicationController.new
128
129       if input[0] == :object_for_dataclass
130         object = ac.send input[0], @user_dataclass, input[1]
131         assert_not object, 'Expected no object'
132       else
133         objects = ac.send input[0], input[1]
134         assert objects, 'Expected objects'
135         assert objects.is_a?(Array), 'Expected a array'
136         assert_empty objects
137       end
138     end
139   end
140
141   test "get 10 objects of data class user" do
142     use_token :active
143
144     ac = ApplicationController.new
145
146     objects = ac.send :get_n_objects_of_class, @user_dataclass, 10
147
148     assert objects, 'Expected objects'
149     assert objects.is_a?(ArvadosResourceList), 'Expected an ArvadosResourceList'
150
151     first_object = objects.first
152     assert first_object, 'Expected at least one object'
153     assert_equal 'User', first_object.class.name, 'Expected user object'
154
155     # invoke it again. this time, the preloaded info will be returned
156     objects = ac.send :get_n_objects_of_class, @user_dataclass, 10
157     assert objects, 'Expected objects'
158     assert_equal 'User', objects.first.class.name, 'Expected user object'
159   end
160
161   [ ['User', 10],
162     [nil, 10],
163     [@user_dataclass, 0],
164     [@user_dataclass, -1],
165     [@user_dataclass, nil] ].each do |input|
166     test "get_n_objects for incorrect input #{input}" do
167       use_token :active
168
169       ac = ApplicationController.new
170
171       assert_raise ArgumentError do
172         ac.send :get_n_objects_of_class, input[0], input[1]
173       end
174     end
175   end
176
177   test "collections for object" do
178     use_token :active
179
180     ac = ApplicationController.new
181
182     uuid = api_fixture('collections')['foo_file']['uuid']
183
184     collections = ac.send :collections_for_object, uuid
185
186     assert collections, 'Expected collections'
187     assert collections.is_a?(Array), 'Expected an array'
188     assert collections.size == 1, 'Expected one collection object'
189     assert_equal collections[0][:uuid], uuid, 'Expected uuid not found in collections'
190   end
191
192   test "preload collections for given uuids" do
193     use_token :active
194
195     ac = ApplicationController.new
196
197     uuid1 = api_fixture('collections')['foo_file']['uuid']
198     uuid2 = api_fixture('collections')['bar_file']['uuid']
199
200     uuids = [uuid1, uuid2]
201     collections = ac.send :preload_collections_for_objects, uuids
202
203     assert collections, 'Expected collection'
204     assert collections.is_a?(Hash), 'Expected a hash'
205     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
206     assert collections[uuid1], 'Expected collections for the passed in uuid'
207     assert_equal collections[uuid1].size, 1, 'Expected one collection for the passed in uuid'
208     assert collections[uuid2], 'Expected collections for the passed in uuid'
209     assert_equal collections[uuid2].size, 1, 'Expected one collection for the passed in uuid'
210
211     # invoke again for this same input. this time, the preloaded data will be returned
212     collections = ac.send :preload_collections_for_objects, uuids
213     assert collections, 'Expected collection'
214     assert collections.is_a?(Hash), 'Expected a hash'
215     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
216     assert collections[uuid1], 'Expected collections for the passed in uuid'
217   end
218
219   test "log collections for object" do
220     use_token :active
221
222     ac = ApplicationController.new
223
224     uuid = api_fixture('logs')['system_adds_foo_file']['object_uuid']
225
226     collections = ac.send :log_collections_for_object, uuid
227
228     assert collections, 'Expected collections'
229     assert collections.is_a?(Array), 'Expected an array'
230     assert collections.size == 1, 'Expected one collection object'
231     assert_equal collections[0][:uuid], uuid, 'Expected uuid not found in collections'
232   end
233
234   test "preload log collections for given uuids" do
235     use_token :active
236
237     ac = ApplicationController.new
238
239     uuid1 = api_fixture('logs')['system_adds_foo_file']['object_uuid']
240     uuid2 = api_fixture('collections')['bar_file']['uuid']
241
242     uuids = [uuid1, uuid2]
243     collections = ac.send :preload_log_collections_for_objects, uuids
244
245     assert collections, 'Expected collection'
246     assert collections.is_a?(Hash), 'Expected a hash'
247     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
248     assert collections[uuid1], 'Expected collections for the passed in uuid'
249     assert_equal collections[uuid1].size, 1, 'Expected one collection for the passed in uuid'
250     assert collections[uuid2], 'Expected collections for the passed in uuid'
251     assert_equal collections[uuid2].size, 1, 'Expected one collection for the passed in uuid'
252
253     # invoke again for this same input. this time, the preloaded data will be returned
254     collections = ac.send :preload_log_collections_for_objects, uuids
255     assert collections, 'Expected collection'
256     assert collections.is_a?(Hash), 'Expected a hash'
257     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
258     assert collections[uuid1], 'Expected collections for the passed in uuid'
259   end
260
261   test "object for dataclass" do
262     use_token :active
263
264     ac = ApplicationController.new
265
266     dataclass = ArvadosBase.resource_class_for_uuid(api_fixture('jobs')['running']['uuid'])
267     uuid = api_fixture('jobs')['running']['uuid']
268
269     obj = ac.send :object_for_dataclass, dataclass, uuid
270
271     assert obj, 'Expected object'
272     assert 'Job', obj.class
273     assert_equal uuid, obj['uuid'], 'Expected uuid not found'
274     assert_equal api_fixture('jobs')['running']['script_version'], obj['script_version'],
275       'Expected script_version not found'
276   end
277
278   test "preload objects for dataclass" do
279     use_token :active
280
281     ac = ApplicationController.new
282
283     dataclass = ArvadosBase.resource_class_for_uuid(api_fixture('jobs')['running']['uuid'])
284
285     uuid1 = api_fixture('jobs')['running']['uuid']
286     uuid2 = api_fixture('jobs')['running_cancelled']['uuid']
287
288     uuids = [uuid1, uuid2]
289     users = ac.send :preload_objects_for_dataclass, dataclass, uuids
290
291     assert users, 'Expected objects'
292     assert users.is_a?(Hash), 'Expected a hash'
293
294     assert users.size == 2, 'Expected two objects in the preloaded hash'
295     assert users[uuid1], 'Expected user object for the passed in uuid'
296     assert users[uuid2], 'Expected user object for the passed in uuid'
297
298     # invoke again for this same input. this time, the preloaded data will be returned
299     users = ac.send :preload_objects_for_dataclass, dataclass, uuids
300     assert users, 'Expected objects'
301     assert users.is_a?(Hash), 'Expected a hash'
302     assert users.size == 2, 'Expected two objects in the preloaded hash'
303
304     # invoke again for this with one more uuid
305     uuids << api_fixture('jobs')['foobar']['uuid']
306     users = ac.send :preload_objects_for_dataclass, dataclass, uuids
307     assert users, 'Expected objects'
308     assert users.is_a?(Hash), 'Expected a hash'
309     assert users.size == 3, 'Expected two objects in the preloaded hash'
310   end
311
312   test "preload one collection each for given portable_data_hash list" do
313     use_token :active
314
315     ac = ApplicationController.new
316
317     pdh1 = api_fixture('collections')['foo_file']['portable_data_hash']
318     pdh2 = api_fixture('collections')['bar_file']['portable_data_hash']
319
320     pdhs = [pdh1, pdh2]
321     collections = ac.send :preload_for_pdhs, pdhs
322
323     assert collections, 'Expected collections map'
324     assert collections.is_a?(Hash), 'Expected a hash'
325     # Each pdh has more than one collection; however, we should get only one for each
326     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
327     assert collections[pdh1], 'Expected collections for the passed in pdh #{pdh1}'
328     assert_equal collections[pdh1].size, 1, 'Expected one collection for the passed in pdh #{pdh1}'
329     assert collections[pdh2], 'Expected collections for the passed in pdh #{pdh2}'
330     assert_equal collections[pdh2].size, 1, 'Expected one collection for the passed in pdh #{pdh2}'
331   end
332
333   test "requesting a nonexistent object returns 404" do
334     # We're really testing ApplicationController's find_object_by_uuid.
335     # It's easiest to do that by instantiating a concrete controller.
336     @controller = NodesController.new
337     get(:show, {id: "zzzzz-zzzzz-zzzzzzzzzzzzzzz"}, session_for(:admin))
338     assert_response 404
339   end
340
341   test "requesting to the API server includes client_session_id param" do
342     got_query = nil
343     stub_api_calls
344     stub_api_client.stubs(:post).with do |url, query, opts={}|
345       got_query = query
346       true
347     end.returns fake_api_response('{}', 200, {})
348
349     Rails.configuration.anonymous_user_token =
350       api_fixture("api_client_authorizations", "anonymous", "api_token")
351     @controller = ProjectsController.new
352     test_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz"
353     get(:show, {id: test_uuid})
354
355     assert_includes got_query, 'current_request_id'
356     assert_match /\d{10}-\d{9}/, got_query['current_request_id']
357   end
358
359   test "current_request_id is nil after a request" do
360     @controller = NodesController.new
361     get(:index, {}, session_for(:active))
362     assert_nil Thread.current[:current_request_id]
363   end
364
365   [".navbar .login-menu a",
366    ".navbar .login-menu .dropdown-menu a"
367   ].each do |css_selector|
368     test "login link at #{css_selector.inspect} includes return_to param" do
369       # Without an anonymous token, we're immediately redirected to login.
370       Rails.configuration.anonymous_user_token =
371         api_fixture("api_client_authorizations", "anonymous", "api_token")
372       @controller = ProjectsController.new
373       test_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz"
374       get(:show, {id: test_uuid})
375       login_link = css_select(css_selector).first
376       assert_not_nil(login_link, "failed to select login link")
377       login_href = URI.unescape(login_link.attributes["href"])
378       # The parameter needs to include the full URL to work.
379       assert_includes(login_href, "://")
380       assert_match(/[\?&]return_to=[^&]*\/projects\/#{test_uuid}(&|$)/,
381                    login_href)
382     end
383   end
384
385   test "Workbench returns 4xx when API server is unreachable" do
386     # We're really testing ApplicationController's render_exception.
387     # Our primary concern is that it doesn't raise an error and
388     # return 500.
389     orig_api_server = Rails.configuration.arvados_v1_base
390     begin
391       # The URL should look valid in all respects, and avoid talking over a
392       # network.  100::/64 is the IPv6 discard prefix, so it's perfect.
393       Rails.configuration.arvados_v1_base = "https://[100::f]:1/"
394       @controller = NodesController.new
395       get(:index, {}, session_for(:active))
396       assert_includes(405..422, @response.code.to_i,
397                       "bad response code when API server is unreachable")
398     ensure
399       Rails.configuration.arvados_v1_base = orig_api_server
400     end
401   end
402
403   [
404     [CollectionsController.new, api_fixture('collections')['user_agreement_in_anonymously_accessible_project']],
405     [CollectionsController.new, api_fixture('collections')['user_agreement_in_anonymously_accessible_project'], false],
406     [JobsController.new, api_fixture('jobs')['running_job_in_publicly_accessible_project']],
407     [JobsController.new, api_fixture('jobs')['running_job_in_publicly_accessible_project'], false],
408     [PipelineInstancesController.new, api_fixture('pipeline_instances')['pipeline_in_publicly_accessible_project']],
409     [PipelineInstancesController.new, api_fixture('pipeline_instances')['pipeline_in_publicly_accessible_project'], false],
410     [PipelineTemplatesController.new, api_fixture('pipeline_templates')['pipeline_template_in_publicly_accessible_project']],
411     [PipelineTemplatesController.new, api_fixture('pipeline_templates')['pipeline_template_in_publicly_accessible_project'], false],
412     [ProjectsController.new, api_fixture('groups')['anonymously_accessible_project']],
413     [ProjectsController.new, api_fixture('groups')['anonymously_accessible_project'], false],
414   ].each do |controller, fixture, anon_config=true|
415     test "#{controller} show method with anonymous config #{anon_config ? '' : 'not '}enabled" do
416       if anon_config
417         Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token']
418       else
419         Rails.configuration.anonymous_user_token = false
420       end
421
422       @controller = controller
423
424       get(:show, {id: fixture['uuid']})
425
426       if anon_config
427         assert_response 200
428         if controller.class == JobsController
429           assert_includes @response.inspect, fixture['script']
430         else
431           assert_includes @response.inspect, fixture['name']
432         end
433       else
434         assert_response :redirect
435         assert_match /\/users\/welcome/, @response.redirect_url
436       end
437     end
438   end
439
440   [
441     true,
442     false,
443   ].each do |config|
444     test "invoke show with include_accept_encoding_header config #{config}" do
445       Rails.configuration.include_accept_encoding_header_in_api_requests = config
446
447       @controller = CollectionsController.new
448       get(:show, {id: api_fixture('collections')['foo_file']['uuid']}, session_for(:admin))
449
450       assert_equal([['.', 'foo', 3]], assigns(:object).files)
451     end
452   end
453
454   test 'Edit name and verify that a duplicate is not created' do
455     @controller = ProjectsController.new
456     project = api_fixture("groups")["aproject"]
457     post :update, {
458       id: project["uuid"],
459       project: {
460         name: 'test name'
461       },
462       format: :json
463     }, session_for(:active)
464     assert_includes @response.body, 'test name'
465     updated = assigns(:object)
466     assert_equal updated.uuid, project["uuid"]
467     assert_equal 'test name', updated.name
468   end
469
470   [
471     [VirtualMachinesController.new, 'hostname', false],
472     [UsersController.new, 'first_name', true],
473   ].each do |controller, expect_str, expect_home_link|
474     test "access #{controller.controller_name} index as admin and verify Home link is#{' not' if !expect_home_link} shown" do
475       @controller = controller
476
477       get :index, {}, session_for(:admin)
478
479       assert_response 200
480       assert_includes @response.body, expect_str
481
482       home_link = "/projects/#{api_fixture('users')['active']['uuid']}"
483
484       if expect_home_link
485         refute_empty css_select("[href=\"/projects/#{api_fixture('users')['active']['uuid']}\"]")
486       else
487         assert_empty css_select("[href=\"/projects/#{api_fixture('users')['active']['uuid']}\"]")
488       end
489     end
490   end
491
492   [
493     [VirtualMachinesController.new, 'hostname', true],
494     [UsersController.new, 'first_name', false],
495   ].each do |controller, expect_str, expect_delete_link|
496     test "access #{controller.controller_name} index as admin and verify Delete option is#{' not' if !expect_delete_link} shown" do
497       @controller = controller
498
499       get :index, {}, session_for(:admin)
500
501       assert_response 200
502       assert_includes @response.body, expect_str
503       if expect_delete_link
504         refute_empty css_select('[data-method=delete]')
505       else
506         assert_empty css_select('[data-method=delete]')
507       end
508     end
509   end
510 end