diff --git a/app/models/registration_system.rb b/app/models/registration_system.rb index 5f7b713360ca..e23d727cd7a8 100644 --- a/app/models/registration_system.rb +++ b/app/models/registration_system.rb @@ -83,8 +83,9 @@ def self.update_rhsm_conf(options = {}) return unless option_values[:proxy_address] - write_rhsm_config(:proxy_hostname => option_values[:proxy_address].split(':')[0], - :proxy_port => option_values[:proxy_address].split(':')[1], + proxy_uri = URI.parse(option_values[:proxy_address].include?("://") ? option_values[:proxy_address] : "http://#{option_values[:proxy_address]}") + write_rhsm_config(:proxy_hostname => proxy_uri.host, + :proxy_port => proxy_uri.port, :proxy_user => option_values[:proxy_username], :proxy_password => option_values[:proxy_password]) end diff --git a/spec/models/registration_system_spec.rb b/spec/models/registration_system_spec.rb index 2d65a851fc5d..3426b00b495d 100644 --- a/spec/models/registration_system_spec.rb +++ b/spec/models/registration_system_spec.rb @@ -181,10 +181,10 @@ ssl_verify_depth = 3 # an http proxy server to use - proxy_hostname = 192.0.2.0 + proxy_hostname = ProxyHostnameValue # port for http proxy server - proxy_port = myport + proxy_port = 0 # user name for authenticating to an http proxy, if needed proxy_user = my_dummy_username @@ -195,14 +195,9 @@ EOT end - let(:rhsm_conf) { Tempfile.new(@spec_name.downcase) } + let(:rhsm_conf) { Tempfile.new } before do - @proxy_args = {:registration_http_proxy_server => "192.0.2.0:myport", - :registration_http_proxy_username => "my_dummy_username", - :registration_http_proxy_password => "my_dummy_password"} - - @spec_name = File.basename(__FILE__).split(".rb").first.freeze stub_const("RegistrationSystem::RHSM_CONFIG_FILE", rhsm_conf.path) rhsm_conf.write(original_rhsm_conf) rhsm_conf.close @@ -213,10 +208,22 @@ FileUtils.rm_f("#{rhsm_conf.path}.miq_orig") end - it "will save then update the original config file" do - RegistrationSystem.update_rhsm_conf(@proxy_args) - expect(File.read("#{rhsm_conf.path}.miq_orig")).to eq(original_rhsm_conf) - expect(File.read(rhsm_conf)).to eq(updated_rhsm_conf) + context "will save then update the original config file" do + ["", "http://", "https://"].each do |prefix| + ["proxy.example.com", "192.0.2.0", "[2001:db8::]"].each do |address| + params = { + :registration_http_proxy_server => "#{prefix}#{address}:0", + :registration_http_proxy_username => "my_dummy_username", + :registration_http_proxy_password => "my_dummy_password" + } + + it "with #{params[:registration_http_proxy_server]}" do + RegistrationSystem.update_rhsm_conf(params) + expect(File.read("#{rhsm_conf.path}.miq_orig")).to eq(original_rhsm_conf) + expect(File.read(rhsm_conf)).to eq(updated_rhsm_conf.sub(/ProxyHostnameValue/, address)) + end + end + end end it "with no proxy server will not update the rhsm config file" do @@ -227,10 +234,10 @@ it "with no options will use database valuses" do MiqDatabase.seed MiqDatabase.first.update_attributes( - :registration_http_proxy_server => "192.0.2.0:myport" + :registration_http_proxy_server => "192.0.2.0:0" ) RegistrationSystem.update_rhsm_conf - expect(File.read(rhsm_conf)).to include("proxy_hostname = 192.0.2.0", "proxy_port = myport") + expect(File.read(rhsm_conf)).to include("proxy_hostname = 192.0.2.0", "proxy_port = 0") end end end