solidus_core 1.3.2 → 1.4.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +15 -0
  3. data/app/helpers/spree/base_helper.rb +6 -2
  4. data/app/mailers/spree/base_mailer.rb +1 -1
  5. data/app/mailers/spree/carton_mailer.rb +1 -1
  6. data/app/mailers/spree/order_mailer.rb +2 -2
  7. data/app/mailers/spree/reimbursement_mailer.rb +7 -7
  8. data/app/mailers/spree/test_mailer.rb +3 -2
  9. data/app/models/concerns/spree/user_payment_source.rb +1 -1
  10. data/app/models/spree/address.rb +14 -4
  11. data/app/models/spree/adjustment.rb +11 -19
  12. data/app/models/spree/app_configuration.rb +23 -1
  13. data/app/models/spree/base.rb +9 -0
  14. data/app/models/spree/country.rb +2 -2
  15. data/app/models/spree/exchange.rb +1 -1
  16. data/app/models/spree/gateway.rb +1 -1
  17. data/app/models/spree/item_adjustments.rb +7 -0
  18. data/app/models/spree/line_item.rb +1 -13
  19. data/app/models/spree/order/checkout.rb +19 -16
  20. data/app/models/spree/order/payments.rb +0 -2
  21. data/app/models/spree/order.rb +7 -21
  22. data/app/models/spree/order_contents.rb +60 -4
  23. data/app/models/spree/order_merger.rb +2 -4
  24. data/app/models/spree/order_shipping.rb +1 -1
  25. data/app/models/spree/order_update_attributes.rb +0 -2
  26. data/app/models/spree/order_updater.rb +14 -11
  27. data/app/models/spree/payment.rb +2 -3
  28. data/app/models/spree/payment_create.rb +5 -2
  29. data/app/models/spree/payment_method/store_credit.rb +6 -5
  30. data/app/models/spree/product/scopes.rb +2 -1
  31. data/app/models/spree/product.rb +2 -5
  32. data/app/models/spree/promotion/actions/create_item_adjustments.rb +5 -3
  33. data/app/models/spree/promotion/actions/free_shipping.rb +1 -1
  34. data/app/models/spree/promotion/rules/option_value.rb +2 -3
  35. data/app/models/spree/promotion/rules/product.rb +9 -3
  36. data/app/models/spree/promotion/rules/taxon.rb +33 -7
  37. data/app/models/spree/promotion/rules/user.rb +2 -3
  38. data/app/models/spree/promotion.rb +16 -6
  39. data/app/models/spree/promotion_handler/coupon.rb +1 -1
  40. data/app/models/spree/shipment.rb +12 -16
  41. data/app/models/spree/stock/estimator.rb +1 -1
  42. data/app/models/spree/stock/package.rb +0 -1
  43. data/app/models/spree/stock_item.rb +2 -6
  44. data/app/models/spree/store.rb +2 -1
  45. data/app/models/spree/tax_rate.rb +1 -1
  46. data/app/models/spree/wallet/add_payment_sources_to_wallet.rb +29 -0
  47. data/app/models/spree/wallet/default_payment_builder.rb +26 -0
  48. data/app/views/spree/reimbursement_mailer/reimbursement_email.html.erb +46 -0
  49. data/app/views/spree/reimbursement_mailer/reimbursement_email.text.erb +1 -1
  50. data/app/views/spree/shared/_base_mailer_footer.html.erb +1 -20
  51. data/app/views/spree/shared/_base_mailer_header.html.erb +1 -31
  52. data/config/initializers/spree_user.rb +1 -1
  53. data/config/locales/en.yml +58 -34
  54. data/db/migrate/20130414000512_update_name_fields_on_spree_credit_cards.rb +1 -1
  55. data/db/migrate/20140601011216_set_shipment_total_for_users_upgrading.rb +15 -3
  56. data/db/migrate/20160527070401_rename_shipment_address_field.rb +7 -0
  57. data/db/migrate/20160616232103_remove_user_id_from_promotion_rules.rb +11 -0
  58. data/db/migrate/20160718205341_add_payment_id_index_to_spree_refunds.rb +5 -0
  59. data/db/migrate/20160718205859_add_reimbursement_id_index_to_spree_refunds.rb +5 -0
  60. data/lib/generators/spree/dummy/dummy_generator.rb +1 -1
  61. data/lib/generators/spree/dummy/templates/rails/database.yml +3 -1
  62. data/lib/generators/spree/dummy/templates/rails/test.rb +4 -2
  63. data/lib/generators/spree/install/install_generator.rb +6 -16
  64. data/lib/spree/core/controller_helpers/auth.rb +1 -1
  65. data/lib/spree/core/controller_helpers/common.rb +1 -1
  66. data/lib/spree/core/controller_helpers/order.rb +2 -2
  67. data/lib/spree/core/current_store.rb +5 -1
  68. data/lib/spree/core/delegate_belongs_to.rb +1 -1
  69. data/lib/spree/core/routes.rb +6 -32
  70. data/lib/spree/core/search/base.rb +2 -2
  71. data/lib/spree/core/stock_configuration.rb +6 -0
  72. data/lib/spree/core/version.rb +2 -2
  73. data/lib/spree/core.rb +2 -1
  74. data/lib/spree/i18n.rb +7 -0
  75. data/lib/spree/mailer_previews/carton_preview.rb +2 -1
  76. data/lib/spree/mailer_previews/order_preview.rb +8 -3
  77. data/lib/spree/mailer_previews/reimbursement_preview.rb +11 -0
  78. data/lib/spree/migrations.rb +13 -7
  79. data/lib/spree/permitted_attributes.rb +1 -1
  80. data/lib/spree/testing_support/capybara_ext.rb +6 -1
  81. data/lib/spree/testing_support/common_rake.rb +3 -7
  82. data/lib/spree/testing_support/controller_requests.rb +8 -0
  83. data/lib/spree/testing_support/factories/order_factory.rb +30 -8
  84. data/lib/spree/testing_support/factories/payment_factory.rb +1 -1
  85. data/lib/spree/testing_support/factories/product_factory.rb +6 -0
  86. data/lib/spree/testing_support/factories/reimbursement_factory.rb +1 -0
  87. data/lib/tasks/migrations/copy_shipped_shipments_to_cartons.rake +3 -1
  88. data/solidus_core.gemspec +3 -3
  89. data/spec/helpers/base_helper_spec.rb +18 -2
  90. data/spec/lib/i18n_spec.rb +4 -0
  91. data/spec/lib/spree/core/controller_helpers/order_spec.rb +16 -4
  92. data/spec/lib/spree/core/controller_helpers/payment_parameters_spec.rb +75 -59
  93. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +1 -1
  94. data/spec/lib/spree/core/importer/order_spec.rb +4 -2
  95. data/spec/lib/spree/core/stock_configuration_spec.rb +17 -0
  96. data/spec/lib/spree/core/testing_support/factories/order_factory_spec.rb +195 -0
  97. data/spec/lib/spree/core/testing_support/factories/payment_factory_spec.rb +6 -0
  98. data/spec/lib/spree/core/testing_support/factories/reimbursement_factory_spec.rb +6 -0
  99. data/spec/lib/spree/migrations_spec.rb +3 -9
  100. data/spec/lib/tasks/migrations/copy_shipped_shipments_to_cartons_spec.rb +1 -1
  101. data/spec/mailers/carton_mailer_spec.rb +1 -1
  102. data/spec/mailers/order_mailer_spec.rb +2 -2
  103. data/spec/mailers/reimbursement_mailer_spec.rb +1 -1
  104. data/spec/models/spree/address_spec.rb +22 -0
  105. data/spec/models/spree/adjustment_spec.rb +0 -10
  106. data/spec/models/spree/carton_spec.rb +1 -1
  107. data/spec/models/spree/country_spec.rb +9 -1
  108. data/spec/models/spree/gateway/bogus_spec.rb +1 -1
  109. data/spec/models/spree/item_adjustments_spec.rb +33 -0
  110. data/spec/models/spree/line_item_spec.rb +2 -21
  111. data/spec/models/spree/order/checkout_spec.rb +32 -45
  112. data/spec/models/spree/order/payment_spec.rb +1 -16
  113. data/spec/models/spree/order_cancellations_spec.rb +8 -3
  114. data/spec/models/spree/order_contents_spec.rb +48 -0
  115. data/spec/models/spree/order_merger_spec.rb +8 -5
  116. data/spec/models/spree/order_shipping_spec.rb +3 -4
  117. data/spec/models/spree/order_spec.rb +23 -59
  118. data/spec/models/spree/order_update_attributes_spec.rb +1 -5
  119. data/spec/models/spree/order_updater_spec.rb +39 -11
  120. data/spec/models/spree/payment_create_spec.rb +61 -0
  121. data/spec/models/spree/payment_method/store_credit_spec.rb +23 -10
  122. data/spec/models/spree/payment_spec.rb +17 -4
  123. data/spec/models/spree/product_spec.rb +1 -1
  124. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +8 -13
  125. data/spec/models/spree/promotion/rules/product_spec.rb +33 -0
  126. data/spec/models/spree/promotion/rules/taxon_spec.rb +52 -8
  127. data/spec/models/spree/promotion_spec.rb +18 -12
  128. data/spec/models/spree/return_authorization_spec.rb +0 -16
  129. data/spec/models/spree/shipment_spec.rb +57 -36
  130. data/spec/models/spree/stock/coordinator_spec.rb +0 -5
  131. data/spec/models/spree/stock/package_spec.rb +0 -1
  132. data/spec/models/spree/stock_item_spec.rb +35 -145
  133. data/spec/models/spree/store_spec.rb +24 -10
  134. data/spec/models/spree/tax_rate_spec.rb +2 -2
  135. data/spec/models/spree/user_spec.rb +1 -1
  136. data/spec/models/spree/validations/db_maximum_length_validator_spec.rb +20 -12
  137. data/spec/support/concerns/working_factories.rb +4 -0
  138. metadata +30 -10
  139. data/config/routes.rb +0 -1
