Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix order association (3-0-stable) #332

Merged
merged 4 commits into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(email: user.email, 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 @@ -20,9 +20,6 @@ def create
@user = build_resource(spree_user_params)
if resource.save
set_flash_message(:notice, :signed_up)
if current_order
current_order.associate_user! @user
end
sign_in(:spree_user, @user)
session[:spree_user_signup] = true
respond_with resource, location: after_sign_up_path_for(resource)
Expand Down
30 changes: 30 additions & 0 deletions spec/controllers/spree/user_registrations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,35 @@
spree_post :create, { spree_user: { email: '[email protected]', password: 'foobar123', password_confirmation: 'foobar123' } }
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)
spree_post :create, spree_user: { email: '[email protected]', password: 'foobar123', password_confirmation: 'foobar123' }
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)
spree_post :create, spree_user: { email: '[email protected]', password: 'foobar123', password_confirmation: 'foobar123' }

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)
spree_post :create, spree_user: { email: '[email protected]', password: 'foobar123', password_confirmation: 'foobar123' }

expect(order.reload.user_id).to be_nil
end
end
end
end
56 changes: 47 additions & 9 deletions spec/controllers/spree/user_sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,53 @@

context "#create" do
context "using correct login information" do
it 'properly assigns orders user from guest_token' do
order1 = create(:order, email: user.email, 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'
spree_post :create, spree_user: { email: user.email, password: 'secret' }

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

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

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)
spree_post :create, spree_user: { email: user.email, password: 'secret' }

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)
spree_post :create, spree_user: { email: user.email, password: 'secret' }

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)
spree_post :create, spree_user: { email: user.email, password: 'secret' }

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

context "and html format is used" do
Expand Down