[PATCH conductor 2/3] Rspec tests for Provider Accounts API - update

pblaho at redhat.com pblaho at redhat.com
Wed Aug 15 22:08:05 UTC 2012


From: Petr Blaho <pblaho at redhat.com>

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

Adds rspec tests for Provider Accounts API - update
---
 src/spec/requests/api/provider_accounts_spec.rb |  165 +++++++++++++++++++++++
 src/spec/spec_helper.rb                         |    6 +
 2 files changed, 171 insertions(+), 0 deletions(-)
 create mode 100644 src/spec/requests/api/provider_accounts_spec.rb

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..db30ed0
--- /dev/null
+++ b/src/spec/requests/api/provider_accounts_spec.rb
@@ -0,0 +1,165 @@
+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 "PUT /api/provider_accounts/:provider_account_id" do
+    before(:each) do
+      put "/api/provider_accounts/#{provider_account.id}", xml, headers
+    end
+
+    context "for existing Provider Account" do
+      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) }
+          let(:provider_account) { FactoryGirl.create(:mock_provider_account, :provider => 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/name').size.should be_eql(1)
+              subject.xpath('//provider_account/name').text.should be_eql("mockaccount")
+              subject.xpath('//provider_account/mock_credentials').size.should be_eql(1)
+              subject.xpath('//provider_account/mock_credentials/username').size.should be_eql(1)
+              subject.xpath('//provider_account/mock_credentials/username').text.should be_eql("mockuser")
+              subject.xpath('//provider_account/mock_credentials/password').size.should be_eql(1)
+              subject.xpath('//provider_account/mock_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) }
+          let(:provider_account) { FactoryGirl.create(:ec2_provider_account, :provider => 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/name').size.should be_eql(1)
+              subject.xpath('//provider_account/name').text.should be_eql("ec2account")
+              subject.xpath('//provider_account/ec2_credentials').size.should be_eql(1)
+              subject.xpath('//provider_account/ec2_credentials/access_key').size.should be_eql(1)
+              subject.xpath('//provider_account/ec2_credentials/access_key').text.should be_eql("ec2user")
+              subject.xpath('//provider_account/ec2_credentials/account_number').size.should be_eql(1)
+              subject.xpath('//provider_account/ec2_credentials/account_number').text.should be_eql("ec2account_id")
+              subject.xpath('//provider_account/ec2_credentials/secret_access_key').size.should be_eql(1)
+              subject.xpath('//provider_account/ec2_credentials/secret_access_key').text.should be_eql("ec2password")
+              subject.xpath('//provider_account/ec2_credentials/key').size.should be_eql(1)
+              subject.xpath('//provider_account/ec2_credentials/key').text.should be_eql("ec2x509private")
+              subject.xpath('//provider_account/ec2_credentials/certificate').size.should be_eql(1)
+              subject.xpath('//provider_account/ec2_credentials/certificate').text.should be_eql("ec2x509public")
+            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) }
+          let(:provider_account) { FactoryGirl.create(:mock_provider_account, :provider => 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>
+                         <label></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) }
+          let(:provider_account) { FactoryGirl.create(:ec2_provider_account, :provider => 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
+
+    context "for non existing Provider Account" do
+
+      let(:provider_account) { provider_account = FactoryGirl.create(:mock_provider_account); ProviderAccount.delete(provider_account.id); provider_account }
+      let(:xml) { "" }
+
+      it_behaves_like "http Not Found"
+      it_behaves_like "responding with XML"
+
+      context "XML body" do
+        subject { Nokogiri::XML(response.body) }
+
+        it "have RecordNotFound error message" do
+          subject.xpath('//error').size.should be_eql(1)
+          subject.xpath('//error/code').text.should be_eql('RecordNotFound')
+        end
+      end
+
+    end
+  end
+end
diff --git a/src/spec/spec_helper.rb b/src/spec/spec_helper.rb
index 1149fcf..30ab1d6 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