@@ -43,6 +43,12 @@ module Spree
43
43
  expect(new_payment.source.payment_method_id).to eq payment_method.id
44
44
  end
45
45
 
46
+ it "doesn't modify passed-in hash" do
47
+ original = attributes.dup
48
+ new_payment
49
+ expect(original).to eq(attributes)
50
+ end
51
+
46
52
  context "when payment source not valid" do
47
53
  let(:attributes) do
48
54
  {
@@ -126,5 +132,60 @@ module Spree
126
132
  end
127
133
  end
128
134
  end
135
+
136
+ context "with strong params" do
137
+ let(:valid_attributes) do
138
+ {
139
+ amount: 100,
140
+ payment_method: payment_method,
141
+ source_attributes: {
142
+ expiry: "01 / 99",
143
+ number: '1234567890123',
144
+ verification_value: '123',
145
+ name: 'Foo Bar'
146
+ }
147
+ }
148
+ end
149
+
150
+ context "unpermitted" do
151
+ let(:attributes) { ActionController::Parameters.new(valid_attributes) }
152
+
153
+ it "ignores all attributes" do
154
+ expect(new_payment).to have_attributes(
155
+ amount: 0,
156
+ payment_method: nil,
157
+ source: nil
158
+ )
159
+ end
160
+ end
161
+
162
+ context "partially permitted" do
163
+ let(:attributes) do
164
+ ActionController::Parameters.new(valid_attributes).permit(:amount)
165
+ end
166
+
167
+ it "only uses permitted attributes" do
168
+ expect(new_payment).to have_attributes(
169
+ amount: 100, # permitted
170
+ payment_method: nil, # unpermitted
171
+ source: nil # unpermitted
172
+ )
173
+ end
174
+ end
175
+
176
+ context "all permitted" do
177
+ let(:attributes) do
178
+ ActionController::Parameters.new(valid_attributes).permit!
179
+ end
180
+
181
+ it "creates a payment with all attributes" do
182
+ expect(new_payment).to have_attributes(
183
+ amount: 100,
184
+ payment_method: payment_method,
185
+ source: kind_of(CreditCard)
186
+ )
187
+ end
188
+ end
189
+ end
129
190
  end
130
191
  end
@@ -251,10 +251,17 @@ describe Spree::PaymentMethod::StoreCredit do
251
251
  Spree::PaymentMethod::StoreCredit.new.cancel(auth_code)
252
252
  end
253
253
 
254
- let(:store_credit) { create(:store_credit, amount_used: captured_amount) }
254
+ let(:store_credit) { create(:store_credit, amount: original_amount, amount_used: captured_amount) }
255
255
  let(:auth_code) { "1-SC-20141111111111" }
256
+ let(:original_amount) { 100.0 }
256
257
  let(:captured_amount) { 10.0 }
257
258
 
259
+ shared_examples "a spree payment method" do
260
+ it "returns an ActiveMerchant::Billing::Response" do
261
+ expect(subject).to be_instance_of(ActiveMerchant::Billing::Response)
262
+ end
263
+ end
264
+
258
265
  context "capture event found" do
259
266
  let!(:store_credit_event) {
260
267
  create(:store_credit_capture_event,
@@ -263,9 +270,12 @@ describe Spree::PaymentMethod::StoreCredit do
263
270
  store_credit: store_credit)
264
271
  }
265
272
 
266
- it "creates a store credit for the same amount that was captured" do
267
- expect_any_instance_of(Spree::StoreCredit).to receive(:credit).with(captured_amount, auth_code, store_credit.currency)
268
- subject
273
+ it_behaves_like "a spree payment method"
274
+
275
+ it "refunds the capture amount" do
276
+ expect { subject }.to change{ store_credit.reload.amount_remaining }.
277
+ from(original_amount - captured_amount).
278
+ to(original_amount)
269
279
  end
270
280
  end
271
281
 
@@ -278,9 +288,12 @@ describe Spree::PaymentMethod::StoreCredit do
278
288
  store_credit: store_credit)
