diff --git a/spec/rmt/wizard_scc_page_spec.rb b/spec/rmt/wizard_scc_page_spec.rb index d9a06b4..d5289fd 100644 --- a/spec/rmt/wizard_scc_page_spec.rb +++ b/spec/rmt/wizard_scc_page_spec.rb @@ -127,26 +127,73 @@ expect(Yast::UI).to receive(:CloseDialog) expect_any_instance_of(Net::HTTP::Get).to receive(:basic_auth).with(config['scc']['username'], config['scc']['password']) - - expect(Net::HTTP).to receive(:start).and_return(response_double) - expect(response_double).to receive(:code).and_return(response_code) end - let(:response_double) { instance_double(Net::HTTPResponse) } + context 'when the request completes without errors' do + before do + expect(Net::HTTP).to receive(:start).and_return(response_double) + expect(response_double).to receive(:code).and_return(response_code) + end + + let(:response_double) { instance_double(Net::HTTPResponse) } - context 'when HTTP response code is 200' do - let(:response_code) { '200' } + context 'when HTTP response code is 200' do + let(:response_code) { '200' } - it 'returns true' do - expect(scc_page.scc_credentials_valid?).to be(true) + it 'returns true' do + expect(scc_page.scc_credentials_valid?).to be(true) + end + end + + context 'when HTTP response code is not 200' do + let(:response_code) { '401' } + + it 'returns false' do + expect(scc_page.scc_credentials_valid?).to be(false) + end end end - context 'when HTTP response code is not 200' do - let(:response_code) { '401' } + context 'when SCC times out and the user chooses not to try again' do + before do + expect(Yast::Popup).to receive(:ErrorAnyQuestion).and_return(false) + expect(Net::HTTP).to receive(:start).and_raise(Net::ReadTimeout) + end + + context 'and the user chooses to not try again' do + it 'returns false' do + expect(scc_page.scc_credentials_valid?).to be(false) + end + end + end + + context 'when SCC times out and the user chooses to try again' do + before do + expect(Yast::Popup).to receive(:ErrorAnyQuestion).and_return(true) + expect(Net::HTTP).to receive(:start).and_raise(Net::ReadTimeout) + expect(Net::HTTP).to receive(:start).and_return(response_double) + end + + let(:response_double) { instance_double(Net::HTTPResponse) } + + context 'when SCC responds quickly and the HTTP response code is 200' do + before do + expect(response_double).to receive(:code).and_return(200) + end + + it 'returns true' do + expect(scc_page.scc_credentials_valid?).to be(true) + end + end + + context 'when SCC responds quickly and the HTTP response code is not 200' do + before do + expect(response_double).to receive(:code).and_return(401) + end - it 'returns false' do - expect(scc_page.scc_credentials_valid?).to be(false) + it 'returns false' do + expect(scc_page.scc_credentials_valid?).to be(false) + end end end end diff --git a/src/lib/rmt/wizard_scc_page.rb b/src/lib/rmt/wizard_scc_page.rb index d9d29e5..510946b 100644 --- a/src/lib/rmt/wizard_scc_page.rb +++ b/src/lib/rmt/wizard_scc_page.rb @@ -26,6 +26,8 @@ module RMT; end class RMT::WizardSCCPage < Yast::Client include ::UI::EventDispatcher + YAST_RMT_USER_AGENT = 'yast2-rmt'.freeze + def initialize(config) textdomain 'rmt' @config = config @@ -126,14 +128,27 @@ def scc_credentials_valid? ) ) - uri = URI('https://scc.suse.com/connect/organizations/systems') + uri = URI('https://scc.suse.com/connect/organizations/orders') req = Net::HTTP::Get.new(uri) req.basic_auth(@config['scc']['username'], @config['scc']['password']) - - res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) } + req['User-Agent'] = YAST_RMT_USER_AGENT + + valid_credentials = nil + while valid_credentials.nil? + begin + res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) } + valid_credentials = (res.code.to_i == 200) + rescue Net::ReadTimeout + break valid_credentials = false unless Popup.ErrorAnyQuestion( + _('Request Timeout'), + _("The request to SCC timed out.\n\nWould you like to try again?"), + _('Retry'), _('Cancel'), :focus_yes + ) + end + end UI.CloseDialog - res.code.to_i == 200 + valid_credentials end end