diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb new file mode 100644 index 0000000..16ec807 --- /dev/null +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb @@ -0,0 +1,289 @@ +require 'base64' + +class ShoppingCartPluginController < ProfileController + + include ShoppingCartPlugin::CartHelper + + append_view_path File.join(File.dirname(__FILE__) + '/../views') + before_filter :login_required, :only => [] + + before_filter :login_required, :only => [] + + def get + has_products = !cart.nil? && (cart[:items].keys.size > 0) || false + config = { 'enterprise' => profile.identifier, 'hasProducts' => has_products } + render :text => config.to_json + end + + def add + self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil? + if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) + self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? + self.cart[:items][product.id] += 1 + render :text => { + :ok => true, + :error => {:code => 0}, + :products => [{ + :id => product.id, + :name => product.name, + :price => get_price(product, profile.environment), + :description => product.description, + :picture => product.default_image(:minor), + :quantity => self.cart[:items][product.id] + }] + }.to_json + end + end + + def remove + id = params[:id].to_i + if validate_cart_presence && validate_cart_has_product(id) + self.cart[:items].delete(id) + self.cart = nil if self.cart[:items].empty? + render :text => { + :ok => true, + :error => {:code => 0}, + :product_id => id + }.to_json + end + end + + def list + if validate_cart_presence + products = self.cart[:items].collect do |id, quantity| + product = Product.find(id) + { :id => product.id, + :name => product.name, + :price => get_price(product, profile.environment), + :description => product.description, + :picture => product.default_image(:minor), + :quantity => quantity + } + end + render :text => { + :ok => true, + :error => {:code => 0}, + :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier, + :products => products + }.to_json + end + end + + def update_quantity + quantity = params[:quantity].to_i + id = params[:id].to_i + if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) + product = Product.find(id) + self.cart[:items][product.id] = quantity + render :text => { + :ok => true, + :error => {:code => 0}, + :product_id => id, + :quantity => quantity + }.to_json + end + end + + def clean + self.cart = nil + render :text => { + :ok => true, + :error => {:code => 0} + }.to_json + end + + def buy + @environment = profile.environment + @cart = cart + render :layout => false + end + + def send_request + register_order(params[:customer], self.cart[:items]) + begin + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, self.cart[:items]) + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, self.cart[:items]) + render :text => { + :ok => true, + :message => _('Request sent successfully. Check your email.'), + :error => {:code => 0} + }.to_json + rescue Exception => exception + render :text => { + :ok => false, + :error => { + :code => 6, + :message => exception.message + } + }.to_json + end + end + + def visibility + render :text => self.cart.has_key?(:visibility) ? self.cart[:visibility].to_json : true.to_json + end + + def show + begin + self.cart[:visibility] = true + render :text => { + :ok => true, + :message => _('Basket displayed.'), + :error => {:code => 0} + }.to_json + rescue Exception => exception + render :text => { + :ok => false, + :error => { + :code => 7, + :message => exception.message + } + }.to_json + end + end + + def hide + begin + self.cart[:visibility] = false + render :text => { + :ok => true, + :message => _('Basket hidden.'), + :error => {:code => 0} + }.to_json + rescue Exception => exception + render :text => { + :ok => false, + :error => { + :code => 8, + :message => exception.message + } + }.to_json + end + end + + private + + def validate_same_enterprise + if profile.id != self.cart[:enterprise_id] + render :text => { + :ok => false, + :error => { + :code => 1, + :message => _("Can't join items from different enterprises.") + } + }.to_json + return false + end + true + end + + def validate_cart_presence + if self.cart.nil? + render :text => { + :ok => false, + :error => { + :code => 2, + :message => _("There is no basket.") + } + }.to_json + return false + end + true + end + + def validate_enterprise_has_product(id) + begin + product = profile.products.find(id) + rescue + render :text => { + :ok => false, + :error => { + :code => 3, + :message => _("This enterprise doesn't have this product.") + } + }.to_json + return nil + end + product + end + + def validate_cart_has_product(id) + if !self.cart[:items].has_key?(id) + render :text => { + :ok => false, + :error => { + :code => 4, + :message => _("The basket doesn't have this product.") + } + }.to_json + return false + end + true + end + + def validate_item_quantity(quantity) + if quantity.to_i < 1 + render :text => { + :ok => false, + :error => { + :code => 5, + :message => _("Invalid quantity.") + } + }.to_json + return false + end + true + end + + def register_order(custumer, items) + new_items = {} + items.each do |id, quantity| + product = Product.find(id) + price = product.price || 0 + new_items[id] = {:quantity => quantity, :price => price, :name => product.name} + end + ShoppingCartPlugin::PurchaseOrder.create!( + :seller => profile, + :customer => user, + :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED, + :products_list => new_items, + :customer_name => params[:customer][:name], + :customer_email => params[:customer][:email], + :customer_contact_phone => params[:customer][:contact_phone], + :customer_address => params[:customer][:address], + :customer_city => params[:customer][:city], + :customer_zip_code => params[:customer][:zip_code] + ) + end + + protected + + def cart + @cart ||= + begin + cookies[cookie_key] && YAML.load(Base64.decode64(cookies[cookie_key])) || nil + end + @cart + end + + def cart=(data) + @cart = data + end + + after_filter :save_cookie + def save_cookie + if @cart.nil? && cookies[cookie_key] + cookies.delete(cookie_key) + else + cookies[cookie_key] = { + :value => Base64.encode64(@cart.to_yaml), + :path => "/profile/#{profile.identifier}/plugin/shopping_cart" + } + end + end + + def cookie_key + :_noosfero_session_shopping_cart + end + +end diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb deleted file mode 100644 index 01aacc9..0000000 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb +++ /dev/null @@ -1,288 +0,0 @@ -require 'base64' - -include ShoppingCartPlugin::CartHelper - -class ShoppingCartPluginProfileController < ProfileController - append_view_path File.join(File.dirname(__FILE__) + '/../views') - before_filter :login_required, :only => [] - - before_filter :login_required, :only => [] - - def get - has_products = !cart.nil? && (cart[:items].keys.size > 0) || false - config = { 'enterprise' => profile.identifier, 'hasProducts' => has_products } - render :text => config.to_json - end - - def add - self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil? - if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) - self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? - self.cart[:items][product.id] += 1 - render :text => { - :ok => true, - :error => {:code => 0}, - :products => [{ - :id => product.id, - :name => product.name, - :price => get_price(product, profile.environment), - :description => product.description, - :picture => product.default_image(:minor), - :quantity => self.cart[:items][product.id] - }] - }.to_json - end - end - - def remove - id = params[:id].to_i - if validate_cart_presence && validate_cart_has_product(id) - self.cart[:items].delete(id) - self.cart = nil if self.cart[:items].empty? - render :text => { - :ok => true, - :error => {:code => 0}, - :product_id => id - }.to_json - end - end - - def list - if validate_cart_presence - products = self.cart[:items].collect do |id, quantity| - product = Product.find(id) - { :id => product.id, - :name => product.name, - :price => get_price(product, profile.environment), - :description => product.description, - :picture => product.default_image(:minor), - :quantity => quantity - } - end - render :text => { - :ok => true, - :error => {:code => 0}, - :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier, - :products => products - }.to_json - end - end - - def update_quantity - quantity = params[:quantity].to_i - id = params[:id].to_i - if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) - product = Product.find(id) - self.cart[:items][product.id] = quantity - render :text => { - :ok => true, - :error => {:code => 0}, - :product_id => id, - :quantity => quantity - }.to_json - end - end - - def clean - self.cart = nil - render :text => { - :ok => true, - :error => {:code => 0} - }.to_json - end - - def buy - @environment = profile.environment - @cart = cart - render :layout => false - end - - def send_request - register_order(params[:customer], self.cart[:items]) - begin - ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, self.cart[:items]) - ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, self.cart[:items]) - render :text => { - :ok => true, - :message => _('Request sent successfully. Check your email.'), - :error => {:code => 0} - }.to_json - rescue Exception => exception - render :text => { - :ok => false, - :error => { - :code => 6, - :message => exception.message - } - }.to_json - end - end - - def visibility - render :text => self.cart.has_key?(:visibility) ? self.cart[:visibility].to_json : true.to_json - end - - def show - begin - self.cart[:visibility] = true - render :text => { - :ok => true, - :message => _('Basket displayed.'), - :error => {:code => 0} - }.to_json - rescue Exception => exception - render :text => { - :ok => false, - :error => { - :code => 7, - :message => exception.message - } - }.to_json - end - end - - def hide - begin - self.cart[:visibility] = false - render :text => { - :ok => true, - :message => _('Basket hidden.'), - :error => {:code => 0} - }.to_json - rescue Exception => exception - render :text => { - :ok => false, - :error => { - :code => 8, - :message => exception.message - } - }.to_json - end - end - - private - - def validate_same_enterprise - if profile.id != self.cart[:enterprise_id] - render :text => { - :ok => false, - :error => { - :code => 1, - :message => _("Can't join items from different enterprises.") - } - }.to_json - return false - end - true - end - - def validate_cart_presence - if self.cart.nil? - render :text => { - :ok => false, - :error => { - :code => 2, - :message => _("There is no basket.") - } - }.to_json - return false - end - true - end - - def validate_enterprise_has_product(id) - begin - product = profile.products.find(id) - rescue - render :text => { - :ok => false, - :error => { - :code => 3, - :message => _("This enterprise doesn't have this product.") - } - }.to_json - return nil - end - product - end - - def validate_cart_has_product(id) - if !self.cart[:items].has_key?(id) - render :text => { - :ok => false, - :error => { - :code => 4, - :message => _("The basket doesn't have this product.") - } - }.to_json - return false - end - true - end - - def validate_item_quantity(quantity) - if quantity.to_i < 1 - render :text => { - :ok => false, - :error => { - :code => 5, - :message => _("Invalid quantity.") - } - }.to_json - return false - end - true - end - - def register_order(custumer, items) - new_items = {} - items.each do |id, quantity| - product = Product.find(id) - price = product.price || 0 - new_items[id] = {:quantity => quantity, :price => price, :name => product.name} - end - ShoppingCartPlugin::PurchaseOrder.create!( - :seller => profile, - :customer => user, - :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED, - :products_list => new_items, - :customer_name => params[:customer][:name], - :customer_email => params[:customer][:email], - :customer_contact_phone => params[:customer][:contact_phone], - :customer_address => params[:customer][:address], - :customer_city => params[:customer][:city], - :customer_zip_code => params[:customer][:zip_code] - ) - end - - protected - - def cart - @cart ||= - begin - cookies[cookie_key] && YAML.load(Base64.decode64(cookies[cookie_key])) || nil - end - @cart - end - - def cart=(data) - @cart = data - end - - after_filter :save_cookie - def save_cookie - if @cart.nil? && cookies[cookie_key] - cookies.delete(cookie_key) - else - cookies[cookie_key] = { - :value => Base64.encode64(@cart.to_yaml), - :path => "/profile/#{profile.identifier}/plugin/shopping_cart" - } - end - end - - def cookie_key - :_noosfero_session_shopping_cart - end - -end diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb new file mode 100644 index 0000000..c7331f5 --- /dev/null +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb @@ -0,0 +1,213 @@ +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 ShoppingCartPluginController; def rescue_action(e) raise e end; end + +class ShoppingCartPluginControllerTest < ActionController::TestCase + + def setup + @controller = ShoppingCartPluginController.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_cart + 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_cart + 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_cart + 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_cart + assert !cart? + assert_nothing_raised { get :clean, :profile => enterprise.identifier } + end + + should 'register order on send request' do + product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) + product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) + @controller.stubs(:cart).returns({:items => {product1.id => 1, product2.id => 2}}) + assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do + post :send_request, + :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, + :profile => enterprise.identifier + end + + order = ShoppingCartPlugin::PurchaseOrder.last + + assert_equal 1.99, order.products_list[product1.id][:price] + assert_equal 1, order.products_list[product1.id][:quantity] + assert_equal 2.23, order.products_list[product2.id][:price] + assert_equal 2, order.products_list[product2.id][:quantity] + assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status + end + + should 'register order on send request and not crash if product is not defined' do + product1 = fast_create(Product, :enterprise_id => enterprise.id) + @controller.stubs(:cart).returns({:items => {product1.id => 1}}) + assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do + post :send_request, + :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, + :profile => enterprise.identifier + end + + order = ShoppingCartPlugin::PurchaseOrder.last + + assert_equal 0, order.products_list[product1.id][:price] + end + + private + + def json_response + ActiveSupport::JSON.decode @response.body + end + + def cart? + @controller.send(:cart).nil? + end + + def product_in_cart?(product) + @controller.send(:cart)[:items].has_key?(product.id) + end + + def product_quantity(product) + @controller.send(: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_cart + 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/functional/shopping_cart_plugin_profile_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb deleted file mode 100644 index 9c9acce..0000000 --- a/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb +++ /dev/null @@ -1,213 +0,0 @@ -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 < ActionController::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_cart - 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_cart - 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_cart - 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_cart - assert !cart? - assert_nothing_raised { get :clean, :profile => enterprise.identifier } - end - - should 'register order on send request' do - product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) - product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) - @controller.stubs(:cart).returns({:items => {product1.id => 1, product2.id => 2}}) - assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do - post :send_request, - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, - :profile => enterprise.identifier - end - - order = ShoppingCartPlugin::PurchaseOrder.last - - assert_equal 1.99, order.products_list[product1.id][:price] - assert_equal 1, order.products_list[product1.id][:quantity] - assert_equal 2.23, order.products_list[product2.id][:price] - assert_equal 2, order.products_list[product2.id][:quantity] - assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status - end - - should 'register order on send request and not crash if product is not defined' do - product1 = fast_create(Product, :enterprise_id => enterprise.id) - @controller.stubs(:cart).returns({:items => {product1.id => 1}}) - assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do - post :send_request, - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, - :profile => enterprise.identifier - end - - order = ShoppingCartPlugin::PurchaseOrder.last - - assert_equal 0, order.products_list[product1.id][:price] - end - - private - - def json_response - ActiveSupport::JSON.decode @response.body - end - - def cart? - @controller.send(:cart).nil? - end - - def product_in_cart?(product) - @controller.send(:cart)[:items].has_key?(product.id) - end - - def product_quantity(product) - @controller.send(: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_cart - get :add, :profile => enterprise.identifier, :id => product.id - get :remove, :profile => enterprise.identifier, :id => product.id - end - -end -- libgit2 0.21.2