git-process-lib 2.0.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,7 +17,7 @@ describe GitRemote do
17
17
 
18
18
  clone_repo('master', 'origin') do |gl|
19
19
  gl.remote.name.should == 'origin'
20
- gl.branches.include?('origin/master').should be_true
20
+ gl.branches.include?('origin/master').should be true
21
21
  end
22
22
  end
23
23
 
@@ -27,7 +27,7 @@ describe GitRemote do
27
27
 
28
28
  clone_repo('master', 'a_remote') do |gl|
29
29
  gl.remote.name.should == 'a_remote'
30
- gl.branches.include?('a_remote/master').should be_true
30
+ gl.branches.include?('a_remote/master').should be true
31
31
  end
32
32
  end
33
33
 
@@ -1,7 +1,7 @@
1
1
  require 'git-process/github_configuration'
2
2
  require 'json'
3
3
  require 'github_test_helper'
4
-
4
+ # require 'vcr'
5
5
 
6
6
  describe GitHubService::Configuration, :git_repo_helper do
7
7
  include GitHubTestHelper
@@ -19,10 +19,10 @@ describe GitHubService::Configuration, :git_repo_helper do
19
19
 
20
20
  describe 'create_authorization' do
21
21
 
22
- it "should return an auth_token for a good request" do
22
+ it 'should return an auth_token for a good request' do
23
23
  gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
24
24
  stub_post('https://tu:dfsdf@api.github.com/authorizations', :send => auth_json,
25
- :body => {:token => test_token})
25
+ :body => JSON({:token => test_token}).to_s)
26
26
 
27
27
  ghc.create_authorization().should == test_token
28
28
  end
@@ -36,12 +36,62 @@ describe GitHubService::Configuration, :git_repo_helper do
36
36
  expect { ghc.create_authorization() }.to raise_error Octokit::Unauthorized
37
37
  end
38
38
 
39
+
40
+ it 'should 401 for needing two-factor authentication' do
41
+ gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
42
+
43
+ stub_request(:post, 'https://tu:dfsdf@api.github.com/authorizations').to_return do |request|
44
+ # noinspection RubyStringKeysInHashInspection
45
+ if request.headers.has_key? 'x-github-otp'
46
+ {
47
+ :body => JSON({:token => test_token}).to_s,
48
+ :headers => {'Content-Type' => 'application/json'},
49
+ :status => 200
50
+ }
51
+ else
52
+ {
53
+ :body => '',
54
+ :status => 401,
55
+ :headers => {'X-GitHub-OTP' => 'required; app'}
56
+ }
57
+ end
58
+ end
59
+ GitHubService::Configuration.stub(:ask_for_two_factor).and_return('572269')
60
+
61
+ ghc.create_authorization().should == test_token
62
+ end
63
+
64
+
65
+ it 'should 422 if two-factor authorization already exists' do
66
+ gitlib.remote.add('origin', 'git@github.com:jdigger/git-process.git')
67
+
68
+ stub_request(:post, 'https://tu:dfsdf@api.github.com/authorizations').to_return do |request|
69
+ # noinspection RubyStringKeysInHashInspection
70
+ if request.headers.has_key? 'x-github-otp'
71
+ {
72
+ :body => JSON({errors: [{:resource => 'OauthAccess', :code => 'already_exists'}]}).to_s,
73
+ :headers => {'Content-Type' => 'application/json'},
74
+ :status => 422
75
+ }
76
+ else
77
+ {
78
+ :body => '',
79
+ :status => 401,
80
+ :headers => {'X-GitHub-OTP' => 'required; app'}
81
+ }
82
+ end
83
+ end
84
+ GitHubService::Configuration.stub(:ask_for_two_factor).and_return('572269')
85
+
86
+ expect { ghc.create_authorization() }.to raise_error GitHubService::TokenAlreadyExists
87
+ end
88
+
39
89
  end
40
90
 
41
91
 
42
- describe "auth_token no username or password" do
92
+ describe 'auth_token no username or password' do
43
93
 
44
- it "should get the token from config if it exists" do
94
+ it 'should get the token from config if it exists' do
45
95
  gitlib.config['github.user'] = 'test_user'
46
96
  gitlib.config['gitProcess.github.authToken'] = test_token
47
97
 
