solidus_frontend 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of solidus_frontend might be problematic. Click here for more details.

Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -0
  3. data/Gemfile +6 -0
  4. data/Rakefile +15 -0
  5. data/script/rails +9 -0
  6. data/solidus_frontend.gemspec +28 -0
  7. data/spec/controllers/controller_extension_spec.rb +126 -0
  8. data/spec/controllers/controller_helpers_spec.rb +26 -0
  9. data/spec/controllers/spree/checkout_controller_spec.rb +401 -0
  10. data/spec/controllers/spree/checkout_controller_with_views_spec.rb +36 -0
  11. data/spec/controllers/spree/content_controller_spec.rb +7 -0
  12. data/spec/controllers/spree/current_order_tracking_spec.rb +44 -0
  13. data/spec/controllers/spree/home_controller_spec.rb +27 -0
  14. data/spec/controllers/spree/orders_controller_ability_spec.rb +104 -0
  15. data/spec/controllers/spree/orders_controller_spec.rb +134 -0
  16. data/spec/controllers/spree/orders_controller_transitions_spec.rb +31 -0
  17. data/spec/controllers/spree/products_controller_spec.rb +36 -0
  18. data/spec/controllers/spree/taxons_controller_spec.rb +12 -0
  19. data/spec/features/address_spec.rb +76 -0
  20. data/spec/features/automatic_promotion_adjustments_spec.rb +47 -0
  21. data/spec/features/caching/products_spec.rb +55 -0
  22. data/spec/features/caching/taxons_spec.rb +22 -0
  23. data/spec/features/cart_spec.rb +81 -0
  24. data/spec/features/checkout_spec.rb +477 -0
  25. data/spec/features/checkout_unshippable_spec.rb +35 -0
  26. data/spec/features/coupon_code_spec.rb +227 -0
  27. data/spec/features/currency_spec.rb +18 -0
  28. data/spec/features/free_shipping_promotions_spec.rb +59 -0
  29. data/spec/features/locale_spec.rb +60 -0
  30. data/spec/features/order_spec.rb +73 -0
  31. data/spec/features/products_spec.rb +260 -0
  32. data/spec/features/promotion_code_invalidation_spec.rb +51 -0
  33. data/spec/features/quantity_promotions_spec.rb +128 -0
  34. data/spec/features/taxons_spec.rb +135 -0
  35. data/spec/features/template_rendering_spec.rb +19 -0
  36. data/spec/fixtures/thinking-cat.jpg +0 -0
  37. data/spec/helpers/base_helper_spec.rb +11 -0
  38. data/spec/spec_helper.rb +121 -0
  39. data/spec/support/shared_contexts/checkout_setup.rb +9 -0
  40. data/spec/support/shared_contexts/custom_products.rb +25 -0
  41. data/spec/support/shared_contexts/product_prototypes.rb +30 -0
  42. data/spec/views/spree/checkout/_summary_spec.rb +11 -0
  43. metadata +47 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9eb38570ef6bce049f5e6272dd185bc1fb11edf6
4
- data.tar.gz: d9902707293a5ea4c2b81adb2de212232bb39fcb
3
+ metadata.gz: c88c6794fff02bef96ed6f9126db0b6d968cfdd9
4
+ data.tar.gz: 49b896452e07e8ee514ed86eb642684162378948
5
5
  SHA512:
