From db39710f8a591f6b7861c19de1dbf9c0cc6f1b92 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Fri, 6 May 2011 16:01:16 -0300 Subject: [PATCH] Functional and unit tests --- plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb | 2 +- plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb | 37 +++++++++++++++++++++++++++++++++++++ plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/shopping_cart/test/unit/shopping_cart_plugin/cart_helper_test.rb | 38 ++++++++++++++++++++++++++++++++++++++ plugins/shopping_cart/test/unit/shopping_cart_plugin_test.rb | 22 ++++++++++++++++++++++ 5 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb create mode 100644 plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb create mode 100644 plugins/shopping_cart/test/unit/shopping_cart_plugin/cart_helper_test.rb create mode 100644 plugins/shopping_cart/test/unit/shopping_cart_plugin_test.rb diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb index 617eda0..cbb7859 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb @@ -27,7 +27,7 @@ class ShoppingCartPluginProfileController < ProfileController def remove id = params[:id].to_i - if validate_cart_has_product(id) + if validate_cart_presence && validate_cart_has_product(id) session[:cart][:items].delete(id) session[:cart] = nil if session[:cart][:items].empty? render :text => { diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb new file mode 100644 index 0000000..c882bbd --- /dev/null +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb @@ -0,0 +1,37 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_myprofile_controller' + +# Re-raise errors caught by the controller. +class ShoppingCartPluginMyprofileController; def rescue_action(e) raise e end; end + +class ShoppingCartPluginMyprofileControllerTest < Test::Unit::TestCase + + def setup + @controller = ShoppingCartPluginMyprofileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @enterprise = fast_create(Enterprise) + @admin = create_user('admin').person + @enterprise.add_admin(@admin) + login_as(@admin.identifier) + end + attr_reader :enterprise + + should 'be able to enable shopping cart' do + enterprise.shopping_cart = false + enterprise.save + post :edit, :profile => enterprise.identifier, :shopping_cart => '1' + enterprise.reload + + assert enterprise.shopping_cart + end + + should 'be able to disable shopping cart' do + enterprise.shopping_cart = true + enterprise.save + post :edit, :profile => enterprise.identifier, :shopping_cart => '0' + enterprise.reload + + assert !enterprise.shopping_cart + end +end diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb new file mode 100644 index 0000000..ad1bb4e --- /dev/null +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb @@ -0,0 +1,180 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_profile_controller' + +# Re-raise errors caught by the controller. +class ShoppingCartPluginProfileController; def rescue_action(e) raise e end; end + +class ShoppingCartPluginProfileControllerTest < Test::Unit::TestCase + + def setup + @controller = ShoppingCartPluginProfileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @enterprise = fast_create(Enterprise) + @product = fast_create(Product, :enterprise_id => @enterprise.id) + end + attr_reader :enterprise + attr_reader :product + + should 'add a new product to cart' do + get :add, :profile => enterprise.identifier, :id => product.id + + assert product_in_cart?(product) + assert_equal 1, product_quantity(product) + end + + should 'grow quantity through add' do + get :add, :profile => enterprise.identifier, :id => product.id + assert_equal 1, product_quantity(product) + + get :add, :profile => enterprise.identifier, :id => product.id + assert_equal 2, product_quantity(product) + end + + should 'not add product to cart if it does not exists' do + assert_nothing_raised { get :add, :profile => enterprise.identifier, :id => 9999 } + + assert !product_in_cart?(product) + assert !response_ok? + assert 3, reponse_error_code + end + + should 'remove cart if the product being removed is the last one' do + get :add, :profile => enterprise.identifier, :id => product.id + assert cart? + + get :remove, :profile => enterprise.identifier, :id => product.id + assert !cart? + end + + should 'not try to remove a product if there is no cart' do + instantiate_session + assert !cart? + + assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } + assert !response_ok? + assert_equal 2, reponse_error_code + end + + should 'just remove product if there are other products on cart' do + another_product = fast_create(Product, :enterprise_id => enterprise.id) + get :add, :profile => enterprise.identifier, :id => product.id + get :add, :profile => enterprise.identifier, :id => another_product.id + + get :remove, :profile => enterprise.identifier, :id => product.id + assert cart? + assert !product_in_cart?(product) + end + + should 'not try to remove a product that is not in the cart' do + get :add, :profile => enterprise.identifier, :id => product.id + assert cart? + assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } + + assert !response_ok? + assert_equal 4, reponse_error_code + end + + should 'not try to list the cart if there is no cart' do + instantiate_session + assert !cart? + + assert_nothing_raised { get :list, :profile => enterprise.identifier } + assert !response_ok? + assert_equal 2, reponse_error_code + end + + should 'list products without errors' do + get :add, :profile => enterprise.identifier, :id => product.id + + assert_nothing_raised { get :list, :profile => enterprise.identifier } + assert response_ok? + end + + should 'update the quantity of a product' do + get :add, :profile => enterprise.identifier, :id => product.id + assert 1, product_quantity(product) + + get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 3 + assert 3, product_quantity(product) + end + + should 'not try to update quantity the quantity of a product if there is no cart' do + instantiate_session + assert !cart? + + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } + assert !response_ok? + assert_equal 2, reponse_error_code + end + + should 'not try to update the quantity of a product that is not in the cart' do + get :add, :profile => enterprise.identifier, :id => product.id + assert cart? + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } + + assert !response_ok? + assert_equal 4, reponse_error_code + end + + should 'not update the quantity of a product with a invalid value' do + get :add, :profile => enterprise.identifier, :id => product.id + + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => -1} + assert !response_ok? + assert_equal 5, reponse_error_code + + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 'asdf'} + assert !response_ok? + assert_equal 5, reponse_error_code + end + + should 'clean the cart' do + another_product = fast_create(Product, :enterprise_id => enterprise.id) + get :add, :profile => enterprise.identifier, :id => product.id + get :add, :profile => enterprise.identifier, :id => another_product.id + + assert_nothing_raised { get :clean, :profile => enterprise.identifier } + assert !cart? + end + + should 'not crash if there is no cart' do + instantiate_session + assert !cart? + assert_nothing_raised { get :clean, :profile => enterprise.identifier } + end + + private + + def json_response + ActiveSupport::JSON.decode @response.body + end + + def cart? + !session[:cart].nil? + end + + def product_in_cart?(product) + session[:cart][:items].has_key?(product.id) + end + + def product_quantity(product) + session[:cart][:items][product.id] + end + + def response_ok? + json_response['ok'] + end + + def reponse_error_code + json_response['error']['code'] + end + + # temporary hack...if I don't do this the session stays as an Array instead + # of a TestSession + def instantiate_session + get :add, :profile => enterprise.identifier, :id => product.id + get :remove, :profile => enterprise.identifier, :id => product.id + end + +end diff --git a/plugins/shopping_cart/test/unit/shopping_cart_plugin/cart_helper_test.rb b/plugins/shopping_cart/test/unit/shopping_cart_plugin/cart_helper_test.rb new file mode 100644 index 0000000..977780a --- /dev/null +++ b/plugins/shopping_cart/test/unit/shopping_cart_plugin/cart_helper_test.rb @@ -0,0 +1,38 @@ +require File.dirname(__FILE__) + '/../../../../../test/test_helper' + +class ShoppingCartPlugin::CartHelperTest < Test::Unit::TestCase + + include ShoppingCartPlugin::CartHelper + + def setup + @product = mock() + @product.stubs(:name).returns('Sample') + @product.stubs(:price).returns(nil) + @product.stubs(:discount).returns(nil) + end + + attr_reader :product + + should 'return 0 on sell price if the product have no price' do + assert_equal 0, sell_price(product) + end + + should 'return the price of the product on sell price if there is no discount' do + price = 5.73 + product.stubs(:price).returns(price) + + assert_equal price, sell_price(product) + end + + should 'return the price with discount on sell price if there is a discount' do + price = 5.73 + discount = 1 + product.stubs(:price).returns(price) + product.stubs(:discount).returns(discount) + product.stubs(:price_with_discount).returns(price-discount) + + assert_equal price-discount, sell_price(product) + end + +end + diff --git a/plugins/shopping_cart/test/unit/shopping_cart_plugin_test.rb b/plugins/shopping_cart/test/unit/shopping_cart_plugin_test.rb new file mode 100644 index 0000000..2ad1d5a --- /dev/null +++ b/plugins/shopping_cart/test/unit/shopping_cart_plugin_test.rb @@ -0,0 +1,22 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class ShoppingCartPluginTest < Test::Unit::TestCase + + def setup + @shopping_cart = ShoppingCartPlugin.new + @context = mock() + @profile = mock() + @profile.stubs(:identifier).returns('random-user') + @context.stubs(:profile).returns(@profile) + @shopping_cart.context = @context + @shopping_cart.stubs(:profile).returns(@profile) + end + + attr_reader :shopping_cart + attr_reader :context + + should 'return true to stylesheet' do + assert shopping_cart.stylesheet? + end + +end -- libgit2 0.21.2