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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# frozen_string_literal: true
require_relative "helper"
require "rubygems/commands/signin_command"
require "rubygems/installer"
class TestGemCommandsSigninCommand < Gem::TestCase
def setup
super
credential_setup
Gem.configuration.rubygems_api_key = nil
Gem.configuration.api_keys.clear
@cmd = Gem::Commands::SigninCommand.new
end
def teardown
credential_teardown
super
end
def test_execute_when_not_already_signed_in
sign_in_ui = util_capture { @cmd.execute }
assert_match(/Signed in./, sign_in_ui.output)
end
def test_execute_when_not_already_signed_in_and_not_preexisting_credentials_folder
FileUtils.rm Gem.configuration.credentials_path
sign_in_ui = util_capture { @cmd.execute }
assert_match(/Signed in./, sign_in_ui.output)
end
def test_execute_when_already_signed_in_with_same_host
host = "http://some-gemcutter-compatible-host.org"
util_capture(nil, host) { @cmd.execute }
old_credentials = load_yaml_file Gem.configuration.credentials_path
util_capture(nil, host) { @cmd.execute }
new_credentials = load_yaml_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"
util_capture(nil, nil, api_key) { @cmd.execute }
host = "http://some-gemcutter-compatible-host.org"
util_capture(nil, host, api_key) { @cmd.execute }
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal credentials[:rubygems_api_key], api_key
assert_nil credentials[host]
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(/Enter your #{host} credentials./, sign_in_ui.output)
assert_match(/Signed in./, sign_in_ui.output)
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[host]
end
def test_execute_with_host_permanent_redirect
host = "http://rubygems.example/"
ENV["RUBYGEMS_HOST"] = host
path = "/api/v1/api_key"
redirected_uri = "http://rubygems.example#{path}"
fetcher = Gem::FakeFetcher.new
fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create(
body: "",
code: "308",
msg: "Permanent Redirect",
headers: { "location" => redirected_uri }
)
Gem::RemoteFetcher.fetcher = fetcher
ui = Gem::MockGemUi.new("you@example.com\nsecret\n\n\n")
assert_raise Gem::MockGemUi::TermError do
use_ui ui do
@cmd.execute
end
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, ui.output
end
def test_execute_with_valid_creds_set_for_default_host
util_capture { @cmd.execute }
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_execute_with_key_name_default_scope
email = "you@example.com"
password = "secret"
api_key = "1234abcd"
fetcher = Gem::RemoteFetcher.fetcher
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\n\n"
util_capture(key_name_ui, nil, api_key, fetcher) { @cmd.execute }
user = ENV["USER"] || ENV["USERNAME"]
assert_match "API Key name [#{Socket.gethostname}-#{user}", key_name_ui.output
assert_match "The default access scope is:", key_name_ui.output
assert_match "index_rubygems: y", key_name_ui.output
assert_match "Do you want to customise scopes? [yN]", key_name_ui.output
assert_equal "name=test-key&index_rubygems=true", fetcher.last_request.body
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_execute_with_key_name_and_custom_scope
email = "you@example.com"
password = "secret"
api_key = "1234abcd"
fetcher = Gem::RemoteFetcher.fetcher
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\ny\n\n\ny\n\n\n\n\n\n\n"
util_capture(key_name_ui, nil, api_key, fetcher) { @cmd.execute }
user = ENV["USER"] || ENV["USERNAME"]
assert_match "API Key name [#{Socket.gethostname}-#{user}", key_name_ui.output
assert_match "The default access scope is:", key_name_ui.output
assert_match "Do you want to customise scopes? [yN]", key_name_ui.output
assert_match "show_dashboard (exclusive scope, answering yes will not prompt for other scopes) [yN]", key_name_ui.output
assert_match "index_rubygems [yN]", key_name_ui.output
assert_match "push_rubygem [yN]", key_name_ui.output
assert_match "yank_rubygem [yN]", key_name_ui.output
assert_match "add_owner [yN]", key_name_ui.output
assert_match "remove_owner [yN]", key_name_ui.output
assert_match "access_webhooks [yN]", key_name_ui.output
assert_equal "name=test-key&push_rubygem=true", fetcher.last_request.body
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_execute_with_key_name_and_exclusive_scope
email = "you@example.com"
password = "secret"
api_key = "1234abcd"
fetcher = Gem::RemoteFetcher.fetcher
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\ny\ny\n"
util_capture(key_name_ui, nil, api_key, fetcher) { @cmd.execute }
user = ENV["USER"] || ENV["USERNAME"]
assert_match "API Key name [#{Socket.gethostname}-#{user}", key_name_ui.output
assert_match "The default access scope is:", key_name_ui.output
assert_match "index_rubygems: y", key_name_ui.output
assert_match "Do you want to customise scopes? [yN]", key_name_ui.output
assert_match "show_dashboard (exclusive scope, answering yes will not prompt for other scopes) [yN]", key_name_ui.output
assert_equal "name=test-key&show_dashboard=true", fetcher.last_request.body
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_execute_with_key_name_custom_scope_and_mfa_level_of_ui_only
email = "you@example.com"
password = "secret"
api_key = "1234abcd"
fetcher = Gem::RemoteFetcher.fetcher
mfa_level = "ui_only"
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\ny\n\n\ny\n\n\n\n\n\n\ny"
util_capture(key_name_ui, nil, api_key, fetcher, mfa_level) { @cmd.execute }
user = ENV["USER"] || ENV["USERNAME"]
assert_match "API Key name [#{Socket.gethostname}-#{user}", key_name_ui.output
assert_match "The default access scope is:", key_name_ui.output
assert_match "Do you want to customise scopes? [yN]", key_name_ui.output
assert_match "show_dashboard (exclusive scope, answering yes will not prompt for other scopes) [yN]", key_name_ui.output
assert_match "index_rubygems [yN]", key_name_ui.output
assert_match "push_rubygem [yN]", key_name_ui.output
assert_match "yank_rubygem [yN]", key_name_ui.output
assert_match "add_owner [yN]", key_name_ui.output
assert_match "remove_owner [yN]", key_name_ui.output
assert_match "access_webhooks [yN]", key_name_ui.output
assert_match "Would you like to enable MFA for this key? (strongly recommended) [yn]", key_name_ui.output
assert_equal "name=test-key&push_rubygem=true&mfa=true", fetcher.last_request.body
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_execute_with_key_name_custom_scope_and_mfa_level_of_gem_signin
email = "you@example.com"
password = "secret"
api_key = "1234abcd"
fetcher = Gem::RemoteFetcher.fetcher
mfa_level = "ui_and_gem_signin"
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\ny\n\n\ny\n\n\n\n\n\n\ny"
util_capture(key_name_ui, nil, api_key, fetcher, mfa_level) { @cmd.execute }
user = ENV["USER"] || ENV["USERNAME"]
assert_match "API Key name [#{Socket.gethostname}-#{user}", key_name_ui.output
assert_match "The default access scope is:", key_name_ui.output
assert_match "Do you want to customise scopes? [yN]", key_name_ui.output
assert_match "show_dashboard (exclusive scope, answering yes will not prompt for other scopes) [yN]", key_name_ui.output
assert_match "index_rubygems [yN]", key_name_ui.output
assert_match "push_rubygem [yN]", key_name_ui.output
assert_match "yank_rubygem [yN]", key_name_ui.output
assert_match "add_owner [yN]", key_name_ui.output
assert_match "remove_owner [yN]", key_name_ui.output
assert_match "access_webhooks [yN]", key_name_ui.output
assert_match "Would you like to enable MFA for this key? (strongly recommended) [yn]", key_name_ui.output
assert_equal "name=test-key&push_rubygem=true&mfa=true", fetcher.last_request.body
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_execute_with_warnings
email = "you@example.com"
password = "secret"
api_key = "1234abcd"
fetcher = Gem::RemoteFetcher.fetcher
mfa_level = "disabled"
warning = "/[WARNING/] For protection of your account and gems"
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\n\ny\n\n\n\n\n\ny"
util_capture(key_name_ui, nil, api_key, fetcher, mfa_level, warning) { @cmd.execute }
assert_match warning, key_name_ui.output
end
def test_execute_on_gemserver_without_profile_me_endpoint
host = "http://some-gemcutter-compatible-host.org"
email = "you@example.com"
password = "secret"
api_key = "1234abcd"
fetcher = Gem::RemoteFetcher.fetcher
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\ny\n\n\ny\n\n\n\n\n\n\ny"
# Set the expected response for the Web-API supplied
ENV["RUBYGEMS_HOST"] = host
data_key = "#{ENV["RUBYGEMS_HOST"]}/api/v1/api_key"
fetcher.data[data_key] = HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
use_ui key_name_ui do
@cmd.execute
end
user = ENV["USER"] || ENV["USERNAME"]
assert_match "API Key name [#{Socket.gethostname}-#{user}", key_name_ui.output
assert_match "show_dashboard (exclusive scope, answering yes will not prompt for other scopes) [yN]", key_name_ui.output
assert_match "index_rubygems [yN]", key_name_ui.output
assert_match "push_rubygem [yN]", key_name_ui.output
assert_match "yank_rubygem [yN]", key_name_ui.output
assert_match "add_owner [yN]", key_name_ui.output
assert_match "remove_owner [yN]", key_name_ui.output
assert_match "access_webhooks [yN]", key_name_ui.output
assert_equal "name=test-key&push_rubygem=true", fetcher.last_request.body
end
# Utility method to capture IO/UI within the block passed
def util_capture(ui_stub = nil, host = nil, api_key = nil, fetcher = Gem::FakeFetcher.new, mfa_level = "disabled", warning = nil)
api_key ||= "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
response = HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
profile_response = HTTPResponseFactory.create(body: "mfa: #{mfa_level}\nwarning: #{warning}", code: 200, msg: "OK")
email = "you@example.com"
password = "secret"
# 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
profile = "#{ENV["RUBYGEMS_HOST"]}/api/v1/profile/me.yaml"
fetcher.data[profile] = profile_response
Gem::RemoteFetcher.fetcher = fetcher
sign_in_ui = ui_stub || Gem::MockGemUi.new("#{email}\n#{password}\n\n\n")
use_ui sign_in_ui do
yield
end
sign_in_ui
end
end
|