From ccb15472af7150cc5f29fd9fed8a4239b2f9dc24 Mon Sep 17 00:00:00 2001 From: Kazuto Kusama Date: Sat, 9 Mar 2013 23:02:27 +0900 Subject: [PATCH] Fix login_helpers to login v1 environment without UAA --- lib/cfoundry/concerns/login_helpers.rb | 18 +++++++-- spec/cfoundry/v1/client_spec.rb | 4 +- .../shared_examples/client_login_examples.rb | 40 ++++++++++++++++++- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/lib/cfoundry/concerns/login_helpers.rb b/lib/cfoundry/concerns/login_helpers.rb index 2d22a77..dc7a64f 100644 --- a/lib/cfoundry/concerns/login_helpers.rb +++ b/lib/cfoundry/concerns/login_helpers.rb @@ -3,11 +3,23 @@ module CFoundry module LoginHelpers def login_prompts - @base.uaa.prompts + if @base.uaa + @base.uaa.prompts + else + { + :username => ["text", "Email"], + :password => ["password", "Password"] + } + end end def login(username, password) - @base.token = AuthToken.from_uaa_token_info(@base.uaa.authorize(username, password)) + @base.token = + if @base.uaa + AuthToken.from_uaa_token_info(@base.uaa.authorize(username, password)) + else + AuthToken.new(@base.create_token({:password => password}, username)[:token]) + end end end -end \ No newline at end of file +end diff --git a/spec/cfoundry/v1/client_spec.rb b/spec/cfoundry/v1/client_spec.rb index 6b3f684..0d3a1dd 100644 --- a/spec/cfoundry/v1/client_spec.rb +++ b/spec/cfoundry/v1/client_spec.rb @@ -9,9 +9,11 @@ describe "#login" do include_examples "client login" + include_examples "client login without UAA" end describe "#login_prompts" do include_examples "client login prompts" + include_examples "client login prompts without UAA" end -end \ No newline at end of file +end diff --git a/spec/support/shared_examples/client_login_examples.rb b/spec/support/shared_examples/client_login_examples.rb index d73e0d5..078213a 100644 --- a/spec/support/shared_examples/client_login_examples.rb +++ b/spec/support/shared_examples/client_login_examples.rb @@ -43,4 +43,42 @@ expect(client.token).to be_a(CFoundry::AuthToken) expect(client.token.auth_header).to eq("bearer #{access_token}") end -end \ No newline at end of file +end + +shared_examples_for 'client login prompts without UAA' do + let(:prompts) do + { + :user_id => ["text", "User ID"], + :pin => ["password", "Your 8-digit Pin #"] + } + end + + subject { client.login_prompts } + + it 'returns the prompts without UAA' do + expect(subject).to eq(prompts) + end +end + +shared_examples_for 'client login without UAA' do + let(:email) { 'test@test.com' } + let(:password) { 'secret' } + let(:access_token) { "some-access-token" } + + before do + stub(client.base).create_token { access_token } + end + + subject { client.login(email, password) } + + it 'returns a CC token' do + expect(subject).to be_a(CFoundry::AuthToken) + expect(subject.auth_header).to eq("bearer #{access_token}") + end + + it 'saves the data as the token' do + subject + expect(client.token).to be_a(CFoundry::AuthToken) + expect(client.token.auth_header).to eq("bearer #{access_token}") + end +end