@@ -64,7 +114,7 @@ describe GitHubService::Configuration, :git_repo_helper do
64
114
  gitlib.config['gitProcess.github.authToken'] = ''
65
115
 
66
116
  stub_post('https://test_user:dfsdf@api.github.com/authorizations', :send => auth_json,
67
- :body => {:token => test_token})
117
+ :body => JSON({:token => test_token}).to_s)
68
118
 
69
119
  ghc.auth_token.should == test_token
70
120
  end
@@ -86,7 +136,7 @@ describe GitHubService::Configuration, :git_repo_helper do
86
136
  end
87
137
 
88
138
 
89
- it "should prompt the user and store it in the config" do
139
+ it 'should prompt the user and store it in the config' do
90
140
  gitlib.config['github.user'] = ''
91
141
 
92
142
  GitHubService::Configuration.stub(:ask_for_user).and_return('test_user')
@@ -96,34 +146,34 @@ describe GitHubService::Configuration, :git_repo_helper do
96
146
  end
97
147
 
98
148
 
99
- describe "using GHE instead of GitHub.com" do
149
+ describe 'using GHE instead of GitHub.com' do
100
150
 
101
- it "should use the correct server and path for a non-GitHub.com site" do
151
+ it 'should use the correct server and path for a non-GitHub.com site' do
102
152
  gitlib.remote.add('origin', 'git@myco.com:jdigger/git-process.git')
103
153
 
104
154
  stub_post('https://tu:dfsdf@myco.com/api/v3/authorizations',
105
155
  :send => auth_json,
106
- :body => {:token => test_token})
156
+ :body => JSON({:token => test_token}).to_s)
107
157
 
108
158
  ghc.create_authorization().should == test_token
109
159
  end
110
160
 
111
161
 
112
- it "site should raise an error if remote.origin.url not set" do
162
+ it 'site should raise an error if remote.origin.url not set' do
113
163
  gitlib.config['remote.origin.url'] = ''
114
164
 
115
165
  expect { ghc.base_github_api_url_for_remote }.to raise_error GitHubService::NoRemoteRepository
116
166
  end
117
167
 
118
168
 
119
- it "site should not work for a garbage url address" do
169
+ it 'site should not work for a garbage url address' do
120
170
  gitlib.remote.add('origin', 'garbage')
121
171
 
122
172
  expect { ghc.base_github_api_url_for_remote }.to raise_error URI::InvalidURIError
123
173
  end
124
174
 
125
175
 
126
- it "site should work for an ssh-configured url address" do
176
+ it 'site should work for an ssh-configured url address' do
127
177
  gitlib.remote.add('origin', 'git@github.myco.com:fooble')
128
178
 
129
179
  ghc.base_github_api_url_for_remote.should == 'https://github.myco.com'
@@ -132,7 +182,7 @@ describe GitHubService::Configuration, :git_repo_helper do
132
182
  end
133
183
 
134
184
 
135
- it "#url_to_base_github_api_url" do
185
+ it '#url_to_base_github_api_url' do
136
186
  c = GitHubService::Configuration
137
187
 
138
188
  c.url_to_base_github_api_url('ssh://git@github.myco.com/fooble').should == 'https://github.myco.com'
@@ -145,8 +195,9 @@ describe GitHubService::Configuration, :git_repo_helper do
145
195
 
146
196
 
147
197
  def auth_json
148
- JSON({:note_url => 'http://jdigger.github.com/git-process',
149
- :scopes => %w(repo user gist), :note => "Git-Process"})
198
+ JSON({:scopes => %w(repo user gist),
199
+ :note => 'Git-Process',
200
+ :note_url => 'http://jdigger.github.com/git-process'}).to_s
150
201
  end
151
202
 
152
203
  end
@@ -15,7 +15,7 @@ describe GitHub::PullRequest, :git_repo_helper do
15
15
 
16
16
 
17
17
  def pull_request
18
- @pr ||= GitHub::PullRequest.new(gitlib, 'test_remote', 'test_repo', :user => 'test_user')
18
+ @pr ||= GitHub::PullRequest.new(gitlib, 'test_remote', 'tester/test_repo', :user => 'test_user')
19
19
  end
20
20
 
21
21
 
@@ -28,7 +28,7 @@ describe GitHub::PullRequest, :git_repo_helper do
28
28
  describe '#create' do
29
29
 
30
30
  it 'should return a pull request for a good request' do
