提交 2c6e0bc7 编写于 作者: M Mislav Marohnić

Rename & retry creating Access Token if there's a name collision

If there's already an Access Token named "hub for {USER}@{HOSTNAME}",
add a number in the end and increment it maximum of 9 times until saving
of the Access Token succeeds.
上级 b8bc80c5
...@@ -35,6 +35,68 @@ Feature: OAuth authentication ...@@ -35,6 +35,68 @@ Feature: OAuth authentication
And the file "../home/.config/hub" should contain "oauth_token: OTOKEN" And the file "../home/.config/hub" should contain "oauth_token: OTOKEN"
And the file "../home/.config/hub" should have mode "0600" And the file "../home/.config/hub" should have mode "0600"
Scenario: Rename & retry creating authorization if there's a token name collision
Given the GitHub API server:
"""
require 'socket'
require 'etc'
machine_id = "#{Etc.getlogin}@#{Socket.gethostname}"
post('/authorizations') {
assert_basic_auth 'mislav', 'kitty'
if params[:note] == "hub for #{machine_id} 3"
json :token => 'OTOKEN'
else
status 422
json :message => 'Validation Failed',
:errors => [{
:resource => 'OauthAccess',
:code => 'already_exists',
:field => 'description'
}]
end
}
get('/user') {
json :login => 'MiSlAv'
}
post('/user/repos') {
json :full_name => 'mislav/dotfiles'
}
"""
When I run `hub create` interactively
When I type "mislav"
And I type "kitty"
Then the output should contain "github.com username:"
And the exit status should be 0
And the file "../home/.config/hub" should contain "oauth_token: OTOKEN"
Scenario: Avoid getting caught up in infinite recursion while retrying token names
Given the GitHub API server:
"""
tries = 0
post('/authorizations') {
tries += 1
halt 400, json(:message => "too many tries") if tries >= 10
status 422
json :message => 'Validation Failed',
:errors => [{
:resource => 'OauthAccess',
:code => 'already_exists',
:field => 'description'
}]
}
"""
When I run `hub create` interactively
When I type "mislav"
And I type "kitty"
Then the output should contain:
"""
Error creating repository: Unprocessable Entity (HTTP 422)
Duplicate value for "description"
"""
And the exit status should be 1
And the file "../home/.config/hub" should not exist
Scenario: Credentials from GITHUB_USER & GITHUB_PASSWORD Scenario: Credentials from GITHUB_USER & GITHUB_PASSWORD
Given the GitHub API server: Given the GitHub API server:
""" """
......
...@@ -352,6 +352,13 @@ module Hub ...@@ -352,6 +352,13 @@ module Hub
break break
elsif res.status == 401 && res['X-GitHub-OTP'].to_s.include?('required') elsif res.status == 401 && res['X-GitHub-OTP'].to_s.include?('required')
two_factor_code = config.prompt_auth_code two_factor_code = config.prompt_auth_code
elsif res.status == 422 && 'already_exists' == res.data['errors'][0]['code']
if auth_params[:note] =~ / (\d+)$/
res.error! if $1.to_i >= 9
auth_params[:note].succ!
else
auth_params[:note] += ' 2'
end
else else
res.error! res.error!
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册