X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/33e6d3356af3f7eaa484dbaa7a671aa25f5042e4..0eb72b526bf8bbb011551ecf019f604e17a534f1:/apps/workbench/test/controllers/application_controller_test.rb diff --git a/apps/workbench/test/controllers/application_controller_test.rb b/apps/workbench/test/controllers/application_controller_test.rb index 30da596266..d38e88f2a6 100644 --- a/apps/workbench/test/controllers/application_controller_test.rb +++ b/apps/workbench/test/controllers/application_controller_test.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + require 'test_helper' class ApplicationControllerTest < ActionController::TestCase @@ -59,6 +63,7 @@ class ApplicationControllerTest < ActionController::TestCase [:preload_collections_for_objects, [] ], [:preload_log_collections_for_objects, [] ], [:preload_objects_for_dataclass, [] ], + [:preload_for_pdhs, [] ], ].each do |input| test "preload data for empty array input #{input}" do use_token :active @@ -90,6 +95,8 @@ class ApplicationControllerTest < ActionController::TestCase [:preload_objects_for_dataclass, nil], [:object_for_dataclass, 'some_dataclass', nil], [:object_for_dataclass, nil, 'some_uuid'], + [:preload_for_pdhs, 'input not an array'], + [:preload_for_pdhs, nil], ].each do |input| test "preload data for wrong type input #{input}" do use_token :active @@ -112,6 +119,7 @@ class ApplicationControllerTest < ActionController::TestCase [:collections_for_object, 'no-such-uuid' ], [:log_collections_for_object, 'no-such-uuid' ], [:object_for_dataclass, 'no-such-uuid' ], + [:collection_for_pdh, 'no-such-pdh' ], ].each do |input| test "get data for no such uuid #{input}" do use_token :active @@ -125,6 +133,7 @@ class ApplicationControllerTest < ActionController::TestCase objects = ac.send input[0], input[1] assert objects, 'Expected objects' assert objects.is_a?(Array), 'Expected a array' + assert_empty objects end end end @@ -212,7 +221,7 @@ class ApplicationControllerTest < ActionController::TestCase ac = ApplicationController.new - uuid = api_fixture('logs')['log4']['object_uuid'] + uuid = api_fixture('logs')['system_adds_foo_file']['object_uuid'] collections = ac.send :log_collections_for_object, uuid @@ -227,7 +236,7 @@ class ApplicationControllerTest < ActionController::TestCase ac = ApplicationController.new - uuid1 = api_fixture('logs')['log4']['object_uuid'] + uuid1 = api_fixture('logs')['system_adds_foo_file']['object_uuid'] uuid2 = api_fixture('collections')['bar_file']['uuid'] uuids = [uuid1, uuid2] @@ -300,6 +309,27 @@ class ApplicationControllerTest < ActionController::TestCase assert users.size == 3, 'Expected two objects in the preloaded hash' end + test "preload one collection each for given portable_data_hash list" do + use_token :active + + ac = ApplicationController.new + + pdh1 = api_fixture('collections')['foo_file']['portable_data_hash'] + pdh2 = api_fixture('collections')['bar_file']['portable_data_hash'] + + pdhs = [pdh1, pdh2] + collections = ac.send :preload_for_pdhs, pdhs + + assert collections, 'Expected collections map' + assert collections.is_a?(Hash), 'Expected a hash' + # Each pdh has more than one collection; however, we should get only one for each + assert collections.size == 2, 'Expected two objects in the preloaded collection hash' + assert collections[pdh1], 'Expected collections for the passed in pdh #{pdh1}' + assert_equal collections[pdh1].size, 1, 'Expected one collection for the passed in pdh #{pdh1}' + assert collections[pdh2], 'Expected collections for the passed in pdh #{pdh2}' + assert_equal collections[pdh2].size, 1, 'Expected one collection for the passed in pdh #{pdh2}' + end + test "requesting a nonexistent object returns 404" do # We're really testing ApplicationController's find_object_by_uuid. # It's easiest to do that by instantiating a concrete controller. @@ -308,6 +338,50 @@ class ApplicationControllerTest < ActionController::TestCase assert_response 404 end + test "requesting to the API server includes client_session_id param" do + got_query = nil + stub_api_calls + stub_api_client.stubs(:post).with do |url, query, opts={}| + got_query = query + true + end.returns fake_api_response('{}', 200, {}) + + Rails.configuration.anonymous_user_token = + api_fixture("api_client_authorizations", "anonymous", "api_token") + @controller = ProjectsController.new + test_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz" + get(:show, {id: test_uuid}) + + assert_includes got_query, 'current_request_id' + assert_match /\d{10}-\d{9}/, got_query['current_request_id'] + end + + test "current_request_id is nil after a request" do + @controller = NodesController.new + get(:index, {}, session_for(:active)) + assert_nil Thread.current[:current_request_id] + end + + [".navbar .login-menu a", + ".navbar .login-menu .dropdown-menu a" + ].each do |css_selector| + test "login link at #{css_selector.inspect} includes return_to param" do + # Without an anonymous token, we're immediately redirected to login. + Rails.configuration.anonymous_user_token = + api_fixture("api_client_authorizations", "anonymous", "api_token") + @controller = ProjectsController.new + test_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz" + get(:show, {id: test_uuid}) + login_link = css_select(css_selector).first + assert_not_nil(login_link, "failed to select login link") + login_href = URI.unescape(login_link.attributes["href"]) + # The parameter needs to include the full URL to work. + assert_includes(login_href, "://") + assert_match(/[\?&]return_to=[^&]*\/projects\/#{test_uuid}(&|$)/, + login_href) + end + end + test "Workbench returns 4xx when API server is unreachable" do # We're really testing ApplicationController's render_exception. # Our primary concern is that it doesn't raise an error and @@ -327,36 +401,110 @@ class ApplicationControllerTest < ActionController::TestCase end [ - ['collections', false, api_fixture('collections')['user_agreement_in_anonymously_accessible_project']], - ['jobs', false, api_fixture('jobs')['running_job_in_publicly_accessible_project']], - ['pipeline_instances', false, api_fixture('pipeline_instances')['pipeline_in_publicly_accessible_project']], - ['pipeline_templates', false, api_fixture('pipeline_templates')['pipeline_template_in_publicly_accessible_project']], - ['projects', false, api_fixture('groups')['anonymously_accessible_project']], - ].each do |controller, use_config, fixture| - test "#{controller} show method with anonymous config enabled #{use_config}" do - if use_config + [CollectionsController.new, api_fixture('collections')['user_agreement_in_anonymously_accessible_project']], + [CollectionsController.new, api_fixture('collections')['user_agreement_in_anonymously_accessible_project'], false], + [JobsController.new, api_fixture('jobs')['running_job_in_publicly_accessible_project']], + [JobsController.new, api_fixture('jobs')['running_job_in_publicly_accessible_project'], false], + [PipelineInstancesController.new, api_fixture('pipeline_instances')['pipeline_in_publicly_accessible_project']], + [PipelineInstancesController.new, api_fixture('pipeline_instances')['pipeline_in_publicly_accessible_project'], false], + [PipelineTemplatesController.new, api_fixture('pipeline_templates')['pipeline_template_in_publicly_accessible_project']], + [PipelineTemplatesController.new, api_fixture('pipeline_templates')['pipeline_template_in_publicly_accessible_project'], false], + [ProjectsController.new, api_fixture('groups')['anonymously_accessible_project']], + [ProjectsController.new, api_fixture('groups')['anonymously_accessible_project'], false], + ].each do |controller, fixture, anon_config=true| + test "#{controller} show method with anonymous config #{anon_config ? '' : 'not '}enabled" do + if anon_config Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token'] else Rails.configuration.anonymous_user_token = false end - case controller - when 'collections' - @controller = CollectionsController.new - when 'jobs' - @controller = JobsController.new - when 'pipeline_instances' - @controller = PipelineInstancesController.new - when 'pipeline_templates' - @controller = PipelineTemplatesController.new - when 'projects' - @controller = ProjectsController.new - end + @controller = controller get(:show, {id: fixture['uuid']}) - assert_response :redirect - assert_match /welcome/, @response.redirect_url + if anon_config + assert_response 200 + if controller.class == JobsController + assert_includes @response.inspect, fixture['script'] + else + assert_includes @response.inspect, fixture['name'] + end + else + assert_response :redirect + assert_match /\/users\/welcome/, @response.redirect_url + end + end + end + + [ + true, + false, + ].each do |config| + test "invoke show with include_accept_encoding_header config #{config}" do + Rails.configuration.include_accept_encoding_header_in_api_requests = config + + @controller = CollectionsController.new + get(:show, {id: api_fixture('collections')['foo_file']['uuid']}, session_for(:admin)) + + assert_equal([['.', 'foo', 3]], assigns(:object).files) + end + end + + test 'Edit name and verify that a duplicate is not created' do + @controller = ProjectsController.new + project = api_fixture("groups")["aproject"] + post :update, { + id: project["uuid"], + project: { + name: 'test name' + }, + format: :json + }, session_for(:active) + assert_includes @response.body, 'test name' + updated = assigns(:object) + assert_equal updated.uuid, project["uuid"] + assert_equal 'test name', updated.name + end + + [ + [VirtualMachinesController.new, 'hostname', false], + [UsersController.new, 'first_name', true], + ].each do |controller, expect_str, expect_home_link| + test "access #{controller.controller_name} index as admin and verify Home link is#{' not' if !expect_home_link} shown" do + @controller = controller + + get :index, {}, session_for(:admin) + + assert_response 200 + assert_includes @response.body, expect_str + + home_link = "/projects/#{api_fixture('users')['active']['uuid']}" + + if expect_home_link + refute_empty css_select("[href=\"/projects/#{api_fixture('users')['active']['uuid']}\"]") + else + assert_empty css_select("[href=\"/projects/#{api_fixture('users')['active']['uuid']}\"]") + end + end + end + + [ + [VirtualMachinesController.new, 'hostname', true], + [UsersController.new, 'first_name', false], + ].each do |controller, expect_str, expect_delete_link| + test "access #{controller.controller_name} index as admin and verify Delete option is#{' not' if !expect_delete_link} shown" do + @controller = controller + + get :index, {}, session_for(:admin) + + assert_response 200 + assert_includes @response.body, expect_str + if expect_delete_link + refute_empty css_select('[data-method=delete]') + else + assert_empty css_select('[data-method=delete]') + end end end end