jira-ruby 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe JIRA::HttpClient do
4
+
5
+ let(:basic_client) do
6
+ options = JIRA::Client::DEFAULT_OPTIONS.merge(JIRA::HttpClient::DEFAULT_OPTIONS)
7
+ JIRA::HttpClient.new(options)
8
+ end
9
+
10
+ let(:response) do
11
+ response = mock("response")
12
+ response.stub(:kind_of?).with(Net::HTTPSuccess).and_return(true)
13
+ response
14
+ end
15
+
16
+ it "creates an instance of Net:HTTP for a basic auth client" do
17
+ basic_client.basic_auth_http_conn.class.should == Net::HTTP
18
+ end
19
+
20
+ it "responds to the http methods" do
21
+ body = ''
22
+ headers = mock()
23
+ basic_auth_http_conn = mock()
24
+ request = mock()
25
+ basic_client.stub(:basic_auth_http_conn => basic_auth_http_conn)
26
+ request.should_receive(:basic_auth)
27
+ .with(basic_client.options[:username], basic_client.options[:password])
28
+ .exactly(5).times.and_return(request)
29
+ basic_auth_http_conn.should_receive(:request).exactly(5).times.with(request).and_return(response)
30
+ [:delete, :get, :head].each do |method|
31
+ Net::HTTP.const_get(method.capitalize).should_receive(:new).with('/path', headers).and_return(request)
32
+ basic_client.make_request(method, '/path', nil, headers).should == response
33
+ end
34
+ [:post, :put].each do |method|
35
+ Net::HTTP.const_get(method.capitalize).should_receive(:new).with('/path', headers).and_return(request)
36
+ request.should_receive(:body=).with(body).and_return(request)
37
+ basic_client.make_request(method, '/path', body, headers).should == response
38
+ end
39
+ end
40
+
41
+ it "performs a basic http client request" do
42
+ body = nil
43
+ headers = mock()
44
+ basic_auth_http_conn = mock()
45
+ http_request = mock()
46
+ Net::HTTP::Get.should_receive(:new).with('/foo', headers).and_return(http_request)
47
+
48
+ basic_auth_http_conn.should_receive(:request).with(http_request).and_return(response)
49
+ http_request.should_receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).and_return(http_request)
50
+ basic_client.stub(:basic_auth_http_conn => basic_auth_http_conn)
51
+ basic_client.make_request(:get, '/foo', body, headers)
52
+ end
53
+
54
+ it "returns a URI" do
55
+ uri = URI.parse(basic_client.options[:site])
56
+ basic_client.uri.should == uri
57
+ end
58
+
59
+ it "sets up a http connection with options" do
60
+ http_conn = mock()
61
+ uri = mock()
62
+ host = mock()
63
+ port = mock()
64
+ uri.should_receive(:host).and_return(host)
65
+ uri.should_receive(:port).and_return(port)
66
+ Net::HTTP.should_receive(:new).with(host, port).and_return(http_conn)
67
+ http_conn.should_receive(:use_ssl=).with(basic_client.options[:use_ssl]).and_return(http_conn)
68
+ http_conn.should_receive(:verify_mode=).with(basic_client.options[:ssl_verify_mode]).and_return(http_conn)
69
+ basic_client.http_conn(uri).should == http_conn
70
+ end
71
+
72
+ it "returns a http connection" do
73
+ http_conn = mock()
74
+ uri = mock()
75
+ basic_client.should_receive(:uri).and_return(uri)
76
+ basic_client.should_receive(:http_conn).and_return(http_conn)
77
+ basic_client.basic_auth_http_conn.should == http_conn
78
+ end
79
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe JIRA::OauthClient do
4
+
5
+ let(:oauth_client) do
6
+ options = { :consumer_key => 'foo', :consumer_secret => 'bar' }
7
+ options = JIRA::Client::DEFAULT_OPTIONS.merge(options)
8
+ JIRA::OauthClient.new(options)
9
+ end
10
+
11
+ let(:response) do
12
+ response = mock("response")
13
+ response.stub(:kind_of?).with(Net::HTTPSuccess).and_return(true)
14
+ response
15
+ end
16
+
17
+ describe "authenticating with oauth" do
18
+ it "prepends the context path to all authorization and rest paths" do
19
+ options = [:request_token_path, :authorize_path, :access_token_path]
20
+ defaults = JIRA::Client::DEFAULT_OPTIONS.merge(JIRA::OauthClient::DEFAULT_OPTIONS)
21
+ options.each do |key|
22
+ oauth_client.options[key].should == defaults[:context_path] + defaults[key]
23
+ end
24
+ end
25
+
26
+ it "creates a Oauth::Consumer on initialize" do
27
+ oauth_client.consumer.class.should == OAuth::Consumer
28
+ oauth_client.consumer.key.should == oauth_client.key
29
+ oauth_client.consumer.secret.should == oauth_client.secret
30
+ end
31
+
32
+ it "returns an OAuth request_token" do
33
+ # Cannot just check for method delegation as http connection will be attempted
34
+ request_token = OAuth::RequestToken.new(oauth_client.consumer)
35
+ oauth_client.consumer.stub(:get_request_token => request_token)
36
+ oauth_client.get_request_token.should == request_token
37
+ end
38
+
39
+ it "allows setting the request token" do
40
+ token = mock()
41
+ OAuth::RequestToken.should_receive(:new).with(oauth_client.consumer, 'foo', 'bar').and_return(token)
42
+
43
+ request_token = oauth_client.set_request_token('foo', 'bar')
44
+
45
+ request_token.should == token
46
+ oauth_client.request_token.should == token
47
+ end
48
+
49
+ it "allows setting the consumer key" do
50
+ oauth_client.key.should == 'foo'
51
+ end
52
+
53
+ it "allows setting the consumer secret" do
54
+ oauth_client.secret.should == 'bar'
55
+ end
56
+
57
+ describe "the access token" do
58
+
59
+ it "initializes" do
60
+ request_token = OAuth::RequestToken.new(oauth_client.consumer)
61
+ oauth_client.consumer.stub(:get_request_token => request_token)
62
+ mock_access_token = mock()
63
+ request_token.should_receive(:get_access_token).with(:oauth_verifier => 'abc123').and_return(mock_access_token)
64
+ oauth_client.init_access_token(:oauth_verifier => 'abc123')
65
+ oauth_client.access_token.should == mock_access_token
66
+ end
67
+
68
+ it "raises an exception when accessing without initialisation" do
69
+ expect {
70
+ oauth_client.access_token
71
+ }.to raise_exception(JIRA::OauthClient::UninitializedAccessTokenError,
72
+ "init_access_token must be called before using the client")
73
+ end
74
+
75
+ it "allows setting the access token" do
76
+ token = mock()
77
+ OAuth::AccessToken.should_receive(:new).with(oauth_client.consumer, 'foo', 'bar').and_return(token)
78
+
79
+ access_token = oauth_client.set_access_token('foo', 'bar')
80
+
81
+ access_token.should == token
82
+ oauth_client.access_token.should == token
83
+ end
84
+ end
85
+
86
+ describe "http" do
87
+ it "responds to the http methods" do
88
+ headers = mock()
89
+ mock_access_token = mock()
90
+ oauth_client.stub(:access_token => mock_access_token)
91
+ [:delete, :get, :head].each do |method|
92
+ mock_access_token.should_receive(method).with('/path', headers).and_return(response)
93
+ oauth_client.make_request(method, '/path', '', headers)
94
+ end
95
+ [:post, :put].each do |method|
96
+ mock_access_token.should_receive(method).with('/path', '', headers).and_return(response)
97
+ oauth_client.make_request(method, '/path', '', headers)
98
+ end
99
+ end
100
+
101
+ it "performs a request" do
102
+ body = nil
103
+ headers = mock()
104
+ access_token = mock()
105
+ access_token.should_receive(:send).with(:get, '/foo', headers).and_return(response)
106
+ oauth_client.stub(:access_token => access_token)
107
+ oauth_client.request(:get, '/foo', body, headers)
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe JIRA::RequestClient do
4
+
5
+ it "raises an exception for non success responses" do
6
+ response = mock()
7
+ response.stub(:kind_of?).with(Net::HTTPSuccess).and_return(false)
8
+ rc = JIRA::RequestClient.new
9
+ rc.should_receive(:make_request).with(:get, '/foo', '', {}).and_return(response)
10
+ expect {
11
+ rc.request(:get, '/foo', '', {})
12
+ }.to raise_exception(JIRA::HTTPError)
13
+ end
14
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,7 @@ Dir["./spec/support/**/*.rb"].each {|f| require f}
7
7
  require 'jira'
