solidus_core 1.0.2 → 1.0.3

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

Potentially problematic release.


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

Files changed (216) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -0
  3. data/Gemfile +3 -0
  4. data/Rakefile +16 -0
  5. data/script/rails +9 -0
  6. data/solidus_core.gemspec +48 -0
  7. data/spec/fixtures/thinking-cat.jpg +0 -0
  8. data/spec/helpers/base_helper_spec.rb +173 -0
  9. data/spec/helpers/order_helper_spec.rb +12 -0
  10. data/spec/helpers/products_helper_spec.rb +220 -0
  11. data/spec/helpers/taxons_helper_spec.rb +17 -0
  12. data/spec/lib/calculated_adjustments_spec.rb +7 -0
  13. data/spec/lib/i18n_spec.rb +123 -0
  14. data/spec/lib/search/base_spec.rb +86 -0
  15. data/spec/lib/search/variant_spec.rb +92 -0
  16. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +66 -0
  17. data/spec/lib/spree/core/controller_helpers/order_spec.rb +92 -0
  18. data/spec/lib/spree/core/controller_helpers/search_spec.rb +17 -0
  19. data/spec/lib/spree/core/controller_helpers/store_spec.rb +16 -0
  20. data/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb +39 -0
  21. data/spec/lib/spree/core/current_store_spec.rb +36 -0
  22. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +22 -0
  23. data/spec/lib/spree/core/importer/order_spec.rb +431 -0
  24. data/spec/lib/spree/core/role_configuration_spec.rb +138 -0
  25. data/spec/lib/spree/core/validators/email_spec.rb +48 -0
  26. data/spec/lib/spree/localized_number_spec.rb +38 -0
  27. data/spec/lib/spree/migrations_spec.rb +36 -0
  28. data/spec/lib/spree/money_spec.rb +127 -0
  29. data/spec/lib/tasks/exchanges_spec.rb +231 -0
  30. data/spec/lib/tasks/migrations/copy_shipped_shipments_to_cartons_spec.rb +115 -0
  31. data/spec/lib/tasks/order_capturing_spec.rb +56 -0
  32. data/spec/mailers/carton_mailer_spec.rb +43 -0
  33. data/spec/mailers/order_mailer_spec.rb +122 -0
  34. data/spec/mailers/reimbursement_mailer_spec.rb +40 -0
  35. data/spec/mailers/test_mailer_spec.rb +15 -0
  36. data/spec/models/spree/ability_spec.rb +276 -0
  37. data/spec/models/spree/address_spec.rb +250 -0
  38. data/spec/models/spree/adjustment_reason_spec.rb +13 -0
  39. data/spec/models/spree/adjustment_spec.rb +177 -0
  40. data/spec/models/spree/app_configuration_spec.rb +20 -0
  41. data/spec/models/spree/asset_spec.rb +24 -0
  42. data/spec/models/spree/calculator/default_tax_spec.rb +127 -0
  43. data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +25 -0
  44. data/spec/models/spree/calculator/flat_rate_spec.rb +47 -0
  45. data/spec/models/spree/calculator/flexi_rate_spec.rb +41 -0
  46. data/spec/models/spree/calculator/percent_on_line_item_spec.rb +15 -0
  47. data/spec/models/spree/calculator/price_sack_spec.rb +30 -0
  48. data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +51 -0
  49. data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +23 -0
  50. data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +13 -0
  51. data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +52 -0
  52. data/spec/models/spree/calculator/shipping/per_item_spec.rb +20 -0
  53. data/spec/models/spree/calculator/shipping/price_sack_spec.rb +30 -0
  54. data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +36 -0
  55. data/spec/models/spree/calculator/tiered_percent_spec.rb +47 -0
  56. data/spec/models/spree/calculator_spec.rb +36 -0
  57. data/spec/models/spree/carton_spec.rb +133 -0
  58. data/spec/models/spree/classification_spec.rb +15 -0
  59. data/spec/models/spree/concerns/display_money_spec.rb +43 -0
  60. data/spec/models/spree/concerns/user_methods_spec.rb +41 -0
  61. data/spec/models/spree/credit_card_spec.rb +334 -0
  62. data/spec/models/spree/customer_return_spec.rb +276 -0
  63. data/spec/models/spree/exchange_spec.rb +79 -0
  64. data/spec/models/spree/gateway/bogus_simple.rb +20 -0
  65. data/spec/models/spree/gateway/bogus_spec.rb +13 -0
  66. data/spec/models/spree/gateway_spec.rb +82 -0
  67. data/spec/models/spree/inventory_unit_spec.rb +307 -0
  68. data/spec/models/spree/item_adjustments_spec.rb +256 -0
  69. data/spec/models/spree/line_item_spec.rb +191 -0
  70. data/spec/models/spree/option_type_spec.rb +14 -0
  71. data/spec/models/spree/option_value_spec.rb +22 -0
  72. data/spec/models/spree/order/address_spec.rb +50 -0
  73. data/spec/models/spree/order/adjustments_spec.rb +39 -0
  74. data/spec/models/spree/order/callbacks_spec.rb +42 -0
  75. data/spec/models/spree/order/checkout_spec.rb +902 -0
  76. data/spec/models/spree/order/currency_updater_spec.rb +32 -0
  77. data/spec/models/spree/order/finalizing_spec.rb +111 -0
  78. data/spec/models/spree/order/payment_spec.rb +210 -0
  79. data/spec/models/spree/order/risk_assessment_spec.rb +68 -0
  80. data/spec/models/spree/order/state_machine_spec.rb +221 -0
  81. data/spec/models/spree/order/tax_spec.rb +84 -0
  82. data/spec/models/spree/order/totals_spec.rb +24 -0
  83. data/spec/models/spree/order/updating_spec.rb +18 -0
  84. data/spec/models/spree/order/validations_spec.rb +15 -0
  85. data/spec/models/spree/order_cancellations_spec.rb +120 -0
  86. data/spec/models/spree/order_capturing_spec.rb +116 -0
  87. data/spec/models/spree/order_contents_spec.rb +265 -0
  88. data/spec/models/spree/order_inventory_spec.rb +228 -0
  89. data/spec/models/spree/order_mutex_spec.rb +85 -0
  90. data/spec/models/spree/order_promotion_spec.rb +31 -0
  91. data/spec/models/spree/order_shipping_spec.rb +247 -0
  92. data/spec/models/spree/order_spec.rb +1412 -0
  93. data/spec/models/spree/order_stock_location_spec.rb +18 -0
  94. data/spec/models/spree/order_updater_spec.rb +299 -0
  95. data/spec/models/spree/payment_method/store_credit_spec.rb +294 -0
  96. data/spec/models/spree/payment_method_spec.rb +96 -0
  97. data/spec/models/spree/payment_spec.rb +1044 -0
  98. data/spec/models/spree/permission_sets/base_spec.rb +12 -0
  99. data/spec/models/spree/permission_sets/configuration_display.rb +82 -0
  100. data/spec/models/spree/permission_sets/configuration_management_spec.rb +50 -0
  101. data/spec/models/spree/permission_sets/dashboard_display_spec.rb +22 -0
  102. data/spec/models/spree/permission_sets/order_display_spec.rb +49 -0
  103. data/spec/models/spree/permission_sets/order_management_spec.rb +36 -0
  104. data/spec/models/spree/permission_sets/product_display_spec.rb +60 -0
  105. data/spec/models/spree/permission_sets/product_management_spec.rb +40 -0
  106. data/spec/models/spree/permission_sets/promotion_display_spec.rb +34 -0
  107. data/spec/models/spree/permission_sets/promotion_management_spec.rb +26 -0
  108. data/spec/models/spree/permission_sets/report_display_spec.rb +24 -0
  109. data/spec/models/spree/permission_sets/restricted_transfer_management_spec.rb +132 -0
  110. data/spec/models/spree/permission_sets/stock_display_spec.rb +26 -0
  111. data/spec/models/spree/permission_sets/stock_management_spec.rb +24 -0
  112. data/spec/models/spree/permission_sets/user_display_spec.rb +36 -0
  113. data/spec/models/spree/permission_sets/user_management_spec.rb +28 -0
  114. data/spec/models/spree/preference_spec.rb +80 -0
  115. data/spec/models/spree/preferences/configuration_spec.rb +30 -0
  116. data/spec/models/spree/preferences/preferable_spec.rb +294 -0
  117. data/spec/models/spree/preferences/scoped_store_spec.rb +58 -0
  118. data/spec/models/spree/preferences/static_model_preferences_spec.rb +78 -0
  119. data/spec/models/spree/preferences/statically_configurable_spec.rb +60 -0
  120. data/spec/models/spree/preferences/store_spec.rb +39 -0
  121. data/spec/models/spree/price_spec.rb +42 -0
  122. data/spec/models/spree/product/scopes_spec.rb +148 -0
  123. data/spec/models/spree/product_duplicator_spec.rb +103 -0
  124. data/spec/models/spree/product_filter_spec.rb +26 -0
  125. data/spec/models/spree/product_property_spec.rb +20 -0
  126. data/spec/models/spree/product_spec.rb +437 -0
  127. data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +96 -0
  128. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +165 -0
  129. data/spec/models/spree/promotion/actions/create_quantity_adjustments_spec.rb +115 -0
  130. data/spec/models/spree/promotion/actions/free_shipping_spec.rb +40 -0
  131. data/spec/models/spree/promotion/rules/first_order_spec.rb +75 -0
  132. data/spec/models/spree/promotion/rules/item_total_spec.rb +67 -0
  133. data/spec/models/spree/promotion/rules/nth_order_spec.rb +70 -0
  134. data/spec/models/spree/promotion/rules/one_use_per_user_spec.rb +42 -0
  135. data/spec/models/spree/promotion/rules/option_value_spec.rb +94 -0
  136. data/spec/models/spree/promotion/rules/product_spec.rb +143 -0
  137. data/spec/models/spree/promotion/rules/taxon_spec.rb +102 -0
  138. data/spec/models/spree/promotion/rules/user_logged_in_spec.rb +27 -0
  139. data/spec/models/spree/promotion/rules/user_spec.rb +37 -0
  140. data/spec/models/spree/promotion_builder_spec.rb +118 -0
  141. data/spec/models/spree/promotion_category_spec.rb +17 -0
  142. data/spec/models/spree/promotion_code/code_builder_spec.rb +79 -0
  143. data/spec/models/spree/promotion_code_spec.rb +187 -0
  144. data/spec/models/spree/promotion_handler/cart_spec.rb +114 -0
  145. data/spec/models/spree/promotion_handler/coupon_spec.rb +335 -0
  146. data/spec/models/spree/promotion_handler/free_shipping_spec.rb +47 -0
  147. data/spec/models/spree/promotion_handler/page_spec.rb +44 -0
  148. data/spec/models/spree/promotion_rule_spec.rb +28 -0
  149. data/spec/models/spree/promotion_spec.rb +767 -0
  150. data/spec/models/spree/refund_spec.rb +204 -0
  151. data/spec/models/spree/reimbursement/credit_spec.rb +36 -0
  152. data/spec/models/spree/reimbursement/reimbursement_type_engine_spec.rb +140 -0
  153. data/spec/models/spree/reimbursement/reimbursement_type_validator_spec.rb +83 -0
  154. data/spec/models/spree/reimbursement_performer_spec.rb +30 -0
  155. data/spec/models/spree/reimbursement_spec.rb +231 -0
  156. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +51 -0
  157. data/spec/models/spree/reimbursement_type/credit_spec.rb +53 -0
  158. data/spec/models/spree/reimbursement_type/exchange_spec.rb +46 -0
  159. data/spec/models/spree/reimbursement_type/original_payment_spec.rb +107 -0
  160. data/spec/models/spree/reimbursement_type/store_credit_spec.rb +97 -0
  161. data/spec/models/spree/return_authorization_spec.rb +290 -0
  162. data/spec/models/spree/return_item/eligibility_validator/default_spec.rb +77 -0
  163. data/spec/models/spree/return_item/eligibility_validator/inventory_shipped_spec.rb +58 -0
  164. data/spec/models/spree/return_item/eligibility_validator/no_reimbursements_spec.rb +85 -0
  165. data/spec/models/spree/return_item/eligibility_validator/order_completed_spec.rb +32 -0
  166. data/spec/models/spree/return_item/eligibility_validator/rma_required_spec.rb +29 -0
  167. data/spec/models/spree/return_item/eligibility_validator/time_since_purchase_spec.rb +35 -0
  168. data/spec/models/spree/return_item/exchange_variant_eligibility/same_option_value_spec.rb +65 -0
  169. data/spec/models/spree/return_item/exchange_variant_eligibility/same_product_spec.rb +43 -0
  170. data/spec/models/spree/return_item_spec.rb +775 -0
  171. data/spec/models/spree/returns_calculator_spec.rb +14 -0
  172. data/spec/models/spree/shipment_spec.rb +709 -0
  173. data/spec/models/spree/shipping_calculator_spec.rb +45 -0
  174. data/spec/models/spree/shipping_method_spec.rb +88 -0
  175. data/spec/models/spree/shipping_rate_spec.rb +142 -0
  176. data/spec/models/spree/state_spec.rb +14 -0
  177. data/spec/models/spree/stock/availability_validator_spec.rb +83 -0
  178. data/spec/models/spree/stock/coordinator_spec.rb +116 -0
  179. data/spec/models/spree/stock/differentiator_spec.rb +39 -0
  180. data/spec/models/spree/stock/estimator_spec.rb +146 -0
  181. data/spec/models/spree/stock/inventory_unit_builder_spec.rb +38 -0
  182. data/spec/models/spree/stock/package_spec.rb +163 -0
  183. data/spec/models/spree/stock/packer_spec.rb +91 -0
  184. data/spec/models/spree/stock/prioritizer_spec.rb +125 -0
  185. data/spec/models/spree/stock/quantifier_spec.rb +115 -0
  186. data/spec/models/spree/stock/splitter/backordered_spec.rb +29 -0
  187. data/spec/models/spree/stock/splitter/base_spec.rb +21 -0
  188. data/spec/models/spree/stock/splitter/shipping_category_spec.rb +50 -0
  189. data/spec/models/spree/stock/splitter/weight_spec.rb +29 -0
  190. data/spec/models/spree/stock_item_spec.rb +426 -0
  191. data/spec/models/spree/stock_location_spec.rb +279 -0
  192. data/spec/models/spree/stock_movement_spec.rb +56 -0
  193. data/spec/models/spree/stock_transfer_spec.rb +290 -0
  194. data/spec/models/spree/store_credit_category_spec.rb +17 -0
  195. data/spec/models/spree/store_credit_event_spec.rb +314 -0
  196. data/spec/models/spree/store_credit_spec.rb +876 -0
  197. data/spec/models/spree/store_spec.rb +55 -0
  198. data/spec/models/spree/tax_category_spec.rb +27 -0
  199. data/spec/models/spree/tax_rate_spec.rb +378 -0
  200. data/spec/models/spree/taxon_spec.rb +74 -0
  201. data/spec/models/spree/taxonomy_spec.rb +18 -0
  202. data/spec/models/spree/tracker_spec.rb +21 -0
  203. data/spec/models/spree/transfer_item_spec.rb +264 -0
  204. data/spec/models/spree/unit_cancel_spec.rb +148 -0
  205. data/spec/models/spree/user_spec.rb +223 -0
  206. data/spec/models/spree/validations/db_maximum_length_validator_spec.rb +23 -0
  207. data/spec/models/spree/variant/scopes_spec.rb +55 -0
  208. data/spec/models/spree/variant_spec.rb +546 -0
  209. data/spec/models/spree/zone_spec.rb +305 -0
  210. data/spec/spec_helper.rb +78 -0
  211. data/spec/support/big_decimal.rb +5 -0
  212. data/spec/support/concerns/default_price.rb +34 -0
  213. data/spec/support/dummy_ability.rb +4 -0
  214. data/spec/support/test_gateway.rb +2 -0
  215. metadata +229 -3
  216. data/lib/spree/testing_support/rspec-activemodel-mocks_patch.rb +0 -8
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ class FakesController < ApplicationController
4
+ include Spree::Core::ControllerHelpers::Search
5
+ end
6
+
7
+ describe Spree::Core::ControllerHelpers::Search, type: :controller do
8
+ controller(FakesController) {}
9
+
10
+ describe '#build_searcher' do
11
+ it 'returns Spree::Core::Search::Base instance' do
12
+ allow(controller).to receive_messages(try_spree_current_user: create(:user),
13
+ current_currency: 'USD')
14
+ expect(controller.build_searcher({}).class).to eq Spree::Core::Search::Base
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ class FakesController < ApplicationController
4
+ include Spree::Core::ControllerHelpers::Store
5
+ end
6
+
7
+ describe Spree::Core::ControllerHelpers::Store, type: :controller do
8
+ controller(FakesController) {}
9
+
10
+ describe '#current_store' do
11
+ let!(:store) { create :store, default: true }
12
+ it 'returns current store' do
13
+ expect(controller.current_store).to eq store
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ class FakesController < ApplicationController
4
+ include Spree::Core::ControllerHelpers::StrongParameters
5
+ end
6
+
7
+ describe Spree::Core::ControllerHelpers::StrongParameters, type: :controller do
8
+ controller(FakesController) {}
9
+
10
+ describe '#permitted_attributes' do
11
+ it 'returns Spree::PermittedAttributes module' do
12
+ expect(controller.permitted_attributes).to eq Spree::PermittedAttributes
13
+ end
14
+ end
15
+
16
+ describe '#permitted_payment_attributes' do
17
+ it 'returns Array class' do
18
+ expect(controller.permitted_payment_attributes.class).to eq Array
19
+ end
20
+ end
21
+
22
+ describe '#permitted_checkout_attributes' do
23
+ it 'returns Array class' do
24
+ expect(controller.permitted_checkout_attributes.class).to eq Array
25
+ end
26
+ end
27
+
28
+ describe '#permitted_order_attributes' do
29
+ it 'returns Array class' do
30
+ expect(controller.permitted_order_attributes.class).to eq Array
31
+ end
32
+ end
33
+
34
+ describe '#permitted_product_attributes' do
35
+ it 'returns Array class' do
36
+ expect(controller.permitted_product_attributes.class).to eq Array
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Core::CurrentStore do
4
+ describe "#store" do
5
+ subject { Spree::Core::CurrentStore.new(request).store }
6
+
7
+ context "with a default" do
8
+ let(:request) { double(headers: {}, env: {}) }
9
+ let!(:store_1) { create :store, default: true }
10
+
11
+ it "returns the default store" do
12
+ expect(subject).to eq(store_1)
13
+ end
14
+
15
+ context "with a domain match" do
16
+ let(:request) { double(headers: {}, env: { "SERVER_NAME" => url } ) }
17
+ let(:url) { "server-name.org" }
18
+ let!(:store_2) { create :store, default: false, url: url }
19
+
20
+ it "returns the store with the matching domain" do
21
+ expect(subject).to eq(store_2)
22
+ end
23
+
24
+ context "with headers" do
25
+ let(:request) { double(headers: { "HTTP_SPREE_STORE" => headers_code }, env: {}) }
26
+ let(:headers_code) { "HEADERS" }
27
+ let!(:store_3) { create :store, code: headers_code, default: false }
28
+
29
+ it "returns the store with the matching code" do
30
+ expect(subject).to eq(store_3)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ # This is a bit of a insane spec I have to admit
4
+ # Chosed the spree_payment_methods table because it has a `name` column
5
+ # already. Stubs wouldn't work here (the delegation runs before this spec is
6
+ # loaded) and adding a column here might make the test even crazy so here we go
7
+ module Spree
8
+ class DelegateBelongsToStubModel < Spree::Base
9
+ self.table_name = "spree_payment_methods"
10
+ belongs_to :product
11
+ delegate_belongs_to :product, :name
12
+ end
13
+
14
+ describe DelegateBelongsToStubModel do
15
+ context "model has column attr delegated to associated object" do
16
+ it "doesnt touch the associated object" do
17
+ expect(subject).not_to receive(:product)
18
+ subject.name
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,431 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ module Core
5
+ describe Importer::Order do
6
+
7
+ let!(:country) { create(:country) }
8
+ let!(:state) { country.states.first || create(:state, :country => country) }
9
+ let!(:stock_location) { create(:stock_location, admin_name: 'Admin Name') }
10
+
11
+ let(:user) { stub_model(LegacyUser, :email => 'fox@mudler.com') }
12
+ let(:shipping_method) { create(:shipping_method) }
13
+ let(:payment_method) { create(:check_payment_method) }
14
+
15
+ let(:product) { product = Spree::Product.create(:name => 'Test',
16
+ :sku => 'TEST-1',
17
+ :price => 33.22)
18
+ product.shipping_category = create(:shipping_category)
19
+ product.save
20
+ product }
21
+
22
+ let(:variant) { variant = product.master
23
+ variant.stock_items.each { |si| si.update_attribute(:count_on_hand, 10) }
24
+ variant }
25
+
26
+ let(:sku) { variant.sku }
27
+ let(:variant_id) { variant.id }
28
+
29
+ let(:line_items) {{ "0" => { :variant_id => variant.id, :quantity => 5 }}}
30
+ let(:ship_address) {{
31
+ :address1 => '123 Testable Way',
32
+ :firstname => 'Fox',
33
+ :lastname => 'Mulder',
34
+ :city => 'Washington',
35
+ :country_id => country.id,
36
+ :state_id => state.id,
37
+ :zipcode => '66666',
38
+ :phone => '666-666-6666'
39
+ }}
40
+
41
+ it 'can import an order number' do
42
+ params = { number: '123-456-789' }
43
+ order = Importer::Order.import(user, params)
44
+ expect(order.number).to eq '123-456-789'
45
+ end
46
+
47
+ it 'optionally add completed at' do
48
+ params = { email: 'test@test.com',
49
+ completed_at: Time.now,
50
+ line_items_attributes: line_items }
51
+ order = Importer::Order.import(user,params)
52
+ expect(order).to be_completed
53
+ expect(order.state).to eq 'complete'
54
+ end
55
+
56
+ it "assigns order[email] over user email to order" do
57
+ params = { email: 'wooowww@test.com' }
58
+ order = Importer::Order.import(user,params)
59
+ expect(order.email).to eq params[:email]
60
+ end
61
+
62
+ context "assigning a user to an order" do
63
+ let(:other_user) { stub_model(LegacyUser, :email => 'dana@scully.com') }
64
+
65
+ context "as an admin" do
66
+ before { allow(user).to receive_messages :has_spree_role? => true }
67
+
68
+ context "a user's id is not provided" do
69
+ context "nil user id is provided" do
70
+ it "unassociates the admin user from the order" do
71
+ params = { user_id: nil }
72
+ order = Importer::Order.import(user, params)
73
+ expect(order.user_id).to be_nil
74
+ end
75
+ end
76
+
77
+ context "another user's id is provided" do
78
+ it "permits the user to be assigned" do
79
+ params = { user_id: other_user.id }
80
+ order = Importer::Order.import(user, params)
81
+ expect(order.user_id).to eq(other_user.id)
82
+ end
83
+ end
84
+ end
85
+
86
+ context "a user's id is not provided" do
87
+ it "doesn't unassociate the admin from the order" do
88
+ params = { }
89
+ order = Importer::Order.import(user, params)
90
+ expect(order.user_id).to eq(user.id)
91
+ end
92
+ end
93
+ end
94
+
95
+ context "as a user" do
96
+ before { allow(user).to receive_messages :has_spree_role? => false }
97
+ it "does not assign the order to the other user" do
98
+ params = { user_id: other_user.id }
99
+ order = Importer::Order.import(user, params)
100
+ expect(order.user_id).to eq(user.id)
101
+ end
102
+ end
103
+ end
104
+
105
+ it 'can build an order from API with just line items' do
106
+ params = { :line_items_attributes => line_items }
107
+
108
+ expect(Importer::Order).to receive(:ensure_variant_id_from_params).and_return({variant_id: variant.id, quantity: 5})
109
+ order = Importer::Order.import(user,params)
110
+ expect(order.user).to eq(nil)
111
+ line_item = order.line_items.first
112
+ expect(line_item.quantity).to eq(5)
113
+ expect(line_item.variant_id).to eq(variant_id)
114
+ end
115
+
116
+ it 'handles line_item updating exceptions' do
117
+ line_items['0'][:currency] = 'GBP'
118
+ params = { :line_items_attributes => line_items }
119
+
120
+ expect {
121
+ order = Importer::Order.import(user, params)
122
+ }.to raise_error /Validation failed/
123
+ end
124
+
125
+ it 'can build an order from API with variant sku' do
126
+ params = { :line_items_attributes => {
127
+ "0" => { :sku => sku, :quantity => 5 } }}
128
+
129
+ order = Importer::Order.import(user,params)
130
+
131
+ line_item = order.line_items.first
132
+ expect(line_item.variant_id).to eq(variant_id)
133
+ expect(line_item.quantity).to eq(5)
134
+ end
135
+
136
+ it 'can build an order from API shipping address' do
137
+ params = { :ship_address_attributes => ship_address,
138
+ :line_items_attributes => line_items }
139
+
140
+ order = Importer::Order.import(user,params)
141
+ expect(order.ship_address.address1).to eq '123 Testable Way'
142
+ end
143
+
144
+ it 'can build an order from API with country attributes' do
145
+ ship_address.delete(:country_id)
146
+ ship_address[:country] = { 'iso' => 'US' }
147
+ params = { :ship_address_attributes => ship_address,
148
+ :line_items_attributes => line_items }
149
+
150
+ order = Importer::Order.import(user,params)
151
+ expect(order.ship_address.country.iso).to eq 'US'
152
+ end
153
+
154
+ it 'can build an order from API with state attributes' do
155
+ ship_address.delete(:state_id)
156
+ ship_address[:state] = { 'name' => state.name }
157
+ params = { :ship_address_attributes => ship_address,
158
+ :line_items_attributes => line_items }
159
+
160
+ order = Importer::Order.import(user,params)
161
+ expect(order.ship_address.state.name).to eq 'Alabama'
162
+ end
163
+
164
+ context "with a different currency" do
165
+ before { variant.price_in("GBP").update_attribute(:price, 18.99) }
166
+
167
+ it "sets the order currency" do
168
+ params = {
169
+ currency: "GBP"
170
+ }
171
+ order = Importer::Order.import(user,params)
172
+ expect(order.currency).to eq "GBP"
173
+ end
174
+
175
+ it "can handle it when a line order price is specified" do
176
+ params = {
177
+ currency: "GBP",
178
+ line_items_attributes: line_items
179
+ }
180
+ line_items["0"].merge! currency: "GBP", price: 1.99
181
+ order = Importer::Order.import(user, params)
182
+ expect(order.currency).to eq "GBP"
183
+ expect(order.line_items.first.price).to eq 1.99
184
+ expect(order.line_items.first.currency).to eq "GBP"
185
+ end
186
+ end
187
+
188
+ context "state passed is not associated with country" do
189
+ let(:params) do
190
+ params = { :ship_address_attributes => ship_address,
191
+ :line_items_attributes => line_items }
192
+ end
193
+
194
+ let(:other_state) { create(:state, name: "Uhuhuh", country: create(:country)) }
195
+
196
+ before do
197
+ ship_address.delete(:state_id)
198
+ ship_address[:state] = { 'name' => other_state.name }
199
+ end
200
+
201
+ it 'sets states name instead of state id' do
202
+ order = Importer::Order.import(user,params)
203
+ expect(order.ship_address.state_name).to eq other_state.name
204
+ end
205
+ end
206
+
207
+ it 'sets state name if state record not found' do
208
+ ship_address.delete(:state_id)
209
+ ship_address[:state] = { 'name' => 'XXX' }
210
+ params = { :ship_address_attributes => ship_address,
211
+ :line_items_attributes => line_items }
212
+
213
+ order = Importer::Order.import(user,params)
214
+ expect(order.ship_address.state_name).to eq 'XXX'
215
+ end
216
+
217
+ context 'variant not deleted' do
218
+ it 'ensures variant id from api' do
219
+ hash = { sku: variant.sku }
220
+ Importer::Order.ensure_variant_id_from_params(hash)
221
+ expect(hash[:variant_id]).to eq variant.id
222
+ end
223
+ end
224
+
225
+ context 'variant was deleted' do
226
+ it 'raise error as variant shouldnt be found' do
227
+ variant.product.destroy
228
+ hash = { sku: variant.sku }
229
+ expect {
230
+ Importer::Order.ensure_variant_id_from_params(hash)
231
+ }.to raise_error ActiveRecord::RecordNotFound
232
+ end
233
+ end
234
+
235
+ it 'ensures_country_id for country fields' do
236
+ [:name, :iso, :iso_name, :iso3].each do |field|
237
+ address = { :country => { field => country.send(field) }}
238
+ Importer::Order.ensure_country_id_from_params(address)
239
+ expect(address[:country_id]).to eq country.id
240
+ end
241
+ end
242
+
243
+ it "raises with proper message when cant find country" do
244
+ address = { :country => { "name" => "NoNoCountry" } }
245
+ expect {
246
+ Importer::Order.ensure_country_id_from_params(address)
247
+ }.to raise_error ActiveRecord::RecordNotFound
248
+ end
249
+
250
+ it 'ensures_state_id for state fields' do
251
+ [:name, :abbr].each do |field|
252
+ address = { country_id: country.id, :state => { field => state.send(field) }}
253
+ Importer::Order.ensure_state_id_from_params(address)
254
+ expect(address[:state_id]).to eq state.id
255
+ end
256
+ end
257
+
258
+ context "shipments" do
259
+ let(:params) do
260
+ { :shipments_attributes => [
261
+ { :tracking => '123456789',
262
+ :cost => '14.99',
263
+ :shipping_method => shipping_method.name,
264
+ :stock_location => stock_location.name,
265
+ :inventory_units => [{ :sku => sku }]
266
+ }
267
+ ] }
268
+ end
269
+
270
+ it 'ensures variant exists and is not deleted' do
271
+ expect(Importer::Order).to receive(:ensure_variant_id_from_params).and_call_original
272
+ order = Importer::Order.import(user,params)
273
+ end
274
+
275
+ it 'builds them properly' do
276
+ order = Importer::Order.import(user, params)
277
+ shipment = order.shipments.first
278
+
279
+ expect(shipment.cost.to_f).to eq 14.99
280
+ expect(shipment.inventory_units.first.variant_id).to eq product.master.id
281
+ expect(shipment.tracking).to eq '123456789'
282
+ expect(shipment.shipping_rates.first.cost).to eq 14.99
283
+ expect(shipment.selected_shipping_rate).to eq(shipment.shipping_rates.first)
284
+ expect(shipment.stock_location).to eq stock_location
285
+ expect(order.shipment_total.to_f).to eq 14.99
286
+ end
287
+
288
+ it "accepts admin name for stock location" do
289
+ params[:shipments_attributes][0][:stock_location] = stock_location.admin_name
290
+ order = Importer::Order.import(user, params)
291
+ shipment = order.shipments.first
292
+
293
+ expect(shipment.stock_location).to eq stock_location
294
+ end
295
+
296
+ it "raises if cant find stock location" do
297
+ params[:shipments_attributes][0][:stock_location] = "doesnt exist"
298
+ expect {
299
+ order = Importer::Order.import(user,params)
300
+ }.to raise_error ActiveRecord::RecordNotFound
301
+ end
302
+
303
+ context 'when completed_at and shipped_at present' do
304
+ let(:params) do
305
+ {
306
+ :completed_at => 2.days.ago,
307
+ :shipments_attributes => [
308
+ { :tracking => '123456789',
309
+ :cost => '4.99',
310
+ :shipped_at => 1.day.ago,
311
+ :shipping_method => shipping_method.name,
312
+ :stock_location => stock_location.name,
313
+ :inventory_units => [{ :sku => sku }]
314
+ }
315
+ ]
316
+ }
317
+ end
318
+
319
+ it 'builds them properly' do
320
+ order = Importer::Order.import(user, params)
321
+ shipment = order.shipments.first
322
+
323
+ expect(shipment.cost.to_f).to eq 4.99
324
+ expect(shipment.inventory_units.first.variant_id).to eq product.master.id
325
+ expect(shipment.tracking).to eq '123456789'
326
+ expect(shipment.shipped_at).to be_present
327
+ expect(shipment.shipping_rates.first.cost).to eq 4.99
328
+ expect(shipment.selected_shipping_rate).to eq(shipment.shipping_rates.first)
329
+ expect(shipment.stock_location).to eq stock_location
330
+ expect(shipment.state).to eq('shipped')
331
+ expect(shipment.inventory_units.all?(&:shipped?)).to be true
332
+ expect(order.shipment_state).to eq('shipped')
333
+ expect(order.shipment_total.to_f).to eq 4.99
334
+ end
335
+ end
336
+ end
337
+
338
+ it 'adds adjustments' do
339
+ params = { :adjustments_attributes => [
340
+ { label: 'Shipping Discount', amount: -4.99 },
341
+ { label: 'Promotion Discount', amount: -3.00 }] }
342
+
343
+ order = Importer::Order.import(user,params)
344
+ expect(order.adjustments.all?(&:closed?)).to be true
345
+ expect(order.adjustments.first.label).to eq 'Shipping Discount'
346
+ expect(order.adjustments.first.amount).to eq -4.99
347
+ end
348
+
349
+ it "calculates final order total correctly" do
350
+ params = {
351
+ adjustments_attributes: [
352
+ { label: 'Promotion Discount', amount: -3.00 }
353
+ ],
354
+ line_items_attributes: {
355
+ "0" => {
356
+ variant_id: variant.id,
357
+ quantity: 5
358
+ }
359
+ }
360
+ }
361
+
362
+ order = Importer::Order.import(user,params)
363
+ expect(order.item_total).to eq(166.1)
364
+ expect(order.total).to eq(163.1) # = item_total (166.1) - adjustment_total (3.00)
365
+
366
+ end
367
+
368
+ it 'builds a payment using state' do
369
+ params = { :payments_attributes => [{ amount: '4.99',
370
+ payment_method: payment_method.name,
371
+ state: 'completed' }] }
372
+ order = Importer::Order.import(user,params)
373
+ expect(order.payments.first.amount).to eq 4.99
374
+ end
375
+
376
+ it 'builds a payment using status as fallback' do
377
+ params = { :payments_attributes => [{ amount: '4.99',
378
+ payment_method: payment_method.name,
379
+ status: 'completed' }] }
380
+ order = Importer::Order.import(user,params)
381
+ expect(order.payments.first.amount).to eq 4.99
382
+ end
383
+
384
+ it 'build a source payment using years and month' do
385
+ params = { :payments_attributes => [{
386
+ amount: '4.99',
387
+ payment_method: payment_method.name,
388
+ status: 'completed',
389
+ source: {
390
+ name: 'Fox',
391
+ last_digits: "7424",
392
+ cc_type: "visa",
393
+ year: '2022',
394
+ month: "5"
395
+ }
396
+ }]}
397
+
398
+ order = Importer::Order.import(user, params)
399
+ expect(order.payments.first.source.last_digits).to eq '7424'
400
+ end
401
+
402
+ it 'handles source building exceptions when do not have years and month' do
403
+ params = { :payments_attributes => [{
404
+ amount: '4.99',
405
+ payment_method: payment_method.name,
406
+ status: 'completed',
407
+ source: {
408
+ name: 'Fox',
409
+ last_digits: "7424",
410
+ cc_type: "visa"
411
+ }
412
+ }]}
413
+
414
+ expect {
415
+ order = Importer::Order.import(user, params)
416
+ }.to raise_error /Validation failed: Credit card Month is not a number, Credit card Year is not a number/
417
+ end
418
+
419
+ context "raises error" do
420
+ it "clears out order from db" do
421
+ params = { :payments_attributes => [{ payment_method: "XXX" }] }
422
+ count = Order.count
423
+
424
+ expect { order = Importer::Order.import(user,params) }.to raise_error ActiveRecord::RecordNotFound
425
+ expect(Order.count).to eq count
426
+ end
427
+ end
428
+
429
+ end
430
+ end
431
+ end