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,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::ReimbursementPerformer, :type => :model do
4
+ let(:reimbursement) { create(:reimbursement, return_items_count: 1) }
5
+ let(:return_item) { reimbursement.return_items.first }
6
+ let(:reimbursement_type) { double("ReimbursementType") }
7
+ let(:reimbursement_type_hash) { { reimbursement_type => [return_item]} }
8
+
9
+ before do
10
+ expect(Spree::ReimbursementPerformer).to receive(:calculate_reimbursement_types).and_return(reimbursement_type_hash)
11
+ end
12
+
13
+ describe ".simulate" do
14
+ subject { Spree::ReimbursementPerformer.simulate(reimbursement) }
15
+
16
+ it "reimburses each calculated reimbursement types with the correct return items as a simulation" do
17
+ expect(reimbursement_type).to receive(:reimburse).with(reimbursement, [return_item], true)
18
+ subject
19
+ end
20
+ end
21
+
22
+ describe '.perform' do
23
+ subject { Spree::ReimbursementPerformer.perform(reimbursement) }
24
+
25
+ it "reimburses each calculated reimbursement types with the correct return items as a simulation" do
26
+ expect(reimbursement_type).to receive(:reimburse).with(reimbursement, [return_item], false)
27
+ subject
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,215 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Reimbursement, type: :model do
4
+
5
+ describe ".before_create" do
6
+ describe "#generate_number" do
7
+ context "number is assigned" do
8
+ let(:number) { '123' }
9
+ let(:reimbursement) { Spree::Reimbursement.new(number: number) }
10
+
11
+ it "should return the assigned number" do
12
+ reimbursement.save
13
+ expect(reimbursement.number).to eq number
14
+ end
15
+ end
16
+
17
+ context "number is not assigned" do
18
+ let(:reimbursement) { Spree::Reimbursement.new(number: nil) }
19
+
20
+ before do
21
+ allow(reimbursement).to receive_messages(valid?: true)
22
+ end
23
+
24
+ it "should assign number with random RI number" do
25
+ reimbursement.save
26
+ expect(reimbursement.number).to be =~ /RI\d{9}/
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "#display_total" do
33
+ let(:total) { 100.50 }
34
+ let(:currency) { "USD" }
35
+ let(:order) { Spree::Order.new(currency: currency) }
36
+ let(:reimbursement) { Spree::Reimbursement.new(total: total, order: order) }
37
+
38
+ subject { reimbursement.display_total }
39
+
40
+ it "returns the value as a Spree::Money instance" do
41
+ expect(subject).to eq Spree::Money.new(total)
42
+ end
43
+
44
+ it "uses the order's currency" do
45
+ expect(subject.money.currency.to_s).to eq currency
46
+ end
47
+ end
48
+
49
+ describe "#perform!" do
50
+ let!(:adjustments) { [] } # placeholder to ensure it gets run prior the "before" at this level
51
+
52
+ let!(:tax_rate) { nil }
53
+ let!(:tax_zone) { create(:zone, default_tax: true) }
54
+
55
+ let(:order) { create(:order_with_line_items, state: 'payment', line_items_count: 1, line_items_price: line_items_price, shipment_cost: 0) }
56
+ let(:line_items_price) { BigDecimal.new(10) }
57
+ let(:line_item) { order.line_items.first }
58
+ let(:inventory_unit) { line_item.inventory_units.first }
59
+ let(:payment) { build(:payment, amount: payment_amount, order: order, state: 'completed') }
60
+ let(:payment_amount) { order.total }
61
+ let(:customer_return) { build(:customer_return, return_items: [return_item]) }
62
+ let(:return_item) { build(:return_item, inventory_unit: inventory_unit) }
63
+
64
+ let!(:default_refund_reason) { Spree::RefundReason.find_or_create_by!(name: Spree::RefundReason::RETURN_PROCESSING_REASON, mutable: false) }
65
+
66
+ let(:reimbursement) { create(:reimbursement, customer_return: customer_return, order: order, return_items: [return_item]) }
67
+
68
+ subject { reimbursement.perform! }
69
+
70
+ before do
71
+ order.shipments.each do |shipment|
72
+ shipment.inventory_units.update_all state: 'shipped'
73
+ shipment.update_column('state', 'shipped')
74
+ end
75
+ order.reload
76
+ order.update!
77
+ if payment
78
+ payment.save!
79
+ order.next! # confirm
80
+ end
81
+ order.next! # completed
82
+ customer_return.save!
83
+ return_item.accept!
84
+ end
85
+
86
+ it "refunds the total amount" do
87
+ subject
88
+ expect(reimbursement.unpaid_amount).to eq 0
89
+ end
90
+
91
+ it "creates a refund" do
92
+ expect {
93
+ subject
94
+ }.to change{ Spree::Refund.count }.by(1)
95
+ expect(Spree::Refund.last.amount).to eq order.total
96
+ end
97
+
98
+ context 'with additional tax' do
99
+ let!(:tax_rate) { create(:tax_rate, name: "Sales Tax", amount: 0.10, included_in_price: false, zone: tax_zone) }
100
+
101
+ it 'saves the additional tax and refunds the total' do
102
+ expect {
103
+ subject
104
+ }.to change { Spree::Refund.count }.by(1)
105
+ return_item.reload
106
+ expect(return_item.additional_tax_total).to be > 0
107
+ expect(return_item.additional_tax_total).to eq line_item.additional_tax_total
108
+ expect(reimbursement.total).to eq line_item.pre_tax_amount + line_item.additional_tax_total
109
+ expect(Spree::Refund.last.amount).to eq line_item.pre_tax_amount + line_item.additional_tax_total
110
+ end
111
+ end
112
+
113
+ context 'with included tax' do
114
+ let!(:tax_rate) { create(:tax_rate, name: "VAT Tax", amount: 0.1, included_in_price: true, zone: tax_zone) }
115
+
116
+ it 'saves the included tax and refunds the total' do
117
+ expect {
118
+ subject
119
+ }.to change { Spree::Refund.count }.by(1)
120
+ return_item.reload
121
+ expect(return_item.included_tax_total).to be < 0
122
+ expect(return_item.included_tax_total).to eq line_item.included_tax_total
123
+ expect(reimbursement.total).to eq (line_item.pre_tax_amount + line_item.included_tax_total).round(2)
124
+ expect(Spree::Refund.last.amount).to eq (line_item.pre_tax_amount + line_item.included_tax_total).round(2)
125
+ end
126
+ end
127
+
128
+ context 'when reimbursement cannot be fully performed' do
129
+ let!(:non_return_refund) { create(:refund, amount: 1, payment: payment) }
130
+
131
+ it 'raises IncompleteReimbursement error' do
132
+ expect { subject }.to raise_error(Spree::Reimbursement::IncompleteReimbursementError)
133
+ end
134
+ end
135
+
136
+ context "when exchange is required" do
137
+ let(:exchange_variant) { build(:variant) }
138
+ before { return_item.exchange_variant = exchange_variant }
139
+ it "generates an exchange shipment for the order for the exchange items" do
140
+ expect { subject }.to change { order.reload.shipments.count }.by 1
141
+ expect(order.shipments.last.inventory_units.first.variant).to eq exchange_variant
142
+ end
143
+ end
144
+
145
+ it "triggers the reimbursement mailer to be sent" do
146
+ expect(Spree::ReimbursementMailer).to receive(:reimbursement_email).with(reimbursement.id) { double(deliver_later: true) }
147
+ subject
148
+ end
149
+ end
150
+
151
+ describe "#return_items_requiring_exchange" do
152
+ it "returns only the return items that require an exchange" do
153
+ return_items = [double(exchange_required?: true), double(exchange_required?: true),double(exchange_required?: false)]
154
+ allow(subject).to receive(:return_items) { return_items }
155
+ expect(subject.return_items_requiring_exchange).to eq return_items.take(2)
156
+ end
157
+ end
158
+
159
+ describe "#calculated_total" do
160
+ context 'with return item amounts that would round up if added' do
161
+ let(:reimbursement) { Spree::Reimbursement.new }
162
+
163
+ subject { reimbursement.calculated_total }
164
+
165
+ before do
166
+ reimbursement.return_items << Spree::ReturnItem.new(pre_tax_amount: 10.003)
167
+ reimbursement.return_items << Spree::ReturnItem.new(pre_tax_amount: 10.003)
168
+ end
169
+
170
+ it 'rounds down' do
171
+ expect(subject).to eq 20
172
+ end
173
+ end
174
+
175
+ context 'with a return item amount that should round up' do
176
+ let(:reimbursement) { Spree::Reimbursement.new }
177
+
178
+ subject { reimbursement.calculated_total }
179
+
180
+ before do
181
+ reimbursement.return_items << Spree::ReturnItem.new(pre_tax_amount: 19.998)
182
+ end
183
+
184
+ it 'rounds up' do
185
+ expect(subject).to eq 20
186
+ end
187
+ end
188
+ end
189
+
190
+ describe '.build_from_customer_return' do
191
+ let(:customer_return) { create(:customer_return, line_items_count: 5) }
192
+
193
+ let!(:pending_return_item) { customer_return.return_items.first.tap { |ri| ri.update!(acceptance_status: 'pending') } }
194
+ let!(:accepted_return_item) { customer_return.return_items.second.tap(&:accept!) }
195
+ let!(:rejected_return_item) { customer_return.return_items.third.tap(&:reject!) }
196
+ let!(:manual_intervention_return_item) { customer_return.return_items.fourth.tap(&:require_manual_intervention!) }
197
+ let!(:already_reimbursed_return_item) { customer_return.return_items.fifth }
198
+
199
+ let!(:previous_reimbursement) { create(:reimbursement, order: customer_return.order, return_items: [already_reimbursed_return_item]) }
200
+
201
+ subject { Spree::Reimbursement.build_from_customer_return(customer_return) }
202
+
203
+ it 'connects to the accepted return items' do
204
+ expect(subject.return_items.to_a).to eq [accepted_return_item]
205
+ end
206
+
207
+ it 'connects to the order' do
208
+ expect(subject.order).to eq customer_return.order
209
+ end
210
+
211
+ it 'connects to the customer_return' do
212
+ expect(subject.customer_return).to eq customer_return
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::ReimbursementTaxCalculator, :type => :model do
4
+
5
+ let!(:tax_rate) { nil }
6
+
7
+ let(:reimbursement) { create(:reimbursement, return_items_count: 1) }
8
+ let(:return_item) { reimbursement.return_items.first }
9
+ let(:line_item) { return_item.inventory_unit.line_item }
10
+
11
+ subject do
12
+ Spree::ReimbursementTaxCalculator.call(reimbursement)
13
+ end
14
+
15
+ context 'without taxes' do
16
+ let!(:tax_rate) { nil }
17
+
18
+ it 'leaves the return items additional_tax_total and included_tax_total at zero' do
19
+ subject
20
+
21
+ expect(return_item.additional_tax_total).to eq 0
22
+ expect(return_item.included_tax_total).to eq 0
23
+ end
24
+ end
25
+
26
+ context 'with additional tax' do
27
+ let!(:tax_rate) { create(:tax_rate, name: "Sales Tax", amount: 0.10, included_in_price: false, zone: tax_zone) }
28
+ let(:tax_zone) { create(:zone, default_tax: true) }
29
+
30
+ it 'sets additional_tax_total on the return items' do
31
+ subject
32
+ return_item.reload
33
+
34
+ expect(return_item.additional_tax_total).to be > 0
35
+ expect(return_item.additional_tax_total).to eq line_item.additional_tax_total
36
+ end
37
+ end
38
+
39
+ context 'with included tax' do
40
+ let!(:tax_rate) { create(:tax_rate, name: "VAT Tax", amount: 0.1, included_in_price: true, zone: tax_zone) }
41
+ let(:tax_zone) { create(:zone, default_tax: true) }
42
+
43
+ it 'sets included_tax_total on the return items' do
44
+ subject
45
+ return_item.reload
46
+
47
+ expect(return_item.included_tax_total).to be < 0
48
+ expect(return_item.included_tax_total).to eq line_item.included_tax_total
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe ReimbursementType::Credit, :type => :model do
5
+ let(:reimbursement) { create(:reimbursement, return_items_count: 1) }
6
+ let(:return_item) { reimbursement.return_items.first }
7
+ let(:payment) { reimbursement.order.payments.first }
8
+ let(:simulate) { false }
9
+ let!(:default_refund_reason) { Spree::RefundReason.find_or_create_by!(name: Spree::RefundReason::RETURN_PROCESSING_REASON, mutable: false) }
10
+ let(:creditable) { DummyCreditable.new(amount: 99.99) }
11
+
12
+ class DummyCreditable < Spree::Base
13
+ attr_accessor :amount
14
+ self.table_name = 'spree_payments' # Your creditable class should not use this table
15
+ end
16
+
17
+ subject { Spree::ReimbursementType::Credit.reimburse(reimbursement, [return_item], simulate)}
18
+
19
+ before do
20
+ reimbursement.update!(total: reimbursement.calculated_total)
21
+ allow(Spree::ReimbursementType::Credit).to receive(:create_creditable).and_return(creditable)
22
+ end
23
+
24
+ describe '.reimburse' do
25
+ context 'simulate is true' do
26
+ let(:simulate) { true }
27
+
28
+ it 'creates one readonly lump credit for all outstanding balance payable to the customer' do
29
+ expect(subject.map(&:class)).to eq [Spree::Reimbursement::Credit]
30
+ expect(subject.map(&:readonly?)).to eq [true]
31
+ expect(subject.sum(&:amount)).to eq reimbursement.return_items.to_a.sum(&:total)
32
+ end
33
+
34
+ it 'does not save to the database' do
35
+ expect { subject }.to_not change { Spree::Reimbursement::Credit.count }
36
+ end
37
+ end
38
+
39
+ context 'simulate is false' do
40
+ let(:simulate) { false }
41
+
42
+ before do
43
+ expect(creditable).to receive(:save).and_return(true)
44
+ end
45
+
46
+ it 'creates one lump credit for all outstanding balance payable to the customer' do
47
+ expect { subject }.to change { Spree::Reimbursement::Credit.count }.by(1)
48
+ expect(subject.sum(&:amount)).to eq reimbursement.return_items.to_a.sum(&:total)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe ReimbursementType::Exchange, :type => :model do
5
+ describe '.reimburse' do
6
+ let(:reimbursement) { create(:reimbursement, return_items_count: 1) }
7
+ let(:return_items) { reimbursement.return_items }
8
+ let(:new_exchange) { double("Exchange") }
9
+ let(:simulate) { true }
10
+
11
+ subject { Spree::ReimbursementType::Exchange.reimburse(reimbursement, return_items, simulate)}
12
+
13
+ context 'return items are supplied' do
14
+ before do
15
+ expect(Spree::Exchange).to receive(:new).with(reimbursement.order, return_items).and_return(new_exchange)
16
+ end
17
+
18
+ context "simulate is true" do
19
+
20
+ it 'does not perform an exchange and returns the exchange object' do
21
+ expect(new_exchange).not_to receive(:perform!)
22
+ expect(subject).to eq [new_exchange]
23
+ end
24
+ end
25
+
26
+ context "simulate is false" do
27
+ let(:simulate) { false }
28
+
29
+ it 'performs an exchange and returns the exchange object' do
30
+ expect(new_exchange).to receive(:perform!)
31
+ expect(subject).to eq [new_exchange]
32
+ end
33
+ end
34
+ end
35
+
36
+ context "no return items are supplied" do
37
+ let(:return_items) { [] }
38
+
39
+ it 'does not perform an exchange and returns an empty array' do
40
+ expect(new_exchange).not_to receive(:perform!)
41
+ expect(subject).to eq []
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe ReimbursementType::OriginalPayment, :type => :model do
5
+ let(:reimbursement) { create(:reimbursement, return_items_count: 1) }
6
+ let(:return_item) { reimbursement.return_items.first }
7
+ let(:payment) { reimbursement.order.payments.first }
8
+ let(:simulate) { false }
9
+ let!(:default_refund_reason) { Spree::RefundReason.find_or_create_by!(name: Spree::RefundReason::RETURN_PROCESSING_REASON, mutable: false) }
10
+
11
+ subject { Spree::ReimbursementType::OriginalPayment.reimburse(reimbursement, [return_item], simulate)}
12
+
13
+ before { reimbursement.update!(total: reimbursement.calculated_total) }
14
+
15
+ describe ".reimburse" do
16
+ context "simulate is true" do
17
+ let(:simulate) { true }
18
+
19
+ it "returns an array of readonly refunds" do
20
+ expect(subject.map(&:class)).to eq [Spree::Refund]
21
+ expect(subject.map(&:readonly?)).to eq [true]
22
+ end
23
+ end
24
+
25
+ context "simulate is false" do
26
+ it 'performs the refund' do
27
+ expect {
28
+ subject
29
+ }.to change { payment.refunds.count }.by(1)
30
+ expect(payment.refunds.sum(:amount)).to eq reimbursement.return_items.to_a.sum(&:total)
31
+ end
32
+ end
33
+
34
+ context 'when no credit is allowed on the payment' do
35
+ before do
36
+ expect_any_instance_of(Spree::Payment).to receive(:credit_allowed).and_return 0
37
+ end
38
+
39
+ it 'returns an empty array' do
40
+ expect(subject).to eq []
41
+ end
42
+ end
43
+
44
+ context 'when a payment is negative' do
45
+ before do
46
+ expect_any_instance_of(Spree::Payment).to receive(:amount).and_return -100
47
+ end
48
+
49
+ it 'returns an empty array' do
50
+ expect(subject).to eq []
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end