Skip to content

Commit

Permalink
Merge pull request #463 from offmango/fix_guest_token_errors_when_upg…
Browse files Browse the repository at this point in the history
…rading_spree

Fixes errors around guest_token when upgrading to Spree 3.7
  • Loading branch information
damianlegawiec authored Jul 1, 2019
2 parents a718402 + f408ced commit e44f653
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 4 additions & 8 deletions config/initializers/warden.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# Merges users orders to their account after sign in and sign up.
Warden::Manager.after_set_user except: :fetch do |user, auth, _opts|
guest_token = auth.cookies.signed[:guest_token]
token = auth.cookies.signed[:token]
token = auth.cookies.signed[:guest_token] || auth.cookies.signed[:token]
token_attr = Spree::Order.has_attribute?(:token) ? :token : :guest_token

if token.present? && user.is_a?(Spree::User)
Spree::Order.incomplete.where(token: token, user_id: nil).each do |order|
order.associate_user!(user)
end
elsif guest_token.present? && user.is_a?(Spree::User)
Spree::Order.incomplete.where(guest_token: guest_token, user_id: nil).each do |order|
Spree::Order.incomplete.where(token_attr => token, user_id: nil).each do |order|
order.associate_user!(user)
end
end
end
end

Warden::Manager.before_logout do |_user, auth, _opts|
auth.cookies.delete(:guest_token)
Expand Down
20 changes: 20 additions & 0 deletions spec/controllers/spree/user_sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@
end
end

context 'with a guest_token from a pre-3.7 version of Spree present' do
before do
request.cookie_jar.signed[:guest_token] = 'ABC'
request.cookie_jar.signed[:token] = 'DEF'
end

it 'assigns the correct token attribute for the order' do
if Spree.version.to_f > 3.6
order = create(:order, email: user.email, token: 'ABC', user_id: nil, created_by_id: nil)
else
order = create(:order, email: user.email, guest_token: 'ABC', user_id: nil, created_by_id: nil)
end
spree_post :create, spree_user: { email: user.email, password: 'secret' }

order.reload
expect(order.user_id).to eq user.id
expect(order.created_by_id).to eq user.id
end
end

context "and html format is used" do
it "redirects to default after signing in" do
spree_post :create, spree_user: { email: user.email, password: 'secret' }
Expand Down

0 comments on commit e44f653

Please sign in to comment.