From 4c4969e86b39d3eb088c8e8fbc11ab2e6824243b Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 18 Jul 2017 13:21:29 +0200 Subject: [PATCH] Do not couple with controller's ivar and session --- app/controllers/checkout_controller.rb | 5 +++ app/services/reset_order_service.rb | 15 ++++++-- spec/controllers/checkout_controller_spec.rb | 40 ++++++++++++++++++-- spec/services/reset_order_service_spec.rb | 31 ++++++--------- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index c21a9def25..49c4b61ec7 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -41,6 +41,7 @@ class CheckoutController < Spree::CheckoutController set_default_ship_address ResetOrderService.new(self).call + session[:access_token] = current_order.token flash[:success] = t(:order_processed_successfully) respond_to do |format| @@ -59,6 +60,10 @@ class CheckoutController < Spree::CheckoutController end end + def expire_current_order + session[:order_id] = nil + @current_order = nil + end private diff --git a/app/services/reset_order_service.rb b/app/services/reset_order_service.rb index faef5bff63..28eb8ff64b 100644 --- a/app/services/reset_order_service.rb +++ b/app/services/reset_order_service.rb @@ -3,13 +3,20 @@ class ResetOrderService < SimpleDelegator distributor = current_order.distributor token = current_order.token - session[:order_id] = nil - __getobj__.instance_variable_set(:@current_order, nil) - current_order(true) + controller.expire_current_order + build_new_order(distributor, token) + end + private + + def build_new_order(distributor, token) + current_order(true) current_order.set_distributor!(distributor) current_order.tokenized_permission.token = token current_order.tokenized_permission.save! - session[:access_token] = token + end + + def controller + __getobj__ end end diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index ca0d67ab52..1b90541d86 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -85,10 +85,42 @@ describe CheckoutController do controller.send(:clear_ship_address) end - it "sets the new order's token to the same as the old order" do - order = controller.current_order(true) - spree_post :update, order: {} - expect(controller.current_order.token).to eq order.token + context 'when completing the order' do + before do + order.state = 'complete' + allow(order).to receive(:update_attributes).and_return(true) + allow(order).to receive(:next).and_return(true) + allow(order).to receive(:set_distributor!).and_return(true) + end + + it "sets the new order's token to the same as the old order" do + order = controller.current_order(true) + spree_post :update, order: {} + expect(controller.current_order.token).to eq order.token + end + + it 'expires the current order' do + allow(controller).to receive(:expire_current_order) + put :update, order: {} + expect(controller).to have_received(:expire_current_order) + end + + it 'sets the access_token of the session' do + put :update, order: {} + expect(session[:access_token]).to eq(controller.current_order.token) + end + end + end + + describe '#expire_current_order' do + it 'empties the order_id of the session' do + expect(session).to receive(:[]=).with(:order_id, nil) + controller.expire_current_order + end + + it 'resets the @current_order ivar' do + controller.expire_current_order + expect(controller.instance_variable_get(:@current_order)).to be_nil end end diff --git a/spec/services/reset_order_service_spec.rb b/spec/services/reset_order_service_spec.rb index 8577069013..30ffca82ad 100644 --- a/spec/services/reset_order_service_spec.rb +++ b/spec/services/reset_order_service_spec.rb @@ -14,15 +14,21 @@ describe ResetOrderService do tokenized_permission: tokenized_permission, ) end - let(:session) { double(:session) } - let(:controller) { double(:controller, current_order: new_order, session: session) } + let(:controller) do + double( + :controller, + current_order: new_order, + expire_current_order: true + ) + end let(:reset_order_service) { described_class.new(controller) } before do - allow(current_order).to receive(:tokenized_permission).and_return(tokenized_permission) + allow(current_order) + .to receive(:tokenized_permission) + .and_return(tokenized_permission) + allow(tokenized_permission).to receive(:token=) - allow(session).to receive(:[]=).with(:order_id, nil) - allow(session).to receive(:[]=).with(:access_token, current_token) end describe '#call' do @@ -31,16 +37,6 @@ describe ResetOrderService do expect(controller).to have_received(:current_order).once.with(true) end - it 'empties the order_id of the session' do - reset_order_service.call - expect(session).to have_received(:[]=).with(:order_id, nil) - end - - it 'resets the @current_order var' do - reset_order_service.call - expect(controller.instance_variable_get(:@current_order)).to be_nil - end - it 'sets the new order\'s distributor to the same as the old order' do reset_order_service.call @@ -60,10 +56,5 @@ describe ResetOrderService do reset_order_service.call expect(tokenized_permission).to have_received(:save!) end - - it 'sets the access_token of the session' do - reset_order_service.call - expect(session).to have_received(:[]=).with(:access_token, current_token) - end end end