[PATCH conductor 6/7] Rspec tests for Provider Accounts API - create

pblaho at redhat.com pblaho at redhat.com
Wed Aug 15 17:11:29 UTC 2012


From: Petr Blaho <pblaho at redhat.com>

https://www.aeolusproject.org/redmine/issues/3653

Adds rspec tests for Provider Accounts API - create
---
 src/spec/matchers/have_content_type.rb          |    3 +-
 src/spec/requests/api/provider_accounts_spec.rb |  135 +++++++++++++++++++++++
 src/spec/spec_helper.rb                         |    6 +
 3 files changed, 143 insertions(+), 1 deletions(-)
 create mode 100644 src/spec/requests/api/provider_accounts_spec.rb

diff --git a/src/spec/matchers/have_content_type.rb b/src/spec/matchers/have_content_type.rb
index f8da334..2070a20 100644
--- a/src/spec/matchers/have_content_type.rb
+++ b/src/spec/matchers/have_content_type.rb
@@ -29,6 +29,7 @@ RSpec::Matchers.define :have_content_type do |content_type|
   end
 
   match do |response|
+    @response = response
     _, content, charset = *content_type_header.match(CONTENT_HEADER_MATCHER).to_a
 
     if @charset
@@ -55,6 +56,6 @@ RSpec::Matchers.define :have_content_type do |content_type|
   end
 
   def content_type_header
-    response.headers['Content-Type']
+    @response.headers['Content-Type']
   end
 end
diff --git a/src/spec/requests/api/provider_accounts_spec.rb b/src/spec/requests/api/provider_accounts_spec.rb
new file mode 100644
index 0000000..f973f48
--- /dev/null
+++ b/src/spec/requests/api/provider_accounts_spec.rb
@@ -0,0 +1,135 @@
+require 'spec_helper'
+
+describe "ProviderAccounts" do
+  let(:headers) { {
+    'HTTP_ACCEPT' => 'application/xml',
+    'CONTENT_TYPE' => 'application/xml'
+  } }
+  before(:each) do
+    user = FactoryGirl.create(:admin_permission).user
+    login_as(user)
+  end
+
+  #  describe "GET /api/provider_accounts" do
+  #    before(:each) do
+  #      resp = get '/api/provider_accounts', nil, headers
+  #    end
+  #
+  #    it_behaves_like "http OK"
+  #    it_behaves_like "responding with XML"
+  #  end
+
+  describe "POST /api/providers/:provider_id/provider_accounts" do
+    before(:each) do
+      post "/api/providers/#{provider.id}/provider_accounts", xml, headers
+    end
+
+    context "with valid" do
+      context "mock provider account XML" do
+        let(:xml) do
+          "<provider_account><label>mockaccount</label><credentials><username>mockuser</username><password>mockpassword</password></credentials></provider_account>"
+        end
+        let(:provider) { FactoryGirl.create(:mock_provider) }
+
+        it_behaves_like "http OK"
+        it_behaves_like "responding with XML"
+        context "XML body" do
+          subject { Nokogiri::XML(response.body) }
+          it "should have correct nodes" do
+            subject.xpath('//provider_account').size.should be_eql(1)
+            subject.xpath('//provider_account/label').size.should be_eql(1)
+            subject.xpath('//provider_account/label').text.should be_eql("mockaccount")
+            subject.xpath('//provider_account/credentials').size.should be_eql(1)
+            subject.xpath('//provider_account/credentials/username').size.should be_eql(1)
+            subject.xpath('//provider_account/credentials/username').text.should be_eql("mockuser")
+            subject.xpath('//provider_account/credentials/password').size.should be_eql(1)
+            subject.xpath('//provider_account/credentials/password').text.should be_eql("mockpassword")
+          end
+        end
+      end
+
+      context "ec2 provider account XML" do
+        let(:xml) do
+          DeltaCloud.stub(:valid_credentials?).and_return(true)
+          "<provider_account>
+             <label>ec2account</label>
+             <credentials>
+               <username>ec2user</username>
+               <password>ec2password</password>
+               <account_id>ec2account_id</account_id>
+               <x509private><![CDATA[ec2x509private]]></x509private>
+               <x509public><![CDATA[ec2x509public]]></x509public>
+             </credentials>
+           </provider_account>"
+        end
+        let(:provider) { FactoryGirl.create(:ec2_provider) }
+
+        it_behaves_like "http OK"
+        it_behaves_like "responding with XML"
+        context "XML body" do
+          subject { Nokogiri::XML(response.body) }
+          it "should have correct nodes" do
+            subject.xpath('//provider_account').size.should be_eql(1)
+            subject.xpath('//provider_account/label').size.should be_eql(1)
+            subject.xpath('//provider_account/label').text.should be_eql("ec2account")
+            subject.xpath('//provider_account/credentials').size.should be_eql(1)
+            subject.xpath('//provider_account/credentials/username').size.should be_eql(1)
+            subject.xpath('//provider_account/credentials/username').text.should be_eql("ec2user")
+            subject.xpath('//provider_account/credentials/password').size.should be_eql(1)
+            subject.xpath('//provider_account/credentials/password').text.should be_eql("ec2password")
+            subject.xpath('//provider_account/credentials/x509private').size.should be_eql(1)
+            subject.xpath('//provider_account/credentials/x509private').text.should be_eql("ec2x509private")
+          end
+        end
+      end
+    end
+
+    context "with invalid" do
+      context "mock provider account XML" do
+        # omit label
+        let(:xml) do
+          "<provider_account><label></label><credentials><username>mockuser</username><password>mockpassword</password></credentials></provider_account>"
+        end
+        let(:provider) { FactoryGirl.create(:mock_provider) }
+
+        it_behaves_like "http Bad Request"
+        it_behaves_like "responding with XML"
+        context "XML body" do
+          subject { Nokogiri::XML(response.body) }
+          it "should have some errors" do
+            subject.xpath('//errors').size.should be_eql(1)
+            subject.xpath('//errors/error').size.should <= 1
+          end
+        end
+
+      end
+
+      context "ec2 provider account XML" do
+        # omit label
+        let(:xml) do
+          DeltaCloud.stub(:valid_credentials?).and_return(true)
+          "<provider_account>
+           <credentials>
+             <username>ec2user</username>
+             <password>ec2password</password>
+             <account_id>ec2account_id</account_id>
+             <x509private><![CDATA[ec2x509private]]></x509private>
+             <x509public><![CDATA[ec2x509public]]></x509public>
+           </credentials>
+         </provider_account>"
+        end
+        let(:provider) { FactoryGirl.create(:ec2_provider) }
+
+        it_behaves_like "http Bad Request"
+        it_behaves_like "responding with XML"
+        context "XML body" do
+          subject { Nokogiri::XML(response.body) }
+          it "should have some errors" do
+            subject.xpath('//errors').size.should be_eql(1)
+            subject.xpath('//errors/error').size.should <= 1
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/src/spec/spec_helper.rb b/src/spec/spec_helper.rb
index dd47849..ae89927 100644
--- a/src/spec/spec_helper.rb
+++ b/src/spec/spec_helper.rb
@@ -115,4 +115,10 @@ RSpec.configure do |config|
   #config.after(:each, :type => :controller) do
   #  current_user_session.destroy
   #end
+
+  config.include Warden::Test::Helpers, :type => :request
+  config.after(:each, :type => :request) do
+    Warden.test_reset!
+  end
+
 end
-- 
1.7.7.6




More information about the aeolus-devel mailing list