foreman
foreman copied to clipboard
Fixes #36919 - Add URL validation to HTTP Proxy URL field.
Before PR:
After PR:
To Test:
- Apply PR
- Create an HTTP Proxy with a nonfqdn value
- Hit Test Connection
Could we have a clear error message, something like: Please enter a valid proxy URL.
@lfu PR updated, here is the new screenshot
To a bit more guiding: I suspect that you can change this bit: https://github.com/theforeman/foreman/blob/74933559fafc3f52494845bf82a5a67d83b2d584/app/controllers/http_proxies_controller.rb#L26-L33
To
def test_connection
http_proxy = HttpProxy.new(http_proxy_params)
if http_proxy.invalid?
# TODO: convert http_proxy.errors to some exception somehow
end
http_proxy.test_connection(params[:test_url])
render :json => {:status => 'success', :message => _("HTTP Proxy connection successful.")}, :status => :ok
rescue => e
render :json => {:status => 'failure', :message => e.message}, :status => :unprocessable_entity
end
See https://guides.rubyonrails.org/active_record_validations.html#valid-questionmark-and-invalid-questionmark & https://guides.rubyonrails.org/active_record_validations.html#validations-overview-errors as well
The main benefit is that you reuse the already existing validations. It may error out if the name is not specified though, which not be what you want.
To a bit more guiding: I suspect that you can change this bit:
https://github.com/theforeman/foreman/blob/74933559fafc3f52494845bf82a5a67d83b2d584/app/controllers/http_proxies_controller.rb#L26-L33
To
def test_connection http_proxy = HttpProxy.new(http_proxy_params) if http_proxy.invalid? # TODO: convert http_proxy.errors to some exception somehow end http_proxy.test_connection(params[:test_url]) render :json => {:status => 'success', :message => _("HTTP Proxy connection successful.")}, :status => :ok rescue => e render :json => {:status => 'failure', :message => e.message}, :status => :unprocessable_entity endSee https://guides.rubyonrails.org/active_record_validations.html#valid-questionmark-and-invalid-questionmark & https://guides.rubyonrails.org/active_record_validations.html#validations-overview-errors as well
The main benefit is that you reuse the already existing validations. It may error out if the name is not specified though, which not be what you want.
Ok will go with this approach
@lfu updated, @ekohl how does this look?
@ekohl when you get a chance can this one get a review, I have added tests.
@ekohl can this one get back on your radar to review? It should be a quick one and I did tests as you asked.
@ekohl I'd like to merge this, but your previous change request is blocking it.