279
289
  }
280
290
 
281
- it "creates a store credit for the same amount that was captured" do
282
- expect_any_instance_of(Spree::StoreCredit).to receive(:void).with(auth_code)
283
- subject
291
+ it_behaves_like "a spree payment method"
292
+
293
+ it "refunds the capture amount" do
294
+ expect { subject }.to change{ store_credit.reload.amount_remaining }.
295
+ from(original_amount - captured_amount).
296
+ to(original_amount)
284
297
  end
285
298
  end
286
299
 
@@ -289,9 +302,9 @@ describe Spree::PaymentMethod::StoreCredit do
289
302
  Spree::PaymentMethod::StoreCredit.new.cancel('INVALID')
290
303
  end
291
304
 
292
- it "returns false" do
293
- expect(subject).to be false
294
- end
305
+ it_behaves_like "a spree payment method"
306
+
307
+ it { expect(subject.success?).to be(false) }
295
308
  end
296
309
  end
297
310
  end
@@ -655,13 +655,12 @@ describe Spree::Payment, type: :model do
655
655
  end
656
656
 
657
657
  context "completed orders" do
658
- let(:payment_method) { create(:check_payment_method) }
659
658
  before { allow(order).to receive_messages completed?: true }
660
659
 
661
660
  it "updates payment_state and shipments" do
