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.

Fixed settings user id on customer details plus rewrite flaky tests

Damian Legawiec038b547b1f2f
diff --git a/backend/app/controllers/spree/admin/orders/customer_details_controller.rb b/backend/app/controllers/spree/admin/orders/customer_details_controller.rb
index 92fcd63ee7..9db5933016 100644
--- a/backend/app/controllers/spree/admin/orders/customer_details_controller.rb
+++ b/backend/app/controllers/spree/admin/orders/customer_details_controller.rb
@@ -18,6 +18,7 @@ module Spree
end
def update
+ params[:order][:user_id] = nil if guest_checkout?
if @order.update(order_params)
@order.associate_user!(@user, @order.email.blank?) unless guest_checkout?
@order.next if @order.address?
diff --git a/backend/spec/controllers/spree/admin/orders/customer_details_controller_spec.rb b/backend/spec/controllers/spree/admin/orders/customer_details_controller_spec.rb
index 7f09f75248..0534953917 100644
--- a/backend/spec/controllers/spree/admin/orders/customer_details_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/orders/customer_details_controller_spec.rb
@@ -8,197 +8,69 @@ describe Spree::Admin::Orders::CustomerDetailsController, type: :controller do
let(:store) { Spree::Store.default }
let(:user) { create(:user) }
+ let(:country) { create(:country) }
+ let(:state) { create(:state, country: country) }
let(:order) do
- mock_model(
- Spree::Order,
- total: 100,
- number: 'R123456789',
- billing_address: mock_model(Spree::Address)
- )
- create(:order, total: 100, number: 'R123456789', store: store)
+ create(:order_with_line_items, total: 100, number: 'R123456789', store: store, state: 'cart', user: nil, email: '[email protected]')
+ end
+
+ let(:valid_attributes) do
+ {
+ order_id: order.number,
+ order: {
+ email: '',
+ use_billing: '',
+ bill_address_attributes: build(:address, firstname: 'Jane', country: country, state: state).attributes,
+ ship_address_attributes: build(:address, firstname: 'Jane', country: country, state: state).attributes,
+ user_id: user.id.to_s
+ },
+ guest_checkout: guest_checkout
+ }
end
describe '#update' do
- let(:attributes) do
- {
- order_id: order.number,
- order: {
- email: '',
- use_billing: '',
- bill_address_attributes: { firstname: 'john' },
- ship_address_attributes: { firstname: 'john' },
- user_id: user.id.to_s
- },
- guest_checkout: 'true'
- }
- end
+ let(:attributes) { valid_attributes }
def send_request(params = {})
put :update, params: params
end
- context 'using guest checkout' do
- context 'having valid parameters' do
- before do
- allow(order).to receive_messages(update: true)
- allow(order).to receive_messages(next: false)
- allow(order).to receive_messages(address?: false)
- allow(order).to receive_messages(refresh_shipment_rates: true)
- end
-
- context 'having successful response' do
- before { send_request(attributes) }
-
- it { expect(response).to have_http_status(302) }
- it { expect(response).to redirect_to(edit_admin_order_url(order)) }
- end
+ before { send_request(attributes) }
- context 'with correct method flow' do
- after { send_request(attributes) }
+ context 'using guest checkout' do
+ let(:guest_checkout) { 'true' }
- it { expect(order).to receive(:update).with(ActionController::Parameters.new(attributes[:order]).permit(permitted_order_attributes)) }
- it { expect(order).not_to receive(:next) }
- it { expect(order).to receive(:address?) }
- it 'does refresh the shipment rates with all shipping methods' do
- expect(order).to receive(:refresh_shipment_rates).
- with(Spree::ShippingMethod::DISPLAY_ON_BACK_END)
- end
- it { expect(controller).to receive(:load_order).and_call_original }
- it { expect(controller).to receive(:guest_checkout?).twice.and_call_original }
- it { expect(controller).not_to receive(:load_user).and_call_original }
- end
+ context 'valid parameters' do
+ it { expect(response).to redirect_to(edit_admin_order_url(order)) }
+ it { expect(order.reload.bill_address.firstname).to eq('Jane') }
+ it { expect(order.reload.user).to be_nil }
end
- context 'having invalid parameters' do
- before do
- allow(order).to receive_messages(update: false)
+ context 'invalid parameters' do
+ let(:attributes) do
+ valid_attributes.deep_merge(order: { ship_address_attributes: { 'firstname' => '' } })
end
- context 'having failure response' do
- before { send_request(attributes) }
-
- it { expect(response).to render_template(:edit) }
- end
-
- context 'with correct method flow' do
- after { send_request(attributes) }
-
- it { expect(order).to receive(:update).with(ActionController::Parameters.new(attributes[:order]).permit(permitted_order_attributes)) }
- it { expect(controller).to receive(:load_order).and_call_original }
- it { expect(controller).to receive(:guest_checkout?).and_call_original }
- it { expect(controller).not_to receive(:load_user).and_call_original }
- end
+ it { expect(response).to render_template(:edit) }
end
end
- context 'without using guest checkout' do
- let(:changed_attributes) { attributes.merge(guest_checkout: 'false') }
-
- context 'having valid parameters' do
- before do
- allow(Spree.user_class).to receive(:find_by).and_return(user)
- allow(order).to receive_messages(update: true)
- allow(order).to receive_messages(next: false)
- allow(order).to receive_messages(address?: false)
- allow(order).to receive_messages(refresh_shipment_rates: true)
- allow(order).to receive_messages(associate_user!: true)
- allow(controller).to receive(:guest_checkout?).and_return(false)
- allow(order).to receive(:associate_user!)
- end
-
- context 'having successful response' do
- before { send_request(changed_attributes) }
-
- it { expect(response).to have_http_status(302) }
- it { expect(response).to redirect_to(edit_admin_order_url(order)) }
- end
-
- context 'with correct method flow' do
- after { send_request(changed_attributes) }
+ context 'user order' do
+ let(:guest_checkout) { 'false' }
- it { expect(order).to receive(:update).with(ActionController::Parameters.new(attributes[:order]).permit(permitted_order_attributes)) }
- it { expect(order).to receive(:associate_user!).with(user, order.email.blank?) }
- it { expect(order).not_to receive(:next) }
- it { expect(order).to receive(:address?) }
- it 'does refresh the shipment rates with all shipping methods' do
- expect(order).to receive(:refresh_shipment_rates).
- with(Spree::ShippingMethod::DISPLAY_ON_BACK_END)
- end
- it { expect(controller).to receive(:load_order).and_call_original }
- it { expect(controller).to receive(:guest_checkout?).twice.and_call_original }
- it { expect(controller).to receive(:load_user).and_call_original }
- end
+ context 'valid parameters' do
+ it { expect(response).to redirect_to(edit_admin_order_url(order)) }
+ it { expect(order.reload.bill_address.firstname).to eq('Jane') }
+ it { expect(order.reload.user).to eq(user) }
end
- context 'having invalid parameters' do
- before do
- allow(Spree.user_class).to receive(:find_by).and_return(false)
- allow(controller).to receive(:guest_checkout?).and_return(false)
- end
-
- context 'having failure response' do
- before { send_request(changed_attributes) }
-
- it { expect(response).to render_template(:edit) }
+ context 'invalid parameters' do
+ let(:attributes) do
+ valid_attributes.merge(guest_checkout: 'false', order: { user_id: 99_999_999 })
end
- context 'with correct method flow' do
- after { send_request(changed_attributes) }
-
- it { expect(order).not_to receive(:update).with(ActionController::Parameters.new(attributes[:order]).permit(permitted_order_attributes)) }
- it { expect(controller).to receive(:load_order).and_call_original }
- it { expect(controller).to receive(:guest_checkout?).and_call_original }
- it { expect(controller).to receive(:load_user).and_call_original }
- end
- end
-
- describe '#load_user' do
- context 'having valid parameters' do
- before do
- allow(Spree.user_class).to receive(:find_by).and_return(user)
- allow(order).to receive_messages(update: true)
- allow(order).to receive_messages(next: false)
- allow(order).to receive_messages(address?: false)
- allow(order).to receive_messages(refresh_shipment_rates: true)
- allow(order).to receive_messages(associate_user!: true)
- allow(controller).to receive(:guest_checkout?).and_return(false)
- allow(order).to receive(:associate_user!)
- end
-
- it 'expects to assign user' do
- send_request(changed_attributes)
- expect(assigns[:user]).to eq(user)
- end
-
- context 'with correct method flow' do
- after { send_request(changed_attributes) }
-
- it { expect(Spree.user_class).to receive(:find_by).with(id: user.id.to_s).and_return(user) }
- end
- end
-
- context 'with invalid parameters' do
- before do
- allow(Spree.user_class).to receive(:find_by).and_return(nil)
- allow(controller).to receive(:guest_checkout?).and_return(false)
- end
-
- it 'expects to not assign user' do
- send_request(changed_attributes)
- expect(assigns[:user]).not_to eq(user)
- end
-
- context 'with correct method flow' do
- after { send_request(changed_attributes) }
-
- it { expect(Spree.user_class).to receive(:find_by).with(id: user.id.to_s).and_return(nil) }
- it 'expects user class to receive find_by with email' do
- expect(Spree.user_class).to receive(:find_by).
- with(email: changed_attributes[:order][:email]).and_return(nil)
- end
- end
- end
+ it { expect(response).to render_template(:edit) }
end
end
end