Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Cover the package owner fix with regression tests

Damian Legawiec2c01f3395420
diff --git a/spree/core/spec/models/spree/stock/package_spec.rb b/spree/core/spec/models/spree/stock/package_spec.rb
index 30f24f878c..b28c7d2547 100644
--- a/spree/core/spec/models/spree/stock/package_spec.rb
+++ b/spree/core/spec/models/spree/stock/package_spec.rb
@@ -13,6 +13,38 @@ module Spree
build(:inventory_unit, variant: variant, order: order)
end
+ # The regression this guards: during checkout a fulfillment belongs to a
+ # Cart while its units carried an order_id from elsewhere (a cart id
+ # dereferenced as an order id). Walking unit → order handed the carrier
+ # provider a stranger's ship address; the fulfillment's own owner is the
+ # authoritative link.
+ describe '#owner' do
+ it 'prefers the owner the fulfillment supplied' do
+ cart = create(:cart, store: @default_store, ship_address: create(:address))
+ fulfillment = create(:shipment, cart: cart, order: nil, stock_location: create(:stock_location))
+
+ package = fulfillment.to_package
+
+ expect(package.owner).to eq(cart)
+ end
+
+ it 'never returns a stranger order reachable through a unit' do
+ stranger = create(:order)
+ cart = create(:cart, store: @default_store)
+ fulfillment = create(:shipment, cart: cart, order: nil, stock_location: create(:stock_location))
+ fulfillment.fulfillment_items.update_all(order_id: stranger.id)
+
+ expect(fulfillment.to_package.owner).to eq(cart)
+ end
+
+ it 'falls back to the units for a package with no fulfillment' do
+ package = Package.new(stock_location)
+ package.add build_inventory_unit
+
+ expect(package.owner).to eq(order)
+ end
+ end
+
it 'calculates the weight of all the contents' do
4.times { subject.add build_inventory_unit }
expect(subject.weight).to eq(100.0)
diff --git a/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb b/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb
index 7f83968317..6fe3228ff7 100644
--- a/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb
+++ b/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb
@@ -100,6 +100,29 @@ RSpec.describe SpreeEasyPost::DeliveryRateProvider do
expect(ground.metadata['easypost_shipment_id']).to eq('shp_1')
end
+ # During checkout the package belongs to a Cart, not an Order — the
+ # regression this guards had the provider walking to a stranger's order
+ # (whose nil address then killed rating inside the provider's rescue).
+ it 'quotes for a cart-owned package against the cart ship address' do
+ cart = create(:cart, store: store, ship_address: create(:address))
+ line_item = create(:line_item, cart: cart, order: nil)
+ fulfillment = create(:shipment, cart: cart, order: nil, stock_location: create(:stock_location))
+ fulfillment.set_up_inventory('on_hand', line_item.variant, cart, line_item)
+
+ estimates = provider.estimates(fulfillment.to_package)
+
+ expect(estimates.size).to eq(2)
+ end
+
+ it 'declines to quote when the owner has no ship address yet' do
+ cart = create(:cart, store: store, ship_address: nil)
+ line_item = create(:line_item, cart: cart, order: nil)
+ fulfillment = create(:shipment, cart: cart, order: nil, stock_location: create(:stock_location))
+ fulfillment.set_up_inventory('on_hand', line_item.variant, cart, line_item)
+
+ expect(provider.estimates(fulfillment.to_package)).to eq([])
+ end
+
# A carrier outage must degrade to "method not offered", never break
# the rate refresh.
it 'returns nothing and reports when the API call fails' do