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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
SD-1356 remove overwritting default user addresses during checkout. (#11132)
Kacper Mękarski37133c542f97
diff --git a/core/app/models/spree/order/address_book.rb b/core/app/models/spree/order/address_book.rbindex 1e849c26a0..78334c8375 100644--- a/core/app/models/spree/order/address_book.rb+++ b/core/app/models/spree/order/address_book.rb@@ -22,7 +22,6 @@ module Spreeaddress = Spree::Address.find_by(id: id)if address && address.user_id == user_idself['bill_address_id'] = address.id- user.update_attribute(:bill_address_id, address.id)bill_address.reloadelseself['bill_address_id'] = nil@@ -31,14 +30,13 @@ module Spreedef bill_address_attributes=(attributes)self.bill_address = update_or_create_address(attributes)- user.bill_address = bill_address if user+ user.bill_address = bill_address if user && user.bill_address.nil?enddef ship_address_id=(id)address = Spree::Address.find_by(id: id)if address && address.user_id == user_idself['ship_address_id'] = address.id- user.update_attribute(:ship_address_id, address.id)ship_address.reloadelseself['ship_address_id'] = nil@@ -47,7 +45,7 @@ module Spreedef ship_address_attributes=(attributes)self.ship_address = update_or_create_address(attributes)- user.ship_address = ship_address if user+ user.ship_address = ship_address if user && user.ship_address.nil?endprivatediff --git a/core/lib/spree/testing_support/factories/address_factory.rb b/core/lib/spree/testing_support/factories/address_factory.rbindex 89e7a4ba7b..0da9eee165 100644--- a/core/lib/spree/testing_support/factories/address_factory.rb+++ b/core/lib/spree/testing_support/factories/address_factory.rb@@ -3,7 +3,7 @@ FactoryBot.define dofirstname { 'John' }lastname { 'Doe' }company { 'Company' }- address1 { '10 Lovely Street' }+ sequence(:address1) { |n| "#{n} Lovely Street" }address2 { 'Northwest' }city { 'Herndon' }zipcode { '35005' }diff --git a/core/lib/spree/testing_support/factories/user_factory.rb b/core/lib/spree/testing_support/factories/user_factory.rbindex ad70459e66..d3810a08d4 100644--- a/core/lib/spree/testing_support/factories/user_factory.rb+++ b/core/lib/spree/testing_support/factories/user_factory.rb@@ -15,8 +15,12 @@ FactoryBot.define doendfactory :user_with_addresses, aliases: [:user_with_addreses] do- ship_address- bill_address+ ship_address do |user_with_addresses|+ create(:address, user: user_with_addresses.instance)+ end+ bill_address do |user_with_addresses|+ create(:address, user: user_with_addresses.instance)+ endendendenddiff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rbindex 0d0924564f..cd0b56c49c 100644--- a/core/spec/models/spree/order_spec.rb+++ b/core/spec/models/spree/order_spec.rb@@ -708,7 +708,7 @@ describe Spree::Order, type: :model doendcontext 'when the user is not persisted' do- let(:user) { FactoryBot.build(:user_with_addreses) }+ let(:user) { build(:user) }it 'does not persist the user' doexpect { order.associate_user!(user) }.@@ -1440,4 +1440,134 @@ describe Spree::Order, type: :model doit { expect { order.valid? }.to change(order, :number).to('R1234') }end++ describe "bill_address_id=" do+ let(:user) { create(:user) }+ let(:order) { create(:order, user: user) }+ let(:address) { create(:address, user: user) }++ subject { order.bill_address_id = address.id }++ context 'when assigned address exist' do+ context 'when assigned address belongs to user' do+ it 'assigns address to order as bill address' do+ expect(order.bill_address_id).not_to eq address.id+ subject+ expect(order.bill_address_id).to eq address.id+ end++ it 'does not set address as user default bill address' do+ subject+ expect(user.bill_address_id).not_to eq address.id+ end+ end+ end+ end++ describe '#bill_address_attributes=' do+ let(:order) { create(:order, user: user) }+ let(:address_attributes) { attributes_for(:address) }++ subject { order.bill_address_attributes = address_attributes }++ context 'when user has default bill address' do+ let!(:user) { create(:user_with_addresses) }++ it 'does not change user default bill addresss' do+ expect(user.bill_address_id).not_to be nil++ expect { subject }.not_to change { user.bill_address_id }+ end+ end++ context 'when user does not have any addresses' do+ let!(:user) { create(:user) }++ it 'changes user default bill addresss' do+ expect(user.bill_address_id).to be nil+ expect(user.addresses).to be_empty++ expect { subject }.to change { user.bill_address_id }+ end+ end++ context 'when user has address but without default bill address' do+ let(:address) { create(:address, user: user) }+ let(:user) { create(:user_with_addresses, bill_address: nil) }++ before { user.addresses << address }++ it 'changes user default bill addresss' do+ expect(user.bill_address_id).to be nil+ expect(user.addresses).not_to be_empty++ expect { subject }.to change { user.bill_address_id }+ end+ end+ end++ describe "ship_address_id=" do+ let(:user) { create(:user) }+ let(:order) { create(:order, user: user) }+ let(:address) { create(:address, user: user) }++ subject { order.ship_address_id = address.id }++ context 'when assigned address exist' do+ context 'when assigned address belongs to user' do+ it 'assigns address to order as ship address' do+ expect(order.ship_address_id).not_to eq address.id+ subject+ expect(order.ship_address_id).to eq address.id+ end++ it 'does not set address as user default ship address' do+ subject+ expect(user.ship_address_id).not_to eq address.id+ end+ end+ end+ end++ describe '#ship_address_attributes=' do+ let(:order) { create(:order, user: user) }+ let(:address_attributes) { attributes_for(:address) }++ subject { order.ship_address_attributes = address_attributes }++ context 'when user has default ship address' do+ let!(:user) { create(:user_with_addresses) }++ it 'does not change user default ship addresss' do+ expect(user.ship_address_id).not_to be nil++ expect { subject }.not_to change { user.ship_address_id }+ end+ end++ context 'when user does not have any addresses' do+ let!(:user) { create(:user) }++ it 'changes user default ship addresss' do+ expect(user.ship_address_id).to be nil+ expect(user.addresses).to be_empty++ expect { subject }.to change { user.ship_address_id }+ end+ end++ context 'when user has address but without default ship address' do+ let(:address) { create(:address, user: user) }+ let(:user) { create(:user_with_addresses, ship_address: nil) }++ before { user.addresses << address }++ it 'changes user default ship addresss' do+ expect(user.ship_address_id).to be nil+ expect(user.addresses).not_to be_empty++ expect { subject }.to change { user.ship_address_id }+ end+ end+ endenddiff --git a/core/spec/services/spree/checkout/update_spec.rb b/core/spec/services/spree/checkout/update_spec.rbindex 264e5d1194..b8254cc717 100644--- a/core/spec/services/spree/checkout/update_spec.rb+++ b/core/spec/services/spree/checkout/update_spec.rb@@ -62,29 +62,16 @@ describe Spree::Checkout::Update, type: :service doenddescribe 'update address' do- let(:order) { create(:order_with_line_items) }+ let(:user) { create(:user_with_addresses) }+ let(:order) { create(:order_with_line_items, user: user, bill_address: user.bill_address, ship_address: user.ship_address, state: order_state) }let(:state) { create(:state) }let(:country) { state.country }- let(:update_service) { described_class.new }- let(:address) do- {- firstname: 'John',- lastname: 'Doe',- address1: '7735 Old Georgetown Road',- city: 'Bethesda',- phone: '3014445002',- zipcode: '20814',- state_id: state.id,- country_iso: country.iso- }- end- let(:order_params) do- ActionController::Parameters.new(- order: {- ship_address_attributes: address- }- )- end+ let(:update_service) { described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil) }+ let(:order_params) { ActionController::Parameters.new(order: {+ ship_address_id: ship_address_id,+ bill_address_id: bill_address_id,+ ship_address_attributes: address_attributes+ }) }let(:permitted_attributes) doSpree::PermittedAttributes.checkout_attributes + [bill_address_attributes: Spree::PermittedAttributes.address_attributes,@@ -92,14 +79,111 @@ describe Spree::Checkout::Update, type: :service do]end- it 'should set order back to address state' do- expect(order.state).not_to eq 'address'- expect(order.ship_address.state.id).not_to eq state.id+ context 'at cart state' do+ let(:order_state) { 'cart' }+ let(:ship_address_id) { nil }+ let(:bill_address_id) { nil }+ let(:address_attributes) do+ {+ firstname: 'John',+ lastname: 'Doe',+ address1: '7735 Old Georgetown Road',+ city: 'Bethesda',+ phone: '3014445002',+ zipcode: '20814',+ state_id: state.id,+ country_iso: country.iso+ }+ end- update_service.send(:call, order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)+ it 'should set order back to address state' do+ expect(order.state).not_to eq 'address'+ expect(order.ship_address.state.id).not_to eq state.id++ update_service- expect(order.state).to eq 'address'- expect(order.ship_address.state.id).to eq state.id+ expect(order.state).to eq 'address'+ expect(order.ship_address.state.id).to eq state.id+ end+ end++ context 'at address state' do+ let(:order_state) { 'address' }+ let(:ship_address_id) { order.ship_address_id }+ let(:bill_address_id) { order.bill_address_id }+ let(:address_attributes) { nil }+ let(:address) { create(:address, user: user) }++ shared_examples 'user default addresses did not change' do+ it 'does not change user default addresses' do+ update_service++ expect(user.reload.bill_address_id).not_to eq order.reload.bill_address+ expect(user.reload.bill_address_id).not_to eq order.reload.ship_address+ expect(user.reload.ship_address_id).not_to eq order.reload.bill_address+ expect(user.reload.ship_address_id).not_to eq order.reload.ship_address+ end+ end++ shared_examples 'checkout is in address step' do+ it 'keeps checkout in address step' do+ update_service++ expect(order.reload.state).to eq 'address'+ end+ end++ context 'when address did not change' do+ it_behaves_like 'user default addresses did not change'+ it_behaves_like 'checkout is in address step'++ it 'does not change order addresses' do+ expect(update_service).to be_success++ expect(order.reload.bill_address_id).to eq user.bill_address_id+ expect(order.reload.ship_address_id).to eq user.ship_address_id+ end+ end++ context 'when ship address changed' do+ let(:ship_address_id) { address.id }++ it_behaves_like 'user default addresses did not change'+ it_behaves_like 'checkout is in address step'++ it 'should update order ship address' do+ expect(update_service).to be_success+ expect(order.reload.bill_address_id).to eq user.bill_address_id+ expect(order.reload.ship_address_id).to eq address.id+ end+ end++ context 'when bill address changed' do+ let(:bill_address_id) { address.id }++ it_behaves_like 'user default addresses did not change'+ it_behaves_like 'checkout is in address step'++ it 'should update order bill address' do+ expect(update_service).to be_success+ expect(order.reload.bill_address_id).to eq address.id+ expect(order.reload.ship_address_id).to eq user.ship_address_id+ end+ end++ context 'when ship and bill address changed' do+ let(:ship_address_id) { address.id }+ let(:bill_address_id) { address.id }++ it_behaves_like 'user default addresses did not change'+ it_behaves_like 'checkout is in address step'++ it 'should update both order addresses' do+ expect(update_service).to be_success+ expect(order.reload.bill_address_id).to eq address.id+ expect(order.reload.ship_address_id).to eq address.id+ end+ endendenddiff --git a/frontend/spec/controllers/spree/checkout_controller_spec.rb b/frontend/spec/controllers/spree/checkout_controller_spec.rbindex df89587953..9014f6f199 100644--- a/frontend/spec/controllers/spree/checkout_controller_spec.rb+++ b/frontend/spec/controllers/spree/checkout_controller_spec.rb@@ -302,6 +302,7 @@ describe Spree::CheckoutController, type: :controller doendcontext 'when addresses are the same but have different ids' do+ let(:ship_address) { create(:address, bill_address.attributes.except('id')) }let(:use_billing) { false }let(:bill_address_params) { default_bill_address.attributes.except(:user_id, :created_at, :updated_at) }let(:ship_address_params) { default_ship_address.attributes.except(:user_id, :created_at, :updated_at) }