1require 'rubygems/test_case'
2require 'rubygems'
3require 'rubygems/command'
4require 'rubygems/gemcutter_utilities'
5
6class TestGemGemcutterUtilities < Gem::TestCase
7
8  def setup
9    super
10
11    ENV['RUBYGEMS_HOST'] = nil
12    Gem.configuration.rubygems_api_key = nil
13
14    @cmd = Gem::Command.new '', 'summary'
15    @cmd.extend Gem::GemcutterUtilities
16  end
17
18  def teardown
19    ENV['RUBYGEMS_HOST'] = nil
20    Gem.configuration.rubygems_api_key = nil
21
22    super
23  end
24
25  def test_alternate_key_alternate_host
26    keys = {
27      :rubygems_api_key => 'KEY',
28      "http://rubygems.engineyard.com" => "EYKEY"
29    }
30
31    FileUtils.mkdir_p File.dirname Gem.configuration.credentials_path
32
33    open Gem.configuration.credentials_path, 'w' do |f|
34      f.write keys.to_yaml
35    end
36
37    ENV["RUBYGEMS_HOST"] = "http://rubygems.engineyard.com"
38
39    Gem.configuration.load_api_keys
40
41    assert_equal 'EYKEY', @cmd.api_key
42  end
43
44  def test_api_key
45    keys = { :rubygems_api_key => 'KEY' }
46    FileUtils.mkdir_p File.dirname Gem.configuration.credentials_path
47
48    open Gem.configuration.credentials_path, 'w' do |f|
49      f.write keys.to_yaml
50    end
51
52    Gem.configuration.load_api_keys
53
54    assert_equal 'KEY', @cmd.api_key
55  end
56
57  def test_api_key_override
58    keys = { :rubygems_api_key => 'KEY', :other => 'OTHER' }
59    FileUtils.mkdir_p File.dirname Gem.configuration.credentials_path
60
61    open Gem.configuration.credentials_path, 'w' do |f|
62      f.write keys.to_yaml
63    end
64
65    Gem.configuration.load_api_keys
66
67    @cmd.add_key_option
68    @cmd.handle_options %w[--key other]
69
70    assert_equal 'OTHER', @cmd.api_key
71  end
72
73  def test_host
74    assert_equal 'https://rubygems.org', @cmd.host
75  end
76
77  def test_host_RUBYGEMS_HOST
78    ENV['RUBYGEMS_HOST'] = 'https://other.example'
79
80    assert_equal 'https://other.example', @cmd.host
81  end
82
83  def test_host_RUBYGEMS_HOST_empty
84    ENV['RUBYGEMS_HOST'] = ''
85
86    assert_equal 'https://rubygems.org', @cmd.host
87  end
88
89  def test_sign_in
90    api_key     = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
91    util_sign_in [api_key, 200, 'OK']
92
93    assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
94    assert @fetcher.last_request["authorization"]
95    assert_match %r{Signed in.}, @sign_in_ui.output
96
97    credentials = YAML.load_file Gem.configuration.credentials_path
98    assert_equal api_key, credentials[:rubygems_api_key]
99  end
100
101  def test_sign_in_with_host
102    api_key     = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
103
104    util_sign_in [api_key, 200, 'OK'], 'http://example.com', ['http://example.com']
105
106    assert_match "Enter your http://example.com credentials.",
107                 @sign_in_ui.output
108    assert @fetcher.last_request["authorization"]
109    assert_match %r{Signed in.}, @sign_in_ui.output
110
111    credentials = YAML.load_file Gem.configuration.credentials_path
112    assert_equal api_key, credentials[:rubygems_api_key]
113  end
114
115  def test_sign_in_with_host_nil
116    api_key     = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
117
118    util_sign_in [api_key, 200, 'OK'], nil, [nil]
119
120    assert_match "Enter your RubyGems.org credentials.",
121                 @sign_in_ui.output
122    assert @fetcher.last_request["authorization"]
123    assert_match %r{Signed in.}, @sign_in_ui.output
124
125    credentials = YAML.load_file Gem.configuration.credentials_path
126    assert_equal api_key, credentials[:rubygems_api_key]
127  end
128
129  def test_sign_in_with_host_ENV
130    api_key     = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
131    util_sign_in [api_key, 200, 'OK'], 'http://example.com'
132
133    assert_match "Enter your http://example.com credentials.",
134                 @sign_in_ui.output
135    assert @fetcher.last_request["authorization"]
136    assert_match %r{Signed in.}, @sign_in_ui.output
137
138    credentials = YAML.load_file Gem.configuration.credentials_path
139    assert_equal api_key, credentials[:rubygems_api_key]
140  end
141
142  def test_sign_in_skips_with_existing_credentials
143    api_key     = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
144    Gem.configuration.rubygems_api_key = api_key
145
146    util_sign_in [api_key, 200, 'OK']
147
148    assert_equal "", @sign_in_ui.output
149  end
150
151  def test_sign_in_with_other_credentials_doesnt_overwrite_other_keys
152    api_key       = 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'
153    other_api_key = 'f46dbb18bb6a9c97cdc61b5b85c186a17403cdcbf'
154
155    FileUtils.mkdir_p File.dirname(Gem.configuration.credentials_path)
156    open Gem.configuration.credentials_path, 'w' do |f|
157      f.write Hash[:other_api_key, other_api_key].to_yaml
158    end
159    util_sign_in [api_key, 200, 'OK']
160
161    assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
162    assert_match %r{Signed in.}, @sign_in_ui.output
163
164    credentials   = YAML.load_file Gem.configuration.credentials_path
165    assert_equal api_key, credentials[:rubygems_api_key]
166    assert_equal other_api_key, credentials[:other_api_key]
167  end
168
169  def test_sign_in_with_bad_credentials
170    skip 'Always uses $stdin on windows' if Gem.win_platform?
171
172    assert_raises Gem::MockGemUi::TermError do
173      util_sign_in ['Access Denied.', 403, 'Forbidden']
174    end
175
176    assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
177    assert_match %r{Access Denied.}, @sign_in_ui.output
178  end
179
180  def util_sign_in response, host = nil, args = []
181    skip 'Always uses $stdin on windows' if Gem.win_platform?
182
183    email    = 'you@example.com'
184    password = 'secret'
185
186    if host
187      ENV['RUBYGEMS_HOST'] = host
188    else
189      host = Gem.host
190    end
191
192    @fetcher = Gem::FakeFetcher.new
193    @fetcher.data["#{host}/api/v1/api_key"] = response
194    Gem::RemoteFetcher.fetcher = @fetcher
195
196    @sign_in_ui = Gem::MockGemUi.new "#{email}\n#{password}\n"
197
198    use_ui @sign_in_ui do
199      if args.length > 0 then
200        @cmd.sign_in(*args)
201      else
202        @cmd.sign_in
203      end
204    end
205  end
206
207  def test_verify_api_key
208    keys = {:other => 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903'}
209    FileUtils.mkdir_p File.dirname(Gem.configuration.credentials_path)
210    File.open Gem.configuration.credentials_path, 'w' do |f|
211      f.write keys.to_yaml
212    end
213    Gem.configuration.load_api_keys
214
215    assert_equal 'a5fdbb6ba150cbb83aad2bb2fede64cf040453903',
216                 @cmd.verify_api_key(:other)
217  end
218
219  def test_verify_missing_api_key
220    assert_raises Gem::MockGemUi::TermError do
221      @cmd.verify_api_key :missing
222    end
223  end
224
225end
226