spree_core 3.2.1 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. checksums.yaml +4 -4
  2. data/lib/spree/core/version.rb +1 -1
  3. data/spree_core.gemspec +1 -1
  4. metadata +3 -206
  5. data/spec/fixtures/microdata.html +0 -22
  6. data/spec/fixtures/microdata_itemref.html +0 -15
  7. data/spec/fixtures/microdata_no_itemscope.html +0 -20
  8. data/spec/fixtures/thinking-cat.jpg +0 -0
  9. data/spec/helpers/base_helper_spec.rb +0 -200
  10. data/spec/helpers/products_helper_spec.rb +0 -289
  11. data/spec/lib/calculated_adjustments_spec.rb +0 -7
  12. data/spec/lib/i18n_spec.rb +0 -123
  13. data/spec/lib/search/base_spec.rb +0 -86
  14. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +0 -103
  15. data/spec/lib/spree/core/controller_helpers/order_spec.rb +0 -110
  16. data/spec/lib/spree/core/controller_helpers/search_spec.rb +0 -17
  17. data/spec/lib/spree/core/controller_helpers/store_spec.rb +0 -72
  18. data/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb +0 -39
  19. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +0 -22
  20. data/spec/lib/spree/core/importer/order_spec.rb +0 -605
  21. data/spec/lib/spree/core/number_generator_spec.rb +0 -175
  22. data/spec/lib/spree/core/token_generator_spec.rb +0 -24
  23. data/spec/lib/spree/core/validators/email_spec.rb +0 -53
  24. data/spec/lib/spree/core_spec.rb +0 -23
  25. data/spec/lib/spree/localized_number_spec.rb +0 -48
  26. data/spec/lib/spree/migrations_spec.rb +0 -36
  27. data/spec/lib/spree/money_spec.rb +0 -122
  28. data/spec/lib/tasks/exchanges_spec.rb +0 -136
  29. data/spec/mailers/order_mailer_spec.rb +0 -122
  30. data/spec/mailers/reimbursement_mailer_spec.rb +0 -47
  31. data/spec/mailers/shipment_mailer_spec.rb +0 -81
  32. data/spec/mailers/test_mailer_spec.rb +0 -38
  33. data/spec/models/option_type_prototype_spec.rb +0 -9
  34. data/spec/models/spree/ability_spec.rb +0 -251
  35. data/spec/models/spree/address_spec.rb +0 -402
  36. data/spec/models/spree/adjustable/adjuster/base_spec.rb +0 -10
  37. data/spec/models/spree/adjustable/adjuster/promotion_spec.rb +0 -211
  38. data/spec/models/spree/adjustable/adjuster/tax_spec.rb +0 -86
  39. data/spec/models/spree/adjustable/adjustments_updater_spec.rb +0 -26
  40. data/spec/models/spree/adjustment_spec.rb +0 -189
  41. data/spec/models/spree/app_configuration_spec.rb +0 -26
  42. data/spec/models/spree/asset_spec.rb +0 -28
  43. data/spec/models/spree/calculator/default_tax_spec.rb +0 -152
  44. data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +0 -25
  45. data/spec/models/spree/calculator/flat_rate_spec.rb +0 -47
  46. data/spec/models/spree/calculator/flexi_rate_spec.rb +0 -41
  47. data/spec/models/spree/calculator/percent_on_line_item_spec.rb +0 -15
  48. data/spec/models/spree/calculator/price_sack_spec.rb +0 -30
  49. data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +0 -47
  50. data/spec/models/spree/calculator/shipping.rb +0 -8
  51. data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +0 -23
  52. data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +0 -13
  53. data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +0 -52
  54. data/spec/models/spree/calculator/shipping/per_item_spec.rb +0 -20
  55. data/spec/models/spree/calculator/shipping/price_sack_spec.rb +0 -29
  56. data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +0 -40
  57. data/spec/models/spree/calculator/tiered_percent_spec.rb +0 -51
  58. data/spec/models/spree/calculator_spec.rb +0 -69
  59. data/spec/models/spree/classification_spec.rb +0 -93
  60. data/spec/models/spree/concerns/display_money_spec.rb +0 -43
  61. data/spec/models/spree/concerns/user_methods_spec.rb +0 -82
  62. data/spec/models/spree/concerns/vat_price_calculation_spec.rb +0 -66
  63. data/spec/models/spree/country_spec.rb +0 -55
  64. data/spec/models/spree/credit_card_spec.rb +0 -328
  65. data/spec/models/spree/customer_return_spec.rb +0 -240
  66. data/spec/models/spree/exchange_spec.rb +0 -75
  67. data/spec/models/spree/gateway/bogus_simple.rb +0 -20
  68. data/spec/models/spree/gateway/bogus_spec.rb +0 -13
  69. data/spec/models/spree/gateway_spec.rb +0 -61
  70. data/spec/models/spree/image_spec.rb +0 -8
  71. data/spec/models/spree/inventory_unit_spec.rb +0 -256
  72. data/spec/models/spree/line_item_spec.rb +0 -346
  73. data/spec/models/spree/option_type_spec.rb +0 -14
  74. data/spec/models/spree/option_value_spec.rb +0 -18
  75. data/spec/models/spree/order/address_spec.rb +0 -50
  76. data/spec/models/spree/order/adjustments_spec.rb +0 -29
  77. data/spec/models/spree/order/callbacks_spec.rb +0 -42
  78. data/spec/models/spree/order/checkout_spec.rb +0 -770
  79. data/spec/models/spree/order/currency_updater_spec.rb +0 -32
  80. data/spec/models/spree/order/finalizing_spec.rb +0 -114
  81. data/spec/models/spree/order/helpers_spec.rb +0 -5
  82. data/spec/models/spree/order/payment_spec.rb +0 -214
  83. data/spec/models/spree/order/risk_assessment_spec.rb +0 -84
  84. data/spec/models/spree/order/shipments_spec.rb +0 -43
  85. data/spec/models/spree/order/state_machine_spec.rb +0 -212
  86. data/spec/models/spree/order/store_credit_spec.rb +0 -426
  87. data/spec/models/spree/order/tax_spec.rb +0 -84
  88. data/spec/models/spree/order/totals_spec.rb +0 -24
  89. data/spec/models/spree/order/updating_spec.rb +0 -18
  90. data/spec/models/spree/order/validations_spec.rb +0 -15
  91. data/spec/models/spree/order_contents_spec.rb +0 -297
  92. data/spec/models/spree/order_inventory_spec.rb +0 -239
  93. data/spec/models/spree/order_merger_spec.rb +0 -135
  94. data/spec/models/spree/order_spec.rb +0 -1046
  95. data/spec/models/spree/order_updater_spec.rb +0 -305
  96. data/spec/models/spree/payment/gateway_options_spec.rb +0 -127
  97. data/spec/models/spree/payment/store_credit_spec.rb +0 -60
  98. data/spec/models/spree/payment_method/store_credit_spec.rb +0 -291
  99. data/spec/models/spree/payment_method_spec.rb +0 -103
  100. data/spec/models/spree/payment_spec.rb +0 -919
  101. data/spec/models/spree/preference_spec.rb +0 -80
  102. data/spec/models/spree/preferences/configuration_spec.rb +0 -30
  103. data/spec/models/spree/preferences/preferable_spec.rb +0 -344
  104. data/spec/models/spree/preferences/scoped_store_spec.rb +0 -58
  105. data/spec/models/spree/preferences/store_spec.rb +0 -46
  106. data/spec/models/spree/price_spec.rb +0 -128
  107. data/spec/models/spree/product/scopes_spec.rb +0 -183
  108. data/spec/models/spree/product_duplicator_spec.rb +0 -103
  109. data/spec/models/spree/product_filter_spec.rb +0 -26
  110. data/spec/models/spree/product_option_type_spec.rb +0 -9
  111. data/spec/models/spree/product_promotion_rule_spec.rb +0 -9
  112. data/spec/models/spree/product_property_spec.rb +0 -26
  113. data/spec/models/spree/product_spec.rb +0 -629
  114. data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +0 -113
  115. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +0 -148
  116. data/spec/models/spree/promotion/actions/create_line_items_spec.rb +0 -86
  117. data/spec/models/spree/promotion/actions/free_shipping_spec.rb +0 -36
  118. data/spec/models/spree/promotion/rules/country_spec.rb +0 -36
  119. data/spec/models/spree/promotion/rules/first_order_spec.rb +0 -75
  120. data/spec/models/spree/promotion/rules/item_total_spec.rb +0 -282
  121. data/spec/models/spree/promotion/rules/one_use_per_user_spec.rb +0 -42
  122. data/spec/models/spree/promotion/rules/option_value_spec.rb +0 -90
  123. data/spec/models/spree/promotion/rules/product_spec.rb +0 -143
  124. data/spec/models/spree/promotion/rules/taxon_spec.rb +0 -102
  125. data/spec/models/spree/promotion/rules/user_logged_in_spec.rb +0 -27
  126. data/spec/models/spree/promotion/rules/user_spec.rb +0 -45
  127. data/spec/models/spree/promotion_action_spec.rb +0 -10
  128. data/spec/models/spree/promotion_category_spec.rb +0 -17
  129. data/spec/models/spree/promotion_handler/cart_spec.rb +0 -102
  130. data/spec/models/spree/promotion_handler/coupon_spec.rb +0 -323
  131. data/spec/models/spree/promotion_handler/free_shipping_spec.rb +0 -48
  132. data/spec/models/spree/promotion_handler/page_spec.rb +0 -44
  133. data/spec/models/spree/promotion_rule_spec.rb +0 -29
  134. data/spec/models/spree/promotion_rule_taxon_spec.rb +0 -9
  135. data/spec/models/spree/promotion_rule_user_spec.rb +0 -9
  136. data/spec/models/spree/promotion_spec.rb +0 -679
  137. data/spec/models/spree/property_prototype_spec.rb +0 -9
  138. data/spec/models/spree/property_spec.rb +0 -5
  139. data/spec/models/spree/prototype_spec.rb +0 -5
  140. data/spec/models/spree/prototype_taxon_spec.rb +0 -9
  141. data/spec/models/spree/refund_reason_spec.rb +0 -20
  142. data/spec/models/spree/refund_spec.rb +0 -195
  143. data/spec/models/spree/reimbursement/credit_spec.rb +0 -36
  144. data/spec/models/spree/reimbursement/reimbursement_type_engine_spec.rb +0 -140
  145. data/spec/models/spree/reimbursement/reimbursement_type_validator_spec.rb +0 -83
  146. data/spec/models/spree/reimbursement_performer_spec.rb +0 -30
  147. data/spec/models/spree/reimbursement_spec.rb +0 -188
  148. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +0 -63
  149. data/spec/models/spree/reimbursement_type/credit_spec.rb +0 -53
  150. data/spec/models/spree/reimbursement_type/exchange_spec.rb +0 -46
  151. data/spec/models/spree/reimbursement_type/original_payment_spec.rb +0 -55
  152. data/spec/models/spree/reimbursement_type/store_credit_spec.rb +0 -101
  153. data/spec/models/spree/return_authorization_reason_spec.rb +0 -7
  154. data/spec/models/spree/return_authorization_spec.rb +0 -230
  155. data/spec/models/spree/return_item/eligibility_validator/default_spec.rb +0 -77
  156. data/spec/models/spree/return_item/eligibility_validator/inventory_shipped_spec.rb +0 -58
  157. data/spec/models/spree/return_item/eligibility_validator/no_reimbursements_spec.rb +0 -61
  158. data/spec/models/spree/return_item/eligibility_validator/order_completed_spec.rb +0 -32
  159. data/spec/models/spree/return_item/eligibility_validator/rma_required_spec.rb +0 -29
  160. data/spec/models/spree/return_item/eligibility_validator/time_since_purchase_spec.rb +0 -35
  161. data/spec/models/spree/return_item/exchange_variant_eligibility/same_option_value_spec.rb +0 -65
  162. data/spec/models/spree/return_item/exchange_variant_eligibility/same_product_spec.rb +0 -43
  163. data/spec/models/spree/return_item_spec.rb +0 -731
  164. data/spec/models/spree/returns_calculator_spec.rb +0 -14
  165. data/spec/models/spree/role_spec.rb +0 -7
  166. data/spec/models/spree/shipment_spec.rb +0 -742
  167. data/spec/models/spree/shipping_calculator_spec.rb +0 -45
  168. data/spec/models/spree/shipping_category_spec.rb +0 -19
  169. data/spec/models/spree/shipping_method_spec.rb +0 -95
  170. data/spec/models/spree/shipping_rate_spec.rb +0 -140
  171. data/spec/models/spree/state_spec.rb +0 -29
  172. data/spec/models/spree/stock/availability_validator_spec.rb +0 -36
  173. data/spec/models/spree/stock/content_item_spec.rb +0 -31
  174. data/spec/models/spree/stock/coordinator_spec.rb +0 -61
  175. data/spec/models/spree/stock/differentiator_spec.rb +0 -39
  176. data/spec/models/spree/stock/estimator_spec.rb +0 -202
  177. data/spec/models/spree/stock/inventory_unit_builder_spec.rb +0 -38
  178. data/spec/models/spree/stock/package_spec.rb +0 -182
  179. data/spec/models/spree/stock/packer_spec.rb +0 -70
  180. data/spec/models/spree/stock/prioritizer_spec.rb +0 -125
  181. data/spec/models/spree/stock/quantifier_spec.rb +0 -126
  182. data/spec/models/spree/stock/splitter/backordered_spec.rb +0 -29
  183. data/spec/models/spree/stock/splitter/base_spec.rb +0 -21
  184. data/spec/models/spree/stock/splitter/shipping_category_spec.rb +0 -47
  185. data/spec/models/spree/stock/splitter/weight_spec.rb +0 -32
  186. data/spec/models/spree/stock_item_spec.rb +0 -432
  187. data/spec/models/spree/stock_location_spec.rb +0 -243
  188. data/spec/models/spree/stock_movement_spec.rb +0 -120
  189. data/spec/models/spree/stock_transfer_spec.rb +0 -50
  190. data/spec/models/spree/store_credit_event_spec.rb +0 -101
  191. data/spec/models/spree/store_credit_spec.rb +0 -786
  192. data/spec/models/spree/store_spec.rb +0 -78
  193. data/spec/models/spree/tax_category_spec.rb +0 -32
  194. data/spec/models/spree/tax_rate_spec.rb +0 -561
  195. data/spec/models/spree/taxon_spec.rb +0 -85
  196. data/spec/models/spree/taxonomy_spec.rb +0 -18
  197. data/spec/models/spree/tracker_spec.rb +0 -21
  198. data/spec/models/spree/user_spec.rb +0 -203
  199. data/spec/models/spree/variant_spec.rb +0 -818
  200. data/spec/models/spree/zone_member_spec.rb +0 -38
  201. data/spec/models/spree/zone_spec.rb +0 -472
  202. data/spec/spec_helper.rb +0 -79
  203. data/spec/support/big_decimal.rb +0 -5
  204. data/spec/support/concerns/adjustment_source.rb +0 -23
  205. data/spec/support/concerns/default_price.rb +0 -37
  206. data/spec/support/rake.rb +0 -13
  207. data/spec/support/test_gateway.rb +0 -2
