>From 81c64d047d9a1a505ca763b94d9d7668efae9afb Mon Sep 17 00:00:00 2001
From: Richard Su <rwsu@redhat.com>
Date: Mon, 23 Jul 2012 17:34:13 -0700
Subject: [PATCH conductor] Refactor provider account api index and show tests

Moved mocked objects into begin blocks. Reduced the number of times begin blocks
are called by eliminating unncessary let and it.
---
 .../provider_accounts_controller_spec.rb           |  230 ++++++--------------
 1 files changed, 71 insertions(+), 159 deletions(-)

diff --git a/src/spec/controllers/provider_accounts_controller_spec.rb b/src/spec/controllers/provider_accounts_controller_spec.rb
index 1f40c75..99d32ab 100644
--- a/src/spec/controllers/provider_accounts_controller_spec.rb
+++ b/src/spec/controllers/provider_accounts_controller_spec.rb
@@ -20,37 +20,35 @@ describe ProviderAccountsController do
 
   render_views
 
-  shared_examples_for "having XML with provider accounts" do
-    # TODO: implement more attributes checks
-    subject { Nokogiri::XML(response.body) }
-    context "list of provider accounts" do
-      let(:xml_provider_accounts) { subject.xpath('//provider_accounts/provider_account') }
-      context "number of provider accounts" do
-        it { xml_provider_accounts.size.should be_eql(number_of_provider_accounts) }
-      end
-      it "should have correct provider accounts" do
-        provider_accounts.each do |provider_account|
-          xml_provider_account = xml_provider_accounts.xpath("//provider_account[@id=\"#{provider_account.id}\"]")
-          xml_provider_account.xpath('name').text.should be_eql(provider_account.name.to_s)
-          xml_provider_account.xpath('@href').text.should be_eql(api_provider_account_url(provider_account))
-        end
-      end
-      it "should have not incorrect provider accounts" do
-        other_provider_accounts.each do |provider_account|
-          xml_provider_account = xml_provider_accounts.xpath("//provider_account[@id=\"#{provider_account.id}\"]")
-          xml_provider_account.should be_empty
-        end
-      end
+  def validate_provider_accounts_XML(response, expected_number_of_accounts, provider_accounts, other_provider_accounts)
+    response.status.should be_eql(200)
+    response.should have_content_type("application/xml")
+    response.body.should be_xml
+    subject = Nokogiri::XML(response.body)
+    xml_provider_accounts = subject.xpath('//provider_accounts/provider_account')
+    xml_provider_accounts.size.should == expected_number_of_accounts
+    provider_accounts.each do |provider_account|
+      xml_provider_account = xml_provider_accounts.xpath("//provider_account[@id=\"#{provider_account.id}\"]")
+      xml_provider_account.xpath('name').text.should be_eql(provider_account.name.to_s)
+      xml_provider_account.xpath('@href').text.should be_eql(api_provider_account_url(provider_account))
+    end
+    other_provider_accounts.each do |provider_account|
+      xml_provider_account = xml_provider_accounts.xpath("//provider_account[@id=\"#{provider_account.id}\"]")
+      xml_provider_account.should be_empty
     end
   end
 
-  shared_examples_for "having correct set of credentials" do
-    it "should be correct" do
-      provider_account.credentials.each do |credential|
-        label = credential.credential_definition.label
-        value = credential.value
-        xml_provider_account.xpath('//' + label).text.should be_eql(value)
-      end
+  def validate_show_provider_account_XML(response, provider_account)
+    response.status.should be_eql(200)
+    response.should have_content_type("application/xml")
+    response.body.should be_xml
+    body = Nokogiri::XML(response.body)
+    xml_provider_account= body.xpath("//provider_account[@id=\"#{provider_account.id}\"]")
+    xml_provider_account.xpath('@href').text.should be_eql(api_provider_account_url(provider_account))
+    provider_account.credentials.each do |credential|
+      label = credential.credential_definition.label
+      value = credential.value
+      xml_provider_account.xpath('//' + label).text.should be_eql(value)
     end
   end
 
@@ -166,153 +164,67 @@ describe ProviderAccountsController do
 
         describe "#index" do
 
-          context "with parent provider specified" do
-
-            before(:each) do
-              # call these to create them in database
-              provider_accounts
-              other_provider_accounts
-
-              get :index, :provider_id => provider.id
-            end
+          before(:each) do
+            # call these to create them in database
+            other_provider = FactoryGirl.create(:mock_provider)
 
-            let(:provider) { FactoryGirl.create(:mock_provider) }
-            let(:other_provider) { FactoryGirl.create(:mock_provider) }
-            let(:other_provider_accounts) do
-              3.times do
-                pa = FactoryGirl.build(:mock_provider_account_seq, :provider => other_provider)
-                pa.stub!(:validate_credentials).and_return(true)
-                pa.save
-              end
-              other_provider.provider_accounts
+            @other_provider_accounts = Array.new
+            3.times do
+              pa = FactoryGirl.build(:mock_provider_account_seq, :provider => other_provider)
+              pa.stub!(:validate_credentials).and_return(true)
+              pa.save
+              @other_provider_accounts << pa
             end
 