662
661
  expect(order.updater).to receive(:update_payment_state)
663
662
  expect(order.updater).to receive(:update_shipment_state)
664
- Spree::Payment.create!(amount: 100, order: order, payment_method: payment_method)
663
+ Spree::Payment.create(amount: 100, order: order)
665
664
  end
666
665
  end
667
666
 
@@ -688,7 +687,7 @@ describe Spree::Payment, type: :model do
688
687
  context "with multiple payment attempts" do
689
688
  let(:attributes) { attributes_for(:credit_card) }
690
689
  around do |example|
691
- ActiveSupport::Deprecation.silence{ example.run }
690
+ Spree::Deprecation.silence{ example.run }
692
691
  end
693
692
 
694
693
  it "should not try to create profiles on old failed payment attempts" do
@@ -749,7 +748,7 @@ describe Spree::Payment, type: :model do
749
748
  describe "#apply_source_attributes" do
750
749
  # This method is deprecated
751
750
  around do |example|
752
- ActiveSupport::Deprecation.silence do
751
+ Spree::Deprecation.silence do
753
752
  example.run
754
753
  end
755
754
  end
@@ -1161,4 +1160,18 @@ describe Spree::Payment, type: :model do
1161
1160
  end
1162
1161
  end
1163
1162
  end
1163
+
1164
+ describe "#payment_method" do
1165
+ context 'with a soft-deleted payment method' do
1166
+ before do
1167
+ gateway.save!
1168
+ payment.save!
1169
+ gateway.destroy
1170
+ end
1171
+
1172
+ it "works with a soft deleted payment method" do
1173
+ expect(payment.reload.payment_method).to eq(gateway)
1174
+ end
1175
+ end
1176
+ end
1164
1177
  end
@@ -47,7 +47,7 @@ describe Spree::Product, type: :model do
47
47
  end
48
48
 
49
49
  it "should touch taxons" do
50
- taxon = create(:taxon, products: [product])
50
+ taxon = create(:taxon, product_ids: [product.id])
51
51
  taxon.update_columns(updated_at: 1.day.ago)
