spree_core 3.0.5 → 3.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/Gemfile +3 -0
  4. data/Rakefile +30 -0
  5. data/app/assets/javascripts/spree.js.coffee.erb +1 -1
  6. data/app/models/spree/ability.rb +1 -1
  7. data/app/models/spree/base.rb +3 -1
  8. data/app/models/spree/order_updater.rb +2 -1
  9. data/app/models/spree/price.rb +7 -12
  10. data/app/models/spree/product.rb +3 -2
  11. data/app/models/spree/reimbursement.rb +1 -1
  12. data/app/models/spree/state.rb +2 -0
  13. data/app/models/spree/zone.rb +1 -1
  14. data/lib/spree/core/version.rb +1 -1
  15. data/lib/spree/testing_support/shoulda_matcher_configuration.rb +6 -0
  16. data/script/rails +9 -0
  17. data/spec/fixtures/thinking-cat.jpg +0 -0
  18. data/spec/helpers/base_helper_spec.rb +137 -0
  19. data/spec/helpers/products_helper_spec.rb +224 -0
  20. data/spec/lib/calculated_adjustments_spec.rb +7 -0
  21. data/spec/lib/i18n_spec.rb +123 -0
  22. data/spec/lib/search/base_spec.rb +86 -0
  23. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +101 -0
  24. data/spec/lib/spree/core/controller_helpers/order_spec.rb +95 -0
  25. data/spec/lib/spree/core/controller_helpers/search_spec.rb +17 -0
  26. data/spec/lib/spree/core/controller_helpers/store_spec.rb +16 -0
  27. data/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb +39 -0
  28. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +22 -0
  29. data/spec/lib/spree/core/importer/order_spec.rb +502 -0
  30. data/spec/lib/spree/core/validators/email_spec.rb +53 -0
  31. data/spec/lib/spree/localized_number_spec.rb +38 -0
  32. data/spec/lib/spree/migrations_spec.rb +34 -0
  33. data/spec/lib/spree/money_spec.rb +122 -0
  34. data/spec/lib/tasks/exchanges_spec.rb +136 -0
  35. data/spec/mailers/order_mailer_spec.rb +124 -0
  36. data/spec/mailers/reimbursement_mailer_spec.rb +47 -0
  37. data/spec/mailers/shipment_mailer_spec.rb +63 -0
  38. data/spec/mailers/test_mailer_spec.rb +24 -0
  39. data/spec/models/spree/ability_spec.rb +246 -0
  40. data/spec/models/spree/address_spec.rb +291 -0
  41. data/spec/models/spree/adjustable/adjustments_updater_spec.rb +286 -0
  42. data/spec/models/spree/adjustment_spec.rb +163 -0
  43. data/spec/models/spree/app_configuration_spec.rb +23 -0
  44. data/spec/models/spree/asset_spec.rb +25 -0
  45. data/spec/models/spree/calculator/default_tax_spec.rb +127 -0
  46. data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +25 -0
  47. data/spec/models/spree/calculator/flat_rate_spec.rb +47 -0
  48. data/spec/models/spree/calculator/flexi_rate_spec.rb +41 -0
  49. data/spec/models/spree/calculator/percent_on_line_item_spec.rb +15 -0
  50. data/spec/models/spree/calculator/price_sack_spec.rb +30 -0
  51. data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +51 -0
  52. data/spec/models/spree/calculator/shipping.rb +8 -0
  53. data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +23 -0
  54. data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +13 -0
  55. data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +52 -0
  56. data/spec/models/spree/calculator/shipping/per_item_spec.rb +20 -0
  57. data/spec/models/spree/calculator/shipping/price_sack_spec.rb +29 -0
  58. data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +40 -0
  59. data/spec/models/spree/calculator/tiered_percent_spec.rb +51 -0
  60. data/spec/models/spree/calculator_spec.rb +69 -0
  61. data/spec/models/spree/classification_spec.rb +93 -0
  62. data/spec/models/spree/concerns/display_money_spec.rb +43 -0
  63. data/spec/models/spree/country_spec.rb +18 -0
  64. data/spec/models/spree/credit_card_spec.rb +324 -0
  65. data/spec/models/spree/customer_return_spec.rb +262 -0
  66. data/spec/models/spree/exchange_spec.rb +75 -0
  67. data/spec/models/spree/gateway/bogus_simple.rb +20 -0
  68. data/spec/models/spree/gateway/bogus_spec.rb +13 -0
  69. data/spec/models/spree/gateway_spec.rb +54 -0
  70. data/spec/models/spree/image_spec.rb +5 -0
  71. data/spec/models/spree/inventory_unit_spec.rb +242 -0
  72. data/spec/models/spree/line_item_spec.rb +267 -0
  73. data/spec/models/spree/option_type_spec.rb +14 -0
  74. data/spec/models/spree/option_value_spec.rb +13 -0
  75. data/spec/models/spree/order/address_spec.rb +50 -0
  76. data/spec/models/spree/order/adjustments_spec.rb +29 -0
  77. data/spec/models/spree/order/callbacks_spec.rb +42 -0
  78. data/spec/models/spree/order/checkout_spec.rb +764 -0
  79. data/spec/models/spree/order/currency_updater_spec.rb +32 -0
  80. data/spec/models/spree/order/finalizing_spec.rb +117 -0
  81. data/spec/models/spree/order/helpers_spec.rb +5 -0
  82. data/spec/models/spree/order/payment_spec.rb +214 -0
  83. data/spec/models/spree/order/risk_assessment_spec.rb +84 -0
  84. data/spec/models/spree/order/shipments_spec.rb +43 -0
  85. data/spec/models/spree/order/state_machine_spec.rb +216 -0
  86. data/spec/models/spree/order/tax_spec.rb +84 -0
  87. data/spec/models/spree/order/totals_spec.rb +24 -0
  88. data/spec/models/spree/order/updating_spec.rb +18 -0
  89. data/spec/models/spree/order/validations_spec.rb +15 -0
  90. data/spec/models/spree/order_contents_spec.rb +256 -0
  91. data/spec/models/spree/order_inventory_spec.rb +228 -0
  92. data/spec/models/spree/order_merger_spec.rb +133 -0
  93. data/spec/models/spree/order_spec.rb +954 -0
  94. data/spec/models/spree/order_updater_spec.rb +283 -0
  95. data/spec/models/spree/payment/gateway_options_spec.rb +119 -0
  96. data/spec/models/spree/payment_method_spec.rb +95 -0
  97. data/spec/models/spree/payment_spec.rb +926 -0
  98. data/spec/models/spree/preference_spec.rb +80 -0
  99. data/spec/models/spree/preferences/configuration_spec.rb +30 -0
  100. data/spec/models/spree/preferences/preferable_spec.rb +348 -0
  101. data/spec/models/spree/preferences/scoped_store_spec.rb +58 -0
  102. data/spec/models/spree/preferences/store_spec.rb +46 -0
  103. data/spec/models/spree/price_spec.rb +42 -0
  104. data/spec/models/spree/product/scopes_spec.rb +148 -0
  105. data/spec/models/spree/product_duplicator_spec.rb +103 -0
  106. data/spec/models/spree/product_filter_spec.rb +26 -0
  107. data/spec/models/spree/product_option_type_spec.rb +5 -0
  108. data/spec/models/spree/product_property_spec.rb +11 -0
  109. data/spec/models/spree/product_spec.rb +474 -0
  110. data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +50 -0
  111. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +148 -0
  112. data/spec/models/spree/promotion/actions/create_line_items_spec.rb +86 -0
  113. data/spec/models/spree/promotion/actions/free_shipping_spec.rb +36 -0
  114. data/spec/models/spree/promotion/rules/first_order_spec.rb +75 -0
  115. data/spec/models/spree/promotion/rules/item_total_spec.rb +282 -0
  116. data/spec/models/spree/promotion/rules/one_use_per_user_spec.rb +42 -0
  117. data/spec/models/spree/promotion/rules/option_value_spec.rb +90 -0
  118. data/spec/models/spree/promotion/rules/product_spec.rb +143 -0
  119. data/spec/models/spree/promotion/rules/taxon_spec.rb +102 -0
  120. data/spec/models/spree/promotion/rules/user_logged_in_spec.rb +27 -0
  121. data/spec/models/spree/promotion/rules/user_spec.rb +37 -0
  122. data/spec/models/spree/promotion_action_spec.rb +10 -0
  123. data/spec/models/spree/promotion_category_spec.rb +17 -0
  124. data/spec/models/spree/promotion_handler/cart_spec.rb +102 -0
  125. data/spec/models/spree/promotion_handler/coupon_spec.rb +323 -0
  126. data/spec/models/spree/promotion_handler/free_shipping_spec.rb +48 -0
  127. data/spec/models/spree/promotion_handler/page_spec.rb +44 -0
  128. data/spec/models/spree/promotion_rule_spec.rb +29 -0
  129. data/spec/models/spree/promotion_spec.rb +603 -0
  130. data/spec/models/spree/property_spec.rb +5 -0
  131. data/spec/models/spree/prototype_spec.rb +5 -0
  132. data/spec/models/spree/refund_spec.rb +195 -0
  133. data/spec/models/spree/reimbursement/credit_spec.rb +36 -0
  134. data/spec/models/spree/reimbursement/reimbursement_type_engine_spec.rb +140 -0
  135. data/spec/models/spree/reimbursement/reimbursement_type_validator_spec.rb +83 -0
  136. data/spec/models/spree/reimbursement_performer_spec.rb +30 -0
  137. data/spec/models/spree/reimbursement_spec.rb +215 -0
  138. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +51 -0
  139. data/spec/models/spree/reimbursement_type/credit_spec.rb +53 -0
  140. data/spec/models/spree/reimbursement_type/exchange_spec.rb +46 -0
  141. data/spec/models/spree/reimbursement_type/original_payment_spec.rb +55 -0
  142. data/spec/models/spree/return_authorization_spec.rb +250 -0
  143. data/spec/models/spree/return_item/eligibility_validator/default_spec.rb +77 -0
  144. data/spec/models/spree/return_item/eligibility_validator/inventory_shipped_spec.rb +58 -0
  145. data/spec/models/spree/return_item/eligibility_validator/no_reimbursements_spec.rb +61 -0
  146. data/spec/models/spree/return_item/eligibility_validator/order_completed_spec.rb +32 -0
  147. data/spec/models/spree/return_item/eligibility_validator/rma_required_spec.rb +29 -0
  148. data/spec/models/spree/return_item/eligibility_validator/time_since_purchase_spec.rb +35 -0
  149. data/spec/models/spree/return_item/exchange_variant_eligibility/same_option_value_spec.rb +65 -0
  150. data/spec/models/spree/return_item/exchange_variant_eligibility/same_product_spec.rb +43 -0
  151. data/spec/models/spree/return_item_spec.rb +682 -0
  152. data/spec/models/spree/returns_calculator_spec.rb +14 -0
  153. data/spec/models/spree/shipment_spec.rb +740 -0
  154. data/spec/models/spree/shipping_calculator_spec.rb +45 -0
  155. data/spec/models/spree/shipping_category_spec.rb +5 -0
  156. data/spec/models/spree/shipping_method_spec.rb +88 -0
  157. data/spec/models/spree/shipping_rate_spec.rb +141 -0
  158. data/spec/models/spree/state_spec.rb +18 -0
  159. data/spec/models/spree/stock/availability_validator_spec.rb +36 -0
  160. data/spec/models/spree/stock/content_item_spec.rb +22 -0
  161. data/spec/models/spree/stock/coordinator_spec.rb +51 -0
  162. data/spec/models/spree/stock/differentiator_spec.rb +39 -0
  163. data/spec/models/spree/stock/estimator_spec.rb +154 -0
  164. data/spec/models/spree/stock/inventory_unit_builder_spec.rb +38 -0
  165. data/spec/models/spree/stock/package_spec.rb +194 -0
  166. data/spec/models/spree/stock/packer_spec.rb +70 -0
  167. data/spec/models/spree/stock/prioritizer_spec.rb +125 -0
  168. data/spec/models/spree/stock/quantifier_spec.rb +97 -0
  169. data/spec/models/spree/stock/splitter/backordered_spec.rb +29 -0
  170. data/spec/models/spree/stock/splitter/base_spec.rb +21 -0
  171. data/spec/models/spree/stock/splitter/shipping_category_spec.rb +47 -0
  172. data/spec/models/spree/stock/splitter/weight_spec.rb +32 -0
  173. data/spec/models/spree/stock_item_spec.rb +410 -0
  174. data/spec/models/spree/stock_location_spec.rb +243 -0
  175. data/spec/models/spree/stock_movement_spec.rb +56 -0
  176. data/spec/models/spree/stock_transfer_spec.rb +50 -0
  177. data/spec/models/spree/store_spec.rb +50 -0
  178. data/spec/models/spree/tax_category_spec.rb +27 -0
  179. data/spec/models/spree/tax_rate_spec.rb +382 -0
  180. data/spec/models/spree/taxon_spec.rb +74 -0
  181. data/spec/models/spree/taxonomy_spec.rb +18 -0
  182. data/spec/models/spree/tracker_spec.rb +21 -0
  183. data/spec/models/spree/user_spec.rb +130 -0
  184. data/spec/models/spree/validations/db_maximum_length_validator_spec.rb +24 -0
  185. data/spec/models/spree/variant_spec.rb +523 -0
  186. data/spec/models/spree/zone_spec.rb +444 -0
  187. data/spec/spec_helper.rb +74 -0
  188. data/spec/support/big_decimal.rb +5 -0
  189. data/spec/support/concerns/adjustment_source_spec.rb +23 -0
  190. data/spec/support/concerns/default_price_spec.rb +28 -0
  191. data/spec/support/rake.rb +13 -0
  192. data/spec/support/test_gateway.rb +2 -0
  193. data/spree_core.gemspec +48 -0
  194. metadata +185 -4
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::ReturnItem::EligibilityValidator::RMARequired, :type => :model do
4
+ let(:return_item) { create(:return_item) }
5
+ let(:validator) { Spree::ReturnItem::EligibilityValidator::RMARequired.new(return_item) }
6
+
7
+ describe "#eligible_for_return?" do
8
+ subject { validator.eligible_for_return? }
9
+
10
+ context "there is an rma on the return item" do
11
+ it "returns true" do
12
+ expect(subject).to be true
13
+ end
14
+ end
15
+
16
+ context "there is no rma on the return item" do
17
+ before { allow(return_item).to receive(:return_authorization).and_return(nil) }
18
+
19
+ it "returns false" do
20
+ expect(subject).to be false
21
+ end
22
+
23
+ it "sets an error" do
24
+ subject
25
+ expect(validator.errors[:rma_required]).to eq Spree.t('return_item_rma_ineligible')
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::ReturnItem::EligibilityValidator::TimeSincePurchase, :type => :model do
4
+ let(:inventory_unit) { create(:inventory_unit, order: create(:shipped_order)) }
5
+ let(:return_item) { create(:return_item, inventory_unit: inventory_unit) }
6
+ let(:validator) { Spree::ReturnItem::EligibilityValidator::TimeSincePurchase.new(return_item) }
7
+
8
+ describe "#eligible_for_return?" do
9
+ subject { validator.eligible_for_return? }
10
+
11
+ context "it is within the return timeframe" do
12
+ it "returns true" do
13
+ completed_at = return_item.inventory_unit.order.completed_at - (Spree::Config[:return_eligibility_number_of_days].days / 2)
14
+ return_item.inventory_unit.order.update_attributes(completed_at: completed_at)
15
+ expect(subject).to be true
16
+ end
17
+ end
18
+
19
+ context "it is past the return timeframe" do
20
+ before do
21
+ completed_at = return_item.inventory_unit.order.completed_at - Spree::Config[:return_eligibility_number_of_days].days - 1.day
22
+ return_item.inventory_unit.order.update_attributes(completed_at: completed_at)
23
+ end
24
+
25
+ it "returns false" do
26
+ expect(subject).to be false
27
+ end
28
+
29
+ it "sets an error" do
30
+ subject
31
+ expect(validator.errors[:number_of_days]).to eq Spree.t('return_item_time_period_ineligible')
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ module ReturnItem::ExchangeVariantEligibility
5
+ describe SameOptionValue, :type => :model do
6
+ describe ".eligible_variants" do
7
+ let(:color_option_type) { create(:option_type, name: "color") }
8
+ let(:waist_option_type) { create(:option_type, name: "waist") }
9
+ let(:inseam_option_type) { create(:option_type, name: "inseam") }
10
+
11
+ let(:blue_option_value) { create(:option_value, name: "blue", option_type: color_option_type) }
12
+ let(:red_option_value) { create(:option_value, name: "red", option_type: color_option_type) }
13
+
14
+ let(:three_two_waist_option_value) { create(:option_value, name: 32, option_type: waist_option_type) }
15
+ let(:three_four_waist_option_value) { create(:option_value, name: 34, option_type: waist_option_type) }
16
+
17
+ let(:three_zero_inseam_option_value) { create(:option_value, name: 30, option_type: inseam_option_type) }
18
+ let(:three_one_inseam_option_value) { create(:option_value, name: 31, option_type: inseam_option_type) }
19
+
20
+ let(:product) { create(:product, option_types: [color_option_type, waist_option_type, inseam_option_type]) }
21
+
22
+ let!(:variant) { create(:variant, product: product, option_values: [blue_option_value, three_two_waist_option_value, three_zero_inseam_option_value]) }
23
+ let!(:same_option_values_variant) { create(:variant, product: product, option_values: [blue_option_value, three_two_waist_option_value, three_one_inseam_option_value]) }
24
+ let!(:different_color_option_value_variant) { create(:variant, product: product, option_values: [red_option_value, three_two_waist_option_value, three_one_inseam_option_value]) }
25
+ let!(:different_waist_option_value_variant) { create(:variant, product: product, option_values: [blue_option_value, three_four_waist_option_value, three_one_inseam_option_value]) }
26
+
27
+ before do
28
+ @original_option_type_restrictions = SameOptionValue.option_type_restrictions
29
+ SameOptionValue.option_type_restrictions = ["color", "waist"]
30
+ end
31
+
32
+ after { SameOptionValue.option_type_restrictions = @original_option_type_restrictions }
33
+
34
+ subject { SameOptionValue.eligible_variants(variant.reload) }
35
+
36
+ it "returns all other variants for the same product with the same option value for the specified option type" do
37
+ Spree::StockItem.update_all(count_on_hand: 10)
38
+
39
+ expect(subject.sort).to eq [variant, same_option_values_variant].sort
40
+ end
41
+
42
+ it "does not return variants for another product" do
43
+ other_product_variant = create(:variant)
44
+ expect(subject).not_to include other_product_variant
45
+ end
46
+
47
+ context "no option value restrictions are specified" do
48
+ before do
49
+ @original_option_type_restrictions = SameOptionValue.option_type_restrictions
50
+ SameOptionValue.option_type_restrictions = []
51
+ end
52
+
53
+ after { SameOptionValue.option_type_restrictions = @original_option_type_restrictions }
54
+
55
+ it "returns all variants for the product" do
56
+ Spree::StockItem.update_all(count_on_hand: 10)
57
+
58
+ expect(subject.sort).to eq [variant, same_option_values_variant, different_waist_option_value_variant, different_color_option_value_variant].sort
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ module ReturnItem::ExchangeVariantEligibility
5
+ describe SameProduct, :type => :model do
6
+ describe ".eligible_variants" do
7
+
8
+ context "product has no variants" do
9
+ it "returns the master variant for the same product" do
10
+ product = create(:product)
11
+ product.master.stock_items.first.update_column(:count_on_hand, 10)
12
+
13
+ expect(SameProduct.eligible_variants(product.master)).to eq [product.master]
14
+ end
15
+ end
16
+
17
+ context "product has variants" do
18
+ it "returns all variants for the same product" do
19
+ product = create(:product, variants: 3.times.map { create(:variant) })
20
+ product.variants.map { |v| v.stock_items.first.update_column(:count_on_hand, 10) }
21
+
22
+ expect(SameProduct.eligible_variants(product.variants.first).sort).to eq product.variants.sort
23
+ end
24
+ end
25
+
26
+ it "does not return variants for another product" do
27
+ variant = create(:variant)
28
+ other_product_variant = create(:variant)
29
+ expect(SameProduct.eligible_variants(variant)).not_to include other_product_variant
30
+ end
31
+
32
+ it "only returns variants that are on hand" do
33
+ product = create(:product, variants: 2.times.map { create(:variant) })
34
+ in_stock_variant = product.variants.first
35
+
36
+ in_stock_variant.stock_items.first.update_column(:count_on_hand, 10)
37
+ expect(SameProduct.eligible_variants(in_stock_variant)).to eq [in_stock_variant]
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,682 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples "an invalid state transition" do |status, expected_status|
4
+ let(:status) { status }
5
+
6
+ it "cannot transition to #{expected_status}" do
7
+ expect { subject }.to raise_error(StateMachines::InvalidTransition)
8
+ end
9
+ end
10
+
11
+ describe Spree::ReturnItem, :type => :model do
12
+
13
+ all_reception_statuses = Spree::ReturnItem.state_machines[:reception_status].states.map(&:name).map(&:to_s)
14
+ all_acceptance_statuses = Spree::ReturnItem.state_machines[:acceptance_status].states.map(&:name).map(&:to_s)
15
+
16
+ before do
17
+ allow_any_instance_of(Spree::Order).to receive_messages(return!: true)
18
+ end
19
+
20
+ describe '#receive!' do
21
+ let(:now) { Time.now }
22
+ let(:inventory_unit) { create(:inventory_unit, state: 'shipped') }
23
+ let(:return_item) { create(:return_item, inventory_unit: inventory_unit) }
24
+
25
+ before do
26
+ inventory_unit.update_attributes!(state: 'shipped')
27
+ return_item.update_attributes!(reception_status: 'awaiting')
28
+ allow(return_item).to receive(:eligible_for_return?).and_return(true)
29
+ end
30
+
31
+ subject { return_item.receive! }
32
+
33
+
34
+ it 'returns the inventory unit' do
35
+ subject
36
+ expect(inventory_unit.reload.state).to eq 'returned'
37
+ end
38
+
39
+ it 'attempts to accept the return item' do
40
+ expect(return_item).to receive(:attempt_accept)
41
+ subject
42
+ end
43
+
44
+ context 'with a stock location' do
45
+ let(:stock_item) { inventory_unit.find_stock_item }
46
+ let!(:customer_return) { create(:customer_return_without_return_items, return_items: [return_item], stock_location_id: inventory_unit.shipment.stock_location_id) }
47
+
48
+ before do
49
+ inventory_unit.update_attributes!(state: 'shipped')
50
+ return_item.update_attributes!(reception_status: 'awaiting')
51
+ end
52
+
53
+ it 'increases the count on hand' do
54
+ expect { subject }.to change { stock_item.reload.count_on_hand }.by(1)
55
+ end
56
+
57
+ context "when the variant is not resellable" do
58
+ before { return_item.update_attributes(resellable: false) }
59
+ it { expect { subject }.not_to change { stock_item.reload.count_on_hand } }
60
+ end
61
+
62
+ context 'when variant does not track inventory' do
63
+ before do
64
+ inventory_unit.update_attributes!(state: 'shipped')
65
+ inventory_unit.variant.update_attributes!(track_inventory: false)
66
+ return_item.update_attributes!(reception_status: 'awaiting')
67
+ end
68
+
69
+ it 'does not increase the count on hand' do
70
+ expect { subject }.to_not change { stock_item.reload.count_on_hand }
71
+ end
72
+ end
73
+
74
+ context 'when the restock_inventory preference is false' do
75
+ before do
76
+ Spree::Config[:restock_inventory] = false
77
+ end
78
+
79
+ it 'does not increase the count on hand' do
80
+ expect { subject }.to_not change { stock_item.reload.count_on_hand }
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#display_pre_tax_amount" do
87
+ let(:pre_tax_amount) { 21.22 }
88
+ let(:return_item) { build(:return_item, pre_tax_amount: pre_tax_amount) }
89
+
90
+ it "returns a Spree::Money" do
91
+ expect(return_item.display_pre_tax_amount).to eq(Spree::Money.new(pre_tax_amount))
92
+ end
93
+ end
94
+
95
+ describe ".default_refund_amount_calculator" do
96
+ it "defaults to the default refund amount calculator" do
97
+ expect(Spree::ReturnItem.refund_amount_calculator).to eq Spree::Calculator::Returns::DefaultRefundAmount
98
+ end
99
+ end
100
+
101
+ describe "pre_tax_amount calculations on create" do
102
+ let(:inventory_unit) { build(:inventory_unit) }
103
+ before { subject.save! }
104
+
105
+ context "pre tax amount is not specified" do
106
+ subject { build(:return_item, inventory_unit: inventory_unit) }
107
+
108
+ context "not an exchange" do
109
+ it { expect(subject.pre_tax_amount).to eq Spree::Calculator::Returns::DefaultRefundAmount.new.compute(subject) }
110
+ end
111
+
112
+ context "an exchange" do
113
+ subject { build(:exchange_return_item) }
114
+
115
+ it { expect(subject.pre_tax_amount).to eq 0.0 }
116
+ end
117
+ end
118
+
119
+ context "pre tax amount is specified" do
120
+ subject { build(:return_item, inventory_unit: inventory_unit, pre_tax_amount: 100) }
121
+
122
+ it { expect(subject.pre_tax_amount).to eq 100 }
123
+ end
124
+ end
125
+
126
+ describe ".from_inventory_unit" do
127
+ let(:inventory_unit) { build(:inventory_unit) }
128
+
129
+ subject { Spree::ReturnItem.from_inventory_unit(inventory_unit) }
130
+
131
+ context "with a cancelled return item" do
132
+ let!(:return_item) { create(:return_item, inventory_unit: inventory_unit, reception_status: 'cancelled') }
133
+
134
+ it { is_expected.not_to be_persisted }
135
+ end
136
+
137
+ context "with a non-cancelled return item" do
138
+ let!(:return_item) { create(:return_item, inventory_unit: inventory_unit) }
139
+
140
+ it { is_expected.to be_persisted }
141
+ end
142
+ end
143
+
144
+ describe "reception_status state_machine" do
145
+ subject(:return_item) { create(:return_item) }
146
+
147
+ it "starts off in the awaiting state" do
148
+ expect(return_item).to be_awaiting
149
+ end
150
+ end
151
+
152
+ describe "acceptance_status state_machine" do
153
+ subject(:return_item) { create(:return_item) }
154
+
155
+ it "starts off in the pending state" do
156
+ expect(return_item).to be_pending
157
+ end
158
+ end
159
+
160
+ describe "#receive" do
161
+ let(:inventory_unit) { create(:inventory_unit, order: create(:shipped_order)) }
162
+ let(:return_item) { create(:return_item, reception_status: status, inventory_unit: inventory_unit) }
163
+
164
+ subject { return_item.receive! }
165
+
166
+ context "awaiting status" do
167
+ let(:status) { 'awaiting' }
168
+
169
+ before do
170
+ expect(return_item.inventory_unit).to receive(:return!)
171
+ end
172
+
173
+ before { subject }
174
+
175
+ it "transitions successfully" do
176
+ expect(return_item).to be_received
177
+ end
178
+ end
179
+
180
+ (all_reception_statuses - ['awaiting']).each do |invalid_transition_status|
181
+ context "return_item has a reception status of #{invalid_transition_status}" do
182
+ it_behaves_like "an invalid state transition", invalid_transition_status, 'received'
183
+ end
184
+ end
185
+ end
186
+
187
+ describe "#cancel" do
188
+ let(:return_item) { create(:return_item, reception_status: status) }
189
+
190
+ subject { return_item.cancel! }
191
+
192
+ context "awaiting status" do
193
+ let(:status) { 'awaiting' }
194
+
195
+ before { subject }
196
+
197
+ it "transitions successfully" do
198
+ expect(return_item).to be_cancelled
199
+ end
200
+ end
201
+
202
+ (all_reception_statuses - ['awaiting']).each do |invalid_transition_status|
203
+ context "return_item has a reception status of #{invalid_transition_status}" do
204
+ it_behaves_like "an invalid state transition", invalid_transition_status, 'cancelled'
205
+ end
206
+ end
207
+ end
208
+
209
+ describe "#give" do
210
+ let(:return_item) { create(:return_item, reception_status: status) }
211
+
212
+ subject { return_item.give! }
213
+
214
+ context "awaiting status" do
215
+ let(:status) { 'awaiting' }
216
+
217
+ before { subject }
218
+
219
+ it "transitions successfully" do
220
+ expect(return_item).to be_given_to_customer
221
+ end
222
+ end
223
+
224
+ (all_reception_statuses - ['awaiting']).each do |invalid_transition_status|
225
+ context "return_item has a reception status of #{invalid_transition_status}" do
226
+ it_behaves_like "an invalid state transition", invalid_transition_status, 'give_to_customer'
227
+ end
228
+ end
229
+ end
230
+
231
+ describe "#attempt_accept" do
232
+ let(:return_item) { create(:return_item, acceptance_status: status) }
233
+ let(:validator_errors) { {} }
234
+ let(:validator_double) { double(errors: validator_errors) }
235
+
236
+ subject { return_item.attempt_accept! }
237
+
238
+ before do
239
+ allow(return_item).to receive(:validator).and_return(validator_double)
240
+ end
241
+
242
+ context "pending status" do
243
+ let(:status) { 'pending' }
244
+
245
+ before do
246
+ allow(return_item).to receive(:eligible_for_return?).and_return(true)
247
+ subject
248
+ end
249
+
250
+ it "transitions successfully" do
251
+ expect(return_item).to be_accepted
252
+ end
253
+
254
+ it "has no acceptance status errors" do
255
+ expect(return_item.acceptance_status_errors).to be_empty
256
+ end
257
+ end
258
+
259
+ (all_acceptance_statuses - ['accepted', 'pending']).each do |invalid_transition_status|
260
+ context "return_item has an acceptance status of #{invalid_transition_status}" do
261
+ it_behaves_like "an invalid state transition", invalid_transition_status, 'accepted'
262
+ end
263
+ end
264
+
265
+ context "not eligible for return" do
266
+ let(:status) { 'pending' }
267
+ let(:validator_errors) { { number_of_days: "Return Item is outside the eligible time period" } }
268
+
269
+ before do
270
+ allow(return_item).to receive(:eligible_for_return?).and_return(false)
271
+ end
272
+
273
+ context "manual intervention required" do
274
+ before do
275
+ allow(return_item).to receive(:requires_manual_intervention?).and_return(true)
276
+ subject
277
+ end
278
+
279
+ it "transitions to manual intervention required" do
280
+ expect(return_item).to be_manual_intervention_required
281
+ end
282
+
283
+ it "sets the acceptance status errors" do
284
+ expect(return_item.acceptance_status_errors).to eq validator_errors
285
+ end
286
+ end
287
+
288
+ context "manual intervention not required" do
289
+ before do
290
+ allow(return_item).to receive(:requires_manual_intervention?).and_return(false)
291
+ subject
292
+ end
293
+
294
+ it "transitions to rejected" do
295
+ expect(return_item).to be_rejected
296
+ end
297
+
298
+ it "sets the acceptance status errors" do
299
+ expect(return_item.acceptance_status_errors).to eq validator_errors
300
+ end
301
+ end
302
+ end
303
+ end
304
+
305
+ describe "#reject" do
306
+ let(:return_item) { create(:return_item, acceptance_status: status) }
307
+
308
+ subject { return_item.reject! }
309
+
310
+ context "pending status" do
311
+ let(:status) { 'pending' }
312
+
313
+ before { subject }
314
+
315
+ it "transitions successfully" do
316
+ expect(return_item).to be_rejected
317
+ end
318
+
319
+ it "has no acceptance status errors" do
320
+ expect(return_item.acceptance_status_errors).to be_empty
321
+ end
322
+ end
323
+
324
+ (all_acceptance_statuses - ['accepted', 'pending', 'manual_intervention_required']).each do |invalid_transition_status|
325
+ context "return_item has an acceptance status of #{invalid_transition_status}" do
326
+ it_behaves_like "an invalid state transition", invalid_transition_status, 'rejected'
327
+ end
328
+ end
329
+ end
330
+
331
+ describe "#accept" do
332
+ let(:return_item) { create(:return_item, acceptance_status: status) }
333
+
334
+ subject { return_item.accept! }
335
+
336
+ context "pending status" do
337
+ let(:status) { 'pending' }
338
+
339
+ before { subject }
340
+
341
+ it "transitions successfully" do
342
+ expect(return_item).to be_accepted
343
+ end
344
+
345
+ it "has no acceptance status errors" do
346
+ expect(return_item.acceptance_status_errors).to be_empty
347
+ end
348
+ end
349
+
350
+ (all_acceptance_statuses - ['accepted', 'pending', 'manual_intervention_required']).each do |invalid_transition_status|
351
+ context "return_item has an acceptance status of #{invalid_transition_status}" do
352
+ it_behaves_like "an invalid state transition", invalid_transition_status, 'accepted'
353
+ end
354
+ end
355
+ end
356
+
357
+ describe "#require_manual_intervention" do
358
+ let(:return_item) { create(:return_item, acceptance_status: status) }
359
+
360
+ subject { return_item.require_manual_intervention! }
361
+
362
+ context "pending status" do
363
+ let(:status) { 'pending' }
364
+
365
+ before { subject }
366
+
367
+ it "transitions successfully" do
368
+ expect(return_item).to be_manual_intervention_required
369
+ end
370
+
371
+ it "has no acceptance status errors" do
372
+ expect(return_item.acceptance_status_errors).to be_empty
373
+ end
374
+ end
375
+
376
+ (all_acceptance_statuses - ['accepted', 'pending', 'manual_intervention_required']).each do |invalid_transition_status|
377
+ context "return_item has an acceptance status of #{invalid_transition_status}" do
378
+ it_behaves_like "an invalid state transition", invalid_transition_status, 'manual_intervention_required'
379
+ end
380
+ end
381
+ end
382
+
383
+ describe 'validity for reimbursements' do
384
+ let(:return_item) { create(:return_item, acceptance_status: acceptance_status) }
385
+ let(:acceptance_status) { 'pending' }
386
+
387
+ before { return_item.reimbursement = build(:reimbursement) }
388
+
389
+ subject { return_item }
390
+
391
+ context 'when acceptance_status is accepted' do
392
+ let(:acceptance_status) { 'accepted' }
393
+
394
+ it 'is valid' do
395
+ expect(subject).to be_valid
396
+ end
397
+ end
398
+
399
+ context 'when acceptance_status is accepted' do
400
+ let(:acceptance_status) { 'pending' }
401
+
402
+ it 'is valid' do
403
+ expect(subject).to_not be_valid
404
+ expect(subject.errors.messages).to eq({reimbursement: [I18n.t(:cannot_be_associated_unless_accepted, scope: 'activerecord.errors.models.spree/return_item.attributes.reimbursement')]})
405
+ end
406
+ end
407
+ end
408
+
409
+ describe "#exchange_requested?" do
410
+ context "exchange variant exists" do
411
+ before { allow(subject).to receive(:exchange_variant) { mock_model(Spree::Variant) } }
412
+ it { expect(subject.exchange_requested?).to eq true }
413
+ end
414
+ context "exchange variant does not exist" do
415
+ before { allow(subject).to receive(:exchange_variant) { nil } }
416
+ it { expect(subject.exchange_requested?).to eq false }
417
+ end
418
+ end
419
+
420
+ describe "#exchange_processed?" do
421
+ context "exchange inventory unit exists" do
422
+ before { allow(subject).to receive(:exchange_inventory_unit) { mock_model(Spree::InventoryUnit) } }
423
+ it { expect(subject.exchange_processed?).to eq true }
424
+ end
425
+ context "exchange inventory unit does not exist" do
426
+ before { allow(subject).to receive(:exchange_inventory_unit) { nil } }
427
+ it { expect(subject.exchange_processed?).to eq false }
428
+ end
429
+ end
430
+
431
+ describe "#exchange_required?" do
432
+ context "exchange has been requested and not yet processed" do
433
+ before do
434
+ allow(subject).to receive(:exchange_requested?) { true }
435
+ allow(subject).to receive(:exchange_processed?) { false }
436
+ end
437
+
438
+ it { expect(subject.exchange_required?).to be true }
439
+ end
440
+
441
+ context "exchange has not been requested" do
442
+ before { allow(subject).to receive(:exchange_requested?) { false } }
443
+ it { expect(subject.exchange_required?).to be false }
444
+ end
445
+
446
+ context "exchange has been requested and processed" do
447
+ before do
448
+ allow(subject).to receive(:exchange_requested?) { true }
449
+ allow(subject).to receive(:exchange_processed?) { true }
450
+ end
451
+ it { expect(subject.exchange_required?).to be false }
452
+ end
453
+ end
454
+
455
+ describe "#eligible_exchange_variants" do
456
+ it "uses the exchange variant calculator to compute possible variants to exchange for" do
457
+ return_item = build(:return_item)
458
+ expect(Spree::ReturnItem.exchange_variant_engine).to receive(:eligible_variants).with(return_item.variant)
459
+ return_item.eligible_exchange_variants
460
+ end
461
+ end
462
+
463
+ describe ".exchange_variant_engine" do
464
+ it "defaults to the same product calculator" do
465
+ expect(Spree::ReturnItem.exchange_variant_engine).to eq Spree::ReturnItem::ExchangeVariantEligibility::SameProduct
466
+ end
467
+ end
468
+
469
+ describe "exchange pre_tax_amount" do
470
+ let(:return_item) { build(:return_item) }
471
+
472
+ context "the return item is intended to be exchanged" do
473
+ before do
474
+ return_item.inventory_unit.variant.update_column(:track_inventory, false)
475
+ return_item.exchange_variant = return_item.inventory_unit.variant
476
+ end
477
+
478
+ it do
479
+ return_item.pre_tax_amount = 5.0
480
+ return_item.save!
481
+ expect(return_item.reload.pre_tax_amount).to eq 0.0
482
+ end
483
+ end
484
+
485
+ context "the return item is not intended to be exchanged" do
486
+ it do
487
+ return_item.pre_tax_amount = 5.0
488
+ return_item.save!
489
+ expect(return_item.reload.pre_tax_amount).to eq 5.0
490
+ end
491
+ end
492
+ end
493
+
494
+ describe "#build_exchange_inventory_unit" do
495
+ let(:return_item) { build(:return_item) }
496
+ subject { return_item.build_exchange_inventory_unit }
497
+
498
+ context "the return item is intended to be exchanged" do
499
+ before { allow(return_item).to receive(:exchange_variant).and_return(mock_model(Spree::Variant)) }
500
+
501
+ context "an exchange inventory unit already exists" do
502
+ before { allow(return_item).to receive(:exchange_inventory_unit).and_return(mock_model(Spree::InventoryUnit)) }
503
+ it { expect(subject).to be_nil }
504
+ end
505
+
506
+ context "no exchange inventory unit exists" do
507
+ it "builds a pending inventory unit with references to the return item, variant, and previous inventory unit" do
508
+ expect(subject.variant).to eq return_item.exchange_variant
509
+ expect(subject.pending).to eq true
510
+ expect(subject).not_to be_persisted
511
+ expect(subject.original_return_item).to eq return_item
512
+ expect(subject.line_item).to eq return_item.inventory_unit.line_item
513
+ expect(subject.order).to eq return_item.inventory_unit.order
514
+ end
515
+ end
516
+ end
517
+
518
+ context "the return item is not intended to be exchanged" do
519
+ it { expect(subject).to be_nil }
520
+ end
521
+ end
522
+
523
+ describe "#exchange_shipment" do
524
+ it "returns the exchange inventory unit's shipment" do
525
+ inventory_unit = build(:inventory_unit)
526
+ subject.exchange_inventory_unit = inventory_unit
527
+ expect(subject.exchange_shipment).to eq inventory_unit.shipment
528
+ end
529
+ end
530
+
531
+ describe "#shipment" do
532
+ it "returns the inventory unit's shipment" do
533
+ inventory_unit = build(:inventory_unit)
534
+ subject.inventory_unit = inventory_unit
535
+ expect(subject.shipment).to eq inventory_unit.shipment
536
+ end
537
+ end
538
+
539
+ describe 'inventory_unit uniqueness' do
540
+ let!(:old_return_item) { create(:return_item, reception_status: old_reception_status) }
541
+ let(:old_reception_status) { 'awaiting' }
542
+
543
+ subject do
544
+ build(:return_item, {
545
+ return_authorization: old_return_item.return_authorization,
546
+ inventory_unit: old_return_item.inventory_unit,
547
+ })
548
+ end
549
+
550
+ context 'with other awaiting return items exist for the same inventory unit' do
551
+ let(:old_reception_status) { 'awaiting' }
552
+
553
+ it 'cancels the others' do
554
+ expect {
555
+ subject.save!
556
+ }.to change { old_return_item.reload.reception_status }.from('awaiting').to('cancelled')
557
+ end
558
+
559
+ it 'does not cancel itself' do
560
+ subject.save!
561
+ expect(subject).to be_awaiting
562
+ end
563
+ end
564
+
565
+ context 'with other cancelled return items exist for the same inventory unit' do
566
+ let(:old_reception_status) { 'cancelled' }
567
+
568
+ it 'succeeds' do
569
+ expect { subject.save! }.to_not raise_error
570
+ end
571
+ end
572
+
573
+ context 'with other received return items exist for the same inventory unit' do
574
+ let(:old_reception_status) { 'received' }
575
+
576
+ it 'is invalid' do
577
+ expect(subject).to_not be_valid
578
+ expect(subject.errors.to_a).to eq ["Inventory unit #{subject.inventory_unit_id} has already been taken by return item #{old_return_item.id}"]
579
+ end
580
+ end
581
+
582
+ context 'with other given_to_customer return items exist for the same inventory unit' do
583
+ let(:old_reception_status) { 'given_to_customer' }
584
+
585
+ it 'is invalid' do
586
+ expect(subject).to_not be_valid
587
+ expect(subject.errors.to_a).to eq ["Inventory unit #{subject.inventory_unit_id} has already been taken by return item #{old_return_item.id}"]
588
+ end
589
+ end
590
+ end
591
+
592
+ describe "valid exchange variant" do
593
+ subject { return_item }
594
+
595
+ before { subject.save }
596
+
597
+ context "return item doesn't have an exchange variant" do
598
+ let(:return_item) { create(:return_item) }
599
+
600
+ it "is valid" do
601
+ expect(subject).to be_valid
602
+ end
603
+ end
604
+
605
+ context "return item has an exchange variant" do
606
+ let(:return_item) { create(:exchange_return_item) }
607
+ let(:exchange_variant) { create(:on_demand_variant, product: return_item.inventory_unit.variant.product) }
608
+
609
+ context "the exchange variant is eligible" do
610
+ before { return_item.exchange_variant = exchange_variant }
611
+
612
+ it "is valid" do
613
+ expect(subject).to be_valid
614
+ end
615
+ end
616
+
617
+ context "the exchange variant is not eligible" do
618
+ context "new return item" do
619
+ let(:return_item) { build(:return_item) }
620
+ let(:exchange_variant) { create(:variant, product: return_item.inventory_unit.variant.product) }
621
+
622
+ before { return_item.exchange_variant = exchange_variant }
623
+
624
+ it "is invalid" do
625
+ expect(subject).to_not be_valid
626
+ end
627
+
628
+ it "adds an error message about the invalid exchange variant" do
629
+ subject.valid?
630
+ expect(subject.errors.to_a).to eq ["Invalid exchange variant."]
631
+ end
632
+ end
633
+
634
+ context "the exchange variant has been updated" do
635
+ before do
636
+ other_variant = create(:variant)
637
+ return_item.exchange_variant_id = other_variant.id
638
+ subject.valid?
639
+ end
640
+
641
+ it "is invalid" do
642
+ expect(subject).to_not be_valid
643
+ end
644
+
645
+ it "adds an error message about the invalid exchange variant" do
646
+ expect(subject.errors.to_a).to eq ["Invalid exchange variant."]
647
+ end
648
+ end
649
+
650
+ context "the exchange variant has not been updated" do
651
+ before do
652
+ other_variant = create(:variant)
653
+ return_item.update_column(:exchange_variant_id, other_variant.id)
654
+ return_item.reload
655
+ subject.valid?
656
+ end
657
+
658
+ it "is valid" do
659
+ expect(subject).to be_valid
660
+ end
661
+ end
662
+ end
663
+ end
664
+ end
665
+
666
+ describe "included tax in total" do
667
+ let(:inventory_unit) { create(:inventory_unit, state: 'shipped') }
668
+ let(:return_item) do
669
+ create(
670
+ :return_item,
671
+ inventory_unit: inventory_unit,
672
+ included_tax_total: 10
673
+ )
674
+ end
675
+
676
+ it 'includes included tax total' do
677
+ expect(return_item.pre_tax_amount).to eq 10
678
+ expect(return_item.included_tax_total).to eq 10
679
+ expect(return_item.total).to eq 20
680
+ end
681
+ end
682
+ end