diff --git a/lib/fog/vcloud.rb b/lib/fog/vcloud.rb index 5a0b76766..daba40d32 100644 --- a/lib/fog/vcloud.rb +++ b/lib/fog/vcloud.rb @@ -171,10 +171,13 @@ module Fog "Basic #{Base64.encode64("#{@username}:#{@password}").chomp!}" end + def login_uri + @login_uri ||= get_login_uri + end + # login handles the auth, but we just need the Set-Cookie # header from that call. def do_login - @login_uri ||= get_login_uri @login_results = login @cookie = @login_results.headers['Set-Cookie'] end diff --git a/lib/fog/vcloud/requests/login.rb b/lib/fog/vcloud/requests/login.rb index 299bd6f97..2c86ccc86 100644 --- a/lib/fog/vcloud/requests/login.rb +++ b/lib/fog/vcloud/requests/login.rb @@ -12,7 +12,7 @@ module Fog }, :method => 'POST', :parse => true, - :uri => @login_uri + :uri => login_uri }) end diff --git a/spec/vcloud/requests/get_versions_spec.rb b/spec/vcloud/requests/get_versions_spec.rb index f8faedbc7..073185d12 100644 --- a/spec/vcloud/requests/get_versions_spec.rb +++ b/spec/vcloud/requests/get_versions_spec.rb @@ -1,28 +1,54 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') -if Fog.mocking? - describe Fog::Vcloud, :type => :mock_vcloud_request do - subject { @vcloud } +shared_examples_for "real or mock get_versions requests" do - it { should respond_to :get_versions } + subject { @vcloud } - describe "#get_versions" do - before { @versions = @vcloud.get_versions( @vcloud.versions_uri ) } - subject { @versions } + it { should respond_to :get_versions } - it_should_behave_like "all responses" + describe "#get_versions" do + subject { @vcloud.get_versions( @vcloud.versions_uri ) } - describe "body" do - subject { @versions.body } + it_should_behave_like "all responses" - it { should have(4).keys } - it_should_behave_like "it has the standard xmlns attributes" # 2 keys + describe "body" do + subject { @vcloud.get_versions( @vcloud.versions_uri ).body } - its(:xmlns) { should == "http://www.vmware.com/vcloud/versions" } - its(:VersionInfo) { should == { :LoginUrl => @mock_version[:login_url] , :Version => @mock_version[:version] } } + it { should have(4).keys } + it_should_behave_like "it has the standard xmlns attributes" # 2 keys + its(:xmlns) { should == "http://www.vmware.com/vcloud/versions" } + + its(:VersionInfo) { should be_either_a_hash_or_array } + + describe ":VersionInfo" do + subject { arrayify(@vcloud.get_versions( @vcloud.versions_uri ).body[:VersionInfo]) } + + specify { + subject.each do |version_info| + version_info.should include(:LoginUrl) + version_info[:LoginUrl].should be_a_url + version_info.should include(:Version) + version_info[:Version].should be_an_instance_of String + end + } end end end -else +end + +if Fog.mocking? + describe Fog::Vcloud, :type => :mock_vcloud_request do + + it_should_behave_like "real or mock get_versions requests" + + describe "body" do + subject { @vcloud.get_versions( @vcloud.versions_uri ).body } + its(:VersionInfo) { should == { :LoginUrl => @mock_version[:login_url] , :Version => @mock_version[:version] } } + end + end +else + describe Fog::Vcloud, :type => :vcloud_request do + it_should_behave_like "real or mock get_versions requests" + end end diff --git a/spec/vcloud/requests/login_spec.rb b/spec/vcloud/requests/login_spec.rb index fbd26a132..4bd48754b 100644 --- a/spec/vcloud/requests/login_spec.rb +++ b/spec/vcloud/requests/login_spec.rb @@ -1,11 +1,17 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') +shared_examples_for "real or mock login requests" do + subject { @vcloud } + it_should_behave_like "all login requests" +end + if Fog.mocking? describe Fog::Vcloud, :type => :mock_vcloud_request do - subject { @vcloud } - - it_should_behave_like "all login requests" + it_should_behave_like "real or mock login requests" end else + describe Fog::Vcloud, :type => :vcloud_request do + it_should_behave_like "real or mock login requests" + end end diff --git a/spec/vcloud/spec_helper.rb b/spec/vcloud/spec_helper.rb index 6a983f6df..e45989284 100644 --- a/spec/vcloud/spec_helper.rb +++ b/spec/vcloud/spec_helper.rb @@ -27,6 +27,10 @@ Fog.mock! if ENV['FOG_MOCK'] require "#{current_directory}/../../lib/fog/vcloud/bin" +def arrayify(item) + item.is_a?(Array) ? item : [ item ] +end + shared_examples_for "all responses" do it { should be_an_instance_of Excon::Response } it { should respond_to :body } @@ -218,7 +222,12 @@ Spec::Runner.configure do |config| config.after(:all) do Fog::Vcloud::Mock.data_reset end - config.before(:each, :type => :mock_vcloud_model) do + + config.before(:each, :type => :vcloud_request) do + @vcloud = Fog.services.detect { |service| service == Vcloud }[:vcloud] + end + + config.before(:all, :type => :mock_vcloud_model) do @vcloud = Fog::Vcloud.new(:username => "foo", :password => "bar", :versions_uri => "http://fakey.com/api/versions") end config.before(:all, :type => :mock_vcloud_model) do @@ -332,3 +341,15 @@ Spec::Matchers.define :have_all_attributes_be_nil do actual.class.attributes.all? { |attribute| actual.send(attribute.to_sym) == nil } end end + +Spec::Matchers.define :be_a_url do + match do |actual| + actual.match(/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix) + end +end + +Spec::Matchers.define :be_either_a_hash_or_array do + match do |actual| + actual.is_a?(Hash) || actual.is_a?(Array) + end +end