[rubygems/rubygems] Add error message when api response is a permanent redirect

https://github.com/rubygems/rubygems/commit/ccca30c77a

Co-authored-by: Nick Schwaderer <nick.schwaderer@shopify.com>
This commit is contained in:
Jenny Shen 2022-09-16 08:48:38 -04:00 committed by git
parent 9948b8bfec
commit 1cbf0fd863
4 changed files with 95 additions and 1 deletions

View File

@ -201,7 +201,8 @@ module Gem::GemcutterUtilities
# block was given or shows the response body to the user.
#
# If the response was not successful, shows an error to the user including
# the +error_prefix+ and the response body.
# the +error_prefix+ and the response body. If the response was a permanent redirect,
# shows an error to the user including the redirect location.
def with_response(response, error_prefix = nil)
case response
@ -211,6 +212,13 @@ module Gem::GemcutterUtilities
else
say clean_text(response.body)
end
when Net::HTTPPermanentRedirect, Net::HTTPRedirection then
message = "The request has redirected permanently to #{Gem::Uri.parse(response['location']).origin}. " \
"Please check your defined push host."
message = "#{error_prefix}: #{message}" if error_prefix
say clean_text(message)
terminate_interaction(ERROR_CODE)
else
message = response.body
message = "#{error_prefix}: #{message}" if error_prefix

View File

@ -118,6 +118,23 @@ EOF
assert_match response, @stub_ui.output
end
def test_show_owners_permanent_redirect
host = "http://rubygems.example"
ENV["RUBYGEMS_HOST"] = host
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = ["", 301, "Moved Permanently"]
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do
@cmd.show_owners("freewill")
end
end
end
response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
assert_match response, @stub_ui.output
end
def test_show_owners_key
response = "- email: user1@example.com\n"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"]
@ -158,6 +175,21 @@ EOF
assert_match response, @stub_ui.output
end
def test_add_owners_permanent_redirect
host = "http://rubygems.example"
ENV["RUBYGEMS_HOST"] = host
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners"] = ["", 308, "Permanent Redirect"]
use_ui @stub_ui do
Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
end
response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
assert_match response, @stub_ui.output
end
def test_add_owner_with_host_option_through_execute
host = "http://rubygems.example"
add_owner_response = "Owner added successfully."
@ -216,6 +248,21 @@ EOF
assert_match response, @stub_ui.output
end
def test_remove_owners_permanent_redirect
host = "http://rubygems.example"
ENV["RUBYGEMS_HOST"] = host
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners"] = ["", 308, "Permanent Redirect"]
use_ui @stub_ui do
Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do
@cmd.remove_owners("freewill", ["user-remove1@example.com"])
end
end
response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
assert_match response, @stub_ui.output
end
def test_remove_owners_key
response = "Owner removed successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, "OK"]

View File

@ -325,6 +325,24 @@ class TestGemCommandsPushCommand < Gem::TestCase
assert_match @response, @ui.output
end
def test_sending_gem_to_permanent_redirect_host
@host = "http://rubygems.example"
@fetcher.data["#{@host}/api/v1/gems"] = ["", 308, "Permanent Redirect"]
assert_raise Gem::MockGemUi::TermError do
use_ui @ui do
@cmd.instance_variable_set :@host, @host
Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do
@cmd.send_gem(@path)
end
end
end
response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
assert_match response, @ui.output
end
def test_raises_error_with_no_arguments
def @cmd.sign_in(*); end
assert_raise Gem::CommandLineError do

View File

@ -71,6 +71,27 @@ class TestGemCommandsSigninCommand < Gem::TestCase
assert_equal api_key, credentials[host]
end
def test_execute_with_host_permanent_redirect
host = "http://rubygems.example/"
ENV["RUBYGEMS_HOST"] = host
data_key = "#{host}/api/v1/api_key"
fetcher = Gem::FakeFetcher.new
fetcher.data[data_key] = ["", 308, "Moved Permanently"]
Gem::RemoteFetcher.fetcher = fetcher
ui = Gem::MockGemUi.new("you@example.com\nsecret\n\n\n\n\n\n\n\n\n")
assert_raise Gem::MockGemUi::TermError do
use_ui ui do
Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do
@cmd.execute
end
end
end
response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
assert_match response, ui.output
end
def test_execute_with_valid_creds_set_for_default_host
util_capture { @cmd.execute }