8
8
 
9
9
  RSpec.configure do |config|
10
-
10
+ config.extend ClientsHelper
11
11
  end
12
12
 
13
13
 
@@ -0,0 +1,16 @@
1
+ module ClientsHelper
2
+ def with_each_client
3
+ clients = {}
4
+
5
+ oauth_client = JIRA::Client.new({ :consumer_key => 'foo', :consumer_secret => 'bar' })
6
+ oauth_client.set_access_token('abc', '123')
7
+ clients["http://localhost:2990"] = oauth_client
8
+
9
+ basic_client = JIRA::Client.new({ :username => 'foo', :password => 'bar', :auth_type => :basic, :use_ssl => false })
10
+ clients["http://foo:bar@localhost:2990"] = basic_client
11
+
12
+ clients.each do |site_url, client|
13
+ yield site_url, client
14
+ end
15
+ end
16
+ end
@@ -55,7 +55,7 @@ shared_examples "a resource" do
55
55
  else
56
56
  subject = client.send(class_basename).build(described_class.key_attribute.to_s => '99999')
57
57
  end
58
- stub_request(:put, 'http://localhost:2990' + subject.url).
58
+ stub_request(:put, site_url + subject.url).
59
59
  to_return(:status => 405, :body => "<html><body>Some HTML</body></html>")
