Skip to content

Instantly share code, notes, and snippets.

@petrblaho
Created July 27, 2012 12:57
Show Gist options
  • Save petrblaho/3187800 to your computer and use it in GitHub Desktop.
Save petrblaho/3187800 to your computer and use it in GitHub Desktop.
rspec style compare
ProviderAccountsController
API
when requesting XML
when using admin credentials
#index
with parent provider specified
when there are 3 provider accounts for specified provider
behaves like http OK
response status code
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should be eql 200
behaves like responding with XML
response
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should have content type "application/xml"
body
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should be xml
XML body
behaves like having XML with provider accounts
list of provider accounts
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should have correct provider accounts
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should have not incorrect provider accounts
number of provider accounts
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should be eql 3
with parent provider not specified
when there are 3 provider accounts for specified provider
behaves like http OK
response status code
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should be eql 200
behaves like responding with XML
response
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
/home/pblaho/Workspace/conductor/src/spec/matchers/have_content_type.rb:25: warning: already initialized constant CONTENT_HEADER_MATCHER
should have content type "application/xml"
body
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should be xml
XML body
behaves like having XML with provider accounts
list of provider accounts
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should have correct provider accounts
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should have not incorrect provider accounts
number of provider accounts
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
should be eql 6
#show
when requested provider account exists
behaves like http OK
response status code
should be eql 200
behaves like responding with XML
response
/home/pblaho/Workspace/conductor/src/spec/matchers/have_content_type.rb:25: warning: already initialized constant CONTENT_HEADER_MATCHER
should have content type "application/xml"
body
should be xml
XML body
should have correct provider account
credentials
of mock provider
behaves like having correct set of credentials
should be correct
of ec2 provider
behaves like having correct set of credentials
should be correct
when requested provider account does not exist
behaves like http Not Found
response status code
should be eql 404
behaves like responding with XML
response
/home/pblaho/Workspace/conductor/src/spec/matchers/have_content_type.rb:25: warning: already initialized constant CONTENT_HEADER_MATCHER
should have content type "application/xml"
body
should be xml
XML body
should be eql "Couldn't find ProviderAccount with ID=1" (FAILED - 1)
Failures:
1) ProviderAccountsController API when requesting XML when using admin credentials#show when requested provider account does not exist XML body
Failure/Error: subject.xpath('//error/message').text.should be_eql("Couldn't find ProviderAccount with ID=1")
expected eql?("Couldn't find ProviderAccount with ID=1") to return true, got false
# ./spec/controllers/provider_accounts_controller_spec.rb:313
Finished in 16.43 seconds
22 examples, 1 failure
Failed examples:
rspec ./spec/controllers/provider_accounts_controller_spec.rb:310 # ProviderAccountsController API when requesting XML when using admin credentials#show when requested provider account does not exist XML body
ProviderAccountsController
API
when requesting XML
when using admin credentials
#index
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
selecting parent provider id should return 3 provider accounts
DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:xml] instead. (called from realtime at /usr/lib/ruby/1.8/benchmark.rb:308)
/home/pblaho/Workspace/conductor-tests/src/spec/matchers/have_content_type.rb:25: warning: already initialized constant CONTENT_HEADER_MATCHER
not selecting parent provider id should return all provider accounts (6 of them)
#show
/home/pblaho/Workspace/conductor-tests/src/spec/matchers/have_content_type.rb:25: warning: already initialized constant CONTENT_HEADER_MATCHER
show mock provider account
/home/pblaho/Workspace/conductor-tests/src/spec/matchers/have_content_type.rb:25: warning: already initialized constant CONTENT_HEADER_MATCHER
show ec2 provider account
/home/pblaho/Workspace/conductor-tests/src/spec/matchers/have_content_type.rb:25: warning: already initialized constant CONTENT_HEADER_MATCHER
show a provider account that does not exist (FAILED - 1)
Failures:
1) ProviderAccountsController API when requesting XML when using admin credentials#show show a provider account that does not exist
Failure/Error: subject.xpath('//error/message').text.should be_eql("Couldn't find ProviderAccount with ID=id_that_does_not_exist")
expected eql?("Couldn't find ProviderAccount with ID=id_that_does_not_exist") to return true, got false
# ./spec/controllers/provider_accounts_controller_spec.rb:227
Finished in 4.45 seconds
5 examples, 1 failure
Failed examples:
rspec ./spec/controllers/provider_accounts_controller_spec.rb:218 # ProviderAccountsController API when requesting XML when using admin credentials#show show a provider account that does not exist
#
# Copyright 2011 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'spec_helper'
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
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
end
end
context "UI" do
fixtures :all
before(:each) do
@tuser = FactoryGirl.create :tuser
@provider_account = FactoryGirl.create :mock_provider_account
@provider = @provider_account.provider
@admin_permission = Permission.create :role => Role.find(:first, :conditions => ['name = ?', 'base.provider.admin']),
:permission_object => @provider,
:entity => FactoryGirl.create(:provider_admin_user).entity
@admin = @admin_permission.user
end
it "shows provider accounts as XML list" do
mock_warden(@admin)
get :index, :format => :xml
response.should be_success
# it should have not provider accounts credentials
resp = Hash.from_xml(response.body)
resp['provider_accounts']['provider_account']['provider_credentials'].should be_nil
end
it "doesn't allow to save provider's account if not valid credentials" do
mock_warden(@admin)
post :create, :provider_account => {:provider_id => @provider.id, :credentials_hash => {}}, :provider_id => @provider.id
response.should be_success
response.should render_template("new")
request.flash[:error].should == "Cannot add the provider account."
end
it "should permit users with account modify permission to access edit cloud account interface" do
mock_warden(@admin)
get :edit, :provider_id => @provider, :id => @provider_account.id
response.should be_success
response.should render_template("edit")
end
it "should allow users with account modify password to update a cloud account" do
mock_warden(@admin)
@provider_account.credentials_hash = {:username => 'mockuser2', :password => "foobar"}
@provider_account.stub!(:valid_credentials?).and_return(true)
@provider_account.quota = Quota.new
@provider_account.save.should be_true
post :update, :id => @provider_account.id, :provider_account => { :credentials_hash => {:username => 'mockuser', :password => 'mockpassword'} }
response.should redirect_to edit_provider_path(@provider_account.provider_id, :details_tab => 'accounts')
ProviderAccount.find(@provider_account.id).credentials_hash['password'].should == "mockpassword"
end
it "should allow users with account modify permission to delete a cloud account" do
mock_warden(@admin)
lambda do
post :multi_destroy, :provider_id => @provider_account.provider_id, :accounts_selected => [@provider_account.id]
end.should change(ProviderAccount, :count).by(-1)
response.should redirect_to edit_provider_path(@provider_account.provider_id, :details_tab => 'accounts')
ProviderAccount.find_by_id(@provider_account.id).should be_nil
end
describe "should deny access to users without account modify permission" do
before do
mock_warden(@tuser)
end
it "for edit" do
get :edit, :provider_id => @provider_account.provider_id, :id => @provider_account.id
response.should render_template('layouts/error')
end
it "for update" do
post :update, :id => @provider_account.id, :provider_account => { :password => 'foobar' }
response.should render_template('layouts/error')
end
it "for destroy" do
post :destroy, :id => @provider_account.id
response.should render_template('layouts/error')
end
end
it "should provide ui to create new account" do
mock_warden(@admin)
get :new, :provider_id => @provider.id
response.should be_success
response.should render_template("new")
end
it "should fail to grant access to account UIs for unauthenticated user" do
mock_warden(nil)
get :new
response.should_not be_success
end
end # UI
context "API" do
context "when requesting XML" do
before(:each) do
accept_xml
end
context "when using admin credentials" do
before(:each) do
user = FactoryGirl.create(:admin_permission).user
mock_warden(user)
end
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
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
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
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
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
it_behaves_like "http Not Found"
it_behaves_like "responding with XML"
context "XML body" do
subject { Nokogiri::XML(response.body) }
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
end
end # #show
end
end
end
end
#
# Copyright 2011 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'spec_helper'
describe ProviderAccountsController do
render_views
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
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
context "UI" do
fixtures :all
before(:each) do
@tuser = FactoryGirl.create :tuser
@provider_account = FactoryGirl.create :mock_provider_account
@provider = @provider_account.provider
@admin_permission = Permission.create :role => Role.find(:first, :conditions => ['name = ?', 'base.provider.admin']),
:permission_object => @provider,
:entity => FactoryGirl.create(:provider_admin_user).entity
@admin = @admin_permission.user
end
it "shows provider accounts as XML list" do
mock_warden(@admin)
get :index, :format => :xml
response.should be_success
# it should have not provider accounts credentials
resp = Hash.from_xml(response.body)
resp['provider_accounts']['provider_account']['provider_credentials'].should be_nil
end
it "doesn't allow to save provider's account if not valid credentials" do
mock_warden(@admin)
post :create, :provider_account => {:provider_id => @provider.id, :credentials_hash => {}}, :provider_id => @provider.id
response.should be_success
response.should render_template("new")
request.flash[:error].should == "Cannot add the provider account."
end
it "should permit users with account modify permission to access edit cloud account interface" do
mock_warden(@admin)
get :edit, :provider_id => @provider, :id => @provider_account.id
response.should be_success
response.should render_template("edit")
end
it "should allow users with account modify password to update a cloud account" do
mock_warden(@admin)
@provider_account.credentials_hash = {:username => 'mockuser2', :password => "foobar"}
@provider_account.stub!(:valid_credentials?).and_return(true)
@provider_account.quota = Quota.new
@provider_account.save.should be_true
post :update, :id => @provider_account.id, :provider_account => { :credentials_hash => {:username => 'mockuser', :password => 'mockpassword'} }
response.should redirect_to edit_provider_path(@provider_account.provider_id, :details_tab => 'accounts')
ProviderAccount.find(@provider_account.id).credentials_hash['password'].should == "mockpassword"
end
it "should allow users with account modify permission to delete a cloud account" do
mock_warden(@admin)
lambda do
post :multi_destroy, :provider_id => @provider_account.provider_id, :accounts_selected => [@provider_account.id]
end.should change(ProviderAccount, :count).by(-1)
response.should redirect_to edit_provider_path(@provider_account.provider_id, :details_tab => 'accounts')
ProviderAccount.find_by_id(@provider_account.id).should be_nil
end
describe "should deny access to users without account modify permission" do
before do
mock_warden(@tuser)
end
it "for edit" do
get :edit, :provider_id => @provider_account.provider_id, :id => @provider_account.id
response.should render_template('layouts/error')
end
it "for update" do
post :update, :id => @provider_account.id, :provider_account => { :password => 'foobar' }
response.should render_template('layouts/error')
end
it "for destroy" do
post :destroy, :id => @provider_account.id
response.should render_template('layouts/error')
end
end
it "should provide ui to create new account" do
mock_warden(@admin)
get :new, :provider_id => @provider.id
response.should be_success
response.should render_template("new")
end
it "should fail to grant access to account UIs for unauthenticated user" do
mock_warden(nil)
get :new
response.should_not be_success
end
end # UI
context "API" do
context "when requesting XML" do
before(:each) do
accept_xml
end
context "when using admin credentials" do
before(:each) do
user = FactoryGirl.create(:admin_permission).user
mock_warden(user)
end
describe "#index" do
before(:each) do
# call these to create them in database
other_provider = FactoryGirl.create(:mock_provider)
@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
@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
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
before(:each) do
@mock_provider_account = FactoryGirl.create(:mock_provider_account)
@ec2_provider_account = FactoryGirl.create(:ec2_provider_account)
end
it "show mock provider account" do
get :show, :id => @mock_provider_account.id
validate_show_provider_account_XML(response, @mock_provider_account)
end
it "show ec2 provider account" do
get :show, :id => @ec2_provider_account.id
validate_show_provider_account_XML(response, @ec2_provider_account)
end
it "show a provider account that does not exist" do
get :show, :id => "id_that_does_not_exist"
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
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment