From 4ceaec0ef521bc79a5d06921bec53177de212d01 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 22 Aug 2014 17:34:42 +1000 Subject: [PATCH] Do not error when checking out with a pre-loaded shipping/billing address --- .../darkswarm/services/checkout.js.coffee | 4 +++ .../consumer/shopping/checkout_spec.rb | 33 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/checkout.js.coffee b/app/assets/javascripts/darkswarm/services/checkout.js.coffee index 920c01dad9..cafd4d6718 100644 --- a/app/assets/javascripts/darkswarm/services/checkout.js.coffee +++ b/app/assets/javascripts/darkswarm/services/checkout.js.coffee @@ -32,6 +32,10 @@ Darkswarm.factory 'Checkout', (CurrentOrder, ShippingMethods, PaymentMethods, $h if @ship_address_same_as_billing munged_order.ship_address_attributes = munged_order.bill_address_attributes + # If the order already has a ship and bill address (as with logged in users with + # past orders), and we don't remove id here, then this will set the wrong id for + # ship address, and Rails will error with a 404 for that address. + delete munged_order.ship_address_attributes.id if @paymentMethod()?.method_type == 'gateway' angular.extend munged_order.payments_attributes[0], { diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index c35f01f8e0..54091e854d 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -15,8 +15,8 @@ feature "As a consumer I want to check out my cart", js: true do let(:product) { create(:simple_product, supplier: supplier) } let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor) } - before do + ActionMailer::Base.deliveries.clear add_enterprise_fee enterprise_fee set_order order add_product_to_cart @@ -56,7 +56,7 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content "Donkeys" end - context "When shipping method requires an address" do + context "when shipping method requires an address" do before do toggle_shipping choose sm1.name @@ -92,9 +92,8 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content pm3.name end - describe "Purchasing" do + describe "purchasing" do it "takes us to the order confirmation page when we submit a complete form" do - ActionMailer::Base.deliveries.clear toggle_shipping choose sm2.name toggle_payment @@ -117,7 +116,7 @@ feature "As a consumer I want to check out my cart", js: true do end place_order page.should have_content "Your order has been processed successfully" - ActionMailer::Base.deliveries.length.should == 1 + ActionMailer::Base.deliveries.length.should == 2 email = ActionMailer::Base.deliveries.last site_name = Spree::Config[:site_name] email.subject.should include "#{site_name} Order Confirmation" @@ -189,6 +188,30 @@ feature "As a consumer I want to check out my cart", js: true do end end end + + context "when the customer has a pre-set shipping and billing address" do + before do + # Load up the customer's order and give them a shipping and billing address + # This is equivalent to when the customer has ordered before and their addresses + # are pre-populated. + o = Spree::Order.last + o.ship_address = build(:address) + o.bill_address = build(:address) + o.save! + end + + it "checks out successfully" do + visit checkout_path + checkout_as_guest + choose sm2.name + toggle_payment + choose pm1.name + + place_order + page.should have_content "Your order has been processed successfully" + ActionMailer::Base.deliveries.length.should == 2 + end + end end end end