60
60
  subject.save('foo' => 'bar').should be_false
61
61
  lambda do
@@ -68,7 +68,7 @@ end
68
68
  shared_examples "a resource with a collection GET endpoint" do
69
69
 
70
70
  it "should get the collection" do
71
- stub_request(:get, "http://localhost:2990" + described_class.collection_path(client)).
71
+ stub_request(:get, site_url + described_class.collection_path(client)).
72
72
  to_return(:status => 200, :body => get_mock_from_path(:get))
73
73
  collection = build_receiver.all
74
74
  collection.length.should == expected_collection_length
@@ -84,7 +84,7 @@ shared_examples "a resource with a singular GET endpoint" do
84
84
  it "GETs a single resource" do
85
85
  # E.g., for JIRA::Resource::Project, we need to call
86
86
  # client.Project.find()
87
- stub_request(:get, "http://localhost:2990" + described_class.singular_path(client, key, prefix)).
87
+ stub_request(:get, site_url + described_class.singular_path(client, key, prefix)).
88
88
  to_return(:status => 200, :body => get_mock_from_path(:get, :key => key))
89
89
  subject = client.send(class_basename).find(key, options)
90
90
 
@@ -94,7 +94,7 @@ shared_examples "a resource with a singular GET endpoint" do
94
94
  it "builds and fetches a single resource" do
95
95
  # E.g., for JIRA::Resource::Project, we need to call
96
96
  # client.Project.build('key' => 'ABC123')
97
- stub_request(:get, "http://localhost:2990" + described_class.singular_path(client, key, prefix)).
97
+ stub_request(:get, site_url + described_class.singular_path(client, key, prefix)).
98
98
  to_return(:status => 200, :body => get_mock_from_path(:get, :key => key))
99
99
 
100
100
  subject = build_receiver.build(described_class.key_attribute.to_s => key)
@@ -104,7 +104,7 @@ shared_examples "a resource with a singular GET endpoint" do
104
104
  end
