Do not couple with controller's ivar and session

This commit is contained in:
Pau Perez
2017-07-18 13:21:29 +02:00
committed by Rob Harrington
parent f6c8a11b7b
commit 4c4969e86b
4 changed files with 63 additions and 28 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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