31
- stub_post('https://api.github.com/repos/test_repo/pulls', :body => {:number => 1, :state => 'open'})
31
+ stub_post('https://api.github.com/repos/tester/test_repo/pulls', :body => {:number => 1, :state => 'open'})
32
32
 
33
33
  pull_request.create('test_base', 'test_head', 'test title', 'test body')[:state].should == 'open'
34
34
  end
@@ -36,10 +36,10 @@ describe GitHub::PullRequest, :git_repo_helper do
36
36
 
37
37
  it 'should handle asking for a duplicate pull request' do
38
38
  # trying to create the request should return "HTTP 422: Unprocessable Entity" because it already exists
39
- stub_post('https://api.github.com/repos/test_repo/pulls', :status => 422)
39
+ stub_post('https://api.github.com/repos/tester/test_repo/pulls', :status => 422)
40
40
 
41
41
  # listing all existing pull requests should contain the current branch
42
- stub_get('https://api.github.com/repos/test_repo/pulls?state=open', :status => 200,
42
+ stub_get('https://api.github.com/repos/tester/test_repo/pulls', :status => 200,
43
43
  :body => [{:html_url => 'test_url', :head => {:ref => 'test_head'}, :base => {:ref => 'test_base'}}])
44
44
 
45
45
  pull_request.create('test_base', 'test_head', 'test title', 'test body')[:html_url].should == 'test_url'
@@ -51,7 +51,7 @@ describe GitHub::PullRequest, :git_repo_helper do
51
51
  describe 'get' do
52
52
 
53
53
  it 'should return a pull request for a good request' do
54
- stub_get('https://api.github.com/repos/test_repo/pulls/1', :body => {:number => 1, :state => 'open'})
54
+ stub_get('https://api.github.com/repos/tester/test_repo/pulls/1', :body => {:number => 1, :state => 'open'})
55
55
 
56
56
  pull_request.pull_request(1)[:state].should == 'open'
57
57
  end
@@ -62,19 +62,20 @@ describe GitHub::PullRequest, :git_repo_helper do
62
62
  describe '#close' do
63
63
 
64
64
  it 'should close a good current pull request' do
65
- stub_get('https://api.github.com/repos/test_repo/pulls?state=open', :body => [
66
- {:number => 1, :state => 'open', :html_url => 'test_url', :head => {:ref => 'test_head'},
67
- :base => {:ref => 'test_base'}}])
68
- stub_patch('https://api.github.com/repos/test_repo/pulls/1', :send => JSON({:state => 'closed'}),
65
+ stub_get('https://api.github.com/repos/tester/test_repo/pulls', :body => [
66
+ {:number => 1, :state => 'open', :html_url => 'test_url', :head => {:ref => 'test_head'},
67
+ :base => {:ref => 'test_base'}}])
68
+ stub_patch('https://api.github.com/repos/tester/test_repo/pulls/1', :send => {:state => 'closed'},
69
69
  :body => {:number => 1, :state => 'closed', :html_url => 'test_url', :head => {:ref => 'test_head'},
70
70
  :base => {:ref => 'test_base'}})
71
71
 
72
- pull_request.close('test_base', 'test_head')[:state].should == 'closed'
72
+ close_result = pull_request.close('test_base', 'test_head')
73
+ close_result[:state].should == 'closed'
73
74
  end
74
75
 
75
76
 
76
77
  it 'should close a good current pull request using the pull request number' do
77
- stub_patch('https://api.github.com/repos/test_repo/pulls/1', :send => JSON({:state => 'closed'}),
78
+ stub_patch('https://api.github.com/repos/tester/test_repo/pulls/1', :send => JSON({:state => 'closed'}),
78
79
  :body => {:number => 1, :state => 'closed', :html_url => 'test_url',
79
80
  :head => {:ref => 'test_head'}, :base => {:ref => 'test_base'}})
80
81
 
@@ -83,7 +84,7 @@ describe GitHub::PullRequest, :git_repo_helper do
83
84
 
84
85
 
85
86
  it 'should retry closing a good current pull request when getting a 422' do
86
- stub = stub_request(:patch, 'https://api.github.com/repos/test_repo/pulls/1')
87
+ stub = stub_request(:patch, 'https://api.github.com/repos/tester/test_repo/pulls/1')
87
88
 
88
89
  stub.with(:body => JSON({:state => 'closed'}))
89
90
 
@@ -91,17 +92,17 @@ describe GitHub::PullRequest, :git_repo_helper do
91
92
  to_raise(Octokit::UnprocessableEntity.new).then.
92
93
  to_raise(Octokit::UnprocessableEntity.new).then.
93
94
  to_raise(Octokit::UnprocessableEntity.new).then.
94
- to_return(:status => 200, :body => {:number => 1, :state => 'closed', :html_url => 'test_url',
95
- :head => {:ref => 'test_head'}, :base => {:ref => 'test_base'}})
95
+ to_return(:status => 200, :body => JSON({:number => 1, :state => 'closed', :html_url => 'test_url',
96
+ :head => {:ref => 'test_head'}, :base => {:ref => 'test_base'}}).to_s)
96
97
 
97
98
  pull_request.close(1)[:state].should == 'closed'
98
99
  end
99
100
 
100
101
 
101
102
  it 'should complain about a missing pull request' do
102
- stub_get('https://api.github.com/repos/test_repo/pulls?state=open', :body => [
103
- {:number => 1, :state => 'open', :html_url => 'test_url', :head => {:ref => 'test_head'},
104
- :base => {:ref => 'test_base'}}])
103
+ stub_get('https://api.github.com/repos/tester/test_repo/pulls', :body => [
104
+ {:number => 1, :state => 'open', :html_url => 'test_url', :head => {:ref => 'test_head'},
105
+ :base => {:ref => 'test_base'}}])
105
106
 
106
107
  expect { pull_request.close('test_base', 'missing_head') }.to raise_error GitHub::PullRequest::NotFoundError
107
108
  end
@@ -2,6 +2,32 @@ require 'FileHelpers'
2
2
  require 'git-process/git_process'
3
3
  require 'webmock/rspec'
4
4
 
5
+ ################
6
+ #
7
+ # Monkey-patch WebMock to stop screwing with the capitalization of resource headers
8
+ #
9
+ ################
10
+ module WebMock
11
+ module Util
12
+ class Headers
13
+ def self.normalize_headers(headers)
14
+ headers
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ module Net::HTTPHeader
21
+ def add_field(key, val)
22
+ if @header.key?(key)
23
+ @header[key].push val
24
+ else
25
+ @header[key] = [val]
26
+ end
27
+ end
28
+ end
29
+
30
+
5
31
  module GitHubTestHelper
6
32
 
7
33
 
@@ -12,7 +38,7 @@ module GitHubTestHelper
12
38
  stub.with(:Authorization => "token #{opts[:token]}")
13
39
  end
14
40
 
15
- stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => opts[:body] ? opts[:body] : '')
41
+ stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => to_body(opts[:body]))
16
42
  stub
17
43
  end
18
44
 
@@ -20,13 +46,30 @@ module GitHubTestHelper
20
46
  def stub_post(url, opts = {})
21
47
  stub = stub_request(:post, url)
22
48
 
49
+ with_headers = opts[:headers] || {}
50
+
23
51
  if opts[:token]
24
52
  stub.with(:Authorization => "token #{opts[:token]}")
25
53
  end
26
54
 
27
- stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => opts[:body] ? opts[:body] : '')
55
+ if opts[:send]
56
+ stub.with(:body => to_body(opts[:send]))
57
+ end
28
58
 
