From 7028fbe288dfe53462818002fcd5272d4bb4a9aa Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 27 May 2016 23:26:31 +1000 Subject: [PATCH] Find current_customer via current_distributor and current_user rather than current_order Moving available_payment_methods to enterprises_helper, as per available_shipping_methods --- app/controllers/base_controller.rb | 3 +- app/controllers/shop_controller.rb | 3 +- app/helpers/enterprises_helper.rb | 19 ++- app/helpers/injection_helper.rb | 2 +- app/models/spree/order_decorator.rb | 16 -- app/models/spree/user_decorator.rb | 3 +- app/views/checkout/_order.rabl | 44 ----- app/views/checkout/_payment.html.haml | 2 +- .../enterprises_controller_spec.rb | 13 +- spec/controllers/shop_controller_spec.rb | 141 +++++++-------- .../consumer/shopping/checkout_spec.rb | 15 +- spec/helpers/enterprises_helper_spec.rb | 160 ++++++++++++++++-- spec/helpers/injection_helper_spec.rb | 13 +- spec/models/spree/order_spec.rb | 100 ----------- 14 files changed, 274 insertions(+), 260 deletions(-) delete mode 100644 app/views/checkout/_order.rabl diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index 7bcb8bf3c9..58bf679dc3 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -21,8 +21,7 @@ class BaseController < ApplicationController @order_cycles = OrderCycle.with_distributor(@distributor).active .order(@distributor.preferred_shopfront_order_cycle_order) - customer_tags = current_order.andand.customer.andand.tag_list - applicator = OpenFoodNetwork::TagRuleApplicator.new(@distributor, "FilterOrderCycles", customer_tags) + applicator = OpenFoodNetwork::TagRuleApplicator.new(@distributor, "FilterOrderCycles", current_customer.andand.tag_list) applicator.filter!(@order_cycles) # And default to the only order cycle if there's only the one diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index e244a2a189..091a501ca5 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -55,7 +55,6 @@ class ShopController < BaseController def applicator return @applicator unless @applicator.nil? - customer_tags = current_order.andand.customer.andand.tag_list - @applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterProducts", customer_tags) + @applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterProducts", current_customer.andand.tag_list) end end diff --git a/app/helpers/enterprises_helper.rb b/app/helpers/enterprises_helper.rb index 886546585c..cc2b966b01 100644 --- a/app/helpers/enterprises_helper.rb +++ b/app/helpers/enterprises_helper.rb @@ -3,16 +3,31 @@ module EnterprisesHelper @current_distributor ||= current_order(false).andand.distributor end + def current_customer + return nil unless spree_current_user && current_distributor + @current_customer ||= spree_current_user.customer_of(current_distributor) + end + def available_shipping_methods + return [] unless current_distributor.present? shipping_methods = current_distributor.shipping_methods - customer_tags = current_order.andand.customer.andand.tag_list - applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterShippingMethods", customer_tags) + applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterShippingMethods", current_customer.andand.tag_list) applicator.filter!(shipping_methods) shipping_methods.uniq end + def available_payment_methods + return [] unless current_distributor.present? + payment_methods = current_distributor.payment_methods.available(:front_end).all + + applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterPaymentMethods", current_customer.andand.tag_list) + applicator.filter!(payment_methods) + + payment_methods + end + def managed_enterprises Enterprise.managed_by(spree_current_user) end diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 2d42d43429..50b9c93fca 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -23,7 +23,7 @@ module InjectionHelper end def inject_available_payment_methods - inject_json_ams "paymentMethods", current_order.available_payment_methods, + inject_json_ams "paymentMethods", available_payment_methods, Api::PaymentMethodSerializer, current_order: current_order end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 9125d33991..a4995274b3 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -207,18 +207,6 @@ Spree::Order.class_eval do adjustments.payment_fee.map(&:amount).sum end - # Show payment methods for this distributor - def available_payment_methods - return [] unless distributor.present? - payment_methods = distributor.payment_methods.available(:front_end).all - - customer_tags = customer.andand.tag_list - applicator = OpenFoodNetwork::TagRuleApplicator.new(distributor, "FilterPaymentMethods", customer_tags) - applicator.filter!(payment_methods) - - payment_methods - end - # Does this order have shipments that can be shipped? def ready_to_ship? self.shipments.any?{|s| s.can_ship?} @@ -231,10 +219,6 @@ Spree::Order.class_eval do end end - def available_shipping_methods(display_on = nil) - Spree::ShippingMethod.all_available(self, display_on) - end - def shipping_tax adjustments(:reload).shipping.sum &:included_tax end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index afd32bd294..562b0d10ca 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -38,7 +38,8 @@ Spree.user_class.class_eval do end def customer_of(enterprise) - customers.of(enterprise).first + return nil unless enterprise + customers.find_by_enterprise_id(enterprise) end def send_signup_confirmation diff --git a/app/views/checkout/_order.rabl b/app/views/checkout/_order.rabl deleted file mode 100644 index 778f4428d0..0000000000 --- a/app/views/checkout/_order.rabl +++ /dev/null @@ -1,44 +0,0 @@ -#NOTE: when adding new fields for user input, it may want to be cached in localStorage -# If so, make sure to add it to controller attribute caching - -object current_order -attributes :id, :email, :shipping_method_id, :user_id - -node :display_total do - current_order.display_total.money.to_f -end - -node :payment_method_id do - current_order.payments.first.andand.payment_method_id -end - -child current_order.bill_address => :bill_address do - attributes :phone, :firstname, :lastname, :address1, :address2, :city, :country_id, :state_id, :zipcode -end - -child current_order.ship_address => :ship_address do - attributes :phone, :firstname, :lastname, :address1, :address2, :city, :country_id, :state_id, :zipcode -end - -# This is actually totally decoupled data and should be injected separately into their -# own services - -node :shipping_methods do - Hash[current_distributor.shipping_methods.uniq.collect { |method| - [method.id, { - require_ship_address: method.require_ship_address, - price: method.compute_amount(current_order).to_f, - name: method.name, - description: method.description - }] - }] -end - -node :payment_methods do - Hash[current_order.available_payment_methods.collect { - |method| [method.id, { - name: method.name, - method_type: method.method_type - }] - }] -end diff --git a/app/views/checkout/_payment.html.haml b/app/views/checkout/_payment.html.haml index c0578a8a4c..8d387145d5 100644 --- a/app/views/checkout/_payment.html.haml +++ b/app/views/checkout/_payment.html.haml @@ -17,7 +17,7 @@ -# The problem being how to render the partials .row .small-6.columns - - current_order.available_payment_methods.each do |method| + - available_payment_methods.each do |method| .row .small-12.columns %label diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index 24837ceed9..a4e66313dd 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -32,13 +32,10 @@ describe EnterprisesController do end context "using FilterOrderCycles tag rules" do + let(:user) { create(:user) } let!(:order_cycle3) { create(:simple_order_cycle, distributors: [distributor], orders_open_at: 3.days.ago, orders_close_at: 4.days.from_now) } let!(:oc3_exchange) { order_cycle3.exchanges.outgoing.to_enterprise(distributor).first } - let!(:customer) { create(:customer, enterprise: distributor) } - - before do - order.update_attribute(:customer_id, customer.id) - end + let(:customer) { create(:customer, user: user, enterprise: distributor) } it "shows order cycles allowed by the rules" do create(:filter_order_cycles_tag_rule, @@ -55,6 +52,11 @@ describe EnterprisesController do spree_get :shop, {id: distributor} expect(assigns(:order_cycles)).to include order_cycle1, order_cycle2, order_cycle3 + allow(controller).to receive(:spree_current_user) { user } + + spree_get :shop, {id: distributor} + expect(assigns(:order_cycles)).to include order_cycle1, order_cycle2, order_cycle3 + oc3_exchange.update_attribute(:tag_list, "wholesale") spree_get :shop, {id: distributor} @@ -62,7 +64,6 @@ describe EnterprisesController do expect(assigns(:order_cycles)).not_to include order_cycle3 customer.update_attribute(:tag_list, ["wholesale"]) - order.reload spree_get :shop, {id: distributor} expect(assigns(:order_cycles)).to include order_cycle1, order_cycle2, order_cycle3 diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 777050847d..c3784df428 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -132,86 +132,95 @@ describe ShopController do end end - context "when FilterProducts tag rules are in effect" do - let!(:tagged_customer) { create(:customer, enterprise: distributor, tag_list: "member") } - let!(:untagged_customer) { create(:customer, enterprise: distributor, tag_list: "") } - let!(:order) { create(:order, distributor: distributor) } - let!(:tag_rule) { create(:filter_products_tag_rule, - enterprise: distributor, - preferred_customer_tags: "member", - preferred_variant_tags: "members-only") } - let!(:default_tag_rule) { create(:filter_products_tag_rule, - enterprise: distributor, - is_default: true, - preferred_variant_tags: "members-only") } - let(:product1) { { "id" => 1, "name" => 'product 1', "variants" => [{ "id" => 4, "tag_list" => ["members-only"] }] } } - let(:product2) { { "id" => 2, "name" => 'product 2', "variants" => [{ "id" => 5, "tag_list" => ["members-only"] }, {"id" => 9, "tag_list" => ["something"]}] } } - let(:product3) { { "id" => 3, "name" => 'product 3', "variants" => [{ "id" => 6, "tag_list" => ["something-else"] }] } } - let(:product2_without_v5) { { "id" => 2, "name" => 'product 2', "variants" => [{"id" => 9, "tag_list" => ["something"]}] } } - let!(:products_array) { [product1, product2, product3] } - let!(:products_json) { JSON.unparse( products_array ) } + describe "loading available order cycles" do + let(:user) { create(:user) } + before { allow(controller).to receive(:spree_current_user) { user } } - before do - allow(controller).to receive(:current_order) { order } - end + context "when FilterProducts tag rules are in effect" do + let(:customer) { create(:customer, user: user, enterprise: distributor) } + let!(:tag_rule) { create(:filter_products_tag_rule, + enterprise: distributor, + preferred_customer_tags: "member", + preferred_variant_tags: "members-only") } + let!(:default_tag_rule) { create(:filter_products_tag_rule, + enterprise: distributor, + is_default: true, + preferred_variant_tags: "members-only") } + let(:product1) { { "id" => 1, "name" => 'product 1', "variants" => [{ "id" => 4, "tag_list" => ["members-only"] }] } } + let(:product2) { { "id" => 2, "name" => 'product 2', "variants" => [{ "id" => 5, "tag_list" => ["members-only"] }, {"id" => 9, "tag_list" => ["something"]}] } } + let(:product3) { { "id" => 3, "name" => 'product 3', "variants" => [{ "id" => 6, "tag_list" => ["something-else"] }] } } + let(:product2_without_v5) { { "id" => 2, "name" => 'product 2', "variants" => [{"id" => 9, "tag_list" => ["something"]}] } } + let!(:products_array) { [product1, product2, product3] } + let!(:products_json) { JSON.unparse( products_array ) } - context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do - before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') } - before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') } + before do + allow(controller).to receive(:current_order) { order } + end - let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) } + context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do + before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') } + before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') } - context "when the customer is nil" do - it "applies default action (hide)" do - expect(filtered_products).to include product2_without_v5, product3 - expect(filtered_products).to_not include product1, product2 + let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) } + + context "when the customer is nil" do + it "applies default action (hide)" do + expect(controller.current_customer).to be nil + expect(filtered_products).to include product2_without_v5, product3 + expect(filtered_products).to_not include product1, product2 + end + end + + context "when the customer's tags match" do + before { customer.update_attribute(:tag_list, 'member') } + + it "applies the action (show)" do + expect(controller.current_customer).to eq customer + expect(filtered_products).to include product1, product2, product3 + end + end + + context "when the customer's tags don't match" do + before { customer.update_attribute(:tag_list, 'something') } + + it "applies the default action (hide)" do + expect(controller.current_customer).to eq customer + expect(filtered_products).to include product2_without_v5, product3 + expect(filtered_products).to_not include product1, product2 + end end end - context "when the customer's tags match" do - before { order.update_attribute(:customer_id, tagged_customer.id) } + context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do + before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') } + before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') } - it "applies the action (show)" do - expect(filtered_products).to include product1, product2, product3 + let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) } + + context "when the customer is nil" do + it "applies default action (show)" do + expect(controller.current_customer).to be nil + expect(filtered_products).to include product1, product2, product3 + end end - end - context "when the customer's tags don't match" do - before { order.update_attribute(:customer_id, untagged_customer.id) } + context "when the customer's tags match" do + before { customer.update_attribute(:tag_list, 'member') } - it "applies the default action (hide)" do - expect(filtered_products).to include product2_without_v5, product3 - expect(filtered_products).to_not include product1, product2 + it "applies the action (hide)" do + expect(controller.current_customer).to eq customer + expect(filtered_products).to include product2_without_v5, product3 + expect(filtered_products).to_not include product1, product2 + end end - end - end - context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do - before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') } - before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') } + context "when the customer's tags don't match" do + before { customer.update_attribute(:tag_list, 'something') } - let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) } - - context "when the customer is nil" do - it "applies default action (show)" do - expect(filtered_products).to include product1, product2, product3 - end - end - - context "when the customer's tags match" do - before { order.update_attribute(:customer_id, tagged_customer.id) } - - it "applies the action (hide)" do - expect(filtered_products).to include product2_without_v5, product3 - expect(filtered_products).to_not include product1, product2 - end - end - - context "when the customer's tags don't match" do - before { order.update_attribute(:customer_id, untagged_customer.id) } - - it "applies the default action (show)" do - expect(filtered_products).to include product1, product2, product3 + it "applies the default action (show)" do + expect(controller.current_customer).to eq customer + expect(filtered_products).to include product1, product2, product3 + end end end end diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index e5e4871df0..cbb4d12554 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -106,6 +106,9 @@ feature "As a consumer I want to check out my cart", js: true do end context "using FilterShippingMethods" do + let(:user) { create(:user) } + let(:customer) { create(:customer, user: user, enterprise: distributor) } + it "shows shipping methods allowed by the rule" do # No rules in effect toggle_shipping @@ -131,10 +134,16 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content "Donkeys" page.should_not have_content "Local" - customer = create(:customer, enterprise: distributor, tag_list: "local") - order.update_attribute(:customer_id, customer.id) + quick_login_as(user) + visit checkout_path + + # Default rule in still effect, disallows access to 'Local' + page.should have_content "Frogs" + page.should have_content "Donkeys" + page.should_not have_content "Local" + + customer.update_attribute(:tag_list, "local") visit checkout_path - checkout_as_guest # #local Customer can access 'Local' shipping method page.should have_content "Frogs" diff --git a/spec/helpers/enterprises_helper_spec.rb b/spec/helpers/enterprises_helper_spec.rb index 7a9028e5d7..8e8fa3ed5e 100644 --- a/spec/helpers/enterprises_helper_spec.rb +++ b/spec/helpers/enterprises_helper_spec.rb @@ -1,13 +1,37 @@ require 'spec_helper' describe EnterprisesHelper do + let(:user) { create(:user) } + let(:distributor) { create(:distributor_enterprise) } + let(:some_other_distributor) { create(:distributor_enterprise) } + + before { allow(helper).to receive(:spree_current_user) { user } } + describe "loading available shipping methods" do + let!(:sm1) { create(:shipping_method, require_ship_address: false, distributors: [distributor]) } + let!(:sm2) { create(:shipping_method, require_ship_address: false, distributors: [some_other_distributor]) } + + context "when the order has no current_distributor" do + before do + allow(helper).to receive(:current_distributor) { nil } + end + + it "returns an empty array" do + expect(helper.available_shipping_methods).to eq [] + end + end + + context "when no tag rules are in effect" do + before { allow(helper).to receive(:current_distributor) { distributor } } + + it "finds the shipping methods for the current distributor" do + expect(helper.available_shipping_methods).to_not include sm2 + expect(helper.available_shipping_methods).to include sm1 + end + end context "when FilterShippingMethods tag rules are in effect" do - let!(:distributor) { create(:distributor_enterprise) } - let!(:tagged_customer) { create(:customer, enterprise: distributor, tag_list: "local") } - let!(:untagged_customer) { create(:customer, enterprise: distributor, tag_list: "") } - let!(:order) { create(:order, distributor: distributor) } + let(:customer) { create(:customer, user: user, enterprise: distributor) } let!(:tag_rule) { create(:filter_shipping_methods_tag_rule, enterprise: distributor, preferred_customer_tags: "local", @@ -16,12 +40,13 @@ describe EnterprisesHelper do enterprise: distributor, is_default: true, preferred_shipping_method_tags: "local-delivery") } - let!(:tagged_sm) { create(:shipping_method, require_ship_address: false, name: "Untagged", tag_list: "local-delivery") } - let!(:untagged_sm) { create(:shipping_method, require_ship_address: false, name: "Tagged", tag_list: "") } + let!(:tagged_sm) { sm1 } + let!(:untagged_sm) { sm2 } before do + tagged_sm.update_attribute(:tag_list, 'local-delivery') distributor.shipping_methods = [tagged_sm, untagged_sm] - allow(helper).to receive(:current_order) { order } + allow(helper).to receive(:current_distributor) { distributor } end context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do @@ -30,23 +55,26 @@ describe EnterprisesHelper do context "when the customer is nil" do it "applies default action (hide)" do + expect(helper.current_customer).to be nil expect(helper.available_shipping_methods).to include untagged_sm expect(helper.available_shipping_methods).to_not include tagged_sm end end context "when the customer's tags match" do - before { order.update_attribute(:customer_id, tagged_customer.id) } + before { customer.update_attribute(:tag_list, 'local') } it "applies the action (show)" do + expect(helper.current_customer).to eq customer expect(helper.available_shipping_methods).to include tagged_sm, untagged_sm end end context "when the customer's tags don't match" do - before { order.update_attribute(:customer_id, untagged_customer.id) } + before { customer.update_attribute(:tag_list, 'something') } it "applies the default action (hide)" do + expect(helper.current_customer).to eq customer expect(helper.available_shipping_methods).to include untagged_sm expect(helper.available_shipping_methods).to_not include tagged_sm end @@ -59,27 +87,137 @@ describe EnterprisesHelper do context "when the customer is nil" do it "applies default action (show)" do + expect(helper.current_customer).to be nil expect(helper.available_shipping_methods).to include tagged_sm, untagged_sm end end context "when the customer's tags match" do - before { order.update_attribute(:customer_id, tagged_customer.id) } + before { customer.update_attribute(:tag_list, 'local') } it "applies the action (hide)" do + expect(helper.current_customer).to eq customer expect(helper.available_shipping_methods).to include untagged_sm expect(helper.available_shipping_methods).to_not include tagged_sm end end context "when the customer's tags don't match" do - before { order.update_attribute(:customer_id, untagged_customer.id) } + before { customer.update_attribute(:tag_list, 'something') } it "applies the default action (show)" do + expect(helper.current_customer).to eq customer expect(helper.available_shipping_methods).to include tagged_sm, untagged_sm end end end end end + + describe "loading available payment methods" do + let!(:pm1) { create(:payment_method, distributors: [distributor])} + let!(:pm2) { create(:payment_method, distributors: [some_other_distributor])} + + context "when the order has no current_distributor" do + before do + allow(helper).to receive(:current_distributor) { nil } + end + + it "returns an empty array" do + expect(helper.available_payment_methods).to eq [] + end + end + + context "when no tag rules are in effect" do + before { allow(helper).to receive(:current_distributor) { distributor } } + + it "finds the payment methods for the current distributor" do + expect(helper.available_payment_methods).to_not include pm2 + expect(helper.available_payment_methods).to include pm1 + end + end + + context "when FilterPaymentMethods tag rules are in effect" do + let(:customer) { create(:customer, user: user, enterprise: distributor) } + let!(:tag_rule) { create(:filter_payment_methods_tag_rule, + enterprise: distributor, + preferred_customer_tags: "trusted", + preferred_payment_method_tags: "trusted") } + let!(:default_tag_rule) { create(:filter_payment_methods_tag_rule, + enterprise: distributor, + is_default: true, + preferred_payment_method_tags: "trusted") } + let(:tagged_pm) { pm1 } + let(:untagged_pm) { pm2 } + + before do + tagged_pm.update_attribute(:tag_list, 'trusted') + distributor.payment_methods = [tagged_pm, untagged_pm] + allow(helper).to receive(:current_distributor) { distributor } + end + + context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do + before { tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'visible') } + before { default_tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'hidden') } + + context "when the customer is nil" do + it "applies default action (hide)" do + expect(helper.current_customer).to be nil + expect(helper.available_payment_methods).to include untagged_pm + expect(helper.available_payment_methods).to_not include tagged_pm + end + end + + context "when the customer's tags match" do + before { customer.update_attribute(:tag_list, 'trusted') } + + it "applies the action (show)" do + expect(helper.current_customer).to eq customer + expect(helper.available_payment_methods).to include tagged_pm, untagged_pm + end + end + + context "when the customer's tags don't match" do + before { customer.update_attribute(:tag_list, 'something') } + + it "applies the default action (hide)" do + expect(helper.current_customer).to eq customer + expect(helper.available_payment_methods).to include untagged_pm + expect(helper.available_payment_methods).to_not include tagged_pm + end + end + end + + context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do + before { tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'hidden') } + before { default_tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'visible') } + + context "when the customer is nil" do + it "applies default action (show)" do + expect(helper.current_customer).to be nil + expect(helper.available_payment_methods).to include tagged_pm, untagged_pm + end + end + + context "when the customer's tags match" do + before { customer.update_attribute(:tag_list, 'trusted') } + + it "applies the action (hide)" do + expect(helper.current_customer).to eq customer + expect(helper.available_payment_methods).to include untagged_pm + expect(helper.available_payment_methods).to_not include tagged_pm + end + end + + context "when the customer's tags don't match" do + before { customer.update_attribute(:tag_list, 'something') } + + it "applies the default action (show)" do + expect(helper.current_customer).to eq customer + expect(helper.available_payment_methods).to include tagged_pm, untagged_pm + end + end + end + end + end end diff --git a/spec/helpers/injection_helper_spec.rb b/spec/helpers/injection_helper_spec.rb index e7d775c657..acec197b20 100644 --- a/spec/helpers/injection_helper_spec.rb +++ b/spec/helpers/injection_helper_spec.rb @@ -26,17 +26,20 @@ describe InjectionHelper do it "injects shipping_methods" do sm = create(:shipping_method) - helper.stub(:current_order).and_return order = create(:order) - shipping_methods = double(:shipping_methods, uniq: [sm]) - current_distributor = double(:distributor, shipping_methods: shipping_methods) - allow(helper).to receive(:current_distributor) { current_distributor } + current_distributor = create(:distributor_enterprise, shipping_methods: [sm]) + order = create(:order, distributor: current_distributor) + allow(helper).to receive(:current_order) { order } + allow(helper).to receive(:spree_current_user) { nil } helper.inject_available_shipping_methods.should match sm.id.to_s helper.inject_available_shipping_methods.should match sm.compute_amount(order).to_s end it "injects payment methods" do pm = create(:payment_method) - helper.stub_chain(:current_order, :available_payment_methods).and_return [pm] + current_distributor = create(:distributor_enterprise, payment_methods: [pm]) + order = create(:order, distributor: current_distributor) + allow(helper).to receive(:current_order) { order } + allow(helper).to receive(:spree_current_user) { nil } helper.inject_available_payment_methods.should match pm.id.to_s helper.inject_available_payment_methods.should match pm.name end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 051c78896d..114c7d5fac 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -21,106 +21,6 @@ describe Spree::Order do end end - describe "Payment methods" do - let(:distributor) { create(:distributor_enterprise) } - let(:some_other_distributor) { create(:distributor_enterprise) } - let(:order) { create(:order, distributor: distributor) } - let!(:pm1) { create(:payment_method, distributors: [distributor])} - let!(:pm2) { create(:payment_method, distributors: [some_other_distributor])} - - context "when the order has no distributor" do - let(:order_without_distributor) { create(:order, distributor: nil) } - - it "returns an empty array" do - expect(order_without_distributor.available_payment_methods).to eq [] - end - end - - context "when no tag rules are in effect" do - it "finds the payment methods for the current distributor" do - order.available_payment_methods.include?(pm2).should == false - order.available_payment_methods.include?(pm1).should == true - end - end - - context "when FilterPaymentMethods tag rules are in effect" do - let!(:tagged_customer) { create(:customer, enterprise: distributor, tag_list: "trusted") } - let!(:untagged_customer) { create(:customer, enterprise: distributor, tag_list: "") } - let!(:tag_rule) { create(:filter_payment_methods_tag_rule, - enterprise: distributor, - preferred_customer_tags: "trusted", - preferred_payment_method_tags: "trusted") } - let!(:default_tag_rule) { create(:filter_payment_methods_tag_rule, - enterprise: distributor, - is_default: true, - preferred_payment_method_tags: "trusted") } - let(:tagged_pm) { pm1 } - let(:untagged_pm) { pm2 } - - before do - tagged_pm.update_attribute(:tag_list, 'trusted') - distributor.payment_methods = [tagged_pm, untagged_pm] - end - - context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do - before { tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'visible') } - before { default_tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'hidden') } - - context "when the customer is nil" do - it "applies default action (hide)" do - expect(order.available_payment_methods).to include untagged_pm - expect(order.available_payment_methods).to_not include tagged_pm - end - end - - context "when the customer's tags match" do - before { order.update_attribute(:customer_id, tagged_customer.id) } - - it "applies the action (show)" do - expect(order.available_payment_methods).to include tagged_pm, untagged_pm - end - end - - context "when the customer's tags don't match" do - before { order.update_attribute(:customer_id, untagged_customer.id) } - - it "applies the default action (hide)" do - expect(order.available_payment_methods).to include untagged_pm - expect(order.available_payment_methods).to_not include tagged_pm - end - end - end - - context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do - before { tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'hidden') } - before { default_tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'visible') } - - context "when the customer is nil" do - it "applies default action (show)" do - expect(order.available_payment_methods).to include tagged_pm, untagged_pm - end - end - - context "when the customer's tags match" do - before { order.update_attribute(:customer_id, tagged_customer.id) } - - it "applies the action (hide)" do - expect(order.available_payment_methods).to include untagged_pm - expect(order.available_payment_methods).to_not include tagged_pm - end - end - - context "when the customer's tags don't match" do - before { order.update_attribute(:customer_id, untagged_customer.id) } - - it "applies the default action (show)" do - expect(order.available_payment_methods).to include tagged_pm, untagged_pm - end - end - end - end - end - describe "updating the distribution charge" do let(:order) { build(:order) }