cfoundry 0.6.1.rc2 → 0.6.1.rc3

Sign up to get free protection for your applications and to get access to all the features.
@@ -112,6 +112,8 @@ module CFoundry
112
112
  raise CFoundry::NotFound.new(response.body, 404)
113
113
  when Net::HTTPForbidden
114
114
  raise CFoundry::Denied.new(response.body, 403)
115
+ when Net::HTTPUnauthorized
116
+ raise CFoundry::Unauthorized.new(response.body, 401)
115
117
  else
116
118
  raise CFoundry::BadResponse.new(response.body, response.code)
117
119
  end
@@ -163,6 +165,8 @@ module CFoundry
163
165
  raise CFoundry::NotFound.new(nil, code, request, response)
164
166
  when 403
165
167
  raise CFoundry::Denied.new(nil, code, request, response)
168
+ when 401
169
+ raise CFoundry::Unauthorized.new(nil, code, request, response)
166
170
  else
167
171
  raise CFoundry::BadResponse.new(nil, code, request, response)
168
172
  end
@@ -121,6 +121,9 @@ module CFoundry
121
121
  class Denied < APIError
122
122
  end
123
123
 
124
+ class Unauthorized < APIError
125
+ end
126
+
124
127
  class BadResponse < APIError
125
128
  end
126
129
 
@@ -18,15 +18,11 @@ module CFoundry
18
18
  end
19
19
 
20
20
  def authorize(username, password)
21
+ @username = username
22
+ @password = password
23
+
21
24
  wrap_uaa_errors do
22
- begin
23
- token_issuer.owner_password_grant(username, password)
24
- rescue CF::UAA::BadResponse => e
25
- status_code = e.message[/\d+/] || 400
26
- raise CFoundry::Denied.new("Authorization failed", status_code)
27
- rescue CF::UAA::TargetError
28
- token_issuer.implicit_grant_with_creds(:username => username, :password => password)
29
- end
25
+ authenticate_with_password_grant || authenticate_with_implicit_grant
30
26
  end
31
27
  end
32
28
 
@@ -107,6 +103,26 @@ module CFoundry
107
103
  @uaa_url ||= CF::UAA::Misc.discover_uaa(target)
108
104
  end
109
105
 
106
+ def authenticate_with_password_grant
107
+ begin
108
+ token_issuer.owner_password_grant(@username, @password)
109
+ rescue CF::UAA::BadResponse => e
110
+ status_code = e.message[/\d+/] || 400
111
+ raise CFoundry::Denied.new("Authorization failed", status_code)
112
+ rescue CF::UAA::TargetError
113
+ false
114
+ end
115
+ end
116
+
117
+ def authenticate_with_implicit_grant
118
+ begin
119
+ token_issuer.implicit_grant_with_creds(:username => @username, :password => @password)
120
+ rescue CF::UAA::BadResponse => e
121
+ status_code = e.message[/\d+/] || 400
122
+ raise CFoundry::Denied.new("Authorization failed", status_code)
123
+ end
124
+ end
125
+
110
126
  def wrap_uaa_errors
111
127
  yield
112
128
  rescue CF::UAA::BadResponse
@@ -1,4 +1,4 @@
1
1
  module CFoundry # :nodoc:
2
2
  # CFoundry library version number.
3
- VERSION = "0.6.1.rc2".freeze
3
+ VERSION = "0.6.1.rc3".freeze
4
4
  end
@@ -215,7 +215,7 @@ describe CFoundry::BaseClient do
215
215
  }.to raise_error(CFoundry::NotFound, /result/)
216
216
  end
217
217
 
218
- it "raises NotFound if response is 403" do
218
+ it "raises Denied if response is 403" do
219
219
  stub_request(:get, "http://example.com/something").to_return(
220
220
  :status => 403, :body => "result")
221
221
 
@@ -224,6 +224,15 @@ describe CFoundry::BaseClient do
224
224
  }.to raise_error(CFoundry::Denied, /result/)
225
225
  end
226
226
 
227
+ it "raises Unauthorized if response is 401" do
228
+ stub_request(:get, "http://example.com/something").to_return(
229
+ :status => 401, :body => "result")
230
+
231
+ expect {
232
+ subject.stream_url("http://example.com/something")
233
+ }.to raise_error(CFoundry::Unauthorized, /result/)
234
+ end
235
+
227
236
  it "raises BadRespones if response is unexpected" do
228
237
  stub_request(:get, "http://example.com/something").to_return(
229
238
  :status => 344, :body => "result")
@@ -94,6 +94,7 @@ EOF
94
94
  end
95
95
  end
96
96
 
97
+
97
98
  context 'in an unexpected way' do
98
99
  it 'raises a CFoundry::Denied error' do
99
100
  mock(issuer).owner_password_grant(anything, anything) { raise CF::UAA::BadResponse.new("no_status_code") }
@@ -102,11 +103,24 @@ EOF
102
103
  end
103
104
 
104
105
  context "with a CF::UAA::TargetError" do
105
- it "retries with implicit grant" do
106
+ before do
106
107
  stub(issuer).owner_password_grant { raise CF::UAA::TargetError.new("useless info") }
108
+ end
109
+
110
+ it "retries with implicit grant" do
107
111
  mock(issuer).implicit_grant_with_creds(:username => username, :password => password)
108
112
  expect { subject }.to_not raise_error
109
113
  end
114
+
115
+ it "fails with Denied when given a 401" do
116
+ stub(issuer).implicit_grant_with_creds(anything) { raise CF::UAA::BadResponse.new("status 401") }
117
+ expect { subject }.to raise_error(CFoundry::Denied, "401: Authorization failed")
118
+ end
119
+
120
+ it "fails with Denied when given any other status code" do
121
+ stub(issuer).implicit_grant_with_creds(anything) { raise CF::UAA::BadResponse.new("no status code") }
122
+ expect { subject }.to raise_error(CFoundry::Denied, "400: Authorization failed")
123
+ end
110
124
  end
111
125
  end
112
126
  end
@@ -89,6 +89,14 @@ describe CFoundry::V2::Base do
89
89
  end
90
90
  end
91
91
 
92
+ context 'when an HTTPUnauthorized error occurs' do
93
+ before { stub_request(:any, url).to_return(:status => 401, :body => "YE FOO SHALL BAR") }
94
+
95
+ it 'raises the correct error' do
96
+ expect { subject }.to raise_error CFoundry::Unauthorized, "401: YE FOO SHALL BAR"
97
+ end
98
+ end
99
+
92
100
  context "when any other type of error occurs" do
93
101
  before { stub_request(:any, url).to_return(:status => 411, :body => "NOT LONG ENOUGH") }
94
102
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfoundry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1.rc2
4
+ version: 0.6.1.rc3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -349,7 +349,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
349
349
  version: '0'
350
350
  segments:
351
351
  - 0
352
- hash: 2443474386956052238
352
+ hash: 808997924378056031
353
353
  required_rubygems_version: !ruby/object:Gem::Requirement
354
354
  none: false
355
355
  requirements:
@@ -358,7 +358,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
358
358
  version: 1.3.1
359
359
  requirements: []
360
360
  rubyforge_project: cfoundry
361
- rubygems_version: 1.8.25
361
+ rubygems_version: 1.8.24
362
362
  signing_key:
363
363
  specification_version: 3
364
364
  summary: High-level library for working with the Cloud Foundry API.