29
- stub
59
+ if opts[:two_factor]
60
+ # noinspection RubyStringKeysInHashInspection
61
+ with_headers.merge!({'X-GitHub-OTP'.downcase => [opts[:two_factor]]})
62
+ end
63
+
64
+ if opts[:body]
65
+ # noinspection RubyStringKeysInHashInspection
66
+ opts[:response_headers] = {'Content-Type' => 'application/json'}.merge(opts[:response_headers] || {})
67
+ end
68
+
69
+ stub.with(:headers => with_headers) unless with_headers.empty?
70
+ stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => to_body(opts[:body]), :headers => opts[:response_headers] ? opts[:response_headers] : {})
71
+
72
+ return stub
30
73
  end
31
74
 
32
75
 
@@ -38,12 +81,23 @@ module GitHubTestHelper
38
81
  end
39
82
 
40
83
  if opts[:send]
41
- stub.with(:body => opts[:send])
84
+ stub.with(:body => to_body(opts[:send]))
42
85
  end
43
86
 
44
- stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => opts[:body] ? opts[:body] : '')
87
+ stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => to_body(opts[:body]))
45
88
 
46
89
  stub
47
90
  end
48
91
 
92
+ def to_body(body)
93
+ return '' if body.nil?
94
+
95
+ return body if body.is_a? String
96
+
97
+ if body.is_a? Hash or body.is_a? Array
98
+ return JSON(body).to_s
99
+ end
100
+ raise "Do not know what to do with #{body.class} #{body}"
101
+ end
102
+
49
103
  end
