[rubygems/rubygems] Remove fetcher login in util_sign_in

https://github.com/rubygems/rubygems/commit/8e6bc4485a
This commit is contained in:
Jenny Shen 2023-06-21 13:39:09 -04:00 committed by git
parent 812dbe79f0
commit 836e4eb3cd

View File

@ -95,22 +95,19 @@ class TestGemGemcutterUtilities < Gem::TestCase
end end
def test_sign_in def test_sign_in
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" util_sign_in
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output) assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output)
assert @fetcher.last_request["authorization"] assert @fetcher.last_request["authorization"]
assert_match(/Signed in./, @sign_in_ui.output) assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key] assert_equal @fetcher.api_key, credentials[:rubygems_api_key]
end end
def test_sign_in_with_host def test_sign_in_with_host
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
@fetcher = SignInFetcher.new(host: "http://example.com") @fetcher = SignInFetcher.new(host: "http://example.com")
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), "http://example.com", ["http://example.com"] util_sign_in
assert_match "Enter your http://example.com credentials.", assert_match "Enter your http://example.com credentials.",
@sign_in_ui.output @sign_in_ui.output
@ -118,14 +115,12 @@ class TestGemGemcutterUtilities < Gem::TestCase
assert_match(/Signed in./, @sign_in_ui.output) assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials["http://example.com"] assert_equal @fetcher.api_key, credentials["http://example.com"]
end end
def test_sign_in_with_host_nil def test_sign_in_with_host_nil
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
@fetcher = SignInFetcher.new(host: nil) @fetcher = SignInFetcher.new(host: nil)
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), nil, [nil] util_sign_in(args: [nil])
assert_match "Enter your RubyGems.org credentials.", assert_match "Enter your RubyGems.org credentials.",
@sign_in_ui.output @sign_in_ui.output
@ -133,13 +128,12 @@ class TestGemGemcutterUtilities < Gem::TestCase
assert_match(/Signed in./, @sign_in_ui.output) assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key] assert_equal @fetcher.api_key, credentials[:rubygems_api_key]
end end
def test_sign_in_with_host_ENV def test_sign_in_with_host_ENV
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
@fetcher = SignInFetcher.new(host: "http://example.com") @fetcher = SignInFetcher.new(host: "http://example.com")
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), "http://example.com" util_sign_in
assert_match "Enter your http://example.com credentials.", assert_match "Enter your http://example.com credentials.",
@sign_in_ui.output @sign_in_ui.output
@ -147,29 +141,26 @@ class TestGemGemcutterUtilities < Gem::TestCase
assert_match(/Signed in./, @sign_in_ui.output) assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials["http://example.com"] assert_equal @fetcher.api_key, credentials["http://example.com"]
end end
def test_sign_in_skips_with_existing_credentials def test_sign_in_skips_with_existing_credentials
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" Gem.configuration.rubygems_api_key = @fetcher.api_key
Gem.configuration.rubygems_api_key = api_key
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK") util_sign_in
assert_equal "", @sign_in_ui.output assert_equal "", @sign_in_ui.output
end end
def test_sign_in_skips_with_key_override def test_sign_in_skips_with_key_override
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
Gem.configuration.api_keys[:KEY] = "other" Gem.configuration.api_keys[:KEY] = "other"
@cmd.options[:key] = :KEY @cmd.options[:key] = :KEY
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK") util_sign_in
assert_equal "", @sign_in_ui.output assert_equal "", @sign_in_ui.output
end end
def test_sign_in_with_other_credentials_doesnt_overwrite_other_keys def test_sign_in_with_other_credentials_doesnt_overwrite_other_keys
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
other_api_key = "f46dbb18bb6a9c97cdc61b5b85c186a17403cdcbf" other_api_key = "f46dbb18bb6a9c97cdc61b5b85c186a17403cdcbf"
config = Hash[:other_api_key, other_api_key] config = Hash[:other_api_key, other_api_key]
@ -177,20 +168,20 @@ class TestGemGemcutterUtilities < Gem::TestCase
File.open Gem.configuration.credentials_path, "w" do |f| File.open Gem.configuration.credentials_path, "w" do |f|
f.write Gem::ConfigFile.dump_with_rubygems_yaml(config) f.write Gem::ConfigFile.dump_with_rubygems_yaml(config)
end end
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK") util_sign_in
assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output) assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output)
assert_match(/Signed in./, @sign_in_ui.output) assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key] assert_equal @fetcher.api_key, credentials[:rubygems_api_key]
assert_equal other_api_key, credentials[:other_api_key] assert_equal other_api_key, credentials[:other_api_key]
end end
def test_sign_in_with_bad_credentials def test_sign_in_with_bad_credentials
@fetcher.respond_with_forbidden_api_key_response @fetcher.respond_with_forbidden_api_key_response
assert_raise Gem::MockGemUi::TermError do assert_raise Gem::MockGemUi::TermError do
util_sign_in HTTPResponseFactory.create(body: "Access Denied.", code: 403, msg: "Forbidden") util_sign_in
end end
assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output) assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output)
@ -199,27 +190,16 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_signin_with_env_otp_code def test_signin_with_env_otp_code
ENV["GEM_HOST_OTP_CODE"] = "111111" ENV["GEM_HOST_OTP_CODE"] = "111111"
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK") util_sign_in
assert_match "Signed in with API key:", @sign_in_ui.output assert_match "Signed in with API key:", @sign_in_ui.output
assert_equal "111111", @fetcher.last_request["OTP"] assert_equal "111111", @fetcher.last_request["OTP"]
end end
def test_sign_in_with_correct_otp_code def test_sign_in_with_correct_otp_code
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
response_fail = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
@fetcher.respond_with_require_otp @fetcher.respond_with_require_otp
util_sign_in(proc do util_sign_in(extra_input: "111111\n")
@call_count ||= 0
if (@call_count += 1).odd?
HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized")
else
HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
end
end, nil, [], "111111\n")
assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output
assert_match "Code: ", @sign_in_ui.output assert_match "Code: ", @sign_in_ui.output
@ -232,7 +212,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
@fetcher.respond_with_unauthorized_api_key_response @fetcher.respond_with_unauthorized_api_key_response
assert_raise Gem::MockGemUi::TermError do assert_raise Gem::MockGemUi::TermError do
util_sign_in HTTPResponseFactory.create(body: response, code: 401, msg: "Unauthorized"), nil, [], "111111\n" util_sign_in(extra_input: "111111\n")
end end
assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output
@ -243,8 +223,6 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_sign_in_with_webauthn_enabled def test_sign_in_with_webauthn_enabled
webauthn_verification_url = "rubygems.org/api/v1/webauthn_verification/odow34b93t6aPCdY" webauthn_verification_url = "rubygems.org/api/v1/webauthn_verification/odow34b93t6aPCdY"
response_fail = "You have enabled multifactor authentication"
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
port = 5678 port = 5678
server = TCPServer.new(port) server = TCPServer.new(port)
@ -252,14 +230,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
@fetcher.respond_with_webauthn_url(webauthn_verification_url) @fetcher.respond_with_webauthn_url(webauthn_verification_url)
TCPServer.stub(:new, server) do TCPServer.stub(:new, server) do
Gem::WebauthnListener.stub(:wait_for_otp_code, "Uvh6T57tkWuUnWYo") do Gem::WebauthnListener.stub(:wait_for_otp_code, "Uvh6T57tkWuUnWYo") do
util_sign_in(proc do util_sign_in
@call_count ||= 0
if (@call_count += 1).odd?
HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized")
else
HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
end
end, nil, [], "", webauthn_verification_url)
end end
ensure ensure
server.close server.close
@ -273,8 +244,6 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_sign_in_with_webauthn_enabled_with_error def test_sign_in_with_webauthn_enabled_with_error
webauthn_verification_url = "rubygems.org/api/v1/webauthn_verification/odow34b93t6aPCdY" webauthn_verification_url = "rubygems.org/api/v1/webauthn_verification/odow34b93t6aPCdY"
response_fail = "You have enabled multifactor authentication"
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
port = 5678 port = 5678
server = TCPServer.new(port) server = TCPServer.new(port)
raise_error = ->(*_args) { raise Gem::WebauthnVerificationError, "Something went wrong" } raise_error = ->(*_args) { raise Gem::WebauthnVerificationError, "Something went wrong" }
@ -284,14 +253,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
error = assert_raise Gem::MockGemUi::TermError do error = assert_raise Gem::MockGemUi::TermError do
TCPServer.stub(:new, server) do TCPServer.stub(:new, server) do
Gem::WebauthnListener.stub(:wait_for_otp_code, raise_error) do Gem::WebauthnListener.stub(:wait_for_otp_code, raise_error) do
util_sign_in(proc do util_sign_in
@call_count ||= 0
if (@call_count += 1).odd?
HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized")
else
HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
end
end, nil, [], "", webauthn_verification_url)
end end
ensure ensure
server.close server.close
@ -306,27 +268,10 @@ class TestGemGemcutterUtilities < Gem::TestCase
refute_match "Signed in with API key:", @sign_in_ui.output refute_match "Signed in with API key:", @sign_in_ui.output
end end
def util_sign_in(response, host = nil, args = [], extra_input = "", webauthn_url = nil) def util_sign_in(args: [], extra_input: "")
email = "you@example.com" email = "you@example.com"
password = "secret" password = "secret"
# profile_response = HTTPResponseFactory.create(body: "mfa: disabled\n", code: 200, msg: "OK")
# webauthn_response =
# if webauthn_url
# HTTPResponseFactory.create(body: webauthn_url, code: 200, msg: "OK")
# else
# HTTPResponseFactory.create(body: "You don't have any security devices", code: 422, msg: "Unprocessable Entity")
# end
# if host
# ENV["RUBYGEMS_HOST"] = host
# else
# host = Gem.host
# end
# @fetcher = Gem::FakeFetcher.new
# @fetcher.data["#{host}/api/v1/api_key"] = response
# @fetcher.data["#{host}/api/v1/profile/me.yaml"] = profile_response
# @fetcher.data["#{host}/api/v1/webauthn_verification"] = webauthn_response
ENV["RUBYGEMS_HOST"] = @fetcher.host ENV["RUBYGEMS_HOST"] = @fetcher.host
Gem::RemoteFetcher.fetcher = @fetcher Gem::RemoteFetcher.fetcher = @fetcher
@ -359,7 +304,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
end end
class SignInFetcher < Gem::FakeFetcher class SignInFetcher < Gem::FakeFetcher
attr_reader :host attr_reader :host, :api_key
def initialize(host: nil) def initialize(host: nil)
super() super()