Skip to content

Commit

Permalink
Merge pull request #42 from mvz/improve-order-association
Browse files Browse the repository at this point in the history
Improve order association
  • Loading branch information
stewart committed Apr 5, 2016
2 parents e286672 + acc8b63 commit da7a524
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/initializers/warden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Warden::Manager.after_set_user except: :fetch do |user, auth, opts|
if auth.cookies.signed[:guest_token].present?
if user.is_a?(Spree::User)
Spree::Order.where(guest_token: auth.cookies.signed[:guest_token], user_id: nil).each do |order|
Spree::Order.incomplete.where(guest_token: auth.cookies.signed[:guest_token], user_id: nil).each do |order|
order.associate_user!(user)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def create
set_flash_message(:notice, :signed_up)
sign_in(:spree_user, resource)
session[:spree_user_signup] = true
associate_user
respond_with resource, location: after_sign_up_path_for(resource)
else
clean_up_passwords(resource)
Expand Down
35 changes: 30 additions & 5 deletions spec/controllers/spree/user_registrations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,40 @@
expect(session[:spree_user_signup]).to be true
end

it 'tries to associate user with current_order' do
expect(controller).to receive(:associate_user)
subject
end

it 'redirects to after_sign_up path' do
subject
expect(response).to redirect_to spree.root_path(thing: 7)
end

context 'with a guest token present' do
before do
request.cookie_jar.signed[:guest_token] = 'ABC'
end

it 'assigns orders with the correct token and no user present' do
order = create(:order, guest_token: 'ABC', user_id: nil, created_by_id: nil)
subject
user = Spree::User.find_by_email('[email protected]')

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

it 'does not assign orders with an existing user' do
order = create(:order, guest_token: 'ABC', user_id: 200)
subject

expect(order.reload.user_id).to eq 200
end

it 'does not assign orders with a different token' do
order = create(:order, guest_token: 'DEF', user_id: nil, created_by_id: nil)
subject

expect(order.reload.user_id).to be_nil
end
end
end

context 'when user not valid' do
Expand Down
53 changes: 45 additions & 8 deletions spec/controllers/spree/user_sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,53 @@
end

context "when using correct login information" do
it 'properly assigns orders user from guest_token' do
order1 = create(:order, guest_token: 'ABC', user_id: nil, created_by_id: nil)
order2 = create(:order, guest_token: 'ABC', user_id: 200)
request.cookie_jar.signed[:guest_token] = 'ABC'
context 'with a guest token present' do
before do
request.cookie_jar.signed[:guest_token] = 'ABC'
end

it 'assigns orders with the correct token and no user present' do
order = create(:order, email: user.email, guest_token: 'ABC', user_id: nil, created_by_id: nil)
subject

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

subject
it 'assigns orders with the correct token and no user or email present' do
order = create(:order, guest_token: 'ABC', user_id: nil, created_by_id: nil)
subject

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

it 'does not assign completed orders' do
order = create(:order, email: user.email, guest_token: 'ABC',
user_id: nil, created_by_id: nil,
completed_at: 1.minute.ago)
subject

expect(order1.reload.user_id).to eq user.id
expect(order1.reload.created_by_id).to eq user.id
expect(order2.reload.user_id).to eq 200
order.reload
expect(order.user_id).to be_nil
expect(order.created_by_id).to be_nil
end

it 'does not assign orders with an existing user' do
order = create(:order, guest_token: 'ABC', user_id: 200)
subject

expect(order.reload.user_id).to eq 200
end

it 'does not assign orders with a different token' do
order = create(:order, guest_token: 'DEF', user_id: nil, created_by_id: nil)
subject

expect(order.reload.user_id).to be_nil
end
end

context "when html format is requested" do
Expand Down

0 comments on commit da7a524

Please sign in to comment.