105
105
 
106
106
  it "handles a 404" do
107
- stub_request(:get, "http://localhost:2990" + described_class.singular_path(client, '99999', prefix)).
107
+ stub_request(:get, site_url + described_class.singular_path(client, '99999', prefix)).
108
108
  to_return(:status => 404, :body => '{"errorMessages":["'+class_basename+' Does Not Exist"],"errors": {}}')
109
109
  lambda do
110
110
  client.send(class_basename).find('99999', options)
@@ -116,7 +116,7 @@ shared_examples "a resource with a DELETE endpoint" do
116
116
  it "deletes a resource" do
117
117
  # E.g., for JIRA::Resource::Project, we need to call
118
118
  # client.Project.delete()
119
- stub_request(:delete, "http://localhost:2990" + described_class.singular_path(client, key, prefix)).
119
+ stub_request(:delete, site_url + described_class.singular_path(client, key, prefix)).
120
120
  to_return(:status => 204, :body => nil)
121
121
 
122
122
  subject = build_receiver.build(described_class.key_attribute.to_s => key)
@@ -127,7 +127,7 @@ end
127
127
  shared_examples "a resource with a POST endpoint" do
128
128
 
129
129
  it "saves a new resource" do
130
- stub_request(:post, "http://localhost:2990" + described_class.collection_path(client, prefix)).
130
+ stub_request(:post, site_url + described_class.collection_path(client, prefix)).
131
131
  to_return(:status => 201, :body => get_mock_from_path(:post))
132
132
  subject = build_receiver.build
133
133
  subject.save(attributes_for_post).should be_true
@@ -141,9 +141,9 @@ end
141
141
  shared_examples "a resource with a PUT endpoint" do
142
142
 
143
143
  it "saves an existing component" do
144
- stub_request(:get, "http://localhost:2990" + described_class.singular_path(client, key, prefix)).
144
+ stub_request(:get, site_url + described_class.singular_path(client, key, prefix)).
145
145
  to_return(:status => 200, :body => get_mock_from_path(:get, :key =>key))
146
- stub_request(:put, "http://localhost:2990" + described_class.singular_path(client, key, prefix)).
146
+ stub_request(:put, site_url + described_class.singular_path(client, key, prefix)).
147
147
  to_return(:status => 200, :body => get_mock_from_path(:put, :key => key, :value_if_not_found => nil))
148
148
  subject = build_receiver.build(described_class.key_attribute.to_s => key)
149
149
  subject.fetch
@@ -158,9 +158,9 @@ end
158
158
  shared_examples 'a resource with a PUT endpoint that rejects invalid fields' do
159
159
 
160
160
  it "fails to save with an invalid field" do
161
- stub_request(:get, "http://localhost:2990" + described_class.singular_path(client, key)).
161
+ stub_request(:get, site_url + described_class.singular_path(client, key)).
162
162
  to_return(:status => 200, :body => get_mock_from_path(:get, :key => key))
163
- stub_request(:put, "http://localhost:2990" + described_class.singular_path(client, key)).
163
+ stub_request(:put, site_url + described_class.singular_path(client, key)).
164
164
  to_return(:status => 400, :body => get_mock_from_path(:put, :key => key, :suffix => "invalid"))
165
165
  subject = client.send(class_basename).build(described_class.key_attribute.to_s => key)
