Skip to content

Commit

Permalink
Added unit test for the retry_block option given
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Mar 10, 2019
1 parent 2e49122 commit 2f91553
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lib/registration/connect_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def self.catch_registration_errors(message_prefix: "",
# FIXME: to not break existing translation, this typo should be fixed
# later after SP2: time -> timed
if retry_block
retry if report_error_and_retry?(message_prefix + _("Connection time out.") + "\n",
retry if report_error_and_retry?(message_prefix + _("Connection time out."),
_("Make sure that the registration server is reachable and\n" \
"the connection is reliable."))
else
Expand Down
77 changes: 66 additions & 11 deletions test/connect_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,44 @@ def api_error(code: 400, headers: {}, body: {})
expect(ret).to eq(false)
end

it "reports an error with details on timeout" do
details = _("Make sure that the registration server is reachable and\n" \
"the connection is reliable.")

expect(subject).to receive(:wrap_text).with("Details: #{details}", screen_width - 4)
.and_return("Details wrapped text")
expect(Yast::Report).to receive(:Error).with(
"Registration: " + _("Connection time out.") + "\n\n\nDetails wrapped text"
)
context "on timeout" do
let(:details) do
_("Make sure that the registration server is reachable and\n" \
"the connection is reliable.")
end

context "and retry_block disabled" do
it "reports an error with details on timeout" do
expect(subject).to receive(:wrap_text).with("Details: #{details}", screen_width - 4)
.and_return("Details wrapped text")
expect(Yast::Report).to receive(:Error).with(
"Registration: " + _("Connection time out.") + "\n\n\nDetails wrapped text"
)

helpers.catch_registration_errors(message_prefix: "Registration: ") do
raise Timeout::Error
end
end
end

context "and with retry_block enabled" do
it "shows an error dialog with a retry and details button" do
expect(helpers).to receive(:report_error_and_retry?)
.with("Registration: " + _("Connection time out."), details)

helpers.catch_registration_errors(message_prefix: "Registration: ") do
raise Timeout::Error
helpers.catch_registration_errors(message_prefix: "Registration: ", retry_block: true) do
raise Timeout::Error
end
end

it "retries the given block if selected by the user" do
expect(helpers).to receive(:report_error_and_retry?).twice
.with("Registration: " + _("Connection time out."), details).and_return(true, false)

helpers.catch_registration_errors(message_prefix: "Registration: ", retry_block: true) do
raise Timeout::Error
end
end
end
end

Expand Down Expand Up @@ -111,6 +137,35 @@ def api_error(code: 400, headers: {}, body: {})

context "JSON parse error is received" do
include_examples "old registration server", JSON::ParserError.new("error message")

let(:details) { "server error response details" }
let(:exception) { JSON::ParserError.new(details) }

context "and with retry_block enabled" do
before do
allow(Registration::UrlHelpers).to receive(:registration_url)
.and_return(SUSE::Connect::YaST::DEFAULT_URL)
end

it "shows an error dialog with a retry and details button" do
expect(helpers).to receive(:report_error_and_retry?)
.with("Registration: " + _("Cannot parse the data from server."), details)

helpers.catch_registration_errors(message_prefix: "Registration: ", retry_block: true) do
raise exception
end
end

it "retries the given block if selected by the user" do
expect(helpers).to receive(:report_error_and_retry?).twice
.with("Registration: " + _("Cannot parse the data from server."), details)
.and_return(true, false)

helpers.catch_registration_errors(message_prefix: "Registration: ", retry_block: true) do
raise exception
end
end
end
end

[400, 401, 500, 42].each do |error_code|
Expand Down

0 comments on commit 2f91553

Please sign in to comment.