52
52
  product.taxons.reload
53
53
  expect { subject }.to change{ taxon.reload.updated_at }
@@ -5,8 +5,9 @@ module Spree
5
5
  module Actions
6
6
  describe CreateItemAdjustments, type: :model do
7
7
  let(:order) { create(:order) }
8
- let(:promotion) { create(:promotion) }
9
- let(:action) { CreateItemAdjustments.new }
8
+ let(:promotion) { create(:promotion, :with_line_item_adjustment, adjustment_rate: adjustment_amount) }
9
+ let(:adjustment_amount) { 10 }
10
+ let(:action) { promotion.actions.first! }
10
11
  let!(:line_item) { create(:line_item, order: order) }
11
12
  let(:payload) { { order: order, promotion: promotion } }
12
13
 
@@ -18,9 +19,7 @@ module Spree
18
19
  context "#perform" do
19
20
  # Regression test for https://github.com/spree/spree/issues/3966
20
21
  context "when calculator computes 0" do
21
- before do
22
- allow(action).to receive_messages compute_amount: 0
23
- end
22
+ let(:adjustment_amount) { 0 }
24
23
 
25
24
  it "does not create an adjustment when calculator returns 0" do
26
25
  action.perform(payload)
@@ -29,20 +28,17 @@ module Spree
29
28
  end
30
29
 
31
30
  context "when calculator returns a non-zero value" do
32
- before do
33
- promotion.promotion_actions = [action]
34
- allow(action).to receive_messages compute_amount: 10
35
- end
31
+ let(:adjustment_amount) { 10 }
36
32
 
37
33
  it "creates adjustment with item as adjustable" do
38
34
  action.perform(payload)
39
35
  expect(action.adjustments.count).to eq(1)
40
- expect(line_item.reload.adjustments).to eq(action.adjustments)
36
+ expect(line_item.adjustments).to eq(action.adjustments)
41
37
  end
42
38
 
43
39
  it "creates adjustment with self as source" do
44
40
  action.perform(payload)
45
- expect(line_item.reload.adjustments.first.source).to eq action
41
+ expect(line_item.adjustments.first.source).to eq action
46
42
  end
47
43
 
48
44
  it "does not perform twice on the same item" do
@@ -83,8 +79,7 @@ module Spree
83
79
  end
84
80
 
85
81
  context "when a promotion code is used" do
86
- let(:promotion_code) { create(:promotion_code) }
87
- let(:promotion) { promotion_code.promotion }
82
+ let!(:promotion_code) { create(:promotion_code, promotion: promotion) }
88
83
  let(:payload) { { order: order, promotion: promotion, promotion_code: promotion_code } }
89
84
 
90
85
  it "should connect the adjustment to the promotion_code" do
@@ -84,6 +84,28 @@ describe Spree::Promotion::Rules::Product, type: :model do
84
84
  end
85
85
  end
86
86
  end
87
+
88
+ context "with an invalid match policy" do
89
+ let(:rule) do
90
+ Spree::Promotion::Rules::Product.create!(
91
+ promotion: create(:promotion),
92
+ product_promotion_rules: [
93
+ Spree::ProductPromotionRule.new(product: product),
94
+ ],
95
+ ).tap do |rule|
96
+ rule.preferred_match_policy = 'invalid'
97
+ rule.save!(validate: false)
98
+ end
99
+ end
100
+ let(:product) { order.line_items.first!.product }
101
+ let(:order) { create(:order_with_line_items, line_items_count: 1) }
102
+
103
+ it 'raises' do
104
+ expect {
105
+ rule.eligible?(order)
106
+ }.to raise_error('unexpected match policy: "invalid"')
107
+ end
108
+ end
87
109
  end
88
110
 
89
111
  describe '#actionable?' do
@@ -139,5 +161,16 @@ describe Spree::Promotion::Rules::Product, type: :model do
139
161
  it { is_expected.to be_truthy }
140
162
  end
141
163
  end
164
+
165
+ context 'with an invalid match policy' do
166
+ let(:rule_options) { super().merge(preferred_match_policy: 'invalid') }
167
+ let(:line_item) { rule_line_item }
168
+
169
+ it 'raises' do
170
+ expect {
171
+ rule.actionable?(line_item)
172
+ }.to raise_error('unexpected match policy: "invalid"')
173
+ end
174
+ end
142
175
  end
143
176
  end
