-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgithub_configuration_spec.rb
More file actions
203 lines (138 loc) · 6.11 KB
/
github_configuration_spec.rb
File metadata and controls
203 lines (138 loc) · 6.11 KB
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
require 'git-process/github_configuration'
require 'json'
require 'github_test_helper'
# require 'vcr'
describe GitHubService::Configuration, :git_repo_helper do
include GitHubTestHelper
def test_token
'hfgkdjfgksjhdfkls'
end
def ghc
@ghc ||= GitHubService::Configuration.new(gitlib.config, :user => 'tu', :password => 'dfsdf')
end
describe 'create_authorization' do
it 'should return an auth_token for a good request' do
gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
stub_post('https://tu:dfsdf@api.github.com/authorizations', :send => auth_json,
:body => JSON({:token => test_token}).to_s)
ghc.create_authorization().should == test_token
end
it 'should 401 for bad password' do
gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
stub_post('https://tu:dfsdf@api.github.com/authorizations', :send => auth_json,
:status => 401)
expect { ghc.create_authorization() }.to raise_error Octokit::Unauthorized
end
it 'should 401 for needing two-factor authentication' do
gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
stub_request(:post, 'https://tu:dfsdf@api.github.com/authorizations').to_return do |request|
# noinspection RubyStringKeysInHashInspection
if request.headers.has_key? 'x-github-otp'
{
:body => JSON({:token => test_token}).to_s,
:headers => {'Content-Type' => 'application/json'},
:status => 200
}
else
{
:body => '',
:status => 401,
:headers => {'X-GitHub-OTP' => 'required; app'}
}
end
end
GitHubService::Configuration.stub(:ask_for_two_factor).and_return('572269')
ghc.create_authorization().should == test_token
end
it 'should 422 if two-factor authorization already exists' do
gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
stub_request(:post, 'https://tu:dfsdf@api.github.com/authorizations').to_return do |request|
# noinspection RubyStringKeysInHashInspection
if request.headers.has_key? 'x-github-otp'
{
:body => JSON({errors: [{:resource => 'OauthAccess', :code => 'already_exists'}]}).to_s,
:headers => {'Content-Type' => 'application/json'},
:status => 422
}
else
{
:body => '',
:status => 401,
:headers => {'X-GitHub-OTP' => 'required; app'}
}
end
end
GitHubService::Configuration.stub(:ask_for_two_factor).and_return('572269')
expect { ghc.create_authorization() }.to raise_error GitHubService::TokenAlreadyExists
end
end
describe 'auth_token no username or password' do
it 'should get the token from config if it exists' do
gitlib.config['github.user'] = 'test_user'
gitlib.config['gitProcess.github.authToken'] = test_token
ghc.auth_token.should == test_token
end
end
describe 'auth_token with password but no username' do
def ghc
@ghc ||= GitHubService::Configuration.new(gitlib.config, :user => nil, :password => 'dfsdf')
end
it 'should get the token from the server if it does not exist in config' do
gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
gitlib.config['github.user'] = 'test_user'
gitlib.config['gitProcess.github.authToken'] = ''
stub_post('https://test_user:dfsdf@api.github.com/authorizations', :send => auth_json,
:body => JSON({:token => test_token}).to_s)
ghc.auth_token.should == test_token
end
end
describe "user" do
def ghc
@ghc ||= GitHubService::Configuration.new(gitlib.config, :user => nil, :password => 'dfsdf')
end
it "should get the value from config" do
gitlib.config['github.user'] = 'test_user'
ghc.user.should == 'test_user'
end
it 'should prompt the user and store it in the config' do
gitlib.config['github.user'] = ''
GitHubService::Configuration.stub(:ask_for_user).and_return('test_user')
ghc.user.should == 'test_user'
end
end
describe 'using GHE instead of GitHub.com' do
it 'should use the correct server and path for a non-GitHub.com site' do
gitlib.remote.add('origin', 'git@myco.com:jdigger/git-process.git')
stub_post('https://tu:dfsdf@myco.com/api/v3/authorizations',
:send => auth_json,
:body => JSON({:token => test_token}).to_s)
ghc.create_authorization().should == test_token
end
it 'site should raise an error if remote.origin.url not set' do
gitlib.config['remote.origin.url'] = ''
expect { ghc.base_github_api_url_for_remote }.to raise_error GitHubService::NoRemoteRepository
end
it 'site should not work for a garbage url address' do
gitlib.remote.add('origin', 'garbage')
expect { ghc.base_github_api_url_for_remote }.to raise_error URI::InvalidURIError
end
it 'site should work for an ssh-configured url address' do
gitlib.remote.add('origin', 'git@github.myco.com:fooble')
ghc.base_github_api_url_for_remote.should == 'https://github.myco.com'
end
end
it '#url_to_base_github_api_url' do
c = GitHubService::Configuration
c.url_to_base_github_api_url('ssh://git@github.myco.com/fooble').should == 'https://github.myco.com'
c.url_to_base_github_api_url('git://myco.com/jdigger/git-process.git').should == 'https://myco.com'
c.url_to_base_github_api_url('http://github.myco.com/fooble').should == 'http://github.myco.com'
c.url_to_base_github_api_url('http://tu@github.myco.com/fooble').should == 'http://github.myco.com'
c.url_to_base_github_api_url('https://github.myco.com/fooble').should == 'https://github.myco.com'
c.url_to_base_github_api_url('https://github.com/fooble').should == 'https://api.github.com'
end
def auth_json
JSON({:scopes => %w(repo user gist),
:note => 'Git-Process',
:note_url => 'http://jdigger.github.com/git-process'}).to_s
end
end