spree_core 3.3.0.rc1 → 3.3.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. checksums.yaml +4 -4
  2. data/app/helpers/spree/base_helper.rb +1 -1
  3. data/app/mailers/spree/base_mailer.rb +5 -0
  4. data/app/models/spree/ability.rb +4 -1
  5. data/app/models/spree/image.rb +6 -2
  6. data/app/models/spree/order.rb +10 -9
  7. data/app/models/spree/order/payments.rb +1 -1
  8. data/app/models/spree/payment_method/store_credit.rb +4 -0
  9. data/app/models/spree/shipment.rb +2 -0
  10. data/app/views/spree/shared/_base_mailer_header.html.erb +5 -1
  11. data/app/views/spree/shared/_mailer_line_item.html.erb +5 -1
  12. data/config/routes.rb +0 -3
  13. data/lib/spree/core/components.rb +17 -0
  14. data/lib/spree/core/controller_helpers/auth.rb +3 -1
  15. data/lib/spree/core/engine.rb +1 -0
  16. data/lib/spree/core/validators/email.rb +1 -5
  17. data/lib/spree/core/version.rb +1 -1
  18. data/spree_core.gemspec +1 -1
  19. metadata +4 -204
  20. data/lib/spree/testing_support/shoulda_matcher_configuration.rb +0 -6
  21. data/spec/fixtures/thinking-cat.jpg +0 -0
  22. data/spec/helpers/base_helper_spec.rb +0 -200
  23. data/spec/helpers/products_helper_spec.rb +0 -289
  24. data/spec/lib/calculated_adjustments_spec.rb +0 -7
  25. data/spec/lib/i18n_spec.rb +0 -123
  26. data/spec/lib/search/base_spec.rb +0 -86
  27. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +0 -103
  28. data/spec/lib/spree/core/controller_helpers/order_spec.rb +0 -110
  29. data/spec/lib/spree/core/controller_helpers/search_spec.rb +0 -17
  30. data/spec/lib/spree/core/controller_helpers/store_spec.rb +0 -72
  31. data/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb +0 -39
  32. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +0 -22
  33. data/spec/lib/spree/core/importer/order_spec.rb +0 -607
  34. data/spec/lib/spree/core/number_generator_spec.rb +0 -139
  35. data/spec/lib/spree/core/token_generator_spec.rb +0 -24
  36. data/spec/lib/spree/core/validators/email_spec.rb +0 -54
  37. data/spec/lib/spree/core_spec.rb +0 -23
  38. data/spec/lib/spree/localized_number_spec.rb +0 -54
  39. data/spec/lib/spree/migrations_spec.rb +0 -36
  40. data/spec/lib/spree/money_spec.rb +0 -122
  41. data/spec/lib/tasks/exchanges_spec.rb +0 -136
  42. data/spec/mailers/order_mailer_spec.rb +0 -122
  43. data/spec/mailers/reimbursement_mailer_spec.rb +0 -52
  44. data/spec/mailers/shipment_mailer_spec.rb +0 -81
  45. data/spec/mailers/test_mailer_spec.rb +0 -38
  46. data/spec/models/spree/ability_spec.rb +0 -251
  47. data/spec/models/spree/address_spec.rb +0 -402
  48. data/spec/models/spree/adjustable/adjuster/base_spec.rb +0 -10
  49. data/spec/models/spree/adjustable/adjuster/promotion_spec.rb +0 -211
  50. data/spec/models/spree/adjustable/adjuster/tax_spec.rb +0 -86
  51. data/spec/models/spree/adjustable/adjustments_updater_spec.rb +0 -26
  52. data/spec/models/spree/adjustment_spec.rb +0 -189
  53. data/spec/models/spree/app_configuration_spec.rb +0 -26
  54. data/spec/models/spree/asset_spec.rb +0 -28
  55. data/spec/models/spree/calculator/default_tax_spec.rb +0 -152
  56. data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +0 -25
  57. data/spec/models/spree/calculator/flat_rate_spec.rb +0 -47
  58. data/spec/models/spree/calculator/flexi_rate_spec.rb +0 -41
  59. data/spec/models/spree/calculator/percent_on_line_item_spec.rb +0 -15
  60. data/spec/models/spree/calculator/price_sack_spec.rb +0 -30
  61. data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +0 -47
  62. data/spec/models/spree/calculator/shipping.rb +0 -8
  63. data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +0 -23
  64. data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +0 -13
  65. data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +0 -52
  66. data/spec/models/spree/calculator/shipping/per_item_spec.rb +0 -20
  67. data/spec/models/spree/calculator/shipping/price_sack_spec.rb +0 -29
  68. data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +0 -40
  69. data/spec/models/spree/calculator/tiered_percent_spec.rb +0 -51
  70. data/spec/models/spree/calculator_spec.rb +0 -69
  71. data/spec/models/spree/classification_spec.rb +0 -93
  72. data/spec/models/spree/concerns/display_money_spec.rb +0 -43
  73. data/spec/models/spree/concerns/user_methods_spec.rb +0 -82
  74. data/spec/models/spree/concerns/vat_price_calculation_spec.rb +0 -66
  75. data/spec/models/spree/country_spec.rb +0 -55
  76. data/spec/models/spree/credit_card_spec.rb +0 -328
  77. data/spec/models/spree/customer_return_spec.rb +0 -240
  78. data/spec/models/spree/exchange_spec.rb +0 -75
  79. data/spec/models/spree/gateway/bogus_simple.rb +0 -20
  80. data/spec/models/spree/gateway/bogus_spec.rb +0 -13
  81. data/spec/models/spree/gateway_spec.rb +0 -61
  82. data/spec/models/spree/image_spec.rb +0 -8
  83. data/spec/models/spree/inventory_unit_spec.rb +0 -256
  84. data/spec/models/spree/line_item_spec.rb +0 -348
  85. data/spec/models/spree/option_type_prototype_spec.rb +0 -9
  86. data/spec/models/spree/option_type_spec.rb +0 -14
  87. data/spec/models/spree/option_value_spec.rb +0 -18
  88. data/spec/models/spree/order/address_spec.rb +0 -50
  89. data/spec/models/spree/order/adjustments_spec.rb +0 -29
  90. data/spec/models/spree/order/callbacks_spec.rb +0 -42
  91. data/spec/models/spree/order/checkout_spec.rb +0 -770
  92. data/spec/models/spree/order/currency_updater_spec.rb +0 -32
  93. data/spec/models/spree/order/finalizing_spec.rb +0 -114
  94. data/spec/models/spree/order/helpers_spec.rb +0 -5
  95. data/spec/models/spree/order/payment_spec.rb +0 -214
  96. data/spec/models/spree/order/risk_assessment_spec.rb +0 -84
  97. data/spec/models/spree/order/shipments_spec.rb +0 -43
  98. data/spec/models/spree/order/state_machine_spec.rb +0 -212
  99. data/spec/models/spree/order/store_credit_spec.rb +0 -457
  100. data/spec/models/spree/order/tax_spec.rb +0 -84
  101. data/spec/models/spree/order/totals_spec.rb +0 -24
  102. data/spec/models/spree/order/updating_spec.rb +0 -18
  103. data/spec/models/spree/order/validations_spec.rb +0 -15
  104. data/spec/models/spree/order_contents_spec.rb +0 -332
  105. data/spec/models/spree/order_inventory_spec.rb +0 -247
  106. data/spec/models/spree/order_merger_spec.rb +0 -135
  107. data/spec/models/spree/order_spec.rb +0 -1067
  108. data/spec/models/spree/order_updater_spec.rb +0 -305
  109. data/spec/models/spree/payment/gateway_options_spec.rb +0 -127
  110. data/spec/models/spree/payment/store_credit_spec.rb +0 -60
  111. data/spec/models/spree/payment_method/store_credit_spec.rb +0 -291
  112. data/spec/models/spree/payment_method_spec.rb +0 -108
  113. data/spec/models/spree/payment_spec.rb +0 -922
  114. data/spec/models/spree/preference_spec.rb +0 -80
  115. data/spec/models/spree/preferences/configuration_spec.rb +0 -30
  116. data/spec/models/spree/preferences/preferable_spec.rb +0 -344
  117. data/spec/models/spree/preferences/scoped_store_spec.rb +0 -58
  118. data/spec/models/spree/preferences/store_spec.rb +0 -46
  119. data/spec/models/spree/price_spec.rb +0 -128
  120. data/spec/models/spree/product/scopes_spec.rb +0 -174
  121. data/spec/models/spree/product_duplicator_spec.rb +0 -102
  122. data/spec/models/spree/product_filter_spec.rb +0 -26
  123. data/spec/models/spree/product_option_type_spec.rb +0 -9
  124. data/spec/models/spree/product_promotion_rule_spec.rb +0 -9
  125. data/spec/models/spree/product_property_spec.rb +0 -26
  126. data/spec/models/spree/product_spec.rb +0 -626
  127. data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +0 -113
  128. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +0 -148
  129. data/spec/models/spree/promotion/actions/create_line_items_spec.rb +0 -86
  130. data/spec/models/spree/promotion/actions/free_shipping_spec.rb +0 -36
  131. data/spec/models/spree/promotion/rules/country_spec.rb +0 -36
  132. data/spec/models/spree/promotion/rules/first_order_spec.rb +0 -75
  133. data/spec/models/spree/promotion/rules/item_total_spec.rb +0 -282
  134. data/spec/models/spree/promotion/rules/one_use_per_user_spec.rb +0 -42
  135. data/spec/models/spree/promotion/rules/option_value_spec.rb +0 -90
  136. data/spec/models/spree/promotion/rules/product_spec.rb +0 -143
  137. data/spec/models/spree/promotion/rules/taxon_spec.rb +0 -102
  138. data/spec/models/spree/promotion/rules/user_logged_in_spec.rb +0 -27
  139. data/spec/models/spree/promotion/rules/user_spec.rb +0 -45
  140. data/spec/models/spree/promotion_action_spec.rb +0 -10
  141. data/spec/models/spree/promotion_category_spec.rb +0 -17
  142. data/spec/models/spree/promotion_handler/cart_spec.rb +0 -102
  143. data/spec/models/spree/promotion_handler/coupon_spec.rb +0 -323
  144. data/spec/models/spree/promotion_handler/free_shipping_spec.rb +0 -48
  145. data/spec/models/spree/promotion_handler/page_spec.rb +0 -44
  146. data/spec/models/spree/promotion_rule_spec.rb +0 -29
  147. data/spec/models/spree/promotion_rule_taxon_spec.rb +0 -9
  148. data/spec/models/spree/promotion_rule_user_spec.rb +0 -9
  149. data/spec/models/spree/promotion_spec.rb +0 -674
  150. data/spec/models/spree/property_prototype_spec.rb +0 -9
  151. data/spec/models/spree/property_spec.rb +0 -5
  152. data/spec/models/spree/prototype_spec.rb +0 -5
  153. data/spec/models/spree/prototype_taxon_spec.rb +0 -9
  154. data/spec/models/spree/refund_reason_spec.rb +0 -20
  155. data/spec/models/spree/refund_spec.rb +0 -195
  156. data/spec/models/spree/reimbursement/credit_spec.rb +0 -36
  157. data/spec/models/spree/reimbursement/reimbursement_type_engine_spec.rb +0 -140
  158. data/spec/models/spree/reimbursement/reimbursement_type_validator_spec.rb +0 -83
  159. data/spec/models/spree/reimbursement_performer_spec.rb +0 -30
  160. data/spec/models/spree/reimbursement_spec.rb +0 -188
  161. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +0 -63
  162. data/spec/models/spree/reimbursement_type/credit_spec.rb +0 -53
  163. data/spec/models/spree/reimbursement_type/exchange_spec.rb +0 -46
  164. data/spec/models/spree/reimbursement_type/original_payment_spec.rb +0 -55
  165. data/spec/models/spree/reimbursement_type/store_credit_spec.rb +0 -101
  166. data/spec/models/spree/return_authorization_reason_spec.rb +0 -7
  167. data/spec/models/spree/return_authorization_spec.rb +0 -230
  168. data/spec/models/spree/return_item/eligibility_validator/default_spec.rb +0 -77
  169. data/spec/models/spree/return_item/eligibility_validator/inventory_shipped_spec.rb +0 -58
  170. data/spec/models/spree/return_item/eligibility_validator/no_reimbursements_spec.rb +0 -61
  171. data/spec/models/spree/return_item/eligibility_validator/order_completed_spec.rb +0 -32
  172. data/spec/models/spree/return_item/eligibility_validator/rma_required_spec.rb +0 -29
  173. data/spec/models/spree/return_item/eligibility_validator/time_since_purchase_spec.rb +0 -35
  174. data/spec/models/spree/return_item/exchange_variant_eligibility/same_option_value_spec.rb +0 -65
  175. data/spec/models/spree/return_item/exchange_variant_eligibility/same_product_spec.rb +0 -43
  176. data/spec/models/spree/return_item_spec.rb +0 -734
  177. data/spec/models/spree/returns_calculator_spec.rb +0 -14
  178. data/spec/models/spree/role_spec.rb +0 -7
  179. data/spec/models/spree/shipment_spec.rb +0 -744
  180. data/spec/models/spree/shipping_calculator_spec.rb +0 -45
  181. data/spec/models/spree/shipping_category_spec.rb +0 -19
  182. data/spec/models/spree/shipping_method_spec.rb +0 -125
  183. data/spec/models/spree/shipping_rate_spec.rb +0 -140
  184. data/spec/models/spree/state_spec.rb +0 -29
  185. data/spec/models/spree/stock/availability_validator_spec.rb +0 -42
  186. data/spec/models/spree/stock/content_item_spec.rb +0 -31
  187. data/spec/models/spree/stock/coordinator_spec.rb +0 -61
  188. data/spec/models/spree/stock/differentiator_spec.rb +0 -39
  189. data/spec/models/spree/stock/estimator_spec.rb +0 -202
  190. data/spec/models/spree/stock/inventory_unit_builder_spec.rb +0 -37
  191. data/spec/models/spree/stock/package_spec.rb +0 -182
  192. data/spec/models/spree/stock/packer_spec.rb +0 -70
  193. data/spec/models/spree/stock/prioritizer_spec.rb +0 -125
  194. data/spec/models/spree/stock/quantifier_spec.rb +0 -126
  195. data/spec/models/spree/stock/splitter/backordered_spec.rb +0 -29
  196. data/spec/models/spree/stock/splitter/base_spec.rb +0 -21
  197. data/spec/models/spree/stock/splitter/shipping_category_spec.rb +0 -47
  198. data/spec/models/spree/stock/splitter/weight_spec.rb +0 -32
  199. data/spec/models/spree/stock_item_spec.rb +0 -465
  200. data/spec/models/spree/stock_location_spec.rb +0 -243
  201. data/spec/models/spree/stock_movement_spec.rb +0 -120
  202. data/spec/models/spree/stock_transfer_spec.rb +0 -50
  203. data/spec/models/spree/store_credit_event_spec.rb +0 -101
  204. data/spec/models/spree/store_credit_spec.rb +0 -798
  205. data/spec/models/spree/store_spec.rb +0 -78
  206. data/spec/models/spree/tax_category_spec.rb +0 -32
  207. data/spec/models/spree/tax_rate_spec.rb +0 -561
  208. data/spec/models/spree/taxon_spec.rb +0 -93
  209. data/spec/models/spree/taxonomy_spec.rb +0 -18
  210. data/spec/models/spree/tracker_spec.rb +0 -21
  211. data/spec/models/spree/user_spec.rb +0 -203
  212. data/spec/models/spree/variant_spec.rb +0 -818
  213. data/spec/models/spree/zone_member_spec.rb +0 -38
  214. data/spec/models/spree/zone_spec.rb +0 -472
  215. data/spec/spec_helper.rb +0 -82
  216. data/spec/support/big_decimal.rb +0 -5
  217. data/spec/support/concerns/adjustment_source.rb +0 -23
  218. data/spec/support/concerns/default_price.rb +0 -37
  219. data/spec/support/rake.rb +0 -13
  220. data/spec/support/test_gateway.rb +0 -2