@@ -1,20 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spree::Promotion::Rules::Taxon, type: :model do
4
- let(:rule) { Spree::Promotion::Rules::Taxon.create!(promotion: create(:promotion)) }
4
+ let(:rule) do
5
+ Spree::Promotion::Rules::Taxon.create!(promotion: create(:promotion))
6
+ end
5
7
 
6
- context '#elegible?(order)' do
8
+ context '#eligible?(order)' do
7
9
  let(:taxon){ create :taxon, name: 'first' }
8
10
  let(:taxon2){ create :taxon, name: 'second' }
9
11
  let(:order){ create :order_with_line_items }
10
12
 
11
- before do
12
- rule.save
13
- end
14
-
15
13
  context 'with any match policy' do
16
14
  before do
17
- rule.preferred_match_policy = 'any'
15
+ rule.update!(preferred_match_policy: 'any')
18
16
  end
19
17
 
20
18
  it 'is eligible if order does have any prefered taxon' do
@@ -62,7 +60,7 @@ describe Spree::Promotion::Rules::Taxon, type: :model do
62
60
 
63
61
  context 'with all match policy' do
64
62
  before do
65
- rule.preferred_match_policy = 'all'
63
+ rule.update!(preferred_match_policy: 'all')
66
64
  end
67
65
 
68
66
  it 'is eligible order has all prefered taxons' do
@@ -98,5 +96,51 @@ describe Spree::Promotion::Rules::Taxon, type: :model do
98
96
  it{ expect(rule).to be_eligible(order) }
99
97
  end
100
98
  end
99
+
100
+ context 'with an invalid match policy' do
101
+ before do
102
+ order.products.first.taxons << taxon
103
+ rule.taxons << taxon
104
+ rule.preferred_match_policy = 'invalid'
105
+ rule.save!(validate: false)
106
+ end
107
+
108
+ it 'logs a warning and uses "any" policy' do
109
+ expect(Spree::Deprecation).to(
110
+ receive(:warn).
111
+ with(/has unexpected match policy "invalid"/)
112
+ )
113
+
114
+ expect(
115
+ rule.eligible?(order)
116
+ ).to be_truthy
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#actionable?' do
122
+ let(:line_item) { order.line_items.first! }
123
+ let(:order) { create :order_with_line_items }
124
+ let(:taxon) { create :taxon, name: 'first' }
125
+
126
+ before do
127
+ rule.preferred_match_policy = 'invalid'
128
+ rule.save!(validate: false)
129
+ line_item.product.taxons << taxon
130
+ rule.taxons << taxon
131
+ end
132
+
133
+ context 'with an invalid match policy' do
134
+ it 'logs a warning and uses "any" policy' do
135
+ expect(Spree::Deprecation).to(
136
+ receive(:warn).
137
+ with(/has unexpected match policy "invalid"/)
138
+ )
139
+
140
+ expect(
141
+ rule.actionable?(line_item)
142
+ ).to be_truthy
143
+ end
144
+ end
101
145
  end
102
146
  end
@@ -294,35 +294,35 @@ describe Spree::Promotion, type: :model do
294
294
  end
295
295
  end
296
296
 
297
- context "#expired" do
297
+ context "#inactive" do
298
298
  it "should not be exipired" do
299
- expect(promotion).not_to be_expired
299
+ expect(promotion).not_to be_inactive
300
300
  end
301
301
 
302
- it "should be expired if it hasn't started yet" do
302
+ it "should be inactive if it hasn't started yet" do
303
303
  promotion.starts_at = Time.current + 1.day
304
- expect(promotion).to be_expired
304
+ expect(promotion).to be_inactive
305
305
  end
306
306
 
307
- it "should be expired if it has already ended" do
307
+ it "should be inactive if it has already ended" do
308
308
  promotion.expires_at = Time.current - 1.day
309
- expect(promotion).to be_expired
309
+ expect(promotion).to be_inactive
310
310
  end
311
311
 
312
- it "should not be expired if it has started already" do
312
+ it "should not be inactive if it has started already" do
313
313
  promotion.starts_at = Time.current - 1.day
314
- expect(promotion).not_to be_expired
314
+ expect(promotion).not_to be_inactive
315
315
  end
316
316
 
317
- it "should not be expired if it has not ended yet" do
317
+ it "should not be inactive if it has not ended yet" do
318
318
  promotion.expires_at = Time.current + 1.day
319
- expect(promotion).not_to be_expired
319
+ expect(promotion).not_to be_inactive
320
320
  end
