spec/cfoundry/uaaclient_spec.rb in cfoundry-0.5.0 vs spec/cfoundry/uaaclient_spec.rb in cfoundry-0.5.1.rc1
- old
+ new
@@ -5,10 +5,11 @@
let(:uaa) { CFoundry::UAAClient.new(target) }
let(:auth_header) { "bearer access-token" }
before do
uaa.token = CFoundry::AuthToken.new(auth_header)
+ CF::UAA::Util.default_logger.level = 1
end
shared_examples "UAA wrapper" do
it "converts UAA errors to CFoundry equivalents" do
mock(uaa).wrap_uaa_errors { nil }
@@ -59,37 +60,48 @@
let(:password) { "test" }
let(:creds) { {:username => username, :password => password} }
let(:state) { 'somestate' }
let(:redirect_uri) { 'https://uaa.cloudfoundry.com/redirect/vmc' }
let(:auth) { Object.new }
+ let(:issuer) { Object.new }
subject { uaa.authorize(username, password) }
- before { stub(uaa).token_issuer.stub!.implicit_grant_with_creds { auth } }
+ before do
+ stub(issuer).owner_password_grant { auth }
+ stub(uaa).token_issuer { issuer }
+ end
include_examples "UAA wrapper"
it 'returns the token on successful authentication' do
- stub(uaa).token_issuer.mock!.implicit_grant_with_creds(creds) { auth }
+ mock(issuer).owner_password_grant(username, password) { auth }
expect(subject).to eq auth
end
context 'when authorization fails' do
context 'in the expected way' do
it 'raises a CFoundry::Denied error' do
- stub(uaa).token_issuer.stub!.implicit_grant_with_creds { raise CF::UAA::BadResponse.new("401: FooBar") }
-
+ mock(issuer).owner_password_grant(anything, anything) { raise CF::UAA::BadResponse.new("401: FooBar") }
expect { subject }.to raise_error(CFoundry::Denied, "401: Authorization failed")
end
end
context 'in an unexpected way' do
it 'raises a CFoundry::Denied error' do
- stub(uaa).token_issuer.stub!.implicit_grant_with_creds { raise CF::UAA::BadResponse.new("no_status_code") }
+ mock(issuer).owner_password_grant(anything, anything) { raise CF::UAA::BadResponse.new("no_status_code") }
expect { subject }.to raise_error(CFoundry::Denied, "400: Authorization failed")
end
end
+
+ context "with a CF::UAA::TargetError" do
+ it "retries with implicit grant" do
+ stub(issuer).owner_password_grant { raise CF::UAA::TargetError.new("useless info") }
+ mock(issuer).implicit_grant_with_creds(:username => username, :password => password)
+ expect { subject }.to_not raise_error
+ end
+ end
end
end
describe '#users' do
subject { uaa.users }
@@ -170,11 +182,11 @@
context 'when the score is 0 and the required is 0' do
let(:response) { MultiJson.encode "score" => 0, "requiredScore" => 0 }
it { should == :good }
end
- context 'when the score is less than the required core' do
+ context 'when the score is less than the required score' do
let(:response) { MultiJson.encode "score" => 1, "requiredScore" => 5 }
it { should == :weak }
end
context 'and the score is equal to the required score' do
@@ -257,22 +269,22 @@
expect { subject }.to raise_exception(CFoundry::Denied)
end
end
context "when the block raises CF::UAA::TargetError" do
- let(:error) { CF::UAA::TargetError.new({ "error" => "foo", "error_description" => "bar" }) }
+ let(:error) { CF::UAA::TargetError.new({ :error => "foo", :error_description => "bar" }) }
it "raises CFoundry::UAAError" do
expect { subject }.to raise_exception(CFoundry::UAAError, "foo: bar")
end
end
end
describe "#token_issuer" do
it "has logging level 0 if #trace is true" do
uaa.trace = true
- expect(uaa.send(:token_issuer).logger.level).to eq 0
+ expect(uaa.send(:token_issuer).logger.level).to eq -1
end
it "has logging level 1 if #trace is false" do
uaa.trace = false
expect(uaa.send(:token_issuer).logger.level).to eq 1
@@ -280,14 +292,41 @@
end
describe "#scim" do
it "has logging level 0 if #trace is true" do
uaa.trace = true
- expect(uaa.send(:scim).logger.level).to eq 0
+ expect(uaa.send(:scim).logger.level).to eq -1
end
it "has logging level 1 if #trace is false" do
uaa.trace = false
expect(uaa.send(:scim).logger.level).to eq 1
+ end
+ end
+
+ describe "#try_to_refresh_token!" do
+ it "uses the refresh token to get a new access token" do
+ mock(uaa.send(:token_issuer)).refresh_token_grant(uaa.token.refresh_token) do
+ CF::UAA::TokenInfo.new(
+ :token_type => "bearer",
+ :access_token => "refreshed-token",
+ :refresh_token => "some-refresh-token")
+ end
+
+ uaa.try_to_refresh_token!
+ expect(uaa.token.auth_header).to eq "bearer refreshed-token"
+ expect(uaa.token.refresh_token).to eq "some-refresh-token"
+ end
+
+ context "when the refresh token has expired" do
+ it "returns the current token" do
+ stub(uaa.send(:token_issuer)).refresh_token_grant do
+ raise CF::UAA::TargetError.new
+ end
+
+ expect {
+ uaa.try_to_refresh_token!
+ }.to_not change { uaa.token }
+ end
end
end
end