@@ -1,291 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::PaymentMethod::StoreCredit do
4
- let(:order) { create(:order) }
5
- let(:payment) { create(:payment, order: order) }
6
- let(:gateway_options) { payment.gateway_options }
7
-
8
- context '#authorize' do
9
- subject do
10
- Spree::PaymentMethod::StoreCredit.new.authorize(auth_amount, store_credit, gateway_options)
11
- end
12
-
13
- let(:auth_amount) { store_credit.amount_remaining * 100 }
14
- let(:store_credit) { create(:store_credit) }
15
- let(:gateway_options) { super().merge(originator: originator) }
16
- let(:originator) { nil }
17
-
18
- context 'without an invalid store credit' do
19
- let(:store_credit) { nil }
20
- let(:auth_amount) { 10 }
21
-
22
- it 'declines an unknown store credit' do
23
- expect(subject.success?).to be false
24
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
25
- end
26
- end
27
-
28
- context 'with insuffient funds' do
29
- let(:auth_amount) { (store_credit.amount_remaining * 100) + 1 }
30
-
31
- it 'declines a store credit' do
32
- expect(subject.success?).to be false
33
- expect(subject.message).to include Spree.t('store_credit_payment_method.insufficient_funds')
34
- end
35
- end
36
-
37
- context 'when the currency does not match the order currency' do
38
- let(:store_credit) { create(:store_credit, currency: 'AUD') }
39
-
40
- it 'declines the credit' do
41
- expect(subject.success?).to be false
42
- expect(subject.message).to include Spree.t('store_credit_payment_method.currency_mismatch')
43
- end
44
- end
45
-
46
- context 'with a valid request' do
47
- it 'authorizes a valid store credit' do
48
- expect(subject.success?).to be true
49
- expect(subject.authorization).to_not be_nil
50
- end
51
-
52
- context 'with an originator' do
53
- let(:originator) { double('originator') }
54
-
55
- it 'passes the originator' do
56
- expect_any_instance_of(Spree::StoreCredit).to receive(:authorize).
57
- with(anything, anything, action_originator: originator)
58
- subject
59
- end
60
- end
61
- end
62
- end
63
-
64
- context '#capture' do
65
- subject do
66
- Spree::PaymentMethod::StoreCredit.new.capture(capture_amount, auth_code, gateway_options)
67
- end
68
-
69
- let(:capture_amount) { 10_00 }
70
- let(:auth_code) { auth_event.authorization_code }
71
- let(:gateway_options) { super().merge(originator: originator) }
72
-
73
- let(:authorized_amount) { capture_amount / 100.0 }
74
- let(:auth_event) { create(:store_credit_auth_event, store_credit: store_credit, amount: authorized_amount) }
75
- let(:store_credit) { create(:store_credit, amount_authorized: authorized_amount) }
76
- let(:originator) { nil }
77
-
78
- context 'with an invalid auth code' do
79
- let(:auth_code) { -1 }
80
-
81
- it 'declines an unknown store credit' do
82
- expect(subject.success?).to be false
83
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
84
- end
85
- end
86
-
87
- context 'when unable to authorize the amount' do
88
- let(:authorized_amount) { (capture_amount - 1) / 100 }
89
-
90
- before do
91
- allow_any_instance_of(Spree::StoreCredit).to receive_messages(authorize: true)
92
- end
93
-
94
- it 'declines a store credit' do
95
- expect(subject.success?).to be false
96
- expect(subject.message).to include Spree.t('store_credit_payment_method.insufficient_authorized_amount')
97
- end
98
- end
99
-
100
- context 'when the currency does not match the order currency' do
101
- let(:store_credit) { create(:store_credit, currency: 'AUD', amount_authorized: authorized_amount) }
102
-
103
- it 'declines the credit' do
104
- expect(subject.success?).to be false
105
- expect(subject.message).to include Spree.t('store_credit_payment_method.currency_mismatch')
106
- end
107
- end
108
-
109
- context 'with a valid request' do
110
- it 'captures the store credit' do
111
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
112
- action: Spree::StoreCredit::CAPTURE_ACTION)
113
- expect(subject.success?).to be true
114
- end
115
-
116
- context 'with an originator' do
117
- let(:originator) { double('originator') }
118
-
119
- it 'passes the originator' do
120
- expect_any_instance_of(Spree::StoreCredit).to receive(:capture).
121
- with(anything, anything, anything, action_originator: originator)
122
- subject
123
- end
124
- end
125
- end
126
- end
127
-
128
- context '#void' do
129
- subject do
130
- Spree::PaymentMethod::StoreCredit.new.void(auth_code, gateway_options)
131
- end
132
-
133
- let(:auth_code) { auth_event.authorization_code }
134
- let(:gateway_options) { super().merge(originator: originator) }
135
- let(:auth_event) { create(:store_credit_auth_event) }
136
- let(:originator) { nil }
137
-
138
- context 'with an invalid auth code' do
139
- let(:auth_code) { 1 }
140
-
141
- it 'declines an unknown store credit' do
142
- expect(subject.success?).to be false
143
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
144
- end
145
- end
146
-
147
- context 'when the store credit is not voided successfully' do
148
- before { allow_any_instance_of(Spree::StoreCredit).to receive(:void).and_return false }
149
-
150
- it 'returns an error response' do
151
- expect(subject.success?).to be false
152
- end
153
- end
154
-
155
- it 'voids a valid store credit void request' do
156
- expect(subject.success?).to be true
157
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
158
- action: Spree::StoreCredit::VOID_ACTION)
159
- end
160
-
161
- context 'with an originator' do
162
- let(:originator) { double('originator') }
163
-
164
- it 'passes the originator' do
165
- expect_any_instance_of(Spree::StoreCredit).to receive(:void).with(anything, action_originator: originator)
166
- subject
167
- end
168
- end
169
- end
170
-
171
- context '#purchase' do
172
- it "declines a purchase if it can't find a pending credit for the correct amount" do
173
- amount = 100.0
174
- store_credit = create(:store_credit)
175
- auth_code = store_credit.generate_authorization_code
176
- store_credit.store_credit_events.create!(action: Spree::StoreCredit::ELIGIBLE_ACTION,
177
- amount: amount,
178
- authorization_code: auth_code)
179
- store_credit.store_credit_events.create!(action: Spree::StoreCredit::CAPTURE_ACTION,
180
- amount: amount, authorization_code: auth_code)
181
-
182
- response = subject.purchase(amount * 100.0, store_credit, gateway_options)
183
- expect(response.success?).to be false
184
- expect(response.message).to include Spree.t('store_credit_payment_method.unable_to_find')
185
- end
186
-
187
- it 'captures a purchase if it can find a pending credit for the correct amount' do
188
- amount = 100.0
189
- store_credit = create(:store_credit)
190
- auth_code = store_credit.generate_authorization_code
191
- store_credit.store_credit_events.create!(action: Spree::StoreCredit::ELIGIBLE_ACTION,
192
- amount: amount, authorization_code: auth_code)
193
-
194
- response = subject.purchase(amount * 100.0, store_credit, gateway_options)
195
- expect(response.success?).to be true
196
- expect(response.message).to include Spree.t('store_credit_payment_method.successful_action',
197
- action: Spree::StoreCredit::CAPTURE_ACTION)
198
- end
199
- end
200
-
201
- context '#credit' do
202
- subject do
203
- Spree::PaymentMethod::StoreCredit.new.credit(credit_amount, auth_code, gateway_options)
204
- end
205
-
206
- let(:credit_amount) { 100.0 }
207
- let(:auth_code) { auth_event.authorization_code }
208
- let(:gateway_options) { super().merge(originator: originator) }
209
- let(:auth_event) { create(:store_credit_auth_event) }
210
- let(:originator) { nil }
211
-
212
- context 'with an invalid auth code' do
213
- let(:auth_code) { 1 }
214
-
215
- it 'declines an unknown store credit' do
216
- expect(subject.success?).to be false
217
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
218
- end
219
- end
220
-
221
- context "when the store credit isn't credited successfully" do
222
- before { allow(Spree::StoreCredit).to receive_messages(credit: false) }
223
-
224
- it 'returns an error response' do
225
- expect(subject.success?).to be false
226
- end
227
- end
228
-
229
- context 'with a valid credit request' do
230
- before { allow_any_instance_of(Spree::StoreCredit).to receive_messages(credit: true) }
231
-
232
- it 'credits a valid store credit credit request' do
233
- expect(subject.success?).to be true
234
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
235
- action: Spree::StoreCredit::CREDIT_ACTION)
236
- end
237
- end
238
-
239
- context 'with an originator' do
240
- let(:originator) { double('originator') }
241
-
242
- it 'passes the originator' do
243
- allow_any_instance_of(Spree::StoreCredit).to receive(:credit).
244
- with(anything, anything, anything, action_originator: originator)
245
- subject
246
- end
247
- end
248
- end
249
-
250
- context '#cancel' do
251
- subject do
252
- Spree::PaymentMethod::StoreCredit.new.cancel(auth_code)
253
- end
254
-
255
- let(:store_credit) { create(:store_credit, amount_used: captured_amount) }
256
- let(:auth_code) { '1-SC-20141111111111' }
257
- let(:captured_amount) { 10.0 }
258
-
259
- let!(:capture_event) do
260
- create(:store_credit_auth_event,
261
- action: Spree::StoreCredit::CAPTURE_ACTION,
262
- authorization_code: auth_code,
263
- amount: captured_amount,
264
- store_credit: store_credit)
265
- end
266
-
267
- context 'store credit event found' do
268
- it 'creates a store credit for the same amount that was captured' do
269
- allow_any_instance_of(Spree::StoreCredit).to receive(:credit).
270
- with(captured_amount, auth_code, store_credit.currency)
271
- subject
272
- end
273
-
274
- it 'returns a valid store credit cancel request' do
275
- expect(subject.success?).to be true
276
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
277
- action: Spree::StoreCredit::CANCEL_ACTION)
278
- end
279
- end
280
-
281
- context 'store credit event not found' do
282
- subject do
283
- Spree::PaymentMethod::StoreCredit.new.cancel('INVALID')
284
- end
285
-
286
- it 'returns an error response' do
287
- expect(subject.success?).to be false
288
- end
289
- end
290
- end
291
- end
@@ -1,103 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::PaymentMethod, type: :model do
4
- context "visibility scopes" do
5
- before do
6
- [nil, '', 'both', 'front_end', 'back_end'].each do |display_on|
7
- Spree::Gateway::Test.create(
8
- name: 'Display Both',
9
- display_on: display_on,
10
- active: true,
11
- description: 'foofah'
12
- )
13
- end
14
- end
15
-
16
- it "should have 5 total methods" do
17
- expect(Spree::PaymentMethod.count).to eq(5)
18
- end
19
-
20
- describe "#available" do
21
- it "should return all methods available to front-end/back-end" do
22
- methods = Spree::PaymentMethod.available
23
- expect(methods.size).to eq(3)
24
- expect(methods.pluck(:display_on)).to eq(['both', 'front_end', 'back_end'])
25
- end
26
- end
27
-
28
- describe "#available_on_front_end" do
29
- it "should return all methods available to front-end" do
30
- methods = Spree::PaymentMethod.available_on_front_end
31
- expect(methods.size).to eq(2)
32
- expect(methods.pluck(:display_on)).to eq(['both', 'front_end'])
33
- end
34
- end
35
-
36
- describe "#available_on_back_end" do
37
- it "should return all methods available to back-end" do
38
- methods = Spree::PaymentMethod.available_on_back_end
39
- expect(methods.size).to eq(2)
40
- expect(methods.pluck(:display_on)).to eq(['both', 'back_end'])
41
- end
42
- end
43
- end
44
-
45
- describe '#auto_capture?' do
46
- class TestGateway < Spree::Gateway
47
- def provider_class
48
- Provider
49
- end
50
- end
51
-
52
- let(:gateway) { TestGateway.new }
53
-
54
- subject { gateway.auto_capture? }
55
-
56
- context 'when auto_capture is nil' do
57
- before(:each) do
58
- expect(Spree::Config).to receive('[]').with(:auto_capture).and_return(auto_capture)
59
- end
60
-
61
- context 'and when Spree::Config[:auto_capture] is false' do
62
- let(:auto_capture) { false }
63
-
64
- it 'should be false' do
65
- expect(gateway.auto_capture).to be_nil
66
- expect(subject).to be false
67
- end
68
- end
69
-
70
- context 'and when Spree::Config[:auto_capture] is true' do
71
- let(:auto_capture) { true }
72
-
73
- it 'should be true' do
74
- expect(gateway.auto_capture).to be_nil
75
- expect(subject).to be true
76
- end
77
- end
78
- end
79
-
80
- context 'when auto_capture is not nil' do
81
- before(:each) do
82
- gateway.auto_capture = auto_capture
83
- end
84
-
85
- context 'and is true' do
86
- let(:auto_capture) { true }
87
-
88
- it 'should be true' do
89
- expect(subject).to be true
90
- end
91
- end
92
-
93
- context 'and is false' do
94
- let(:auto_capture) { false }
95
-
96
- it 'should be true' do
97
- expect(subject).to be false
98
- end
99
- end
100
- end
101
- end
102
-
103
- end
@@ -1,919 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::Payment, type: :model do
4
- let(:order) { Spree::Order.create }
5
- let(:refund_reason) { create(:refund_reason) }
6
-
7
- let(:gateway) do
8
- gateway = Spree::Gateway::Bogus.new(active: true)
9
- allow(gateway).to receive_messages source_required: true
10
- gateway
11
- end
12
-
13
- let(:avs_code) { 'D' }
14
- let(:cvv_code) { 'M' }
15
-
16
- let(:card) { create :credit_card }
17
-
18
- let(:payment) do
19
- payment = Spree::Payment.new
20
- payment.source = card
21
- payment.order = order
22
- payment.payment_method = gateway
23
- payment.amount = 5
24
- payment
25
- end
26
-
27
- let(:amount_in_cents) { (payment.amount * 100).round }
28
-
29
- let!(:success_response) do
30
- ActiveMerchant::Billing::Response.new(true, '', {}, {
31
- authorization: '123',
32
- cvv_result: cvv_code,
33
- avs_result: { code: avs_code }
34
- })
35
- end
36
-
37
- let(:failed_response) do
38
- ActiveMerchant::Billing::Response.new(false, '', {}, {})
39
- end
40
-
41
- before(:each) do
42
- # So it doesn't create log entries every time a processing method is called
43
- allow(payment.log_entries).to receive(:create!)
44
- end
45
-
46
- describe 'delegate' do
47
- it { is_expected.to delegate_method(:currency).to(:order) }
48
- end
49
-
50
- describe 'Constants' do
51
- it { expect(Spree::Payment::INVALID_STATES).to eq(%w(failed invalid)) }
52
- end
53
-
54
- describe 'scopes' do
55
- describe '.valid' do
56
- let!(:invalid_payment) do
57
- create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: '', state: 'invalid')
58
- end
59
-
60
- let!(:failed_payment) do
61
- create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: '', state: 'failed')
62
- end
63
-
64
- let!(:checkout_payment) do
65
- create(:payment, avs_response: 'A', cvv_response_code: 'M', cvv_response_message: '', state: 'checkout')
66
- end
67
-
68
- let!(:completed_payment) do
69
- create(:payment, avs_response: 'Y', cvv_response_code: 'N', cvv_response_message: '', state: 'completed')
70
- end
71
-
72
- subject { Spree::Payment.valid }
73
-
74
- it { is_expected.to_not include(invalid_payment) }
75
- it { is_expected.to_not include(failed_payment) }
76
- it { is_expected.to include(checkout_payment) }
77
- it { is_expected.to include(completed_payment) }
78
- end
79
- end
80
-
81
- context '.risky' do
82
-
83
- let!(:payment_1) { create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: 'Match') }
84
- let!(:payment_2) { create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: '') }
85
- let!(:payment_3) { create(:payment, avs_response: 'A', cvv_response_code: 'M', cvv_response_message: 'Match') }
86
- let!(:payment_4) { create(:payment, avs_response: 'Y', cvv_response_code: 'N', cvv_response_message: 'No Match') }
87
-
88
- it 'should not return successful responses' do
89
- expect(subject.class.risky.to_a).to match_array([payment_3, payment_4])
90
- end
91
- end
92
-
93
- context "#captured_amount" do
94
- context "calculates based on capture events" do
95
- it "with 0 capture events" do
96
- expect(payment.captured_amount).to eq(0)
97
- end
98
-
99
- it "with some capture events" do
100
- payment.save
101
- payment.capture_events.create!(amount: 2.0)
102
- payment.capture_events.create!(amount: 3.0)
103
- expect(payment.captured_amount).to eq(5)
104
- end
105
- end
106
- end
107
-
108
- context '#uncaptured_amount' do
109
- context "calculates based on capture events" do
110
- it "with 0 capture events" do
111
- expect(payment.uncaptured_amount).to eq(5.0)
112
- end
113
-
114
- it "with some capture events" do
115
- payment.save
116
- payment.capture_events.create!(amount: 2.0)
117
- payment.capture_events.create!(amount: 3.0)
118
- expect(payment.uncaptured_amount).to eq(0)
119
- end
120
- end
121
- end
122
-
123
- context 'validations' do
124
- it "returns useful error messages when source is invalid" do
125
- payment.source = Spree::CreditCard.new
126
- expect(payment).not_to be_valid
127
- cc_errors = payment.errors['Credit Card']
128
- expect(cc_errors).to include("Number can't be blank")
129
- expect(cc_errors).to include("Month is not a number")
130
- expect(cc_errors).to include("Year is not a number")
131
- expect(cc_errors).to include("Verification Value can't be blank")
132
- end
133
- end
134
-
135
- # Regression test for https://github.com/spree/spree/pull/2224
136
- context 'failure' do
137
- it 'should transition to failed from pending state' do
138
- payment.state = 'pending'
139
- payment.failure
140
- expect(payment.state).to eql('failed')
141
- end
142
-
143
- it 'should transition to failed from processing state' do
144
- payment.state = 'processing'
145
- payment.failure
146
- expect(payment.state).to eql('failed')
147
- end
148
- end
149
-
150
- context 'invalidate' do
151
- it 'should transition from checkout to invalid' do
152
- payment.state = 'checkout'
153
- payment.invalidate
154
- expect(payment.state).to eq('invalid')
155
- end
156
- end
157
-
158
- context "processing" do
159
- describe "#process!" do
160
- it "should purchase if with auto_capture" do
161
- expect(payment.payment_method).to receive(:auto_capture?).and_return(true)
162
- payment.process!
163
- expect(payment).to be_completed
164
- end
165
-
166
- it "should authorize without auto_capture" do
167
- expect(payment.payment_method).to receive(:auto_capture?).and_return(false)
168
- payment.process!
169
- expect(payment).to be_pending
170
- end
171
-
172
- it "should make the state 'processing'" do
173
- expect(payment).to receive(:started_processing!)
174
- payment.process!
175
- end
176
-
177
- it "should invalidate if payment method doesnt support source" do
178
- expect(payment.payment_method).to receive(:supports?).with(payment.source).and_return(false)
179
- expect { payment.process!}.to raise_error(Spree::Core::GatewayError)
180
- expect(payment.state).to eq('invalid')
181
- end
182
-
183
- # Regression test for #4598
184
- it "should allow payments with a gateway_customer_profile_id" do
185
- allow(payment.source).to receive_messages gateway_customer_profile_id: "customer_1"
186
- expect(payment.payment_method).to receive(:supports?).with(payment.source).and_return(false)
187
- expect(payment).to receive(:started_processing!)
188
- payment.process!
189
- end
190
-
191
- # Another regression test for #4598
192
- it "should allow payments with a gateway_payment_profile_id" do
193
- allow(payment.source).to receive_messages gateway_payment_profile_id: "customer_1"
194
- expect(payment.payment_method).to receive(:supports?).with(payment.source).and_return(false)
195
- expect(payment).to receive(:started_processing!)
196
- payment.process!
197
- end
198
- end
199
-
200
- describe "#authorize!" do
201
- it "should call authorize on the gateway with the payment amount" do
202
- expect(payment.payment_method).to receive(:authorize).with(amount_in_cents,
203
- card,
204
- anything).and_return(success_response)
205
- payment.authorize!
206
- end
207
-
208
- it "should call authorize on the gateway with the currency code" do
209
- allow(payment).to receive_messages currency: 'GBP'
210
- expect(payment.payment_method).to receive(:authorize).with(amount_in_cents,
211
- card,
212
- hash_including({currency: "GBP"})).and_return(success_response)
213
- payment.authorize!
214
- end
215
-
216
- it "should log the response" do
217
- payment.save!
218
- expect(payment.log_entries).to receive(:create!).with(details: anything)
219
- payment.authorize!
220
- end
221
-
222
- context "if successful" do
223
- before do
224
- expect(payment.payment_method).to receive(:authorize).with(amount_in_cents,
225
- card,
226
- anything).and_return(success_response)
227
- end
228
-
229
- it "should store the response_code, avs_response and cvv_response fields" do
230
- payment.authorize!
231
- expect(payment.response_code).to eq('123')
232
- expect(payment.avs_response).to eq(avs_code)
233
- expect(payment.cvv_response_code).to eq(cvv_code)
234
- expect(payment.cvv_response_message).to eq(ActiveMerchant::Billing::CVVResult::MESSAGES[cvv_code])
235
- end
236
-
237
- it "should make payment pending" do
238
- expect(payment).to receive(:pend!)
239
- payment.authorize!
240
- end
241
- end
242
-
243
- context "if unsuccessful" do
244
- it "should mark payment as failed" do
245
- allow(gateway).to receive(:authorize).and_return(failed_response)
246
- expect(payment).to receive(:failure)
247
- expect(payment).not_to receive(:pend)
248
- expect {
249
- payment.authorize!
250
- }.to raise_error(Spree::Core::GatewayError)
251
- end
252
- end
253
- end
254
-
255
- describe "#purchase!" do
256
- it "should call purchase on the gateway with the payment amount" do
257
- expect(gateway).to receive(:purchase).with(amount_in_cents, card, anything).and_return(success_response)
258
- payment.purchase!
259
- end
260
-
261
- it "should log the response" do
262
- payment.save!
263
- expect(payment.log_entries).to receive(:create!).with(details: anything)
264
- payment.purchase!
265
- end
266
-
267
- context "if successful" do
268
- before do
269
- expect(payment.payment_method).to receive(:purchase).with(amount_in_cents,
270
- card,
271
- anything).and_return(success_response)
272
- end
273
-
274
- it "should store the response_code and avs_response" do
275
- payment.purchase!
276
- expect(payment.response_code).to eq('123')
277
- expect(payment.avs_response).to eq(avs_code)
278
- end
279
-
280
- it "should make payment complete" do
281
- expect(payment).to receive(:complete!)
282
- payment.purchase!
283
- end
284
-
285
- it "should log a capture event" do
286
- payment.purchase!
287
- expect(payment.capture_events.count).to eq(1)
288
- expect(payment.capture_events.first.amount).to eq(payment.amount)
289
- end
290
-
291
- it "should set the uncaptured amount to 0" do
292
- payment.purchase!
293
- expect(payment.uncaptured_amount).to eq(0)
294
- end
295
- end
296
-
297
- context "if unsuccessful" do
298
- before do
299
- allow(gateway).to receive(:purchase).and_return(failed_response)
300
- expect(payment).to receive(:failure)
301
- expect(payment).not_to receive(:pend)
302
- end
303
-
304
- it "should make payment failed" do
305
- expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
306
- end
307
-
308
- it "should not log a capture event" do
309
- expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
310
- expect(payment.capture_events.count).to eq(0)
311
- end
312
- end
313
- end
314
-
315
- describe "#capture!" do
316
- context "when payment is pending" do
317
- before do
318
- payment.amount = 100
319
- payment.state = 'pending'
320
- payment.response_code = '12345'
321
- end
322
-
323
- context "if successful" do
324
- context 'for entire amount' do
325
- before do
326
- expect(payment.payment_method).to receive(:capture).with(payment.display_amount.money.cents, payment.response_code, anything).and_return(success_response)
327
- end
328
-
329
- it "should make payment complete" do
330
- expect(payment).to receive(:complete!)
331
- payment.capture!
332
- end
333
-
334
- it "logs capture events" do
335
- payment.capture!
336
- expect(payment.capture_events.count).to eq(1)
337
- expect(payment.capture_events.first.amount).to eq(payment.amount)
338
- end
339
- end
340
-
341
- context 'for partial amount' do
342
- let(:original_amount) { payment.money.money.cents }
343
- let(:capture_amount) { original_amount - 100 }
344
-
345
- before do
346
- expect(payment.payment_method).to receive(:capture).with(capture_amount, payment.response_code, anything).and_return(success_response)
347
- end
348
-
349
- it "should make payment complete & create pending payment for remaining amount" do
350
- expect(payment).to receive(:complete!)
351
- payment.capture!(capture_amount)
352
- order = payment.order
353
- payments = order.payments
354
-
355
- expect(payments.size).to eq 2
356
- expect(payments.pending.first.amount).to eq 1
357
- # Payment stays processing for spec because of receive(:complete!) stub.
358
- expect(payments.processing.first.amount).to eq(capture_amount / 100)
359
- expect(payments.processing.first.source).to eq(payments.pending.first.source)
360
- end
361
-
362
- it "logs capture events" do
363
- payment.capture!(capture_amount)
364
- expect(payment.capture_events.count).to eq(1)
365
- expect(payment.capture_events.first.amount).to eq(capture_amount / 100)
366
- end
367
- end
368
- end
369
-
370
- context "if unsuccessful" do
371
- it "should not make payment complete" do
372
- allow(gateway).to receive_messages capture: failed_response
373
- expect(payment).to receive(:failure)
374
- expect(payment).not_to receive(:complete)
375
- expect { payment.capture! }.to raise_error(Spree::Core::GatewayError)
376
- end
377
- end
378
- end
379
-
380
- # Regression test for #2119
381
- context "when payment is completed" do
382
- before do
383
- payment.state = 'completed'
384
- end
385
-
386
- it "should do nothing" do
387
- expect(payment).not_to receive(:complete)
388
- expect(payment.payment_method).not_to receive(:capture)
389
- expect(payment.log_entries).not_to receive(:create!)
390
- payment.capture!
391
- end
392
- end
393
- end
394
-
395
- describe "#void_transaction!" do
396
- before do
397
- payment.response_code = '123'
398
- payment.state = 'pending'
399
- end
400
-
401
- context "when profiles are supported" do
402
- it "should call payment_gateway.void with the payment's response_code" do
403
- allow(gateway).to receive_messages payment_profiles_supported?: true
404
- expect(gateway).to receive(:void).with('123', card, anything).and_return(success_response)
405
- payment.void_transaction!
406
- end
407
- end
408
-
409
- context "when profiles are not supported" do
410
- it "should call payment_gateway.void with the payment's response_code" do
411
- allow(gateway).to receive_messages payment_profiles_supported?: false
412
- expect(gateway).to receive(:void).with('123', anything).and_return(success_response)
413
- payment.void_transaction!
414
- end
415
- end
416
-
417
- it "should log the response" do
418
- expect(payment.log_entries).to receive(:create!).with(details: anything)
419
- payment.void_transaction!
420
- end
421
-
422
- context "if successful" do
423
- it "should update the response_code with the authorization from the gateway" do
424
- # Change it to something different
425
- payment.response_code = 'abc'
426
- payment.void_transaction!
427
- expect(payment.response_code).to eq('12345')
428
- end
429
- end
430
-
431
- context "if unsuccessful" do
432
- it "should not void the payment" do
433
- allow(gateway).to receive_messages void: failed_response
434
- expect(payment).not_to receive(:void)
435
- expect { payment.void_transaction! }.to raise_error(Spree::Core::GatewayError)
436
- end
437
- end
438
-
439
- # Regression test for #2119
440
- context "if payment is already voided" do
441
- before do
442
- payment.state = 'void'
443
- end
444
-
445
- it "should not void the payment" do
446
- expect(payment.payment_method).not_to receive(:void)
447
- payment.void_transaction!
448
- end
449
- end
450
- end
451
-
452
- end
453
-
454
- context "when already processing" do
455
- it "should return nil without trying to process the source" do
456
- payment.state = 'processing'
457
-
458
- expect(payment.process!).to be_nil
459
- end
460
- end
461
-
462
- context "with source required" do
463
- context "raises an error if no source is specified" do
464
- before do
465
- payment.source = nil
466
- end
467
-
468
- specify do
469
- expect { payment.process! }.to raise_error(Spree::Core::GatewayError, Spree.t(:payment_processing_failed))
470
- end
471
- end
472
- end
473
-
474
- context "with source optional" do
475
- context "raises no error if source is not specified" do
476
- before do
477
- payment.source = nil
478
- allow(payment.payment_method).to receive_messages(source_required?: false)
479
- end
480
-
481
- specify do
482
- expect { payment.process! }.not_to raise_error
483
- end
484
- end
485
- end
486
-
487
- describe "#credit_allowed" do
488
- # Regression test for #4403 & #4407
489
- it "is the difference between offsets total and payment amount" do
490
- payment.amount = 100
491
- allow(payment).to receive(:offsets_total).and_return(0)
492
- expect(payment.credit_allowed).to eq(100)
493
- allow(payment).to receive(:offsets_total).and_return(-80)
494
- expect(payment.credit_allowed).to eq(20)
495
- end
496
- end
497
-
498
- describe "#can_credit?" do
499
- it "is true if credit_allowed > 0" do
500
- allow(payment).to receive(:credit_allowed).and_return(100)
501
- expect(payment.can_credit?).to be true
502
- end
503
-
504
- it "is false if credit_allowed is 0" do
505
- allow(payment).to receive(:credit_allowed).and_return(0)
506
- expect(payment.can_credit?).to be false
507
- end
508
- end
509
-
510
- describe "#save" do
511
- context "captured payments" do
512
- it "update order payment total" do
513
- payment = create(:payment, order: order, state: 'completed')
514
- expect(order.payment_total).to eq payment.amount
515
- end
516
- end
517
-
518
- context "not completed payments" do
519
- it "doesn't update order payment total" do
520
- expect {
521
- Spree::Payment.create(amount: 100, order: order)
522
- }.not_to change { order.payment_total }
523
- end
524
-
525
- it "requires a payment method" do
526
- expect(Spree::Payment.create(amount: 100, order: order)).to have(1).error_on(:payment_method)
527
- end
528
- end
529
-
530
- context 'when the payment was completed but now void' do
531
- let(:payment) do
532
- Spree::Payment.create(
533
- amount: 100,
534
- order: order,
535
- state: 'completed'
536
- )
537
- end
538
-
539
- it 'updates order payment total' do
540
- payment.void
541
- expect(order.payment_total).to eq 0
542
- end
543
- end
544
-
545
- context "completed orders" do
546
- before { allow(order).to receive_messages completed?: true }
547
-
548
- it "updates payment_state and shipments" do
549
- expect(order.updater).to receive(:update_payment_state)
550
- expect(order.updater).to receive(:update_shipment_state)
551
- Spree::Payment.create(amount: 100, order: order, payment_method: gateway)
552
- end
553
- end
554
-
555
- context "when profiles are supported" do
556
- before do
557
- allow(gateway).to receive_messages payment_profiles_supported?: true
558
- allow(payment.source).to receive_messages has_payment_profile?: false
559
- end
560
-
561
- context "when there is an error connecting to the gateway" do
562
- it "should call gateway_error " do
563
- message = double("gateway_error")
564
- connection_error = ActiveMerchant::ConnectionError.new(message, nil)
565
- expect(gateway).to receive(:create_profile).and_raise(connection_error)
566
- expect do
567
- Spree::Payment.create(
568
- amount: 100,
569
- order: order,
570
- source: card,
571
- payment_method: gateway
572
- )
573
- end.to raise_error(Spree::Core::GatewayError)
574
- end
575
- end
576
-
577
- context "with multiple payment attempts" do
578
- let(:attributes) { attributes_for(:credit_card) }
579
- it "should not try to create profiles on old failed payment attempts" do
580
- allow_any_instance_of(Spree::Payment).to receive(:payment_method) { gateway }
581
-
582
- order.payments.create!(
583
- source_attributes: attributes,
584
- payment_method: gateway,
585
- amount: 100
586
- )
587
- expect(gateway).to receive(:create_profile).exactly :once
588
- expect(order.payments.count).to eq(1)
589
- order.payments.create!(
590
- source_attributes: attributes,
591
- payment_method: gateway,
592
- amount: 100
593
- )
594
- end
595
-
596
- end
597
-
598
- context "when successfully connecting to the gateway" do
599
- it "should create a payment profile" do
600
- expect(payment.payment_method).to receive :create_profile
601
- payment = Spree::Payment.create(
602
- amount: 100,
603
- order: order,
604
- source: card,
605
- payment_method: gateway
606
- )
607
- end
608
- end
609
- end
610
-
611
- context "when profiles are not supported" do
612
- before { allow(gateway).to receive_messages payment_profiles_supported?: false }
613
-
614
- it "should not create a payment profile" do
615
- expect(gateway).not_to receive :create_profile
616
- payment = Spree::Payment.create(
617
- amount: 100,
618
- order: order,
619
- source: card,
620
- payment_method: gateway
621
- )
622
- end
623
- end
624
- end
625
-
626
- describe "#build_source" do
627
- let(:params) do
628
- {
629
- amount: 100,
630
- payment_method: gateway,
631
- source_attributes: {
632
- expiry:"01 / 99",
633
- number: '1234567890123',
634
- verification_value: '123',
635
- name: 'Spree Commerce'
636
- }
637
- }
638
- end
639
-
640
- it "should build the payment's source" do
641
- payment = Spree::Payment.new(params)
642
- expect(payment).to be_valid
643
- expect(payment.source).not_to be_nil
644
- end
645
-
646
- it "assigns user and gateway to payment source" do
647
- order = create(:order)
648
- source = order.payments.new(params).source
649
-
650
- expect(source.user_id).to eq order.user_id
651
- expect(source.payment_method_id).to eq gateway.id
652
- end
653
-
654
- it "errors when payment source not valid" do
655
- params = { amount: 100, payment_method: gateway,
656
- source_attributes: {expiry: "1 / 12" }}
657
-
658
- payment = Spree::Payment.new(params)
659
- expect(payment).not_to be_valid
660
- expect(payment.source).not_to be_nil
661
- expect(payment.source.error_on(:number).size).to eq(1)
662
- expect(payment.source.error_on(:verification_value).size).to eq(1)
663
- end
664
-
665
- it "does not build a new source when duplicating the model with source_attributes set" do
666
- payment = create(:payment)
667
- payment.source_attributes = params[:source_attributes]
668
- expect { payment.dup }.to_not change { payment.source }
669
- end
670
- end
671
-
672
- describe "#currency" do
673
- before { allow(order).to receive(:currency) { "ABC" } }
674
- it "returns the order currency" do
675
- expect(payment.currency).to eq("ABC")
676
- end
677
- end
678
-
679
- describe "#display_amount" do
680
- it "returns a Spree::Money for this amount" do
681
- expect(payment.display_amount).to eq(Spree::Money.new(payment.amount))
682
- end
683
- end
684
-
685
- # Regression test for #2216
686
- describe "#gateway_options" do
687
- before { allow(order).to receive_messages(last_ip_address: "192.168.1.1") }
688
-
689
- it "contains an IP" do
690
- expect(payment.gateway_options[:ip]).to eq(order.last_ip_address)
691
- end
692
-
693
- it "contains the email address from a persisted order" do
694
- # Sets the payment's order to a different Ruby object entirely
695
- payment.order = Spree::Order.find(payment.order_id)
696
- email = 'foo@example.com'
697
- order.update_attributes(email: email)
698
- expect(payment.gateway_options[:email]).to eq(email)
699
- end
700
- end
701
-
702
- describe "#amount=" do
703
- before do
704
- subject.amount = amount
705
- end
706
-
707
- context "when the amount is a string" do
708
- context "amount is a decimal" do
709
- let(:amount) { '2.99' }
710
-
711
- it '#amount' do
712
- expect(subject.amount).to eql(BigDecimal('2.99'))
713
- end
714
- end
715
-
716
- context "amount is an integer" do
717
- let(:amount) { '2' }
718
-
719
- it '#amount' do
720
- expect(subject.amount).to eql(BigDecimal('2.0'))
721
- end
722
- end
723
-
724
- context "amount contains a dollar sign" do
725
- let(:amount) { '$2.99' }
726
-
727
- it '#amount' do
728
- expect(subject.amount).to eql(BigDecimal('2.99'))
729
- end
730
- end
731
-
732
- context "amount contains a comma" do
733
- let(:amount) { '$2,999.99' }
734
-
735
- it '#amount' do
736
- expect(subject.amount).to eql(BigDecimal('2999.99'))
737
- end
738
- end
739
-
740
- context "amount contains a negative sign" do
741
- let(:amount) { '-2.99' }
742
-
743
- it '#amount' do
744
- expect(subject.amount).to eql(BigDecimal('-2.99'))
745
- end
746
- end
747
-
748
- context "amount is invalid" do
749
- let(:amount) { 'invalid' }
750
-
751
- # this is a strange default for ActiveRecord
752
-
753
- it '#amount' do
754
- expect(subject.amount).to eql(BigDecimal('0'))
755
- end
756
- end
757
-
758
- context "amount is an empty string" do
759
- let(:amount) { '' }
760
-
761
- it '#amount' do
762
- expect(subject.amount).to be_nil
763
- end
764
- end
765
- end
766
-
767
- context "when the amount is a number" do
768
- let(:amount) { 1.55 }
769
-
770
- it '#amount' do
771
- expect(subject.amount).to eql(BigDecimal('1.55'))
772
- end
773
- end
774
-
775
- context "when the locale uses a coma as a decimal separator" do
776
- before(:each) do
777
- I18n.backend.store_translations(:fr, { number: { currency: { format: { delimiter: ' ', separator: ',' } } } })
778
- I18n.locale = :fr
779
- subject.amount = amount
780
- end
781
-
782
- after do
783
- I18n.locale = I18n.default_locale
784
- end
785
-
786
- context "amount is a decimal" do
787
- let(:amount) { '2,99' }
788
-
789
- it '#amount' do
790
- expect(subject.amount).to eql(BigDecimal('2.99'))
791
- end
792
- end
793
-
794
- context "amount contains a $ sign" do
795
- let(:amount) { '2,99 $' }
796
-
797
- it '#amount' do
798
- expect(subject.amount).to eql(BigDecimal('2.99'))
799
- end
800
- end
801
-
802
- context "amount is a number" do
803
- let(:amount) { 2.99 }
804
-
805
- it '#amount' do
806
- expect(subject.amount).to eql(BigDecimal('2.99'))
807
- end
808
- end
809
-
810
- context "amount contains a negative sign" do
811
- let(:amount) { '-2,99 $' }
812
-
813
- it '#amount' do
814
- expect(subject.amount).to eql(BigDecimal('-2.99'))
815
- end
816
- end
817
-
818
- context "amount uses a dot as a decimal separator" do
819
- let(:amount) { '2.99' }
820
-
821
- it '#amount' do
822
- expect(subject.amount).to eql(BigDecimal('2.99'))
823
- end
824
- end
825
- end
826
- end
827
-
828
- describe "is_avs_risky?" do
829
- it "returns false if avs_response included in NON_RISKY_AVS_CODES" do
830
- ('A'..'Z').reject{ |x| subject.class::RISKY_AVS_CODES.include?(x) }.to_a.each do |char|
831
- payment.update_attribute(:avs_response, char)
832
- expect(payment.is_avs_risky?).to eq false
833
- end
834
- end
835
-
836
- it "returns false if avs_response.blank?" do
837
- payment.update_attribute(:avs_response, nil)
838
- expect(payment.is_avs_risky?).to eq false
839
- payment.update_attribute(:avs_response, '')
840
- expect(payment.is_avs_risky?).to eq false
841
- end
842
-
843
- it "returns true if avs_response in RISKY_AVS_CODES" do
844
- # should use avs_response_code helper
845
- ('A'..'Z').reject{ |x| subject.class::NON_RISKY_AVS_CODES.include?(x) }.to_a.each do |char|
846
- payment.update_attribute(:avs_response, char)
847
- expect(payment.is_avs_risky?).to eq true
848
- end
849
- end
850
- end
851
-
852
- describe "is_cvv_risky?" do
853
- it "returns false if cvv_response_code == 'M'" do
854
- payment.update_attribute(:cvv_response_code, "M")
855
- expect(payment.is_cvv_risky?).to eq(false)
856
- end
857
-
858
- it "returns false if cvv_response_code == nil" do
859
- payment.update_attribute(:cvv_response_code, nil)
860
- expect(payment.is_cvv_risky?).to eq(false)
861
- end
862
-
863
- it "returns false if cvv_response_message == ''" do
864
- payment.update_attribute(:cvv_response_message, '')
865
- expect(payment.is_cvv_risky?).to eq(false)
866
- end
867
-
868
- it "returns true if cvv_response_code == [A-Z], omitting D" do
869
- # should use cvv_response_code helper
870
- (%w{N P S U} << "").each do |char|
871
- payment.update_attribute(:cvv_response_code, char)
872
- expect(payment.is_cvv_risky?).to eq(true)
873
- end
874
- end
875
- end
876
-
877
- describe "#editable?" do
878
- subject { payment }
879
-
880
- before do
881
- subject.state = state
882
- end
883
-
884
- context "when the state is 'checkout'" do
885
- let(:state) { 'checkout' }
886
-
887
- its(:editable?) { should be(true) }
888
- end
889
-
890
- context "when the state is 'pending'" do
891
- let(:state) { 'pending' }
892
-
893
- its(:editable?) { should be(true) }
894
- end
895
-
896
- %w[processing completed failed void invalid].each do |state|
897
- context "when the state is '#{state}'" do
898
- let(:state) { state }
899
-
900
- its(:editable?) { should be(false) }
901
- end
902
- end
903
- end
904
-
905
- # Regression test for #4072 (kinda)
906
- # The need for this was discovered in the research for #4072
907
- context "state changes" do
908
- it "are logged to the database" do
909
- expect(payment.state_changes).to be_empty
910
- expect(payment.process!).to be true
911
- expect(payment.state_changes.count).to eq(2)
912
- changes = payment.state_changes.map { |change| { change.previous_state => change.next_state} }
913
- expect(changes).to match_array([
914
- {"checkout" => "processing"},
915
- { "processing" => "pending"}
916
- ])
917
- end
918
- end
919
- end