diff --git a/app/controllers/http_proxies_controller.rb b/app/controllers/http_proxies_controller.rb index fd38d90b0fa..4b85150bd1c 100644 --- a/app/controllers/http_proxies_controller.rb +++ b/app/controllers/http_proxies_controller.rb @@ -24,7 +24,11 @@ def edit end def test_connection - http_proxy = HttpProxy.new(http_proxy_params) + http_proxy = HttpProxy.new({name: 'dummy'}.update(http_proxy_params)) + unless http_proxy.valid? + raise Foreman::Exception, http_proxy.errors.full_messages.join(', ') + end + http_proxy.test_connection(params[:test_url]) render :json => {:status => 'success', :message => _("HTTP Proxy connection successful.")}, :status => :ok diff --git a/test/controllers/http_proxies_controller_test.rb b/test/controllers/http_proxies_controller_test.rb index 1f702c55bec..c9d33b4ab43 100644 --- a/test/controllers/http_proxies_controller_test.rb +++ b/test/controllers/http_proxies_controller_test.rb @@ -54,4 +54,22 @@ def test_update_location_and_organization assert_includes @model.reload.locations.pluck(:id), location_id assert_includes @model.reload.organizations.pluck(:id), organization_id end + + def test_test_connection_success + controller = HttpProxiesController.new + controller.stubs(:http_proxy_params).returns({ url: 'https://some_url'}) + controller.stubs(:params).returns({test_url: "https://some.where.com"}) + RestClient::Request.stubs(:execute).returns(['example', 1]) + controller.expects(:render).with(:json => {:status => "success", :message => "HTTP Proxy connection successful."}, :status => :ok) + controller.test_connection + end + + def test_test_connection_failure + controller = HttpProxiesController.new + controller.stubs(:http_proxy_params).returns({ url: 'https://some_url'}) + controller.stubs(:params).returns({test_url: "https://some.where.com"}) + RestClient::Request.stubs(:execute).raises(StandardError.new('some error')) + controller.expects(:render).with(:json => {:status => 'failure', :message => "some error"}, :status => :unprocessable_entity) + controller.test_connection + end end