6
- metadata.gz: 39a2439df0e38e3195e3d95f4e116b168aa84dd2636686f26247260cd2b677a7abb228799f46732ac552c20df4a1b20ced04e23e5d0255a1f631822cd0671153
7
- data.tar.gz: 3c8687cc2a7b3f21e99cdd75053c30618094540eeafc25df6b28bbc700655bb23abc2941ea381c43429dd4bf3e6356c7a7a73ec4ab869128ccbe900a49e19b50
6
+ metadata.gz: 7c645fa228feca593408d06645c78baeeab75df46828fadea894614217db05ae5f3b33460d10651f59b6be077d3bc2f955bcbc316bec22589ec93eb5db0b5385
7
+ data.tar.gz: c4dac14476e62a517f2f1090f3180751303f799827c7d479d2b05ef7f13ddd933774d0e4b749d5637784e7f8f10f06f018e2af718b719a9153bf8405ce0cb30b
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ ## Spree 2.4.0 (unreleased) ##
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ eval(File.read(File.dirname(__FILE__) + '/../common_spree_dependencies.rb'))
2
+
3
+ gem 'solidus_core', :path => '../core'
4
+ gem 'solidus_api', :path => '../api'
5
+
6
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rspec/core/rake_task'
5
+ require 'spree/testing_support/common_rake'
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ task :default => :spec
10
+
11
+ desc "Generates a dummy app for testing"
12
+ task :test_app do
13
+ ENV['LIB_NAME'] = 'spree/frontend'
14
+ Rake::Task['common:test_app'].invoke
15
+ end
data/script/rails ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/spree/frontend/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'
9
+
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+ version = File.read(File.expand_path("../../SOLIDUS_VERSION", __FILE__)).strip
3
+
4
+ Gem::Specification.new do |s|
5
+ s.platform = Gem::Platform::RUBY
6
+ s.name = 'solidus_frontend'
7
+ s.version = version
8
+ s.summary = 'Cart and storefront for the Solidus e-commerce project.'
9
+ s.description = s.summary
10
+
11
+ s.required_ruby_version = '>= 2.1.0'
12
+ s.author = 'Solidus Team'
13
+ s.email = 'contact@solidus.io'
14
+ s.homepage = 'http://solidus.io/'
15
+ s.rubyforge_project = 'solidus_frontend'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.require_path = 'lib'
19
+ s.requirements << 'none'
20
+
21
+ s.add_dependency 'solidus_api', version
22
+ s.add_dependency 'solidus_core', version
23
+
24
+ s.add_dependency 'canonical-rails', '~> 0.0.4'
25
+ s.add_dependency 'jquery-rails'
26
+
27
+ s.add_development_dependency 'capybara-accessible'
28
+ end
@@ -0,0 +1,126 @@
1
+ require 'spec_helper'
2
+
3
+ # This test tests the functionality within
4
+ # spree/core/controller_helpers/respond_with.rb
5
+ # Rather than duck-punching the existing controllers, let's define a custom one:
6
+ class Spree::CustomController < Spree::BaseController
7
+ def index
8
+ respond_with(Spree::Address.new) do |format|
9
+ format.html { render :text => "neutral" }
10
+ end
11
+ end
12
+
13
+ def create
14
+ # Just need a model with validations
15
+ # Address is good enough, so let's go with that
16
+ address = Spree::Address.new(params[:address])
17
+ respond_with(address)
18
+ end
19
+ end
20
+
21
+ describe Spree::CustomController, :type => :controller do
22
+ after do
23
+ Spree::CustomController.clear_overrides!
24
+ end
25
+
26
+ before do
27
+ @routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
28
+ r.draw {
29
+ get 'index', to: 'spree/custom#index'
30
+ post 'create', to: 'spree/custom#create'
31
+ }
32
+ end
33
+ end
34
+
35
+ context "extension testing" do
36
+ context "index" do
37
+ context "specify symbol for handler instead of Proc" do
38
+ before do
39
+ Spree::CustomController.class_eval do
40
+ respond_override({:index => {:html => {:success => :success_method}}})
41
+
42
+ private
43
+
44
+ def success_method
45
+ render :text => 'success!!!'
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "GET" do
51
+ it "has value success" do
52
+ spree_get :index
53
+ expect(response).to be_success
54
+ assert (response.body =~ /success!!!/)
55
+ end
56
+ end
57
+ end
58
+
59
+ context "render" do
60
+ before do
61
+ Spree::CustomController.instance_eval do
62
+ respond_override({:index => {:html => {:success => lambda { render(:text => 'success!!!') }}}})
63
+ respond_override({:index => {:html => {:failure => lambda { render(:text => 'failure!!!') }}}})
64
+ end
65
+ end
66
+
67
+ describe "GET" do
68
+ it "has value success" do
69
+ spree_get :index
70
+ expect(response).to be_success
71
+ assert (response.body =~ /success!!!/)
72
+ end
73
+ end
74
+ end
75
+
76
+ context "redirect" do
77
+ before do
78
+ Spree::CustomController.instance_eval do
79
+ respond_override({:index => {:html => {:success => lambda { redirect_to('/cart') }}}})
80
+ respond_override({:index => {:html => {:failure => lambda { render(:text => 'failure!!!') }}}})
81
+ end
82
+ end
83
+
84
+ describe "GET" do
85
+ it "has value success" do
86
+ spree_get :index
87
+ expect(response).to be_redirect
88
+ end
89
+ end
90
+ end
91
+
92
+ context "validation error" do
93
+ before do
94
+ Spree::CustomController.instance_eval do
95
+ respond_to :html
96
+ respond_override({:create => {:html => {:success => lambda { render(:text => 'success!!!') }}}})
97
+ respond_override({:create => {:html => {:failure => lambda { render(:text => 'failure!!!') }}}})
98
+ end
99
+ end
100
+
101
+ describe "POST" do
102
+ it "has value success" do
103
+ spree_post :create
104
+ expect(response).to be_success
105
+ assert (response.body =~ /success!/)
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'A different controllers respond_override. Regression test for #1301' do
111
+ before do
112
+ Spree::CheckoutController.instance_eval do
113
+ respond_override({:index => {:html => {:success => lambda { render(:text => 'success!!!') }}}})
114
+ end
115
+ end
116
+
117
+ describe "POST" do
118
+ it "should not effect the wrong controller" do
119
+ spree_get :index
120
+ assert (response.body =~ /neutral/)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ # In this file, we want to test that the controller helpers function correctly
4
+ # So we need to use one of the controllers inside Spree.
5
+ # ProductsController is good.
6
+ describe Spree::ProductsController, :type => :controller do
7
+
8
+ before do
9
+ I18n.enforce_available_locales = false
10
+ expect(I18n).to receive(:available_locales).and_return([:en, :de])
11
+ Spree::Frontend::Config[:locale] = :de
12
+ end
13
+
14
+ after do
15
+ Spree::Frontend::Config[:locale] = :en
16
+ I18n.locale = :en
17
+ I18n.enforce_available_locales = true
18
+ end
19
+
20
+ # Regression test for #1184
21
+ it "sets the default locale based off Spree::Frontend::Config[:locale]" do
22
+ expect(I18n.locale).to eq(:en)
23
+ spree_get :index
24
+ expect(I18n.locale).to eq(:de)
25
+ end
26
+ end
@@ -0,0 +1,401 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::CheckoutController, :type => :controller do
4
+ let(:token) { 'some_token' }
5
+ let(:user) { stub_model(Spree::LegacyUser) }
6
+ let(:order) { FactoryGirl.create(:order_with_totals) }
7
+
8
+ let(:address_params) do
9
+ address = FactoryGirl.build(:address)
10
+ address.attributes.except("created_at", "updated_at")
11
+ end
12
+
13
+ before do
14
+ allow(controller).to receive_messages try_spree_current_user: user
15
+ allow(controller).to receive_messages spree_current_user: user
16
+ allow(controller).to receive_messages current_order: order
17
+ end
18
+
19
+ context "#edit" do
20
+ it 'should check if the user is authorized for :edit' do
21
+ expect(controller).to receive(:authorize!).with(:edit, order, token)
22
+ request.cookie_jar.signed[:guest_token] = token
23
+ spree_get :edit, { state: 'address' }
24
+ end
25
+
26
+ it "should redirect to the cart path unless checkout_allowed?" do
27
+ allow(order).to receive_messages :checkout_allowed? => false
28
+ spree_get :edit, { :state => "delivery" }
29
+ expect(response).to redirect_to(spree.cart_path)
30
+ end
31
+
32
+ it "should redirect to the cart path if current_order is nil" do
33
+ allow(controller).to receive(:current_order).and_return(nil)
34
+ spree_get :edit, { :state => "delivery" }
35
+ expect(response).to redirect_to(spree.cart_path)
36
+ end
37
+
38
+ it "should redirect to cart if order is completed" do
39
+ allow(order).to receive_messages(:completed? => true)
40
+ spree_get :edit, { :state => "address" }
41
+ expect(response).to redirect_to(spree.cart_path)
42
+ end
43
+
44
+ # Regression test for #2280
45
+ it "should redirect to current step trying to access a future step" do
46
+ order.update_column(:state, "address")
47
+ spree_get :edit, { :state => "delivery" }
48
+ expect(response).to redirect_to spree.checkout_state_path("address")
49
+ end
50
+
51
+ context "when entering the checkout" do
52
+ before do
53
+ # The first step for checkout controller is address
54
+ # Transitioning into this state first is required
55
+ order.update_column(:state, "address")
56
+ end
57
+
58
+ it "should associate the order with a user" do
59
+ order.update_column :user_id, nil
60
+ expect(order).to receive(:associate_user!).with(user)
61
+ spree_get :edit, {}, order_id: 1
62
+ end
63
+ end
64
+ end
65
+
66
+ context "#update" do
67
+ it 'should check if the user is authorized for :edit' do
68
+ expect(controller).to receive(:authorize!).with(:edit, order, token)
69
+ request.cookie_jar.signed[:guest_token] = token
70
+ spree_post :update, { state: 'address' }
71
+ end
72
+
73
+ context "save successful" do
74
+ def spree_post_address
75
+ spree_post :update, {
76
+ :state => "address",
77
+ :order => {
78
+ :bill_address_attributes => address_params,
79
+ :use_billing => true
80
+ }
81
+ }
82
+ end
83
+
84
+ before do
85
+ # Must have *a* shipping method and a payment method so updating from address works
86
+ allow(order).to receive_messages :available_shipping_methods => [stub_model(Spree::ShippingMethod)]
87
+ allow(order).to receive_messages :available_payment_methods => [stub_model(Spree::PaymentMethod)]
88
+ allow(order).to receive_messages :ensure_available_shipping_rates => true
89
+ order.line_items << FactoryGirl.create(:line_item)
90
+ end
91
+
92
+ context "with the order in the cart state" do
93
+ before do
94
+ order.update_column(:state, "cart")
95
+ allow(order).to receive_messages :user => user
96
+ end
97
+
98
+ it "should assign order" do
99
+ spree_post :update, {:state => "address"}
100
+ expect(assigns[:order]).not_to be_nil
101
+ end
102
+
103
+ it "should advance the state" do
104
+ spree_post_address
105
+ expect(order.reload.state).to eq("delivery")
106
+ end
107
+
108
+ it "should redirect the next state" do
109
+ spree_post_address
110
+ expect(response).to redirect_to spree.checkout_state_path("delivery")
111
+ end
112
+
113
+ context "current_user respond to save address method" do
114
+ it "calls persist order address on user" do
115
+ expect(user).to receive(:persist_order_address)
116
+ spree_post :update, {
117
+ :state => "address",
118
+ :order => {
119
+ :bill_address_attributes => address_params,
120
+ :use_billing => true
121
+ },
122
+ :save_user_address => "1"
123
+ }
124
+ end
125
+ end
126
+
127
+ context "current_user doesnt respond to persist_order_address" do
128
+ it "doesnt raise any error" do
129
+ expect {
130
+ spree_post :update, {
131
+ :state => "address",
132
+ :order => {
133
+ :bill_address_attributes => address_params,
134
+ :use_billing => true
135
+ },
136
+ :save_user_address => "1"
137
+ }
138
+ }.to_not raise_error
139
+ end
140
+ end
141
+ end
142
+
143
+ context "with the order in the address state" do
144
+ before do
145
+ order.update_columns(ship_address_id: create(:address).id, state: "address")
146
+ allow(order).to receive_messages user: user
147
+ end
148
+
149
+ context "with a billing and shipping address" do
150
+ before do
151
+ @expected_bill_address_id = order.bill_address.id
152
+ @expected_ship_address_id = order.ship_address.id
153
+
154
+ spree_post :update, {
155
+ :state => "address",
156
+ :order => {
157
+ :bill_address_attributes => order.bill_address.attributes.except("created_at", "updated_at"),
158
+ :ship_address_attributes => order.ship_address.attributes.except("created_at", "updated_at"),
159
+ :use_billing => false
160
+ }
161
+ }
162
+
163
+ order.reload
164
+ end
165
+
166
+ it "updates the same billing and shipping address" do
167
+ expect(order.bill_address.id).to eq(@expected_bill_address_id)
168
+ expect(order.ship_address.id).to eq(@expected_ship_address_id)
169
+ end
170
+ end
171
+ end
172
+
173
+ context "when in the confirm state" do
174
+ before do
175
+ allow(order).to receive_messages :confirmation_required? => true
176
+ order.update_column(:state, "confirm")
177
+ allow(order).to receive_messages :user => user
178
+ # An order requires a payment to reach the complete state
179
+ # This is because payment_required? is true on the order
180
+ create(:payment, :amount => order.total, :order => order)
181
+ order.create_proposed_shipments
182
+ order.payments.reload
183
+ end
184
+
185
+ # This inadvertently is a regression test for #2694
186
+ it "should redirect to the order view" do
187
+ spree_post :update, {:state => "confirm"}
188
+ expect(response).to redirect_to spree.order_path(order)
189
+ end
190
+
191
+ it "should populate the flash message" do
192
+ spree_post :update, {:state => "confirm"}
193
+ expect(flash.notice).to eq(Spree.t(:order_processed_successfully))
194
+ end
195
+
196
+ it "should remove completed order from current_order" do
197
+ spree_post :update, {:state => "confirm"}, {:order_id => "foofah"}
198
+ expect(assigns(:current_order)).to be_nil
199
+ expect(assigns(:order)).to eql controller.current_order
200
+ end
201
+ end
202
+ end
203
+
204
+ context "save unsuccessful" do
205
+ before do
206
+ allow(order).to receive_messages :user => user
207
+ allow(order).to receive_messages :update_attributes => false
208
+ end
209
+
210
+ it "should not assign order" do
211
+ spree_post :update, {:state => "address"}
212
+ expect(assigns[:order]).not_to be_nil
213
+ end
214
+
215
+ it "should not change the order state" do
216
+ spree_post :update, { :state => 'address' }
217
+ end
218
+
219
+ it "should render the edit template" do
220
+ spree_post :update, { :state => 'address' }
221
+ expect(response).to render_template :edit
222
+ end
223
+ end
224
+
225
+ context "when current_order is nil" do
226
+ before { allow(controller).to receive_messages :current_order => nil }
227
+
228
+ it "should not change the state if order is completed" do
229
+ expect(order).not_to receive(:update_attribute)
230
+ spree_post :update, {:state => "confirm"}
231
+ end
232
+
233
+ it "should redirect to the cart_path" do
234
+ spree_post :update, {:state => "confirm"}
235
+ expect(response).to redirect_to spree.cart_path
236
+ end
237
+ end
238
+
239
+ context "Spree::Core::GatewayError" do
240
+ before do
241
+ allow(order).to receive_messages :user => user
242
+ allow(order).to receive(:update_attributes).and_raise(Spree::Core::GatewayError.new("Invalid something or other."))
243
+ spree_post :update, {:state => "address"}
244
+ end
245
+
246
+ it "should render the edit template and display exception message" do
247
+ expect(response).to render_template :edit
248
+ expect(flash.now[:error]).to eq(Spree.t(:spree_gateway_error_flash_for_checkout))
249
+ expect(assigns(:order).errors[:base]).to include("Invalid something or other.")
250
+ end
251
+ end
252
+
253
+ context "fails to transition from address" do
254
+ let(:order) do
255
+ FactoryGirl.create(:order_with_line_items).tap do |order|
256
+ order.next!
257
+ expect(order.state).to eq('address')
258
+ end
259
+ end
260
+
261
+ before do
262
+ allow(controller).to receive_messages :current_order => order
263
+ allow(controller).to receive_messages :check_authorization => true
264
+ end
265
+
266
+ context "when the order is invalid" do
267
+ before do
268
+ allow(order).to receive_messages :update_attributes => true, :next => nil
269
+ order.errors.add :base, 'Base error'
270
+ order.errors.add :adjustments, 'error'
271
+ end
272
+
273
+ it "due to the order having errors" do
274
+ spree_put :update, :state => order.state, :order => {}
275
+ expect(flash[:error]).to eq("Base error\nAdjustments error")
276
+ expect(response).to redirect_to(spree.checkout_state_path('address'))
277
+ end
278
+ end
279
+ end
280
+
281
+ context "fails to transition to complete from confirm" do
282
+ let(:order) do
283
+ FactoryGirl.create(:order_with_line_items).tap do |order|
284
+ order.next!
285
+ end
286
+ end
287
+
288
+ before do
289
+ allow(controller).to receive_messages :current_order => order
290
+ allow(controller).to receive_messages :check_authorization => true
291
+ end
292
+
293
+ context "when the country is not a shippable country" do
294
+ before do
295
+ order.ship_address.tap do |address|
296
+ # A different country which is not included in the list of shippable countries
297
+ address.country = FactoryGirl.create(:country, :name => "Australia")
298
+ address.state_name = 'Victoria'
299
+ address.save
300
+ end
301
+
302
+ payment_method = FactoryGirl.create(:simple_credit_card_payment_method)
303
+ payment = FactoryGirl.create(:payment, :payment_method => payment_method)
304
+ order.payments << payment
305
+ end
306
+
307
+ it "due to no available shipping rates for any of the shipments" do
308
+ expect(order.shipments.count).to eq(1)
309
+ order.shipments.first.shipping_rates.delete_all
310
+ order.update_attributes(state: 'confirm')
311
+ spree_put :update, state: order.state, :order => {}
312
+ expect(flash[:error]).to eq(Spree.t(:items_cannot_be_shipped))
313
+ expect(response).to redirect_to(spree.checkout_state_path('confirm'))
314
+ end
315
+ end
316
+ end
317
+
318
+ context "when GatewayError is raised" do
319
+ let(:order) do
320
+ FactoryGirl.create(:order_with_line_items).tap do |order|
321
+ until order.state == 'payment'
322
+ order.next!
323
+ end
324
+ # So that the confirmation step is skipped and we get straight to the action.
325
+ payment_method = FactoryGirl.create(:simple_credit_card_payment_method)
326
+ payment = FactoryGirl.create(:payment, payment_method: payment_method, amount: order.total)
327
+ order.payments << payment
328
+ order.next!
329
+ end
330
+ end
331
+
332
+ before do
333
+ allow(controller).to receive_messages :current_order => order
334
+ allow(controller).to receive_messages :check_authorization => true
335
+ end
336
+
337
+ it "fails to transition from payment to complete" do
338
+ allow_any_instance_of(Spree::Payment).to receive(:process!).and_raise(Spree::Core::GatewayError.new(Spree.t(:payment_processing_failed)))
339
+ spree_put :update, state: order.state, :order => {}
340
+ expect(flash[:error]).to eq(Spree.t(:payment_processing_failed))
341
+ end
342
+ end
343
+ end
344
+
345
+ context "When last inventory item has been purchased" do
346
+ let(:product) { mock_model(Spree::Product, :name => "Amazing Object") }
347
+ let(:variant) { mock_model(Spree::Variant) }
348
+ let(:line_item) { mock_model Spree::LineItem, :insufficient_stock? => true, :amount => 0 }
349
+ let(:order) { create(:order) }
350
+
351
+ before do
352
+ allow(order).to receive_messages(:line_items => [line_item], :state => "payment")
353
+
354
+ configure_spree_preferences do |config|
355
+ config.track_inventory_levels = true
356
+ end
357
+ end
358
+
359
+ context "and back orders are not allowed" do
360
+ before do
361
+ spree_post :update, { :state => "payment" }
362
+ end
363
+
364
+ it "should redirect to cart" do
365
+ expect(response).to redirect_to spree.cart_path
366
+ end
367
+
368
+ it "should set flash message for no inventory" do
369
+ expect(flash[:error]).to eq(Spree.t(:inventory_error_flash_for_insufficient_quantity , :names => "'#{product.name}'" ))
370
+ end
371
+ end
372
+ end
373
+
374
+ context "order doesn't have a delivery step" do
375
+ before do
376
+ allow(order).to receive_messages(:checkout_steps => ["cart", "address", "payment"])
377
+ allow(order).to receive_messages state: "address"
378
+ allow(controller).to receive_messages :check_authorization => true
379
+ end
380
+
381
+ it "doesn't set shipping address on the order" do
382
+ expect(order).to_not receive(:ship_address=)
383
+ spree_post :update, state: order.state
384
+ end
385
+
386
+ it "doesn't remove unshippable items before payment" do
387
+ expect {
388
+ spree_post :update, { :state => "payment" }
389
+ }.to_not change { order.line_items }
390
+ end
391
+ end
392
+
393
+ it "does remove unshippable items before payment" do
394
+ allow(order).to receive_messages :payment_required? => true
395
+ allow(controller).to receive_messages :check_authorization => true
396
+
397
+ expect {
398
+ spree_post :update, { :state => "payment" }
399
+ }.to change { order.line_items }
400
+ end
401
+ end