diff --git a/spec/factories.rb b/spec/factories.rb index 43b9136437..d21d1143fd 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -400,9 +400,10 @@ FactoryBot.define do transient do shipping_method { create(:shipping_method) } end - after(:create) do |shipment, evaluator| - shipment.add_shipping_method(evaluator.shipping_method, true) + shipping_rates { [Spree::ShippingRate.create(shipping_method: shipping_method, selected: true)] } + + after(:create) do |shipment, evaluator| shipment.order.line_items.each do |line_item| line_item.quantity.times { shipment.inventory_units.create(variant_id: line_item.variant_id) } end diff --git a/spec/features/consumer/shopping/orders_spec.rb b/spec/features/consumer/shopping/orders_spec.rb index d67ce5aea9..b8bf8fe149 100644 --- a/spec/features/consumer/shopping/orders_spec.rb +++ b/spec/features/consumer/shopping/orders_spec.rb @@ -88,15 +88,25 @@ feature "Order Management", js: true do let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true, charges_sales_tax: true) } let(:order_cycle) { create(:order_cycle) } let(:shipping_method) { distributor.shipping_methods.first } - let(:order) { create(:completed_order_with_totals, order_cycle: order_cycle, distributor: distributor, user: user, bill_address: address, ship_address: address) } + let(:order) do + create( + :completed_order_with_totals, + order_cycle: order_cycle, + distributor: distributor, + user: user, + bill_address: address, + ship_address: address + ) + end let!(:item1) { order.reload.line_items.first } let!(:item2) { create(:line_item, order: order) } let!(:item3) { create(:line_item, order: order) } before do - shipping_method.calculator.update_attributes(preferred_amount: 5.0) - order.shipments = [create(:shipment_with, :shipping_method, shipping_method: shipping_method)] - order.reload.save + order.shipment.shipping_method.calculator.update_attributes(preferred_amount: 5.0) + order.save + order.reload + quick_login_as user end diff --git a/spec/lib/open_food_network/user_balance_calculator_spec.rb b/spec/lib/open_food_network/user_balance_calculator_spec.rb index 6f2d7cfcde..675b3a91cd 100644 --- a/spec/lib/open_food_network/user_balance_calculator_spec.rb +++ b/spec/lib/open_food_network/user_balance_calculator_spec.rb @@ -3,20 +3,18 @@ require 'spec_helper' module OpenFoodNetwork describe UserBalanceCalculator do - describe "finding the account balance of a user with a hub" do - let!(:user1) { create(:user) } let!(:hub1) { create(:distributor_enterprise) } let!(:o1) { create(:order_with_totals_and_distribution, user: user1, distributor: hub1, completed_at: 1.day.ago) - } #total=10 + } #total=13 (10 + 3 shipping fee) let!(:o2) { create(:order_with_totals_and_distribution, user: user1, distributor: hub1, completed_at: 1.day.ago) - } #total=10 + } #total=13 (10 + 3 shipping fee) let!(:p1) { create(:payment, order: o1, amount: 15.00, state: "completed") } @@ -25,7 +23,7 @@ module OpenFoodNetwork } it "finds the correct balance for this email and enterprise" do - UserBalanceCalculator.new(o1.email, hub1).balance.should == -3 + UserBalanceCalculator.new(o1.email, hub1).balance.should == -9 # = 15 + 2 - 13 - 13 end context "with another hub" do @@ -33,13 +31,13 @@ module OpenFoodNetwork let!(:o3) { create(:order_with_totals_and_distribution, user: user1, distributor: hub2, completed_at: 1.day.ago) - } #total=10 + } #total=13 (10 + 3 shipping fee) let!(:p3) { create(:payment, order: o3, amount: 15.00, state: "completed") } it "does not find the balance for other enterprises" do - UserBalanceCalculator.new(o3.email, hub2).balance.should == 5 + UserBalanceCalculator.new(o3.email, hub2).balance.should == 2 # = 15 - 13 end end @@ -48,13 +46,13 @@ module OpenFoodNetwork let!(:o4) { create(:order_with_totals_and_distribution, user: user2, distributor: hub1, completed_at: 1.day.ago) - } #total=10 + } #total=13 (10 + 3 shipping fee) let!(:p3) { create(:payment, order: o4, amount: 20.00, state: "completed") } it "does not find the balance for other users" do - UserBalanceCalculator.new(o4.email, hub1).balance.should == 10 + UserBalanceCalculator.new(o4.email, hub1).balance.should == 7 # = 20 - 13 end end @@ -68,7 +66,7 @@ module OpenFoodNetwork } it "does not include canceled orders in the balance" do - UserBalanceCalculator.new(o4.email, hub1).balance.should == -3 + UserBalanceCalculator.new(o4.email, hub1).balance.should == -9 # = 15 + 2 - 13 - 13 end end end diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index 66b2b2f3ab..a8693843b0 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -59,64 +59,69 @@ module Spree end describe "Shipment adjustments" do - let(:shipping_method) { create(:shipping_method_with, :flat_rate) } - let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method) } - let(:order) { create(:order, distributor: hub) } let(:hub) { create(:distributor_enterprise, charges_sales_tax: true) } + let(:order) { create(:order, distributor: hub) } let(:line_item) { create(:line_item, order: order) } - let(:adjustment) { order.adjustments(:reload).shipping.first } + + let(:shipping_method) { create(:shipping_method_with, :flat_rate) } + let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) } + describe "the shipping charge" do it "is the adjustment amount" do order.shipments = [shipment] - - adjustment.amount.should == 50 + expect(order.adjustments.first.amount).to eq(50) end end describe "when tax on shipping is disabled" do - before { Config.shipment_inc_vat = false } + before do + allow(Config).to receive(:shipment_inc_vat).and_return(false) + end + it "records 0% tax on shipment adjustments" do - Config.shipping_tax_rate = 0 + allow(Config).to receive(:shipping_tax_rate).and_return(0) order.shipments = [shipment] - adjustment.included_tax.should == 0 + expect(order.adjustments.first.included_tax).to eq(0) end it "records 0% tax on shipments when a rate is set but shipment_inc_vat is false" do - Config.shipping_tax_rate = 0.25 + allow(Config).to receive(:shipping_tax_rate).and_return(0.25) order.shipments = [shipment] - adjustment.included_tax.should == 0 + expect(order.adjustments.first.included_tax).to eq(0) end end describe "when tax on shipping is enabled" do - before { Config.shipment_inc_vat = true } + before do + allow(Config).to receive(:shipment_inc_vat).and_return(true) + end it "takes the shipment adjustment tax included from the system setting" do - Config.shipping_tax_rate = 0.25 + allow(Config).to receive(:shipping_tax_rate).and_return(0.25) order.shipments = [shipment] # Finding the tax included in an amount that's already inclusive of tax: # total - ( total / (1 + rate) ) # 50 - ( 50 / (1 + 0.25) ) # = 10 - adjustment.included_tax.should == 10.00 + expect(order.adjustments.first.included_tax).to eq(10.00) end it "records 0% tax on shipments when shipping_tax_rate is not set" do - Config.shipping_tax_rate = nil + allow(Config).to receive(:shipping_tax_rate).and_return(0) order.shipments = [shipment] - adjustment.included_tax.should == 0 + expect(order.adjustments.first.included_tax).to eq(0) end it "records 0% tax on shipments when the distributor does not charge sales tax" do order.distributor.update_attributes! charges_sales_tax: false order.shipments = [shipment] - adjustment.included_tax.should == 0 + expect(order.adjustments.first.included_tax).to eq(0) end end end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 9cb79e4d3d..316c186787 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -224,23 +224,26 @@ describe Spree::Order do end describe "getting the shipping tax" do + let(:order) { create(:order) } let(:shipping_method) { create(:shipping_method_with, :flat_rate) } - let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method) } - let(:order) { create(:order, shipments: [shipment]) } context "with a taxed shipment" do before do - Spree::Config.shipment_inc_vat = true - Spree::Config.shipping_tax_rate = 0.25 + allow(Spree::Config).to receive(:shipment_inc_vat).and_return(true) + allow(Spree::Config).to receive(:shipping_tax_rate).and_return(0.25) end + let!(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) } + it "returns the shipping tax" do order.shipping_tax.should == 10 end end - it "returns zero when the order has not been shipped" do - order.shipping_tax.should == 0 + context 'when the order has not been shipped' do + it "returns zero when the order has not been shipped" do + order.shipping_tax.should == 0 + end end end @@ -257,21 +260,33 @@ describe Spree::Order do end describe "getting the total tax" do + before do + allow(Spree::Config).to receive(:shipment_inc_vat).and_return(true) + allow(Spree::Config).to receive(:shipping_tax_rate).and_return(0.25) + end + + let(:order) { create(:order) } let(:shipping_method) { create(:shipping_method_with, :flat_rate) } - let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method) } - let(:order) { create(:order, shipments: [shipment]) } + let!(:shipment) do + create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) + end let(:enterprise_fee) { create(:enterprise_fee) } before do - Spree::Config.shipment_inc_vat = true - Spree::Config.shipping_tax_rate = 0.25 - - create(:adjustment, adjustable: order, originator: enterprise_fee, label: "EF", amount: 123, included_tax: 2) + create( + :adjustment, + adjustable: order, + originator: enterprise_fee, + label: "EF", + amount: 123, + included_tax: 2 + ) order.reload end it "returns a sum of all tax on the order" do - order.total_tax.should == 12 + # 12 = 2 (of the enterprise fee adjustment) + 10 (of the shipment adjustment) + expect(order.total_tax).to eq(12) end end @@ -289,19 +304,31 @@ describe Spree::Order do let(:tax_category25) { create(:tax_category, tax_rates: [tax_rate25]) } let(:variant) { create(:variant, product: create(:product, tax_category: tax_category10)) } - let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 46.0)) } - let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method) } let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: tax_category20, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 48.0)) } let(:additional_adjustment) { create(:adjustment, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0)) } let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator, coordinator_fees: [enterprise_fee], distributors: [coordinator], variants: [variant]) } let(:line_item) { create(:line_item, variant: variant, price: 44.0) } - let(:order) { create(:order, line_items: [line_item], shipments: [shipment], bill_address: create(:address), order_cycle: order_cycle, distributor: coordinator, adjustments: [additional_adjustment]) } + let(:order) do + create( + :order, + line_items: [line_item], + bill_address: create(:address), + order_cycle: order_cycle, + distributor: coordinator, + adjustments: [additional_adjustment] + ) + end before do - Spree::Config.shipment_inc_vat = true - Spree::Config.shipping_tax_rate = tax_rate15.amount + allow(Spree::Config).to receive(:shipment_inc_vat).and_return(true) + allow(Spree::Config).to receive(:shipping_tax_rate).and_return(tax_rate15.amount) + end + let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 46.0)) } + let!(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) } + + before do order.create_tax_charge! order.update_distribution_charge! end