Commit db39710f8a591f6b7861c19de1dbf9c0cc6f1b92

Authored by Rodrigo Souto
1 parent 9f455fa1

Functional and unit tests

Also some little fixes
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
... ... @@ -27,7 +27,7 @@ class ShoppingCartPluginProfileController < ProfileController
27 27  
28 28 def remove
29 29 id = params[:id].to_i
30   - if validate_cart_has_product(id)
  30 + if validate_cart_presence && validate_cart_has_product(id)
31 31 session[:cart][:items].delete(id)
32 32 session[:cart] = nil if session[:cart][:items].empty?
33 33 render :text => {
... ...
plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_myprofile_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class ShoppingCartPluginMyprofileController; def rescue_action(e) raise e end; end
  6 +
  7 +class ShoppingCartPluginMyprofileControllerTest < Test::Unit::TestCase
  8 +
  9 + def setup
  10 + @controller = ShoppingCartPluginMyprofileController.new
  11 + @request = ActionController::TestRequest.new
  12 + @response = ActionController::TestResponse.new
  13 + @enterprise = fast_create(Enterprise)
  14 + @admin = create_user('admin').person
  15 + @enterprise.add_admin(@admin)
  16 + login_as(@admin.identifier)
  17 + end
  18 + attr_reader :enterprise
  19 +
  20 + should 'be able to enable shopping cart' do
  21 + enterprise.shopping_cart = false
  22 + enterprise.save
  23 + post :edit, :profile => enterprise.identifier, :shopping_cart => '1'
  24 + enterprise.reload
  25 +
  26 + assert enterprise.shopping_cart
  27 + end
  28 +
  29 + should 'be able to disable shopping cart' do
  30 + enterprise.shopping_cart = true
  31 + enterprise.save
  32 + post :edit, :profile => enterprise.identifier, :shopping_cart => '0'
  33 + enterprise.reload
  34 +
  35 + assert !enterprise.shopping_cart
  36 + end
  37 +end
... ...
plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,180 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_profile_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class ShoppingCartPluginProfileController; def rescue_action(e) raise e end; end
  6 +
  7 +class ShoppingCartPluginProfileControllerTest < Test::Unit::TestCase
  8 +
  9 + def setup
  10 + @controller = ShoppingCartPluginProfileController.new
  11 + @request = ActionController::TestRequest.new
  12 + @response = ActionController::TestResponse.new
  13 + @enterprise = fast_create(Enterprise)
  14 + @product = fast_create(Product, :enterprise_id => @enterprise.id)
  15 + end
  16 + attr_reader :enterprise
  17 + attr_reader :product
  18 +
  19 + should 'add a new product to cart' do
  20 + get :add, :profile => enterprise.identifier, :id => product.id
  21 +
  22 + assert product_in_cart?(product)
  23 + assert_equal 1, product_quantity(product)
  24 + end
  25 +
  26 + should 'grow quantity through add' do
  27 + get :add, :profile => enterprise.identifier, :id => product.id
  28 + assert_equal 1, product_quantity(product)
  29 +
  30 + get :add, :profile => enterprise.identifier, :id => product.id
  31 + assert_equal 2, product_quantity(product)
  32 + end
  33 +
  34 + should 'not add product to cart if it does not exists' do
  35 + assert_nothing_raised { get :add, :profile => enterprise.identifier, :id => 9999 }
  36 +
  37 + assert !product_in_cart?(product)
  38 + assert !response_ok?
  39 + assert 3, reponse_error_code
  40 + end
  41 +
  42 + should 'remove cart if the product being removed is the last one' do
  43 + get :add, :profile => enterprise.identifier, :id => product.id
  44 + assert cart?
  45 +
  46 + get :remove, :profile => enterprise.identifier, :id => product.id
  47 + assert !cart?
  48 + end
  49 +
  50 + should 'not try to remove a product if there is no cart' do
  51 + instantiate_session
  52 + assert !cart?
  53 +
  54 + assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 }
  55 + assert !response_ok?
  56 + assert_equal 2, reponse_error_code
  57 + end
  58 +
  59 + should 'just remove product if there are other products on cart' do
  60 + another_product = fast_create(Product, :enterprise_id => enterprise.id)
  61 + get :add, :profile => enterprise.identifier, :id => product.id
  62 + get :add, :profile => enterprise.identifier, :id => another_product.id
  63 +
  64 + get :remove, :profile => enterprise.identifier, :id => product.id
  65 + assert cart?
  66 + assert !product_in_cart?(product)
  67 + end
  68 +
  69 + should 'not try to remove a product that is not in the cart' do
  70 + get :add, :profile => enterprise.identifier, :id => product.id
  71 + assert cart?
  72 + assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 }
  73 +
  74 + assert !response_ok?
  75 + assert_equal 4, reponse_error_code
  76 + end
  77 +
  78 + should 'not try to list the cart if there is no cart' do
  79 + instantiate_session
  80 + assert !cart?
  81 +
  82 + assert_nothing_raised { get :list, :profile => enterprise.identifier }
  83 + assert !response_ok?
  84 + assert_equal 2, reponse_error_code
  85 + end
  86 +
  87 + should 'list products without errors' do
  88 + get :add, :profile => enterprise.identifier, :id => product.id
  89 +
  90 + assert_nothing_raised { get :list, :profile => enterprise.identifier }
  91 + assert response_ok?
  92 + end
  93 +
  94 + should 'update the quantity of a product' do
  95 + get :add, :profile => enterprise.identifier, :id => product.id
  96 + assert 1, product_quantity(product)
  97 +
  98 + get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 3
  99 + assert 3, product_quantity(product)
  100 + end
  101 +
  102 + should 'not try to update quantity the quantity of a product if there is no cart' do
  103 + instantiate_session
  104 + assert !cart?
  105 +
  106 + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 }
  107 + assert !response_ok?
  108 + assert_equal 2, reponse_error_code
  109 + end
  110 +
  111 + should 'not try to update the quantity of a product that is not in the cart' do
  112 + get :add, :profile => enterprise.identifier, :id => product.id
  113 + assert cart?
  114 + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 }
  115 +
  116 + assert !response_ok?
  117 + assert_equal 4, reponse_error_code
  118 + end
  119 +
  120 + should 'not update the quantity of a product with a invalid value' do
  121 + get :add, :profile => enterprise.identifier, :id => product.id
  122 +
  123 + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => -1}
  124 + assert !response_ok?
  125 + assert_equal 5, reponse_error_code
  126 +
  127 + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 'asdf'}
  128 + assert !response_ok?
  129 + assert_equal 5, reponse_error_code
  130 + end
  131 +
  132 + should 'clean the cart' do
  133 + another_product = fast_create(Product, :enterprise_id => enterprise.id)
  134 + get :add, :profile => enterprise.identifier, :id => product.id
  135 + get :add, :profile => enterprise.identifier, :id => another_product.id
  136 +
  137 + assert_nothing_raised { get :clean, :profile => enterprise.identifier }
  138 + assert !cart?
  139 + end
  140 +
  141 + should 'not crash if there is no cart' do
  142 + instantiate_session
  143 + assert !cart?
  144 + assert_nothing_raised { get :clean, :profile => enterprise.identifier }
  145 + end
  146 +
  147 + private
  148 +
  149 + def json_response
  150 + ActiveSupport::JSON.decode @response.body
  151 + end
  152 +
  153 + def cart?
  154 + !session[:cart].nil?
  155 + end
  156 +
  157 + def product_in_cart?(product)
  158 + session[:cart][:items].has_key?(product.id)
  159 + end
  160 +
  161 + def product_quantity(product)
  162 + session[:cart][:items][product.id]
  163 + end
  164 +
  165 + def response_ok?
  166 + json_response['ok']
  167 + end
  168 +
  169 + def reponse_error_code
  170 + json_response['error']['code']
  171 + end
  172 +
  173 + # temporary hack...if I don't do this the session stays as an Array instead
  174 + # of a TestSession
  175 + def instantiate_session
  176 + get :add, :profile => enterprise.identifier, :id => product.id
  177 + get :remove, :profile => enterprise.identifier, :id => product.id
  178 + end
  179 +
  180 +end
