diff --git a/app/services/orders/cart_reset_service.rb b/app/services/orders/cart_reset_service.rb index bf2d77215a..b811c84097 100644 --- a/app/services/orders/cart_reset_service.rb +++ b/app/services/orders/cart_reset_service.rb @@ -19,8 +19,9 @@ module Orders end def reset_other!(current_user, current_customer) - reset_user_and_customer(current_user) + reset_user(current_user) reset_order_cycle(current_customer) + order.customer = current_customer order.save! end @@ -28,7 +29,7 @@ module Orders attr_reader :order, :distributor, :current_user - def reset_user_and_customer(current_user) + def reset_user(current_user) return unless current_user order.associate_user!(current_user) if order.user.blank? || order.email.blank? diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index dfe87211da..f6049778cf 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -29,6 +29,19 @@ RSpec.describe EnterprisesController do expect(controller.current_order.order_cycle).to be_nil end + context "when the order is linked to a customer" do + it "removes the customer" do + # Make sure the order is linked to a customer + customer = create(:customer) + order.customer = customer + order.save! + + expect do + get :shop, params: { id: distributor } + end.to change { controller.current_order.customer }.to(nil) + end + end + context "when user is logged in" do before { allow(controller).to receive(:spree_current_user) { user } } @@ -39,6 +52,21 @@ RSpec.describe EnterprisesController do expect(controller.current_order.distributor).to eq(distributor) expect(controller.current_order.order_cycle).to be_nil end + + context "when customer doesn't belong to the order's distributor" do + it "sets the order's customer to distributor's customer" do + expected_customer = create(:customer, user:, enterprise: distributor) + + # Make sure the order is linked to a customer + customer = create(:customer) + order.customer = customer + order.save! + + expect do + get :shop, params: { id: distributor } + end.to change { controller.current_order.customer }.to(expected_customer) + end + end end it "sorts order cycles by the distributor's preferred ordering attr" do diff --git a/spec/services/orders/cart_reset_service_spec.rb b/spec/services/orders/cart_reset_service_spec.rb index 77f36021f1..5b2bcedafd 100644 --- a/spec/services/orders/cart_reset_service_spec.rb +++ b/spec/services/orders/cart_reset_service_spec.rb @@ -14,31 +14,104 @@ RSpec.describe Orders::CartResetService do end end - context "if the order's order cycle is not in the list of visible order cycles" do - let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor]) } - let(:order_cycle_list) { instance_double(Shop::OrderCyclesList) } + describe "#reset_other!" do + it "does not reset the user" do + new_user = create(:user) - before do - expect(Shop::OrderCyclesList).to receive(:new).and_return(order_cycle_list) - order.update_attribute :order_cycle, order_cycle + expect do + described_class.new(order, distributor.id.to_s).reset_other!(new_user, nil) + end.not_to change { order.user } end - it "empties order and makes order cycle nil" do - expect(order_cycle_list).to receive(:call).and_return([]) - - Orders::CartResetService.new(order, distributor.id.to_s).reset_other!(nil, nil) - - expect(order.line_items).to be_empty - expect(order.order_cycle).to be_nil + context "when user is missing" do + it "does not reset the user" do + expect do + described_class.new(order, distributor.id.to_s).reset_other!(nil, nil) + end.not_to change { order.user } + end end - it "selects default Order Cycle if there's one" do - other_order_cycle = create(:simple_order_cycle, distributors: [distributor]) - expect(order_cycle_list).to receive(:call).and_return([other_order_cycle]) + context "when order email is blank" do + it "links the order to the current user" do + new_user = create(:user) + order.update(email: nil) - Orders::CartResetService.new(order, distributor.id.to_s).reset_other!(nil, nil) + described_class.new(order, distributor.id.to_s).reset_other!(new_user, nil) - expect(order.order_cycle).to eq other_order_cycle + expect(order.user).to eq(new_user) + end + end + + context "when order user is blank" do + it "links the order to the current user" do + new_user = create(:user) + order.update(user: nil) + + described_class.new(order, distributor.id.to_s).reset_other!(new_user, nil) + + expect(order.user).to eq(new_user) + end + end + + describe "resetting the customer" do + let(:customer) { create(:customer) } + + before do + order.customer = customer + order.save! + end + + it "links the customer to the order" do + new_customer = create(:customer) + + described_class.new(order, distributor.id.to_s).reset_other!(nil, new_customer) + + expect(order.reload.customer).to eq(new_customer) + end + + context "when customer is missing" do + it "removes the customer" do + expect do + described_class.new(order, distributor.id.to_s).reset_other!(nil, nil) + end.to change { order.customer }.to(nil) + end + end + + context "with the same customer as the order's customer" do + it "does not reset the customer" do + expect do + described_class.new(order, distributor.id.to_s).reset_other!(nil, customer) + end.not_to change { order.customer } + end + end + end + + context "if the order's order cycle is not in the list of visible order cycles" do + let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor]) } + let(:order_cycle_list) { instance_double(Shop::OrderCyclesList) } + + before do + expect(Shop::OrderCyclesList).to receive(:new).and_return(order_cycle_list) + order.update_attribute :order_cycle, order_cycle + end + + it "empties order and makes order cycle nil" do + expect(order_cycle_list).to receive(:call).and_return([]) + + Orders::CartResetService.new(order, distributor.id.to_s).reset_other!(nil, nil) + + expect(order.line_items).to be_empty + expect(order.order_cycle).to be_nil + end + + it "selects default Order Cycle if there's one" do + other_order_cycle = create(:simple_order_cycle, distributors: [distributor]) + expect(order_cycle_list).to receive(:call).and_return([other_order_cycle]) + + Orders::CartResetService.new(order, distributor.id.to_s).reset_other!(nil, nil) + + expect(order.order_cycle).to eq other_order_cycle + end end end end