1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
require 'rubygems/test_case'
require 'rubygems'
require 'rubygems/command'
require 'rubygems/gemcutter_utilities'
class TestGemGemcutterUtilities < Gem::TestCase
def setup
super
ENV['RUBYGEMS_HOST'] = nil
Gem.configuration.rubygems_api_key = nil
@cmd = Gem::Command.new '', 'summary'
@cmd.extend Gem::GemcutterUtilities
end
def test_api_key
keys = { :rubygems_api_key => 'KEY' }
FileUtils.mkdir_p File.dirname Gem.configuration.credentials_path
open Gem.configuration.credentials_path, 'w' do |f|
f.write keys.to_yaml
end
Gem.configuration.load_api_keys
assert_equal 'KEY', @cmd.api_key
end
def test_api_key_override
keys = { :rubygems_api_key => 'KEY', :other => 'OTHER' }
FileUtils.mkdir_p File.dirname Gem.configuration.credentials_path
open Gem.configuration.credentials_path, 'w' do |f|
f.write keys.to_yaml
end
Gem.configuration.load_api_keys
@cmd.add_key_option
@cmd.handle_options %w[--key other]
assert_equal 'OTHER', @cmd.api_key
end
def test_sign_in
api_key = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
util_sign_in [api_key, 200, 'OK']
assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
assert @fetcher.last_request["authorization"]
assert_match %r{Signed in.}, @sign_in_ui.output
credentials = YAML.load_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_sign_in_with_host
api_key = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
util_sign_in [api_key, 200, 'OK'], 'http://example.com'
assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
assert @fetcher.last_request["authorization"]
assert_match %r{Signed in.}, @sign_in_ui.output
credentials = YAML.load_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_sign_in_skips_with_existing_credentials
api_key = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
Gem.configuration.rubygems_api_key = api_key
util_sign_in [api_key, 200, 'OK']
assert_equal "", @sign_in_ui.output
end
def test_sign_in_with_other_credentials_doesnt_overwrite_other_keys
api_key = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
other_api_key = 'f46dbb18bb6a9c97cdc61b5b85c186a17403cdcbf'
FileUtils.mkdir_p File.dirname(Gem.configuration.credentials_path)
open Gem.configuration.credentials_path, 'w' do |f|
f.write Hash[:other_api_key, other_api_key].to_yaml
end
util_sign_in [api_key, 200, 'OK']
assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
assert_match %r{Signed in.}, @sign_in_ui.output
credentials = YAML.load_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
assert_equal other_api_key, credentials[:other_api_key]
end
def test_sign_in_with_bad_credentials
skip 'Always uses $stdin on windows' if Gem.win_platform?
assert_raises Gem::MockGemUi::TermError do
util_sign_in ['Access Denied.', 403, 'Forbidden']
end
assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
assert_match %r{Access Denied.}, @sign_in_ui.output
end
def util_sign_in response, host = nil
skip 'Always uses $stdin on windows' if Gem.win_platform?
email = 'you@example.com'
password = 'secret'
if host
ENV['RUBYGEMS_HOST'] = host
else
host = Gem.host
end
@fetcher = Gem::FakeFetcher.new
@fetcher.data["#{host}/api/v1/api_key"] = response
Gem::RemoteFetcher.fetcher = @fetcher
@sign_in_ui = Gem::MockGemUi.new "#{email}\n#{password}\n"
use_ui @sign_in_ui do
@cmd.sign_in
end
end
def test_verify_api_key
keys = {:other => 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'}
FileUtils.mkdir_p File.dirname(Gem.configuration.credentials_path)
File.open Gem.configuration.credentials_path, 'w' do |f|
f.write keys.to_yaml
end
Gem.configuration.load_api_keys
assert_equal 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903',
@cmd.verify_api_key(:other)
end
def test_verify_missing_api_key
assert_raises Gem::MockGemUi::TermError do
@cmd.verify_api_key :missing
end
end
end
|