summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_commands_signin_command.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-08 01:32:18 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-08 01:32:18 +0000
commitc00e84327f14845bd484e76b5ee5dfeb1fa9ce3d (patch)
tree9f558dafa363f4f0118d504a50cd4461e2821cd1 /test/rubygems/test_gem_commands_signin_command.rb
parent6b05153a3a75b74b64553d6a46f501d9ee0f0376 (diff)
Merge rubygems master.
This is RC version of Rubygems 2.7.0. https://github.com/rubygems/rubygems/commit/688fb7e83c13c3fe7c2bb03c49a2db4c82852aee git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/rubygems/test_gem_commands_signin_command.rb')
-rw-r--r--test/rubygems/test_gem_commands_signin_command.rb95
1 files changed, 95 insertions, 0 deletions
diff --git a/test/rubygems/test_gem_commands_signin_command.rb b/test/rubygems/test_gem_commands_signin_command.rb
new file mode 100644
index 0000000000..ec40b71ada
--- /dev/null
+++ b/test/rubygems/test_gem_commands_signin_command.rb
@@ -0,0 +1,95 @@
+# frozen_string_literal: true
+require 'rubygems/test_case'
+require 'rubygems/commands/signin_command'
+require 'rubygems/installer'
+
+class TestGemCommandsSigninCommand < Gem::TestCase
+
+ def setup
+ super
+
+ Gem.configuration.rubygems_api_key = nil
+ Gem.configuration.api_keys.clear
+
+ @cmd = Gem::Commands::SigninCommand.new
+ end
+
+ def teardown
+ credentials_path = Gem.configuration.credentials_path
+ File.delete(credentials_path) if File.exist?(credentials_path)
+ super
+ end
+
+ def test_execute_when_not_already_signed_in
+ sign_in_ui = util_capture() { @cmd.execute }
+ assert_match %r{Signed in.}, sign_in_ui.output
+ end
+
+ def test_execute_when_already_signed_in_with_same_host
+ host = 'http://some-gemcutter-compatible-host.org'
+ sign_in_ui = util_capture(nil, host) { @cmd.execute }
+ old_credentials = YAML.load_file Gem.configuration.credentials_path
+
+ sign_in_ui = util_capture(nil, host) { @cmd.execute }
+ new_credentials = YAML.load_file Gem.configuration.credentials_path
+
+ assert_equal old_credentials[host], new_credentials[host]
+ end
+
+ def test_execute_when_already_signed_in_with_different_host
+ api_key = 'a5fdbb6ba150cbb83aad2bb2fede64cf04045xxxx'
+ sign_in_ui = util_capture(nil, nil, api_key) { @cmd.execute }
+ host = 'http://some-gemcutter-compatible-host.org'
+ sign_in_ui = util_capture(nil, host, api_key) { @cmd.execute }
+ credentials = YAML.load_file Gem.configuration.credentials_path
+
+ assert_equal credentials[:rubygems_api_key], api_key
+
+ assert_equal credentials[host], nil
+ end
+
+ def test_execute_with_host_supplied
+ host = 'http://some-gemcutter-compatible-host.org'
+
+ sign_in_ui = util_capture(nil, host) { @cmd.execute }
+ assert_match %r{Enter your #{host} credentials.}, sign_in_ui.output
+ assert_match %r{Signed in.}, sign_in_ui.output
+
+ api_key = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
+ credentials = YAML.load_file Gem.configuration.credentials_path
+ assert_equal api_key, credentials[host]
+ end
+
+ def test_execute_with_valid_creds_set_for_default_host
+ util_capture {@cmd.execute}
+
+ api_key = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
+ credentials = YAML.load_file Gem.configuration.credentials_path
+
+ assert_equal api_key, credentials[:rubygems_api_key]
+ end
+
+ # Utility method to capture IO/UI within the block passed
+
+ def util_capture ui_stub = nil, host = nil, api_key = nil
+ api_key ||= 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
+ response = [api_key, 200, 'OK']
+ email = 'you@example.com'
+ password = 'secret'
+ fetcher = Gem::FakeFetcher.new
+
+ # Set the expected response for the Web-API supplied
+ ENV['RUBYGEMS_HOST'] = host || Gem::DEFAULT_HOST
+ data_key = "#{ENV['RUBYGEMS_HOST']}/api/v1/api_key"
+ fetcher.data[data_key] = response
+ Gem::RemoteFetcher.fetcher = fetcher
+
+ sign_in_ui = ui_stub || Gem::MockGemUi.new("#{email}\n#{password}\n")
+
+ use_ui sign_in_ui do
+ yield
+ end
+
+ sign_in_ui
+ end
+end