321
321
 
322
- it "should not be expired if current time is within starts_at and expires_at range" do
322
+ it "should not be inactive if current time is within starts_at and expires_at range" do
323
323
  promotion.starts_at = Time.current - 1.day
324
324
  promotion.expires_at = Time.current + 1.day
325
- expect(promotion).not_to be_expired
325
+ expect(promotion).not_to be_inactive
326
326
  end
327
327
  end
328
328
 
@@ -664,6 +664,12 @@ describe Spree::Promotion, type: :model do
664
664
  it { is_expected.not_to be }
665
665
  end
666
666
  end
667
+
668
+ context 'when the line item has an non-promotionable product' do
669
+ let(:rules) { [true_rule] }
670
+ let(:line_item) { build(:line_item) { |li| li.product.promotionable = false } }
671
+ it { is_expected.not_to be }
672
+ end
667
673
  end
668
674
  end
669
675
 
@@ -160,22 +160,6 @@ describe Spree::ReturnAuthorization, type: :model do
160
160
  end
161
161
  end
162
162
 
163
- context "can_receive?" do
164
- before do
165
- pending "TODO: get this method into our fork"
166
- end
167
-
168
- it "should allow_receive when inventory units assigned" do
169
- allow(return_authorization).to receive_messages(inventory_units: [1, 2, 3])
170
- expect(return_authorization.can_receive?).to be true
171
- end
172
-
173
- it "should not allow_receive with no inventory units" do
174
- allow(return_authorization).to receive_messages(inventory_units: [])
175
- expect(return_authorization.can_receive?).to be false
176
- end
177
- end
178
-
179
163
  describe "#refundable_amount" do
180
164
  let(:line_item_price) { 5.0 }
181
165
  let(:line_item_count) { return_authorization.order.line_items.count }
@@ -9,21 +9,41 @@ describe Spree::Shipment, type: :model do
9
9
  order.shipments.create!(
10
10
  state: 'pending',
11
11
  cost: 1,
12
- address: order.ship_address,
13
12
  inventory_units: order.inventory_units,
14
- shipping_rates: [
15
- Spree::ShippingRate.new(
16
- shipping_method: shipping_method,
17
- selected: true
18
- )
19
- ],
13
+ shipping_rates: [shipping_rate],
20
14
  stock_location: stock_location
21
15
  )
22
16
  end
17
+ let(:shipping_rate) do
18
+ Spree::ShippingRate.create!(
19
+ shipping_method: shipping_method,
20
+ selected: true
21
+ )
22
+ end
23
23
 
24
24
  let(:variant) { mock_model(Spree::Variant) }
25
25
  let(:line_item) { mock_model(Spree::LineItem, variant: variant) }
26
26
 
27
+ context '#transfer_to_location' do
28
+ it 'transfers unit to a new shipment with given location' do
29
+ order = create(:completed_order_with_totals, line_items_count: 2)
30
+ shipment = order.shipments.first
31
+ variant = order.inventory_units.map(&:variant).first
32
+
33
+ aggregate_failures("verifying new shipment attributes") do
34
+ expect do
35
+ shipment.transfer_to_location(variant, 1, stock_location)
36
+ end.to change { Spree::Shipment.count }.by(1)
37
+
38
+ new_shipment = order.shipments.last
39
+ expect(new_shipment.number).to_not eq(shipment.number)
40
+ expect(new_shipment.stock_location).to eq(stock_location)
41
+ expect(new_shipment.line_items.count).to eq(1)
42
+ expect(new_shipment.line_items.first.variant).to eq(variant)
43
+ end
44
+ end
45
+ end
46
+
27
47
  # Regression test for https://github.com/spree/spree/issues/4063
28
48
  context "number generation" do
29
49
  before { allow(order).to receive :update! }
@@ -101,6 +121,7 @@ describe Spree::Shipment, type: :model do
101
121
  it 'should equal line items final amount with tax' do
102
122
  shipment = create(:shipment, order: create(:order_with_totals))
103
123
  create :tax_adjustment, adjustable: shipment.order.line_items.first, order: shipment.order
124
+ shipment.order.update!
104
125
  expect(shipment.item_cost).to eql(11.0)
105
126
  end
106
127
  end
@@ -454,7 +475,7 @@ describe Spree::Shipment, type: :model do
454
475
  context "when the shipment is canceled" do