-            context "when there are 3 provider accounts for specified provider" do
-
-              let(:provider_accounts) do
-                3.times do
-                  pa = FactoryGirl.build(:mock_provider_account_seq, :provider => provider)
-                  pa.stub!(:validate_credentials).and_return(true)
-                  pa.save
-                end
-                provider.provider_accounts
-              end
-
-              it_behaves_like "http OK"
-              it_behaves_like "responding with XML"
-
-              context "XML body" do
-                let(:number_of_provider_accounts) { 3 }
-                it_behaves_like "having XML with provider accounts"
-              end
+            @provider = FactoryGirl.create(:mock_provider)
+            @provider_accounts = Array.new
+            3.times do
+              pa = FactoryGirl.build(:mock_provider_account_seq, :provider => @provider)
+              pa.stub!(:validate_credentials).and_return(true)
+              pa.save
+              @provider_accounts << pa
             end
 
           end
 
-          context "with parent provider not specified" do
-
-            before(:each) do
-              # call these to create them in database
-              provider_accounts
-              other_provider_accounts
-
-              get :index
-            end
-
-            let(:provider) { FactoryGirl.create(:mock_provider) }
-            let(:other_provider) { FactoryGirl.create(:mock_provider) }
-            let(:other_provider_accounts) { [] }
-
-            context "when there are 3 provider accounts for specified provider" do
-
-              let(:provider_accounts) do
-                3.times do
-                  pa = FactoryGirl.build(:mock_provider_account_seq, :provider => provider)
-                  pa.stub!(:validate_credentials).and_return(true)
-                  pa.save
-                end
-
-                3.times do
-                  pa = FactoryGirl.build(:mock_provider_account_seq, :provider => other_provider)
-                  pa.stub!(:validate_credentials).and_return(true)
-                  pa.save
-                end
-                other_provider.provider_accounts + provider.provider_accounts
-              end
-
-              it_behaves_like "http OK"
-              it_behaves_like "responding with XML"
-
-              context "XML body" do
-                let(:number_of_provider_accounts) { 6 }
-                it_behaves_like "having XML with provider accounts"
-              end
-            end
+          it "selecting parent provider id should return 3 provider accounts" do
+            get :index, :provider_id => @provider.id
+            validate_provider_accounts_XML(response, 3, @provider_accounts, @other_provider_accounts)
+          end
 
+          it "not selecting parent provider id should return all provider accounts (6 of them)" do
+            get :index
+            validate_provider_accounts_XML(response, 6, @provider_accounts + @other_provider_accounts, Array.new)
           end
+
         end # #index
 
         describe "#show" do
-          context "when requested provider account exists" do
-
-            before(:each) do
-              ProviderAccount.stub(:find).and_return(provider_account)
-
-              get :show, :id => 1
-            end
-
-            let(:provider_account) { FactoryGirl.create(:mock_provider_account); ProviderAccount.last }
-
-            it_behaves_like "http OK"
-            it_behaves_like "responding with XML"
-
-            context "XML body" do
-              # TODO: implement more attributes checks
-              #subject { Hash.from_xml(response.body) }
-              subject { Nokogiri::XML(response.body) }
-              let(:xml_provider_account) { subject.xpath("//provider_account[@id=\"#{provider_account.id}\"]") }
-              it "should have correct provider account" do
-                xml_provider_account.xpath('@href').text.should be_eql(api_provider_account_url(provider_account))
-              end
-              context "credentials" do
-
-                context "of mock provider" do
-                  let(:provider_account) { FactoryGirl.create(:mock_provider_account); ProviderAccount.last }
-
-                  it_behaves_like "having correct set of credentials"
-                end
-
-                context "of ec2 provider" do
-                  let(:provider_account) { FactoryGirl.create(:ec2_provider_account); ProviderAccount.last }
-
-                  it_behaves_like "having correct set of credentials"
-                end
-              end
-            end
-
-          end # when requested provider account exists
-
-          context "when requested provider account does not exist" do
-
-            before(:each) do
-              pa = ProviderAccount.find_by_id(1)
-              pa.delete if pa
-              get :show, :id => 1
-            end
+          before(:each) do
+            @mock_provider_account = FactoryGirl.create(:mock_provider_account)
+            @ec2_provider_account = FactoryGirl.create(:ec2_provider_account)
+          end
 
-            it_behaves_like "http Not Found"
-            it_behaves_like "responding with XML"
+          it "show mock provider account" do
+            get :show, :id => @mock_provider_account.id
+            validate_show_provider_account_XML(response, @mock_provider_account)
+          end
 
-            context "XML body" do
+          it "show ec2 provider account" do
+            get :show, :id => @ec2_provider_account.id
+            validate_show_provider_account_XML(response, @ec2_provider_account)
+          end
 
-              subject { Nokogiri::XML(response.body) }
+          it "show a provider account that does not exist" do
+            get :show, :id => "id_that_does_not_exist"
 
-              it {
-                subject.xpath('//error').size.should be_eql(1)
-                subject.xpath('//error/code').text.should be_eql('RecordNotFound')
-                subject.xpath('//error/message').text.should be_eql("Couldn't find ProviderAccount with ID=1")
-              }
-            end
+            response.status.should be_eql(404)
+            response.should have_content_type("application/xml")
+            response.body.should be_xml
+            subject = Nokogiri::XML(response.body)
+            subject.xpath('//error').size.should be_eql(1)
+            subject.xpath('//error/code').text.should be_eql('RecordNotFound')
+            subject.xpath('//error/message').text.should be_eql("Couldn't find ProviderAccount with ID=id_that_does_not_exist")
           end
         end # #show
       end
-- 
1.7.7.6