... ...
plugins/shopping_cart/test/unit/shopping_cart_plugin/cart_helper_test.rb 0 → 100644
... ... @@ -0,0 +1,38 @@
  1 +require File.dirname(__FILE__) + '/../../../../../test/test_helper'
  2 +
  3 +class ShoppingCartPlugin::CartHelperTest < Test::Unit::TestCase
  4 +
  5 + include ShoppingCartPlugin::CartHelper
  6 +
  7 + def setup
  8 + @product = mock()
  9 + @product.stubs(:name).returns('Sample')
  10 + @product.stubs(:price).returns(nil)
  11 + @product.stubs(:discount).returns(nil)
  12 + end
  13 +
  14 + attr_reader :product
  15 +
  16 + should 'return 0 on sell price if the product have no price' do
  17 + assert_equal 0, sell_price(product)
  18 + end
  19 +
  20 + should 'return the price of the product on sell price if there is no discount' do
  21 + price = 5.73
  22 + product.stubs(:price).returns(price)
  23 +
  24 + assert_equal price, sell_price(product)
  25 + end
  26 +
  27 + should 'return the price with discount on sell price if there is a discount' do
  28 + price = 5.73
  29 + discount = 1
  30 + product.stubs(:price).returns(price)
  31 + product.stubs(:discount).returns(discount)
  32 + product.stubs(:price_with_discount).returns(price-discount)
  33 +
  34 + assert_equal price-discount, sell_price(product)
  35 + end
  36 +
  37 +end
  38 +
... ...
plugins/shopping_cart/test/unit/shopping_cart_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +class ShoppingCartPluginTest < Test::Unit::TestCase
  4 +
  5 + def setup
  6 + @shopping_cart = ShoppingCartPlugin.new
  7 + @context = mock()
  8 + @profile = mock()
  9 + @profile.stubs(:identifier).returns('random-user')
  10 + @context.stubs(:profile).returns(@profile)
  11 + @shopping_cart.context = @context
  12 + @shopping_cart.stubs(:profile).returns(@profile)
  13 + end
  14 +
  15 + attr_reader :shopping_cart
  16 + attr_reader :context
  17 +
  18 + should 'return true to stylesheet' do
  19 + assert shopping_cart.stylesheet?
  20 + end
  21 +
  22 +end
... ...