Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spree 2 Upgrade] Fixed models/order_spec issues related to order.shipping_method #2668

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions spec/controllers/line_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@
end

context "where shipping and payment fees apply" do
let(:distributor) { create(:distributor_enterprise, charges_sales_tax: true, allow_order_changes: true) }
let(:shipping_fee) { 3 }
let(:payment_fee) { 5 }
let(:order) { create(:completed_order_with_fees, distributor: distributor, shipping_fee: shipping_fee, payment_fee: payment_fee) }
let(:order) { create(:completed_order_with_fees, shipping_fee: shipping_fee, payment_fee: payment_fee) }

before do
Spree::Config.shipment_inc_vat = true
Expand Down
3 changes: 1 addition & 2 deletions spec/controllers/spree/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@

describe "removing items from a completed order" do
context "with shipping and transaction fees" do
let(:distributor) { create(:distributor_enterprise, charges_sales_tax: true, allow_order_changes: true) }
let(:order) { create(:completed_order_with_fees, distributor: distributor, shipping_fee: shipping_fee, payment_fee: payment_fee) }
let(:order) { create(:completed_order_with_fees, shipping_fee: shipping_fee, payment_fee: payment_fee) }
let(:line_item1) { order.line_items.first }
let(:line_item2) { order.line_items.second }
let(:shipping_fee) { 3 }
Expand Down
42 changes: 37 additions & 5 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,52 @@
end
end

factory :shipping_method_with_flat_rate, parent: :shipping_method do
calculator { Spree::Calculator::FlatRate.new(preferred_amount: 50.0) }
end

factory :shipment_with_flat_rate, parent: :shipment do
after(:create) do |shipment|
shipment.add_shipping_method(create(:shipping_method_with_flat_rate), true)
end
end

factory :distributor_enterprise_with_tax, parent: :distributor_enterprise do
charges_sales_tax { true }
allow_order_changes { true }
end

factory :shipping_method_with_shipping_fee, parent: :shipping_method do
transient do
shipping_fee 3
end

calculator { build(:calculator_per_item, preferred_amount: shipping_fee) }
require_ship_address { false }
distributors { [create(:distributor_enterprise_with_tax)] }
end

factory :shipment_with_shipping_fee, parent: :shipment do
transient do
shipping_fee 3
end

after(:create) do |shipment, evaluator|
shipping_method = create(:shipping_method_with_shipping_fee, shipping_fee: evaluator.shipping_fee)
shipment.add_shipping_method(shipping_method, true)
end
end

factory :completed_order_with_fees, parent: :order_with_totals_and_distribution do
transient do
shipping_fee 3
payment_fee 5
end

shipping_method do
shipping_calculator = build(:calculator_per_item, preferred_amount: shipping_fee)
create(:shipping_method, calculator: shipping_calculator, require_ship_address: false, distributors: [distributor])
end
shipments { [ create(:shipment_with_shipping_fee, shipping_fee: shipping_fee) ] }

after(:create) do |order, evaluator|
create(:line_item, order: order)
order.create_shipment!
payment_calculator = build(:calculator_per_item, preferred_amount: evaluator.payment_fee)
payment_method = create(:payment_method, calculator: payment_calculator)
create(:payment, order: order, amount: order.total, payment_method: payment_method, state: 'checkout')
Expand Down
35 changes: 14 additions & 21 deletions spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,9 @@
end

describe "an unpaid order with a shipment" do
let(:order) { create(:order_with_totals, shipping_method: shipping_method) }
let(:shipping_method) { create(:shipping_method) }
let(:order) { create(:order_with_totals, shipments: [create(:shipment)]) }

before do
order.create_shipment!
order.reload
order.state = 'complete'
order.shipment.update!(order)
Expand All @@ -210,11 +208,9 @@
end

describe "a paid order with a shipment" do
let(:order) { create(:order, shipping_method: shipping_method) }
let(:shipping_method) { create(:shipping_method) }
let(:order) { create(:order_with_line_items) }

before do
order.create_shipment!
order.payment_state = 'paid'
order.state = 'complete'
order.shipment.update!(order)
Expand All @@ -226,14 +222,13 @@
end

describe "getting the shipping tax" do
let(:order) { create(:order, shipping_method: shipping_method) }
let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 50.0)) }
let(:shipment) { create(:shipment_with_flat_rate) }
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
order.create_shipment!
end

it "returns the shipping tax" do
Expand All @@ -259,15 +254,14 @@
end

describe "getting the total tax" do
let(:order) { create(:order, shipping_method: shipping_method) }
let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 50.0)) }
let(:enterprise_fee) { create(:enterprise_fee) }
let!(:adjustment) { create(:adjustment, adjustable: order, originator: enterprise_fee, label: "EF", amount: 123, included_tax: 2) }
let(:shipment) { create(:shipment_with_flat_rate) }
let(:order) { create(:order, shipments: [shipment]) }
let(:enterprise_fee) { create(:enterprise_fee) }
let!(:adjustment) { create(:adjustment, adjustable: order, originator: enterprise_fee, label: "EF", amount: 123, included_tax: 2) }

before do
Spree::Config.shipment_inc_vat = true
Spree::Config.shipping_tax_rate = 0.25
order.create_shipment!
order.reload
end

Expand All @@ -291,17 +285,18 @@

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) }
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!(:order) { create(:order, shipping_method: shipping_method, bill_address: create(:address), order_cycle: order_cycle, distributor: coordinator, adjustments: [additional_adjustment]) }
let!(:order) { create(:order, shipments: [shipment], bill_address: create(:address), order_cycle: order_cycle, distributor: coordinator, adjustments: [additional_adjustment]) }
let!(:line_item) { create(:line_item, order: order, variant: variant, price: 44.0) }

before do
shipment.add_shipping_method(shipping_method, true)
Spree::Config.shipment_inc_vat = true
Spree::Config.shipping_tax_rate = tax_rate15.amount
order.create_shipment!
Spree::TaxRate.adjust(order)
order.reload.update_distribution_charge!
end
Expand Down Expand Up @@ -391,7 +386,7 @@

describe "emptying the order" do
it "removes shipping method" do
subject.shipping_method = create(:shipping_method)
subject.shipments = [create(:shipment)]
subject.save!
subject.empty!
subject.shipping_method.should == nil
Expand Down Expand Up @@ -639,15 +634,14 @@
end

describe "when a guest order is placed with a registered email" do
let(:order) { create(:order_with_totals_and_distribution, user: nil) }
let(:order) { create(:order_with_totals_and_distribution, user: user) }
let(:payment_method) { create(:payment_method, distributors: [order.distributor]) }
let(:shipping_method) { create(:shipping_method, distributors: [order.distributor]) }
let(:user) { create(:user, email: '[email protected]') }

before do
order.bill_address = create(:address)
order.ship_address = create(:address)
order.shipping_method = shipping_method
order.email = user.email
order.user = nil
order.state = 'cart'
Expand All @@ -661,8 +655,7 @@
end

describe "a completed order with shipping and transaction fees" do
let(:distributor) { create(:distributor_enterprise, charges_sales_tax: true, allow_order_changes: true) }
let(:order) { create(:completed_order_with_fees, distributor: distributor, shipping_fee: shipping_fee, payment_fee: payment_fee) }
let(:order) { create(:completed_order_with_fees, shipping_fee: shipping_fee, payment_fee: payment_fee) }
let(:shipping_fee) { 3 }
let(:payment_fee) { 5 }
let(:item_num) { order.line_items.length }
Expand Down