[前][次][番号順一覧][スレッド一覧]

ruby-changes:73791

From: Jenny <ko1@a...>
Date: Thu, 29 Sep 2022 17:56:52 +0900 (JST)
Subject: [ruby-changes:73791] 1cbf0fd863 (master): [rubygems/rubygems] Add error message when api response is a permanent redirect

https://git.ruby-lang.org/ruby.git/commit/?id=1cbf0fd863

From 1cbf0fd86356ccbac5556ab0f63ea8a4b08fd24d Mon Sep 17 00:00:00 2001
From: Jenny Shen <jenny.shen@s...>
Date: Fri, 16 Sep 2022 08:48:38 -0400
Subject: [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@s...>
---
 lib/rubygems/gemcutter_utilities.rb               | 10 ++++-
 test/rubygems/test_gem_commands_owner_command.rb  | 47 +++++++++++++++++++++++
 test/rubygems/test_gem_commands_push_command.rb   | 18 +++++++++
 test/rubygems/test_gem_commands_signin_command.rb | 21 ++++++++++
 4 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb
index a785159196..1112498357 100644
--- a/lib/rubygems/gemcutter_utilities.rb
+++ b/lib/rubygems/gemcutter_utilities.rb
@@ -201,7 +201,8 @@ module Gem::GemcutterUtilities https://github.com/ruby/ruby/blob/trunk/lib/rubygems/gemcutter_utilities.rb#L201
   # 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 https://github.com/ruby/ruby/blob/trunk/lib/rubygems/gemcutter_utilities.rb#L212
       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
diff --git a/test/rubygems/test_gem_commands_owner_command.rb b/test/rubygems/test_gem_commands_owner_command.rb
index ca77041000..3d0a265eb1 100644
--- a/test/rubygems/test_gem_commands_owner_command.rb
+++ b/test/rubygems/test_gem_commands_owner_command.rb
@@ -118,6 +118,23 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_owner_command.rb#L118
     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@e...\n"
     @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"]
@@ -158,6 +175,21 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_owner_command.rb#L175
     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@e..."])
+      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 https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_owner_command.rb#L248
     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@e..."])
+      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"]
diff --git a/test/rubygems/test_gem_commands_push_command.rb b/test/rubygems/test_gem_commands_push_command.rb
index f38a2ae7a1..c58d3f3692 100644
--- a/test/rubygems/test_gem_commands_push_command.rb
+++ b/test/rubygems/test_gem_commands_push_command.rb
@@ -325,6 +325,24 @@ class TestGemCommandsPushCommand < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_push_command.rb#L325
     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
diff --git a/test/rubygems/test_gem_commands_signin_command.rb b/test/rubygems/test_gem_commands_signin_command.rb
index ce745bff20..940e68b3da 100644
--- a/test/rubygems/test_gem_commands_signin_command.rb
+++ b/test/rubygems/test_gem_commands_signin_command.rb
@@ -71,6 +71,27 @@ class TestGemCommandsSigninCommand < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_signin_command.rb#L71
     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@e...\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 }
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]