data/spec/new_fb_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'git-process/new_fb'
1
+ require File.dirname(__FILE__) + '/../lib/git-process/new_fb'
2
2
  require 'GitRepoHelper'
3
3
  include GitProc
4
4
 
@@ -42,7 +42,7 @@ describe NewFeatureBranch do
42
42
  end
43
43
 
44
44
 
45
- it "should bring committed changes on _parking_ over to the new branch" do
45
+ it 'should bring committed changes on _parking_ over to the new branch' do
46
46
  clone_repo do |gl|
47
47
  gl.checkout('_parking_', :new_branch => 'master')
48
48
  change_file_and_commit('a', '', gl)
@@ -53,12 +53,12 @@ describe NewFeatureBranch do
53
53
 
54
54
  new_branch.name.should == 'test_branch'
55
55
  Dir.chdir(gl.workdir) do |_|
56
- File.exists?('a').should be_true
57
- File.exists?('b').should be_true
56
+ File.exists?('a').should be true
57
+ File.exists?('b').should be true
58
58
  end
59
59
 
60
- gl.config["branch.test_branch.remote"].should == 'origin'
61
- gl.config["branch.test_branch.merge"].should == 'refs/heads/master'
60
+ gl.config['branch.test_branch.remote'].should == 'origin'
61
+ gl.config['branch.test_branch.merge'].should == 'refs/heads/master'
62
62
 
63
63
  gl.fetch
64
64
  gl.branches.parking.should be_nil
@@ -68,7 +68,7 @@ describe NewFeatureBranch do
68
68
  end
69
69
 
70
70
 
71
- it "should move new branch over to the integration branch" do
71
+ it 'should move new branch over to the integration branch' do
72
72
  clone_repo do |gl|
73
73
  gl.checkout('_parking_', :new_branch => 'master')
74
74
  change_file_and_commit('a', '', gitlib)
@@ -80,8 +80,8 @@ describe NewFeatureBranch do
80
80
 
81
81
  new_branch.name.should == 'test_branch'
82
82
  Dir.chdir(gitlib.workdir) do |_|
83
- File.exists?('a').should be_true
84
- File.exists?('b').should be_true
83
+ File.exists?('a').should be true
84
+ File.exists?('b').should be true
85
85
  end
86
86
 
87
87
  gl.config["branch.test_branch.remote"].should == 'origin'
@@ -95,7 +95,7 @@ describe NewFeatureBranch do
95
95
  end
96
96
 
97
97
 
98
- it "should use 'integration_branch' instead of 'remote_master_branch'" do
98
+ it 'should use "integration_branch" instead of "remote_master_branch"' do
99
99
  change_file_and_commit('a', '')
100
100
 
101
101
  new_branch = gitprocess.runner
@@ -104,7 +104,7 @@ describe NewFeatureBranch do
104
104
  end
105
105
 
106
106
 
107
- it "should bring new/uncommitted changes on _parking_ over to the new branch" do
107
+ it 'should bring new/uncommitted changes on _parking_ over to the new branch' do
108
108
  gitlib.branch('origin/master', :base_branch => 'master')
109
109
  gitlib.checkout('_parking_', :new_branch => 'master')
110
110
  change_file_and_commit('a', '')
@@ -115,9 +115,9 @@ describe NewFeatureBranch do
115
115
 
116
116
  new_branch.name.should == 'test_branch'
117
117
  Dir.chdir(gitlib.workdir) do |_|
118
- File.exists?('a').should be_true
119
- File.exists?('b').should be_true
120
- File.exists?('c').should be_true
118
+ File.exists?('a').should be true
119
+ File.exists?('b').should be true
120
+ File.exists?('c').should be true
121
121
  end
122
122
 
123
123
  gitlib.branches.parking.should be_nil