455
476
  let(:address){ create(:address) }
456
477
  let(:order){ create(:order_with_line_items, ship_address: address) }
457
- let(:shipment_with_inventory_units) { create(:shipment, order: order, address: address, state: 'canceled') }
478
+ let(:shipment_with_inventory_units) { create(:shipment, order: order, state: 'canceled') }
458
479
  let(:subject) { shipment_with_inventory_units.ship! }
459
480
  before do
460
481
  allow(order).to receive(:update!)
@@ -565,32 +586,6 @@ describe Spree::Shipment, type: :model do
565
586
  end
566
587
  end
567
588
 
568
- context "after_save" do
569
- context "line item changes" do
570
- before do
571
- shipment.cost = shipment.cost + 10
572
- end
573
-
574
- it "triggers adjustment total recalculation" do
575
- expect(shipment).to receive(:recalculate_adjustments)
576
- shipment.save
577
- end
578
-
579
- it "does not trigger adjustment recalculation if shipment has shipped" do
580
- shipment.state = 'shipped'
581
- expect(shipment).not_to receive(:recalculate_adjustments)
582
- shipment.save
583
- end
584
- end
585
-
586
- context "line item does not change" do
587
- it "does not trigger adjustment total recalculation" do
588
- expect(shipment).not_to receive(:recalculate_adjustments)
589
- shipment.save
590
- end
591
- end
592
- end
593
-
594
589
  context "currency" do
595
590
  it "returns the order currency" do
596
591
  expect(shipment.currency).to eq(order.currency)
@@ -682,7 +677,6 @@ describe Spree::Shipment, type: :model do
682
677
  let(:unshippable_shipment) do
683
678
  create(
684
679
  :shipment,
685
- address: create(:address),
686
680
  stock_location: stock_location,
687
681
  inventory_units: [build(:inventory_unit)]
688
682
  )
@@ -698,7 +692,7 @@ describe Spree::Shipment, type: :model do
698
692
  it 'does not send a confirmation email' do
699
693
  expect {
700
694
  unshippable_shipment.ready!
701
- unshippable_shipment.inventory_units(true).each do |unit|
695
+ unshippable_shipment.inventory_units.reload.each do |unit|
702
696
  expect(unit.state).to eq('shipped')
703
697
  end
704
698
  }.not_to change{ ActionMailer::Base.deliveries.count }
@@ -782,4 +776,31 @@ describe Spree::Shipment, type: :model do
782
776
  expect(shipments.first).to eq(wizard_shipment)
783
777
  end
784
778
  end
779
+
780
+ describe '#selected_shipping_rate_id=' do
781
+ let!(:air_shipping_method) { create(:shipping_method, name: "Air") }
782
+ let(:new_rate) { shipment.add_shipping_method(air_shipping_method) }
783
+
784
+ context 'when the id exists' do
785
+ it 'sets the new shipping rate as selected' do
786
+ expect {
787
+ shipment.selected_shipping_rate_id = new_rate.id
788
+ }.to change { new_rate.selected }.from(false).to(true)
789
+ end
790
+
791
+ it 'sets the old shipping rate as not selected' do
792
+ expect {
793
+ shipment.selected_shipping_rate_id = new_rate.id
794
+ }.to change { shipping_rate.selected }.from(true).to(false)
795
+ end
796
+ end
797
+
798
+ context 'when the id does not exist' do
799
+ it 'raises a RecordNotFound error' do
800
+ expect {
801
+ shipment.selected_shipping_rate_id = -1
802
+ }.to raise_error(ArgumentError)
803
+ end
804
+ end
805
+ end
785
806
  end
@@ -26,11 +26,6 @@ module Spree
26
26
  expect(subject.shipments.size).to eq(1)
27
27
  end
28
28
 
29
- it "puts the order's ship address on the shipments" do
30
- shipments = subject.shipments
31
- expect(shipments.map(&:address)).to eq [order.ship_address]
32
- end
33
-
34
29
  it "builds a shipment for all active stock locations" do
35
30
  subject.shipments.count == StockLocation.count
36
31
  end
@@ -113,7 +113,6 @@ module Spree
113
113
  expect(last_unit.state).to eq 'backordered'
114
114
 
115
115
  expect(shipment.shipping_method).to eq shipping_method
116
- expect(shipment.address).to eq order.ship_address
117
116
  end
118
117
 
119
118
  it 'does not add an inventory unit to a package twice' do