Skip to content

Commit

Permalink
Fixes #36919 - Add URL validation to HTTP Proxy URL field.
Browse files Browse the repository at this point in the history
  • Loading branch information
chris1984 authored and ekohl committed Jun 20, 2024
1 parent ccb4aba commit 8a5b270
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/controllers/http_proxies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/controllers/http_proxies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8a5b270

Please sign in to comment.