166
166
  subject.fetch
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jira-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-15 00:00:00.000000000Z
12
+ date: 2012-07-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth
16
- requirement: &70194827879980 !ruby/object:Gem::Requirement
16
+ requirement: &70203258317280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70194827879980
24
+ version_requirements: *70203258317280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: oauth
27
- requirement: &70194827879560 !ruby/object:Gem::Requirement
27
+ requirement: &70203258316860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70194827879560
35
+ version_requirements: *70203258316860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: railties
38
- requirement: &70194827879140 !ruby/object:Gem::Requirement
38
+ requirement: &70203258316440 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70194827879140
46
+ version_requirements: *70203258316440
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: railties
49
- requirement: &70194827878720 !ruby/object:Gem::Requirement
49
+ requirement: &70203258316020 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70194827878720
57
+ version_requirements: *70203258316020
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activesupport
60
- requirement: &70194827878300 !ruby/object:Gem::Requirement
60
+ requirement: &70203258315600 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70194827878300
68
+ version_requirements: *70203258315600
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
- requirement: &70194827877880 !ruby/object:Gem::Requirement
71
+ requirement: &70203258315180 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70194827877880
79
+ version_requirements: *70203258315180
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: webmock
82
- requirement: &70194827877460 !ruby/object:Gem::Requirement
82
+ requirement: &70203258314760 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70194827877460
90
+ version_requirements: *70203258314760
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rspec
93
- requirement: &70194827877040 !ruby/object:Gem::Requirement
93
+ requirement: &70203258314340 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70194827877040
101
+ version_requirements: *70203258314340
102
102
  description: API for JIRA 5
103
103
  email:
104
104
  executables: []
@@ -110,14 +110,18 @@ files:
110
110
  - README.rdoc
111
111
  - Rakefile
112
112
  - example.rb
113
+ - http-basic-example.rb
113
114
  - jira-ruby.gemspec
114
115
  - lib/jira.rb
115
116
  - lib/jira/base.rb
116
117
  - lib/jira/base_factory.rb
117
118
  - lib/jira/client.rb
118
119
  - lib/jira/has_many_proxy.rb
120
+ - lib/jira/http_client.rb
119
121
  - lib/jira/http_error.rb
122
+ - lib/jira/oauth_client.rb
120
123
  - lib/jira/railtie.rb
124
+ - lib/jira/request_client.rb
121
125
  - lib/jira/resource/attachment.rb
122
126
  - lib/jira/resource/comment.rb
123
127
  - lib/jira/resource/component.rb
@@ -147,7 +151,10 @@ files:
147
151
  - spec/jira/base_spec.rb
148
152
  - spec/jira/client_spec.rb
149
153
  - spec/jira/has_many_proxy_spec.rb
154
+ - spec/jira/http_client_spec.rb
150
155
  - spec/jira/http_error_spec.rb
156
+ - spec/jira/oauth_client_spec.rb
157
+ - spec/jira/request_client_spec.rb
151
158
  - spec/jira/resource/attachment_spec.rb
152
159
  - spec/jira/resource/issue_spec.rb
153
160
  - spec/jira/resource/project_factory_spec.rb
@@ -186,6 +193,7 @@ files:
186
193
  - spec/mock_responses/version/10000.json
187
194
  - spec/mock_responses/version/10000.put.json
188
195
  - spec/spec_helper.rb
196
+ - spec/support/clients_helper.rb
189
197
  - spec/support/matchers/have_attributes.rb
190
198
  - spec/support/matchers/have_many.rb
191
199
  - spec/support/matchers/have_one.rb
@@ -230,7 +238,10 @@ test_files:
230
238
  - spec/jira/base_spec.rb
231
239
  - spec/jira/client_spec.rb
232
240
  - spec/jira/has_many_proxy_spec.rb
241
+ - spec/jira/http_client_spec.rb
233
242
  - spec/jira/http_error_spec.rb
243
+ - spec/jira/oauth_client_spec.rb
244
+ - spec/jira/request_client_spec.rb
234
245
  - spec/jira/resource/attachment_spec.rb
235
246
  - spec/jira/resource/issue_spec.rb
236
247
  - spec/jira/resource/project_factory_spec.rb
@@ -269,6 +280,7 @@ test_files:
269
280
  - spec/mock_responses/version/10000.json
270
281
  - spec/mock_responses/version/10000.put.json
271
282
  - spec/spec_helper.rb
283
+ - spec/support/clients_helper.rb
272
284
  - spec/support/matchers/have_attributes.rb
273
285
  - spec/support/matchers/have_many.rb
274
286
  - spec/support/matchers/have_one.rb