@@ -1,14 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Spree
4
- describe ReturnsCalculator, type: :model do
5
- let(:return_item) { build(:return_item) }
6
- subject { ReturnsCalculator.new }
7
-
8
- it 'compute_shipment must be overridden' do
9
- expect {
10
- subject.compute(return_item)
11
- }.to raise_error(NotImplementedError)
12
- end
13
- end
14
- end
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::Role, type: :model do
4
- describe 'Associations' do
5
- it { is_expected.to have_many(:role_users).class_name('Spree::RoleUser').dependent(:destroy) }
6
- end
7
- end
@@ -1,744 +0,0 @@
1
- require 'spec_helper'
2
- require 'benchmark'
3
-
4
- describe Spree::Shipment, type: :model do
5
- let(:order) { mock_model Spree::Order, backordered?: false,
6
- canceled?: false,
7
- can_ship?: true,
8
- currency: 'USD',
9
- number: 'S12345',
10
- paid?: false,
11
- touch_later: false }
12
- let(:shipping_method) { create(:shipping_method, name: "UPS") }
13
- let(:shipment) do
14
- shipment = Spree::Shipment.new(cost: 1, state: 'pending', stock_location: create(:stock_location))
15
- allow(shipment).to receive_messages order: order
16
- allow(shipment).to receive_messages shipping_method: shipping_method
17
- shipment.save
18
- shipment
19
- end
20
-
21
- let(:variant) { mock_model(Spree::Variant) }
22
- let(:line_item) { mock_model(Spree::LineItem, variant: variant) }
23
-
24
- def create_shipment(order, stock_location)
25
- order.shipments.create({ stock_location_id: stock_location.id }).inventory_units.create(
26
- order_id: order.id,
27
- variant_id: order.line_items.first.variant_id,
28
- line_item_id: order.line_items.first.id
29
- )
30
-
31
- end
32
-
33
- describe "precision of pre_tax_amount" do
34
- let(:line_item) { create :line_item, pre_tax_amount: 4.2051 }
35
-
36
- it "keeps four digits of precision even when reloading" do
37
- # prevent it from updating pre_tax_amount
38
- allow_any_instance_of(Spree::LineItem).to receive(:update_tax_charge)
39
- expect(line_item.reload.pre_tax_amount).to eq(4.2051)
40
- end
41
- end
42
-
43
- # Regression test for #4063
44
- context "number generation" do
45
- before do
46
- allow(order).to receive :update_with_updater!
47
- end
48
-
49
- it "generates a number containing a letter + 11 numbers" do
50
- expect(shipment.number[0]).to eq("H")
51
- expect(/\d{11}/.match(shipment.number)).not_to be_nil
52
- expect(shipment.number.length).to eq(12)
53
- end
54
- end
55
-
56
- it 'is backordered if one if its inventory_units is backordered' do
57
- allow(shipment).to receive_messages(inventory_units: [
58
- mock_model(Spree::InventoryUnit, backordered?: false),
59
- mock_model(Spree::InventoryUnit, backordered?: true)
60
- ])
61
- expect(shipment).to be_backordered
62
- end
63
-
64
- context '#determine_state' do
65
- it 'returns canceled if order is canceled?' do
66
- allow(order).to receive_messages canceled?: true
67
- expect(shipment.determine_state(order)).to eq 'canceled'
68
- end
69
-
70
- it 'returns pending unless order.can_ship?' do
71
- allow(order).to receive_messages can_ship?: false
72
- expect(shipment.determine_state(order)).to eq 'pending'
73
- end
74
-
75
- it 'returns pending if backordered' do
76
- allow(shipment).to receive_messages inventory_units: [mock_model(Spree::InventoryUnit, backordered?: true)]
77
- expect(shipment.determine_state(order)).to eq 'pending'
78
- end
79
-
80
- it 'returns shipped when already shipped' do
81
- allow(shipment).to receive_messages state: 'shipped'
82
- expect(shipment.determine_state(order)).to eq 'shipped'
83
- end
84
-
85
- it 'returns pending when unpaid' do
86
- expect(shipment.determine_state(order)).to eq 'pending'
87
- end
88
-
89
- it 'returns ready when paid' do
90
- allow(order).to receive_messages paid?: true
91
- expect(shipment.determine_state(order)).to eq 'ready'
92
- end
93
-
94
- it 'returns ready when Config.auto_capture_on_dispatch' do
95
- Spree::Config.auto_capture_on_dispatch = true
96
- expect(shipment.determine_state(order)).to eq 'ready'
97
- end
98
- end
99
-
100
- context "display_amount" do
101
- it "retuns a Spree::Money" do
102
- allow(shipment).to receive(:cost) { 21.22 }
103
- expect(shipment.display_amount).to eq(Spree::Money.new(21.22))
104
- end
105
- end
106
-
107
- context "display_final_price" do
108
- it "retuns a Spree::Money" do
109
- allow(shipment).to receive(:final_price) { 21.22 }
110
- expect(shipment.display_final_price).to eq(Spree::Money.new(21.22))
111
- end
112
- end
113
-
114
- context "display_item_cost" do
115
- it "retuns a Spree::Money" do
116
- allow(shipment).to receive(:item_cost) { 21.22 }
117
- expect(shipment.display_item_cost).to eq(Spree::Money.new(21.22))
118
- end
119
- end
120
-
121
- context "#item_cost" do
122
- it 'should equal shipment line items amount with tax' do
123
- order = create(:order_with_line_item_quantity, line_items_quantity: 2)
124
-
125
- stock_location = create(:stock_location)
126
-
127
- create_shipment(order, stock_location)
128
- create_shipment(order, stock_location)
129
-
130
- create :tax_adjustment, adjustable: order.line_items.first, order: order
131
-
132
- expect(order.shipments.first.item_cost).to eql(11.0)
133
- expect(order.shipments.last.item_cost).to eql(11.0)
134
- end
135
-
136
- it 'should equal line items final amount with tax' do
137
- shipment = create(:shipment, order: create(:order_with_line_item_quantity, line_items_quantity: 2))
138
- create :tax_adjustment, adjustable: shipment.order.line_items.first, order: shipment.order
139
- expect(shipment.item_cost).to eql(22.0)
140
- end
141
- end
142
-
143
- it "#discounted_cost" do
144
- shipment = create(:shipment)
145
- shipment.cost = 10
146
- shipment.promo_total = -1
147
- expect(shipment.discounted_cost).to eq(9)
148
- end
149
-
150
- it "#tax_total with included taxes" do
151
- shipment = Spree::Shipment.new
152
- expect(shipment.tax_total).to eq(0)
153
- shipment.included_tax_total = 10
154
- expect(shipment.tax_total).to eq(10)
155
- end
156
-
157
- it "#tax_total with additional taxes" do
158
- shipment = Spree::Shipment.new
159
- expect(shipment.tax_total).to eq(0)
160
- shipment.additional_tax_total = 10
161
- expect(shipment.tax_total).to eq(10)
162
- end
163
-
164
- it "#final_price" do
165
- shipment = Spree::Shipment.new
166
- shipment.cost = 10
167
- shipment.adjustment_total = -2
168
- shipment.included_tax_total = 1
169
- expect(shipment.final_price).to eq(8)
170
- end
171
-
172
- context "manifest" do
173
- let(:order) { Spree::Order.create }
174
- let(:variant) { create(:variant) }
175
- let!(:line_item) { order.contents.add variant }
176
- let!(:shipment) { order.create_proposed_shipments.first }
177
-
178
- it "returns variant expected" do
179
- expect(shipment.manifest.first.variant).to eq variant
180
- end
181
-
182
- context "variant was removed" do
183
- before { variant.destroy }
184
-
185
- it "still returns variant expected" do
186
- expect(shipment.manifest.first.variant).to eq variant
187
- end
188
- end
189
- end
190
-
191
- context 'shipping_rates' do
192
- let(:shipment) { create(:shipment) }
193
- let(:shipping_method1) { create(:shipping_method) }
194
- let(:shipping_method2) { create(:shipping_method) }
195
- let(:shipping_rates) { [
196
- Spree::ShippingRate.new(shipping_method: shipping_method1, cost: 10.00, selected: true),
197
- Spree::ShippingRate.new(shipping_method: shipping_method2, cost: 20.00)
198
- ] }
199
-
200
- it 'returns shipping_method from selected shipping_rate' do
201
- shipment.shipping_rates.delete_all
202
- shipment.shipping_rates.create shipping_method: shipping_method1, cost: 10.00, selected: true
203
- expect(shipment.shipping_method).to eq shipping_method1
204
- end
205
-
206
- context 'refresh_rates' do
207
- let(:mock_estimator) { double('estimator', shipping_rates: shipping_rates) }
208
- before { allow(shipment).to receive(:can_get_rates?){ true } }
209
-
210
- it 'should request new rates, and maintain shipping_method selection' do
211
- expect(Spree::Stock::Estimator).to receive(:new).with(shipment.order).and_return(mock_estimator)
212
- allow(shipment).to receive_messages(shipping_method: shipping_method2)
213
-
214
- expect(shipment.refresh_rates).to eq(shipping_rates)
215
- expect(shipment.reload.selected_shipping_rate.shipping_method_id).to eq(shipping_method2.id)
216
- end
217
-
218
- it 'should handle no shipping_method selection' do
219
- expect(Spree::Stock::Estimator).to receive(:new).with(shipment.order).and_return(mock_estimator)
220
- allow(shipment).to receive_messages(shipping_method: nil)
221
- expect(shipment.refresh_rates).to eq(shipping_rates)
222
- expect(shipment.reload.selected_shipping_rate).not_to be_nil
223
- end
224
-
225
- it 'should not refresh if shipment is shipped' do
226
- expect(Spree::Stock::Estimator).not_to receive(:new)
227
- shipment.shipping_rates.delete_all
228
- allow(shipment).to receive_messages(shipped?: true)
229
- expect(shipment.refresh_rates).to eq([])
230
- end
231
-
232
- it "can't get rates without a shipping address" do
233
- shipment.order.ship_address = nil
234
- expect(shipment.refresh_rates).to eq([])
235
- end
236
-
237
- context 'to_package' do
238
- let(:inventory_units) do
239
- [build(:inventory_unit, line_item: line_item, variant: variant, state: 'on_hand'),
240
- build(:inventory_unit, line_item: line_item, variant: variant, state: 'backordered')]
241
- end
242
-
243
- before do
244
- allow(shipment).to receive(:inventory_units) { inventory_units }
245
- allow(inventory_units).to receive_message_chain(:includes, :joins).and_return inventory_units
246
- end
247
-
248
- it 'should use symbols for states when adding contents to package' do
249
- package = shipment.to_package
250
- expect(package.on_hand.count).to eq 1
251
- expect(package.backordered.count).to eq 1
252
- end
253
- end
254
- end
255
- end
256
-
257
- context "#update!" do
258
- shared_examples_for "immutable once shipped" do
259
- it "should remain in shipped state once shipped" do
260
- shipment.state = 'shipped'
261
- expect(shipment).to receive(:update_columns).with(state: 'shipped', updated_at: kind_of(Time))
262
- shipment.update!(order)
263
- end
264
- end
265
-
266
- shared_examples_for "pending if backordered" do
267
- it "should have a state of pending if backordered" do
268
- allow(shipment).to receive_messages(inventory_units: [mock_model(Spree::InventoryUnit, backordered?: true)])
269
- expect(shipment).to receive(:update_columns).with(state: 'pending', updated_at: kind_of(Time))
270
- shipment.update!(order)
271
- end
272
- end
273
-
274
- context "when order cannot ship" do
275
- before { allow(order).to receive_messages can_ship?: false }
276
- it "should result in a 'pending' state" do
277
- expect(shipment).to receive(:update_columns).with(state: 'pending', updated_at: kind_of(Time))
278
- shipment.update!(order)
279
- end
280
- end
281
-
282
- context "when order is paid" do
283
- before { allow(order).to receive_messages paid?: true }
284
- it "should result in a 'ready' state" do
285
- expect(shipment).to receive(:update_columns).with(state: 'ready', updated_at: kind_of(Time))
286
- shipment.update!(order)
287
- end
288
- it_should_behave_like 'immutable once shipped'
289
- it_should_behave_like 'pending if backordered'
290
- end
291
-
292
- context "when order has balance due" do
293
- before { allow(order).to receive_messages paid?: false }
294
- it "should result in a 'pending' state" do
295
- shipment.state = 'ready'
296
- expect(shipment).to receive(:update_columns).with(state: 'pending', updated_at: kind_of(Time))
297
- shipment.update!(order)
298
- end
299
- it_should_behave_like 'immutable once shipped'
300
- it_should_behave_like 'pending if backordered'
301
- end
302
-
303
- context "when order has a credit owed" do
304
- before { allow(order).to receive_messages payment_state: 'credit_owed', paid?: true }
305
- it "should result in a 'ready' state" do
306
- shipment.state = 'pending'
307
- expect(shipment).to receive(:update_columns).with(state: 'ready', updated_at: kind_of(Time))
308
- shipment.update!(order)
309
- end
310
- it_should_behave_like 'immutable once shipped'
311
- it_should_behave_like 'pending if backordered'
312
- end
313
-
314
- context "when shipment state changes to shipped" do
315
- before do
316
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:send_shipped_email)
317
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)
318
- end
319
-
320
- it "should call after_ship" do
321
- shipment.state = 'pending'
322
- expect(shipment).to receive :after_ship
323
- allow(shipment).to receive_messages determine_state: 'shipped'
324
- expect(shipment).to receive(:update_columns).with(state: 'shipped', updated_at: kind_of(Time))
325
- shipment.update!(order)
326
- end
327
-
328
- context "when using the default shipment handler" do
329
- it "should call the 'perform' method" do
330
- shipment.state = 'pending'
331
- allow(shipment).to receive_messages determine_state: 'shipped'
332
- expect_any_instance_of(Spree::ShipmentHandler).to receive(:perform)
333
- shipment.update!(order)
334
- end
335
- end
336
-
337
- context "when using a custom shipment handler" do
338
- before do
339
- Spree::ShipmentHandler::UPS = Class.new {
340
- def initialize(shipment) true end
341
- def perform() true end
342
- }
343
- end
344
-
345
- it "should call the custom handler's 'perform' method" do
346
- shipment.state = 'pending'
347
- allow(shipment).to receive_messages determine_state: 'shipped'
348
- expect_any_instance_of(Spree::ShipmentHandler::UPS).to receive(:perform)
349
- shipment.update!(order)
350
- end
351
-
352
- after do
353
- Spree::ShipmentHandler.send(:remove_const, :UPS)
354
- end
355
- end
356
-
357
- # Regression test for #4347
358
- context "with adjustments" do
359
- before do
360
- shipment.adjustments << Spree::Adjustment.create(order: order, label: "Label", amount: 5)
361
- end
362
-
363
- it "transitions to shipped" do
364
- shipment.update_column(:state, "ready")
365
- expect { shipment.ship! }.not_to raise_error
366
- end
367
- end
368
- end
369
- end
370
-
371
- context "when order is completed" do
372
- after { Spree::Config.set track_inventory_levels: true }
373
-
374
- before do
375
- allow(order).to receive_messages completed?: true
376
- allow(order).to receive_messages canceled?: false
377
- end
378
-
379
- context "with inventory tracking" do
380
- before { Spree::Config.set track_inventory_levels: true }
381
-
382
- it "should validate with inventory" do
383
- shipment.inventory_units = [create(:inventory_unit)]
384
- expect(shipment.valid?).to be true
385
- end
386
- end
387
-
388
- context "without inventory tracking" do
389
- before { Spree::Config.set track_inventory_levels: false }
390
-
391
- it "should validate with no inventory" do
392
- expect(shipment.valid?).to be true
393
- end
394
- end
395
- end
396
-
397
- context "#cancel" do
398
- it 'cancels the shipment' do
399
- allow(shipment.order).to receive(:update_with_updater!)
400
-
401
- shipment.state = 'pending'
402
- expect(shipment).to receive(:after_cancel)
403
- shipment.cancel!
404
- expect(shipment.state).to eq 'canceled'
405
- end
406
-
407
- it 'restocks the items' do
408
- inventory_unit = mock_model(Spree::InventoryUnit, state: "on_hand", line_item: line_item, variant: variant, quantity: 1)
409
- allow(shipment).to receive_message_chain(inventory_units: [inventory_unit])
410
- shipment.stock_location = mock_model(Spree::StockLocation)
411
- expect(shipment.stock_location).to receive(:restock).with(variant, 1, shipment)
412
- shipment.after_cancel
413
- end
414
-
415
- context "with backordered inventory units" do
416
- let(:order) { create(:order) }
417
- let(:variant) { create(:variant) }
418
- let(:other_order) { create(:order) }
419
-
420
- before do
421
- order.contents.add variant
422
- order.create_proposed_shipments
423
-
424
- other_order.contents.add variant
425
- other_order.create_proposed_shipments
426
- end
427
-
428
- it "doesn't fill backorders when restocking inventory units" do
429
- shipment = order.shipments.first
430
- expect(shipment.inventory_units.count).to eq 1
431
- expect(shipment.inventory_units.first).to be_backordered
432
-
433
- other_shipment = other_order.shipments.first
434
- expect(other_shipment.inventory_units.count).to eq 1
435
- expect(other_shipment.inventory_units.first).to be_backordered
436
-
437
- expect {
438
- shipment.cancel!
439
- }.not_to change { other_shipment.inventory_units.first.state }
440
- end
441
- end
442
- end
443
-
444
- context "#resume" do
445
- it 'transitions state to ready if the order is ready' do
446
- allow(shipment.order).to receive(:update_with_updater!)
447
-
448
- shipment.state = 'canceled'
449
- expect(shipment).to receive(:determine_state).and_return('ready')
450
- expect(shipment).to receive(:after_resume)
451
- shipment.resume!
452
- expect(shipment.state).to eq 'ready'
453
- end
454
-
455
- it 'transitions state to pending if the order is not ready' do
456
- allow(shipment.order).to receive(:update_with_updater!)
457
-
458
- shipment.state = 'canceled'
459
- expect(shipment).to receive(:determine_state).and_return('pending')
460
- expect(shipment).to receive(:after_resume)
461
- shipment.resume!
462
- # Shipment is pending because order is already paid
463
- expect(shipment.state).to eq 'pending'
464
- end
465
-
466
- it 'unstocks them items' do
467
- inventory_unit = mock_model(Spree::InventoryUnit, quantity: 1, line_item: line_item, variant: variant)
468
- allow(shipment).to receive_message_chain(inventory_units: [inventory_unit])
469
- shipment.stock_location = mock_model(Spree::StockLocation)
470
- expect(shipment.stock_location).to receive(:unstock).with(variant, 1, shipment)
471
- shipment.after_resume
472
- end
473
- end
474
-
475
- context "#ship" do
476
- context "when the shipment is canceled" do
477
- let(:shipment_with_inventory_units) { create(:shipment, order: create(:order_with_line_items), state: 'canceled') }
478
- let(:subject) { shipment_with_inventory_units.ship! }
479
- before do
480
- allow(order).to receive(:update_with_updater!)
481
- allow(shipment_with_inventory_units).to receive_messages(require_inventory: false, update_order: true)
482
- end
483
-
484
- it 'unstocks them items' do
485
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)
486
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:send_shipped_email)
487
-
488
- expect(shipment_with_inventory_units.stock_location).to receive(:unstock)
489
- subject
490
- end
491
- end
492
-
493
- ['ready', 'canceled'].each do |state|
494
- context "from #{state}" do
495
- before do
496
- allow(order).to receive(:update_with_updater!)
497
- allow(shipment).to receive_messages(require_inventory: false, update_order: true, state: state)
498
- end
499
-
500
- it "should update shipped_at timestamp" do
501
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)
502
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:send_shipped_email)
503
-
504
- shipment.ship!
505
- expect(shipment.shipped_at).not_to be_nil
506
- # Ensure value is persisted
507
- shipment.reload
508
- expect(shipment.shipped_at).not_to be_nil
509
- end
510
-
511
- it "should send a shipment email" do
512
- mail_message = double 'Mail::Message'
513
- shipment_id = nil
514
- expect(Spree::ShipmentMailer).to receive(:shipped_email) { |*args|
515
- shipment_id = args[0]
516
- mail_message
517
- }
518
- expect(mail_message).to receive :deliver_later
519
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)
520
-
521
- shipment.ship!
522
- expect(shipment_id).to eq(shipment.id)
523
- end
524
-
525
- it "finalizes adjustments" do
526
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)
527
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:send_shipped_email)
528
-
529
- shipment.adjustments.each do |adjustment|
530
- expect(adjustment).to receive(:finalize!)
531
- end
532
- shipment.ship!
533
- end
534
- end
535
- end
536
- end
537
-
538
- context "#ready" do
539
- context 'with Config.auto_capture_on_dispatch == false' do
540
- # Regression test for #2040
541
- it "cannot ready a shipment for an order if the order is unpaid" do
542
- allow(order).to receive_messages(paid?: false)
543
- assert !shipment.can_ready?
544
- end
545
- end
546
-
547
- context 'with Config.auto_capture_on_dispatch == true' do
548
- before do
549
- Spree::Config[:auto_capture_on_dispatch] = true
550
- @order = create :completed_order_with_pending_payment
551
- @shipment = @order.shipments.first
552
- @shipment.cost = @order.ship_total
553
- end
554
-
555
- it "shipments ready for an order if the order is unpaid" do
556
- expect(@shipment.ready?).to be true
557
- end
558
-
559
- it "tells the order to process payment in #after_ship" do
560
- expect(@shipment).to receive(:process_order_payments)
561
- @shipment.ship!
562
- end
563
-
564
- context "order has pending payments" do
565
- let(:payment) do
566
- payment = @order.payments.first
567
- payment.update_attribute :state, 'pending'
568
- payment
569
- end
570
-
571
- it "can fully capture an authorized payment" do
572
- payment.update_attribute(:amount, @order.total)
573
-
574
- expect(payment.amount).to eq payment.uncaptured_amount
575
- @shipment.ship!
576
- expect(payment.reload.uncaptured_amount.to_f).to eq 0
577
- end
578
-
579
- it "can partially capture an authorized payment" do
580
- payment.update_attribute(:amount, @order.total + 50)
581
-
582
- expect(payment.amount).to eq payment.uncaptured_amount
583
- @shipment.ship!
584
- expect(payment.captured_amount).to eq @order.total
585
- expect(payment.captured_amount).to eq payment.amount
586
- expect(payment.order.payments.pending.first.amount).to eq 50
587
- end
588
- end
589
- end
590
- end
591
-
592
- context "updates cost when selected shipping rate is present" do
593
- let(:shipment) { create(:shipment) }
594
-
595
- before { allow(shipment).to receive_message_chain :selected_shipping_rate, cost: 5 }
596
-
597
- it "updates shipment totals" do
598
- shipment.update_amounts
599
- expect(shipment.reload.cost).to eq(5)
600
- end
601
-
602
- it "factors in additional adjustments to adjustment total" do
603
- shipment.adjustments.create!(
604
- order: order,
605
- label: "Additional",
606
- amount: 5,
607
- included: false,
608
- state: "closed"
609
- )
610
- shipment.update_amounts
611
- expect(shipment.reload.adjustment_total).to eq(5)
612
- end
613
-
614
- it "does not factor in included adjustments to adjustment total" do
615
- shipment.adjustments.create!(
616
- order: order,
617
- label: "Included",
618
- amount: 5,
619
- included: true,
620
- state: "closed"
621
- )
622
- shipment.update_amounts
623
- expect(shipment.reload.adjustment_total).to eq(0)
624
- end
625
- end
626
-
627
- context "changes shipping rate via general update" do
628
- let(:order) do
629
- Spree::Order.create(
630
- payment_total: 100, payment_state: 'paid', total: 100, item_total: 100
631
- )
632
- end
633
-
634
- let(:shipment) { Spree::Shipment.create order_id: order.id, stock_location: create(:stock_location) }
635
-
636
- let(:shipping_rate) do
637
- Spree::ShippingRate.create shipment_id: shipment.id, cost: 10
638
- end
639
-
640
- before do
641
- shipment.update_attributes_and_order selected_shipping_rate_id: shipping_rate.id
642
- end
643
-
644
- it "updates everything around order shipment total and state" do
645
- expect(shipment.cost.to_f).to eq 10
646
- expect(shipment.state).to eq 'pending'
647
- expect(shipment.order.total.to_f).to eq 110
648
- expect(shipment.order.payment_state).to eq 'balance_due'
649
- end
650
- end
651
-
652
- context "after_save" do
653
- context "line item changes" do
654
- before do
655
- shipment.cost = shipment.cost + 10
656
- end
657
-
658
- it "triggers adjustment total recalculation" do
659
- expect(shipment).to receive(:recalculate_adjustments)
660
- shipment.save
661
- end
662
-
663
- it "does not trigger adjustment recalculation if shipment has shipped" do
664
- shipment.state = 'shipped'
665
- expect(shipment).not_to receive(:recalculate_adjustments)
666
- shipment.save
667
- end
668
- end
669
-
670
- context "line item does not change" do
671
- it "does not trigger adjustment total recalculation" do
672
- expect(shipment).not_to receive(:recalculate_adjustments)
673
- shipment.save
674
- end
675
- end
676
- end
677
-
678
- context "currency" do
679
- it "returns the order currency" do
680
- expect(shipment.currency).to eq(order.currency)
681
- end
682
- end
683
-
684
- context "nil costs" do
685
- it "sets cost to 0" do
686
- shipment = Spree::Shipment.new
687
- shipment.valid?
688
- expect(shipment.cost).to eq 0
689
- end
690
- end
691
-
692
- context "#tracking_url" do
693
- it "uses shipping method to determine url" do
694
- expect(shipping_method).to receive(:build_tracking_url).with('1Z12345').and_return(:some_url)
695
- shipment.tracking = '1Z12345'
696
-
697
- expect(shipment.tracking_url).to eq(:some_url)
698
- end
699
- end
700
-
701
- context "set up new inventory units" do
702
- # let(:line_item) { double(
703
- let(:variant) { double("Variant", id: 9) }
704
-
705
- let(:inventory_units) { double }
706
-
707
- let(:params) do
708
- { variant_id: variant.id, state: 'on_hand', order_id: order.id, line_item_id: line_item.id }
709
- end
710
-
711
- before { allow(shipment).to receive_messages inventory_units: inventory_units }
712
-
713
- it "associates variant and order" do
714
- expect(inventory_units).to receive(:create).with(params)
715
- unit = shipment.set_up_inventory('on_hand', variant, order, line_item)
716
- end
717
- end
718
-
719
- # Regression test for #3349
720
- context "#destroy" do
721
- it "destroys linked shipping_rates" do
722
- reflection = Spree::Shipment.reflect_on_association(:shipping_rates)
723
- expect(reflection.options[:dependent]).to be(:delete_all)
724
- end
725
- end
726
-
727
- # Regression test for #4072 (kinda)
728
- # The need for this was discovered in the research for #4702
729
- context "state changes" do
730
- before do
731
- # Must be stubbed so transition can succeed
732
- allow(order).to receive_messages paid?: true
733
- end
734
-
735
- it "are logged to the database" do
736
- expect(shipment.state_changes).to be_empty
737
- expect(shipment.ready!).to be true
738
- expect(shipment.state_changes.count).to eq(1)
739
- state_change = shipment.state_changes.first
740
- expect(state_change.previous_state).to eq('pending')
741
- expect(state_change.next_state).to eq('ready')
742
- end
743
- end
744
- end