spree_api 1.3.5 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +14 -6
  2. data/Rakefile +1 -1
  3. data/app/controllers/spree/api/addresses_controller.rb +0 -1
  4. data/app/controllers/spree/api/base_controller.rb +21 -9
  5. data/app/controllers/spree/api/checkouts_controller.rb +40 -20
  6. data/app/controllers/spree/api/countries_controller.rb +0 -1
  7. data/app/controllers/spree/api/inventory_units_controller.rb +1 -1
  8. data/app/controllers/spree/api/option_types_controller.rb +46 -0
  9. data/app/controllers/spree/api/option_values_controller.rb +56 -0
  10. data/app/controllers/spree/api/orders_controller.rb +1 -21
  11. data/app/controllers/spree/api/product_properties_controller.rb +0 -1
  12. data/app/controllers/spree/api/products_controller.rb +12 -27
  13. data/app/controllers/spree/api/properties_controller.rb +61 -0
  14. data/app/controllers/spree/api/shipments_controller.rb +51 -1
  15. data/app/controllers/spree/api/states_controller.rb +32 -0
  16. data/app/controllers/spree/api/stock_items_controller.rb +70 -0
  17. data/app/controllers/spree/api/stock_locations_controller.rb +45 -0
  18. data/app/controllers/spree/api/stock_movements_controller.rb +55 -0
  19. data/app/controllers/spree/api/taxonomies_controller.rb +5 -0
  20. data/app/controllers/spree/api/taxons_controller.rb +16 -2
  21. data/app/controllers/spree/api/variants_controller.rb +1 -1
  22. data/app/controllers/spree/api/zones_controller.rb +0 -1
  23. data/app/helpers/spree/api/api_helpers.rb +34 -7
  24. data/app/models/spree/api_configuration.rb +1 -2
  25. data/app/models/spree/line_item_decorator.rb +1 -1
  26. data/app/models/spree/order_decorator.rb +6 -169
  27. data/app/overrides/api_admin_user_edit_form.rb +7 -6
  28. data/app/views/spree/admin/users/_api_fields.html.erb +7 -8
  29. data/app/views/spree/api/addresses/show.v1.rabl +2 -2
  30. data/app/views/spree/api/adjustments/show.v1.rabl +2 -0
  31. data/app/views/spree/api/credit_cards/show.v1.rabl +2 -0
  32. data/app/views/spree/api/option_types/index.v1.rabl +3 -0
  33. data/app/views/spree/api/option_types/show.v1.rabl +5 -0
  34. data/app/views/spree/api/option_values/index.v1.rabl +3 -0
  35. data/app/views/spree/api/option_values/show.v1.rabl +2 -0
  36. data/app/views/spree/api/orders/could_not_apply_coupon.v1.rabl +2 -0
  37. data/app/views/spree/api/orders/delivery.v1.rabl +2 -2
  38. data/app/views/spree/api/orders/payment.v1.rabl +3 -0
  39. data/app/views/spree/api/orders/show.v1.rabl +8 -0
  40. data/app/views/spree/api/properties/index.v1.rabl +7 -0
  41. data/app/views/spree/api/properties/new.v1.rabl +2 -0
  42. data/app/views/spree/api/properties/show.v1.rabl +2 -0
  43. data/app/views/spree/api/shared/stock_location_required.v1.rabl +2 -0
  44. data/app/views/spree/api/shipments/show.v1.rabl +7 -0
  45. data/app/views/spree/api/states/index.v1.rabl +14 -0
  46. data/app/views/spree/api/states/show.v1.rabl +2 -0
  47. data/app/views/spree/api/stock_items/index.v1.rabl +7 -0
  48. data/app/views/spree/api/stock_items/show.v1.rabl +5 -0
  49. data/app/views/spree/api/stock_locations/index.v1.rabl +7 -0
  50. data/app/views/spree/api/stock_locations/show.v1.rabl +8 -0
  51. data/app/views/spree/api/stock_movements/index.v1.rabl +7 -0
  52. data/app/views/spree/api/stock_movements/show.v1.rabl +5 -0
  53. data/app/views/spree/api/taxonomies/jstree.rabl +8 -0
  54. data/app/views/spree/api/taxons/jstree.rabl +8 -0
  55. data/app/views/spree/api/variants/index.v1.rabl +14 -1
  56. data/app/views/spree/api/variants/variant.v1.rabl +0 -4
  57. data/config/locales/en.yml +1 -0
  58. data/config/routes.rb +29 -3
  59. data/lib/spree/api.rb +0 -2
  60. data/lib/spree/api/controller_setup.rb +6 -1
  61. data/lib/spree/api/testing_support/helpers.rb +1 -1
  62. data/lib/spree/api/testing_support/setup.rb +13 -0
  63. data/spec/controllers/spree/api/base_controller_spec.rb +14 -1
  64. data/spec/controllers/spree/api/checkouts_controller_spec.rb +59 -28
  65. data/spec/controllers/spree/api/line_items_controller_spec.rb +1 -5
  66. data/spec/controllers/spree/api/option_types_controller_spec.rb +116 -0
  67. data/spec/controllers/spree/api/option_values_controller_spec.rb +128 -0
  68. data/spec/controllers/spree/api/orders_controller_spec.rb +11 -93
  69. data/spec/controllers/spree/api/product_properties_controller_spec.rb +2 -2
  70. data/spec/controllers/spree/api/products_controller_spec.rb +18 -76
  71. data/spec/controllers/spree/api/properties_controller_spec.rb +89 -0
  72. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +1 -9
  73. data/spec/controllers/spree/api/shipments_controller_spec.rb +80 -5
  74. data/spec/controllers/spree/api/states_controller_spec.rb +76 -0
  75. data/spec/controllers/spree/api/stock_items_controller_spec.rb +88 -0
  76. data/spec/controllers/spree/api/stock_locations_controller_spec.rb +86 -0
  77. data/spec/controllers/spree/api/stock_movements_controller_spec.rb +72 -0
  78. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +7 -0
  79. data/spec/controllers/spree/api/taxons_controller_spec.rb +29 -2
  80. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +1 -1
  81. data/spec/controllers/spree/api/variants_controller_spec.rb +5 -16
  82. data/spec/fixtures/thinking-cat.jpg +0 -0
  83. data/spec/models/spree/order_spec.rb +7 -328
  84. data/spec/spec_helper.rb +18 -2
  85. data/spree_api.gemspec +2 -1
  86. metadata +67 -16
  87. data/.rspec +0 -1
  88. data/app/controllers/spree/base_controller_decorator.rb +0 -15
  89. data/app/overrides/api_key_spree_layout.rb +0 -7
  90. data/app/views/spree/api/_key.html.erb +0 -4
  91. data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +0 -7
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::StockItemsController do
5
+ render_views
6
+
7
+ let!(:stock_location) { create(:stock_location_with_items) }
8
+ let!(:stock_item) { stock_location.stock_items.order(:id).first }
9
+ let!(:attributes) { [:id, :count_on_hand, :backorderable,
10
+ :stock_location_id, :variant_id] }
11
+
12
+ before do
13
+ stub_authentication!
14
+ end
15
+
16
+ it 'gets list of stock items' do
17
+ api_get :index, stock_location_id: stock_location.to_param
18
+ json_response['stock_items'].first.should have_attributes(attributes)
19
+ json_response['stock_items'].first['variant']['sku'].should eq 'ABC'
20
+ end
21
+
22
+ it 'requires a stock_location_id to be passed as a parameter' do
23
+ api_get :index
24
+ json_response['error'].should =~ /stock_location_id parameter must be provided/
25
+ response.status.should == 422
26
+ end
27
+
28
+ it 'can control the page size through a parameter' do
29
+ api_get :index, stock_location_id: stock_location.to_param, per_page: 1
30
+ json_response['count'].should == 1
31
+ json_response['current_page'].should == 1
32
+ end
33
+
34
+ it 'can query the results through a paramter' do
35
+ stock_item.update_column(:count_on_hand, 30)
36
+ api_get :index, stock_location_id: stock_location.to_param, q: { count_on_hand_eq: '30' }
37
+ json_response['count'].should == 1
38
+ json_response['stock_items'].first['count_on_hand'].should eq 30
39
+ end
40
+
41
+ it 'gets a stock item' do
42
+ api_get :show, stock_location_id: stock_location.to_param, id: stock_item.to_param
43
+ json_response.should have_attributes(attributes)
44
+ json_response['count_on_hand'].should eq stock_item.count_on_hand
45
+ end
46
+
47
+ context 'as an admin' do
48
+ sign_in_as_admin!
49
+
50
+ it 'can create a new stock item' do
51
+ variant = create(:variant)
52
+ variant.stock_items.delete_all
53
+ params = {
54
+ stock_location_id: stock_location.to_param,
55
+ stock_item: {
56
+ variant_id: variant.id,
57
+ count_on_hand: '20'
58
+ }
59
+ }
60
+
61
+ api_post :create, params
62
+ response.status.should == 201
63
+ json_response.should have_attributes(attributes)
64
+ end
65
+
66
+ it 'can update a stock item to add new inventory' do
67
+ stock_item.count_on_hand.should == 10
68
+ params = {
69
+ id: stock_item.to_param,
70
+ stock_item: {
71
+ count_on_hand: 40,
72
+ }
73
+ }
74
+
75
+ api_put :update, params
76
+ response.status.should == 200
77
+ json_response['count_on_hand'].should eq 50
78
+ end
79
+
80
+ it 'can delete a stock item' do
81
+ api_delete :destroy, id: stock_item.to_param
82
+ response.status.should == 204
83
+ lambda { stock_item.reload }.should raise_error(ActiveRecord::RecordNotFound)
84
+ end
85
+ end
86
+ end
87
+ end
88
+
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::StockLocationsController do
5
+ render_views
6
+
7
+ let!(:stock_location) { create(:stock_location) }
8
+ let!(:attributes) { [:id, :name, :address1, :address2, :city, :state_id, :state_name, :country_id, :zipcode, :phone, :active] }
9
+
10
+ before do
11
+ stub_authentication!
12
+ end
13
+
14
+ it "gets list of stock locations" do
15
+ api_get :index
16
+ json_response['stock_locations'].first.should have_attributes(attributes)
17
+ end
18
+
19
+ it "includes the country" do
20
+ api_get :index
21
+ json_response['stock_locations'].first['country'].should_not be_nil
22
+ end
23
+
24
+ it "includes the state" do
25
+ api_get :index
26
+ json_response['stock_locations'].first['state'].should_not be_nil
27
+ end
28
+
29
+ it 'can control the page size through a parameter' do
30
+ create(:stock_location)
31
+ api_get :index, per_page: 1
32
+ json_response['count'].should == 1
33
+ json_response['current_page'].should == 1
34
+ json_response['pages'].should == 2
35
+ end
36
+
37
+ it 'can query the results through a paramter' do
38
+ expected_result = create(:stock_location, name: 'South America')
39
+ api_get :index, q: { name_cont: 'south' }
40
+ json_response['count'].should == 1
41
+ json_response['stock_locations'].first['name'].should eq expected_result.name
42
+ end
43
+
44
+ it "gets a stock location" do
45
+ api_get :show, id: stock_location.to_param
46
+ json_response.should have_attributes(attributes)
47
+ json_response['name'].should eq stock_location.name
48
+ end
49
+
50
+ context "as an admin" do
51
+ sign_in_as_admin!
52
+
53
+ it "can create a new stock location" do
54
+ params = {
55
+ stock_location: {
56
+ name: "North Pole",
57
+ active: true
58
+ }
59
+ }
60
+
61
+ api_post :create, params
62
+ response.status.should == 201
63
+ json_response.should have_attributes(attributes)
64
+ end
65
+
66
+ it "can update a stock location" do
67
+ params = {
68
+ id: stock_location.to_param,
69
+ stock_location: {
70
+ name: "South Pole"
71
+ }
72
+ }
73
+
74
+ api_put :update, params
75
+ response.status.should == 200
76
+ json_response['name'].should eq 'South Pole'
77
+ end
78
+
79
+ it "can delete a stock location" do
80
+ api_delete :destroy, id: stock_location.to_param
81
+ response.status.should == 204
82
+ lambda { stock_location.reload }.should raise_error(ActiveRecord::RecordNotFound)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::StockMovementsController do
5
+ render_views
6
+
7
+ let!(:stock_location) { create(:stock_location_with_items) }
8
+ let!(:stock_item) { stock_location.stock_items.order(:id).first }
9
+ let!(:stock_movement) { create(:stock_movement, stock_item: stock_item) }
10
+ let!(:attributes) { [:id, :quantity, :stock_item_id] }
11
+
12
+ before do
13
+ stub_authentication!
14
+ end
15
+
16
+ it 'gets list of stock movements' do
17
+ api_get :index, stock_location_id: stock_location.to_param
18
+ json_response['stock_movements'].first.should have_attributes(attributes)
19
+ json_response['stock_movements'].first['stock_item']['count_on_hand'].should eq 11
20
+ end
21
+
22
+ it 'requires a stock_location_id to be passed as a parameter' do
23
+ api_get :index
24
+ json_response['error'].should =~ /stock_location_id parameter must be provided/
25
+ response.status.should == 422
26
+ end
27
+
28
+ it 'can control the page size through a parameter' do
29
+ create(:stock_movement, stock_item: stock_item)
30
+ api_get :index, stock_location_id: stock_location.to_param, per_page: 1
31
+ json_response['count'].should == 1
32
+ json_response['current_page'].should == 1
33
+ json_response['pages'].should == 2
34
+ end
35
+
36
+ it 'can query the results through a paramter' do
37
+ expected_result = create(:stock_movement, :received, quantity: 10, stock_item: stock_item)
38
+ api_get :index, stock_location_id: stock_location.to_param, q: { quantity_eq: '10' }
39
+ json_response['count'].should == 1
40
+ end
41
+
42
+ it 'gets a stock movement' do
43
+ api_get :show, stock_location_id: stock_location.to_param, id: stock_movement.to_param
44
+ json_response.should have_attributes(attributes)
45
+ json_response['stock_item_id'].should eq stock_movement.stock_item_id
46
+ end
47
+
48
+ context 'as an admin' do
49
+ sign_in_as_admin!
50
+
51
+ it 'can create a new stock movement' do
52
+ params = {
53
+ stock_location_id: stock_location.to_param,
54
+ stock_movement: {
55
+ stock_item_id: stock_item.to_param
56
+ }
57
+ }
58
+
59
+ api_post :create, params
60
+ response.status.should == 201
61
+ json_response.should have_attributes(attributes)
62
+ end
63
+
64
+ it 'can delete a stock movement' do
65
+ api_delete :destroy, id: stock_movement.to_param
66
+ response.status.should == 204
67
+ lambda { stock_movement.reload }.should raise_error(ActiveRecord::RecordNotFound)
68
+ end
69
+ end
70
+ end
71
+ end
72
+
@@ -59,6 +59,13 @@ module Spree
59
59
  children.first.key?('taxons').should be_true
60
60
  end
61
61
 
62
+ it "gets the jstree-friendly version of a taxonomy" do
63
+ api_get :jstree, :id => taxonomy.id
64
+ json_response["data"].should eq(taxonomy.root.name)
65
+ json_response["attr"].should eq({ "id" => taxonomy.root.id, "name" => taxonomy.root.name})
66
+ json_response["state"].should eq("closed")
67
+ end
68
+
62
69
  it "can learn how to create a new taxonomy" do
63
70
  api_get :new
64
71
  json_response["attributes"].should == attributes.map(&:to_s)
@@ -7,7 +7,7 @@ module Spree
7
7
  let(:taxonomy) { create(:taxonomy) }
8
8
  let(:taxon) { create(:taxon, :name => "Ruby", :taxonomy => taxonomy) }
9
9
  let(:taxon2) { create(:taxon, :name => "Rails", :taxonomy => taxonomy) }
10
- let(:attributes) { ["id", "name", "permalink", "position", "parent_id", "taxonomy_id"] }
10
+ let(:attributes) { ["id", "name", "pretty_name", "permalink", "position", "parent_id", "taxonomy_id"] }
11
11
 
12
12
  before do
13
13
  stub_authentication!
@@ -17,7 +17,7 @@ module Spree
17
17
  end
18
18
 
19
19
  context "as a normal user" do
20
- it "gets all taxons" do
20
+ it "gets all taxons for a taxonomy" do
21
21
  api_get :index, :taxonomy_id => taxonomy.id
22
22
 
23
23
  json_response.first['name'].should eq taxon.name
@@ -27,6 +27,23 @@ module Spree
27
27
  children.first['taxons'].count.should eq 1
28
28
  end
29
29
 
30
+ it "gets all taxons" do
31
+ api_get :index
32
+
33
+ json_response.first['name'].should eq taxonomy.root.name
34
+ children = json_response.first['taxons']
35
+ children.count.should eq 1
36
+ children.first['name'].should eq taxon.name
37
+ children.first['taxons'].count.should eq 1
38
+ end
39
+
40
+ it "can search for a single taxon" do
41
+ api_get :index, :q => { :name_cont => "Ruby" }
42
+
43
+ json_response.count.should == 1
44
+ json_response.first['name'].should eq "Ruby"
45
+ end
46
+
30
47
  it "gets a single taxon" do
31
48
  api_get :show, :id => taxon.id, :taxonomy_id => taxonomy.id
32
49
 
@@ -34,6 +51,16 @@ module Spree
34
51
  json_response['taxons'].count.should eq 1
35
52
  end
36
53
 
54
+ it "gets all taxons in JSTree form" do
55
+ api_get :jstree, :taxonomy_id => taxonomy.id, :id => taxon.id
56
+ response = json_response.first
57
+ response["data"].should eq(taxon2.name)
58
+ response["attr"].should eq({ "name" => taxon2.name, "id" => taxon2.id})
59
+ response["state"].should eq("closed")
60
+ end
61
+
62
+
63
+
37
64
  it "can learn how to create a new taxon" do
38
65
  api_get :new, :taxonomy_id => taxonomy.id
39
66
  json_response["attributes"].should == attributes.map(&:to_s)
@@ -6,7 +6,7 @@ module Spree
6
6
  render_views
7
7
 
8
8
  let!(:product) { create(:product) }
9
- let(:attributes) { [:id, :name, :description, :price, :available_on, :permalink, :count_on_hand, :meta_description, :meta_keywords, :taxon_ids] }
9
+ let(:attributes) { [:id, :name, :description, :price, :available_on, :permalink, :meta_description, :meta_keywords, :taxon_ids] }
10
10
 
11
11
  context "without authentication" do
12
12
  before { Spree::Api::Config[:requires_authentication] = false }
@@ -11,10 +11,9 @@ module Spree
11
11
  variant.option_values << create(:option_value)
12
12
  variant
13
13
  end
14
- let!(:attributes) { [:id, :name, :count_on_hand,
15
- :sku, :price, :weight, :height,
14
+ let!(:attributes) { [:id, :name, :sku, :price, :weight, :height,
16
15
  :width, :depth, :is_master, :cost_price,
17
- :permalink, :product_id, :lock_version] }
16
+ :permalink] }
18
17
 
19
18
  before do
20
19
  stub_authentication!
@@ -78,9 +77,11 @@ module Spree
78
77
  end
79
78
 
80
79
  context "pagination" do
80
+ default_per_page(1)
81
+
81
82
  it "can select the next page of variants" do
82
83
  second_variant = create(:variant)
83
- api_get :index, :page => 2, :per_page => 1
84
+ api_get :index, :page => 2
84
85
  json_response["variants"].first.should have_attributes(attributes)
85
86
  json_response["total_count"].should == 3
86
87
  json_response["current_page"].should == 2
@@ -168,18 +169,6 @@ module Spree
168
169
  response.status.should == 204
169
170
  lambda { variant.reload }.should raise_error(ActiveRecord::RecordNotFound)
170
171
  end
171
-
172
- context "without product" do
173
- sign_in_as_admin!
174
- let(:resource_scoping) { { :product_id => variant.product.to_param } }
175
-
176
- it "will not raise an ActiveRecord::ReadOnlyRecord exception" do
177
- api_put :update, :id => variant.to_param, :variant => { :sku => "12345", :on_hand => 5 }
178
- response.status.should_not eql 422
179
- json_response["exception"].should_not eql "ActiveRecord::ReadOnlyRecord"
180
- end
181
- end
182
-
183
172
  end
184
173
 
185
174
 
Binary file
@@ -2,338 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  module Spree
4
4
  describe Order do
5
- let!(:country) { FactoryGirl.create(:country) }
6
- let!(:state) { country.states.first || FactoryGirl.create(:state, :country => country) }
7
- let(:user) { create(:user) }
8
- let(:product) { Spree::Product.create!(:name => 'Test', :sku => 'TEST-1', :price => 33.22) }
9
- let(:sku) { product.master.sku }
10
- let(:variant) { product.master }
11
- let(:variant_id) { product.master.id }
12
- let(:line_items) {{ "0" => { :variant_id => variant.id, :quantity => 5 }}}
13
- let(:shipping_method) { create(:shipping_method) }
14
- let(:payment_method) { create(:payment_method) }
15
- let(:ship_address) {{
16
- :address1 => '123 Testable Way',
17
- :firstname => 'Fox',
18
- :lastname => 'Mulder',
19
- :city => 'Washington',
20
- :country_id => country.id,
21
- :state_id => state.id,
22
- :zipcode => '666',
23
- :phone => '666-666-6666'
24
- }}
5
+ let(:user) { stub_model(LegacyUser) }
6
+ let(:product) { create :product }
25
7
 
26
- it 'can import an order number' do
27
- params = { number: '123-456-789' }
28
- order = Order.build_from_api(user, params)
29
- order.number.should eq '123-456-789'
30
- end
31
-
32
- it 'optionally add completed at' do
33
- params = {:email => 'test@test.com',
34
- :completed_at => Time.now,
35
- :line_items_attributes => line_items }
36
-
37
- order = Order.build_from_api(user, params)
38
- order.should be_completed
39
- order.state.should eq 'complete'
40
- end
41
-
42
- it "assigns order[email] over user email to order" do
43
- params = { email: 'wooowww@test.com' }
44
- order = Order.build_from_api(user, params)
45
- expect(order.email).to eq params[:email]
46
- end
47
-
48
- context "build order with line items" do
49
- let(:attributes) do
50
- { :variant_id => variant.id, :quantity => 5, :price => 33.77 }
51
- end
52
-
53
- it 'can build an order from API with just line items' do
54
- params = { :line_items_attributes => { "0" => attributes } }
55
- Order.should_receive(:ensure_variant_id_from_api)
56
- order = Order.build_from_api(user, params)
57
-
58
- order.user.should == nil
59
- line_item = order.line_items.first
60
-
61
- expect(line_item.quantity).to eq attributes[:quantity]
62
- expect(line_item.variant_id).to eq attributes[:variant_id]
63
- expect(line_item.price).to eq attributes[:price]
64
- end
65
-
66
- it "ensures quantity values are converted to integer" do
67
- params = {
68
- :line_items_attributes => {
69
- "0" => { :variant_id => variant.id, :quantity => "5" },
70
- "1" => { :variant_id => variant.id, :quantity => "5" }
71
- }
72
- }
73
-
74
- order = Order.build_from_api(user, params)
75
- expect(order.line_items.first.quantity).to eq 10
76
- end
77
- end
78
-
79
- it 'uses line item price if present' do
80
- line_items['0'][:price] = 12.00
81
- params = { :line_items_attributes => line_items }
82
-
83
- order = Order.build_from_api(user, params)
8
+ it 'can build an order from API parameters' do
9
+ variant_id = product.master.id
10
+ order = Order.build_from_api(user, { :line_items_attributes => { "0" => { :variant_id => variant_id, :quantity => 5 }}})
84
11
 
12
+ order.user.should == nil
85
13
  line_item = order.line_items.first
86
- line_item.price.to_f.should == 12.00
87
- end
88
-
89
- it 'handles line_item building exceptions' do
90
- line_items['0'][:variant_id] = 'XXX'
91
- params = { :line_items_attributes => line_items }
92
-
93
- expect {
94
- order = Order.build_from_api(user, params)
95
- }.to raise_error /XXX/
96
- end
97
-
98
- it 'can build an order from API with variant sku' do
99
- params = { :line_items_attributes => {
100
- "0" => { :sku => sku, :quantity => 5 } }}
101
-
102
- order = Order.build_from_api(user, params)
103
-
104
- line_item = order.line_items.first
105
- line_item.variant_id.should == variant_id
106
14
  line_item.quantity.should == 5
107
- end
108
-
109
- it 'handles exceptions when sku is not found' do
110
- params = { :line_items_attributes => {
111
- "0" => { :sku => 'XXX', :quantity => 5 } }}
112
- expect {
113
- order = Order.build_from_api(user, params)
114
- }.to raise_error /XXX/
115
- end
116
-
117
- it 'can build an order from API shipping address' do
118
- params = { :ship_address_attributes => ship_address,
119
- :line_items_attributes => line_items }
120
-
121
- order = Order.build_from_api(user, params)
122
- order.ship_address.address1.should eq '123 Testable Way'
123
- end
124
-
125
- it 'can build an order from API with country attributes' do
126
- ship_address.delete(:country_id)
127
- ship_address[:country] = { 'iso' => 'US' }
128
- params = { :ship_address_attributes => ship_address,
129
- :line_items_attributes => line_items }
130
-
131
- order = Order.build_from_api(user, params)
132
- order.ship_address.country.iso.should eq 'US'
133
- end
134
-
135
- it 'handles country lookup exceptions' do
136
- ship_address.delete(:country_id)
137
- ship_address[:country] = { 'iso' => 'XXX' }
138
- params = { :ship_address_attributes => ship_address,
139
- :line_items_attributes => line_items }
140
-
141
- expect {
142
- order = Order.build_from_api(user, params)
143
- }.to raise_error /XXX/
144
- end
145
-
146
- it 'can build an order from API with state attributes' do
147
- ship_address.delete(:state_id)
148
- ship_address[:state] = { 'name' => state.name }
149
- params = { :ship_address_attributes => ship_address,
150
- :line_items_attributes => line_items }
151
-
152
- order = Order.build_from_api(user, params)
153
- order.ship_address.state.name.should eq 'Alabama'
154
- end
155
-
156
- context "state passed is not associated with country" do
157
- let(:params) do
158
- params = { :ship_address_attributes => ship_address,
159
- :line_items_attributes => line_items }
160
- end
161
-
162
- let(:other_state) { create(:state, name: "Uhuhuh", country: create(:country)) }
163
-
164
- before do
165
- country.states.destroy_all
166
-
167
- ship_address.delete(:state_id)
168
- ship_address[:state] = { 'name' => other_state.name }
169
- end
170
-
171
- it 'sets states name instead of state id' do
172
- order = Order.build_from_api(user, params)
173
- expect(order.ship_address.state_name).to eq other_state.name
174
- end
175
- end
176
-
177
- it 'sets state name if state record not found' do
178
- country.states.destroy_all
179
-
180
- ship_address.delete(:state_id)
181
- ship_address[:state] = { 'name' => 'XXX' }
182
- params = { :ship_address_attributes => ship_address,
183
- :line_items_attributes => line_items }
184
-
185
- order = Order.build_from_api(user, params)
186
- expect(order.ship_address.state_name).to eq 'XXX'
187
- end
188
-
189
- context 'variant not deleted' do
190
- it 'ensures variant id from api' do
191
- hash = { sku: variant.sku }
192
- Order.ensure_variant_id_from_api(hash)
193
- expect(hash[:variant_id]).to eq variant.id
194
- end
195
- end
196
-
197
- context 'variant was deleted' do
198
- it 'raise error as variant shouldnt be found' do
199
- variant.product.delete
200
- hash = { sku: variant.sku }
201
- expect {
202
- Order.ensure_variant_id_from_api(hash)
203
- }.to raise_error
204
- end
205
- end
206
-
207
- it 'ensures_country_id for country fields' do
208
- [:name, :iso, :iso_name, :iso3].each do |field|
209
- address = { :country => { field => country.send(field) }}
210
- Order.ensure_country_id_from_api(address)
211
- address[:country_id].should eq country.id
212
- end
213
- end
214
-
215
- it "raises with proper message when cant find country" do
216
- address = { :country => { "name" => "NoNoCountry" } }
217
- expect {
218
- Order.ensure_country_id_from_api(address)
219
- }.to raise_error /NoNoCountry/
220
- end
221
-
222
- it 'ensures_state_id for state fields' do
223
- [:name, :abbr].each do |field|
224
- address = { country_id: country.id, :state => { field => state.send(field) }}
225
- Order.ensure_state_id_from_api(address)
226
- address[:state_id].should eq state.id
227
- end
228
- end
229
-
230
- context "shippments" do
231
- let(:params) do
232
- { :shipments_attributes => [
233
- { :tracking => '123456789',
234
- :cost => '4.99',
235
- :shipping_method => shipping_method.name,
236
- :inventory_units => [{ :sku => sku }]
237
- }
238
- ] }
239
- end
240
-
241
- it 'ensures variant exists and is not deleted' do
242
- Order.should_receive(:ensure_variant_id_from_api)
243
- order = Order.build_from_api(user, params)
244
- end
245
-
246
- it 'builds them properly' do
247
- order = Order.build_from_api(user, params)
248
-
249
- shipment = order.shipments.first
250
- shipment.inventory_units.first.variant_id.should eq product.master.id
251
- shipment.tracking.should eq '123456789'
252
- shipment.adjustment.amount.should eq 4.99
253
- shipment.adjustment.should be_locked
254
- end
255
- end
256
-
257
- it 'handles shipment building exceptions' do
258
- params = { :shipments_attributes => [{ :tracking => '123456789',
259
- :cost => '4.99',
260
- :shipping_method => 'XXX',
261
- :inventory_units => [{ :sku => sku }]
262
- }] }
263
- expect {
264
- order = Order.build_from_api(user, params)
265
- }.to raise_error /XXX/
266
- end
267
-
268
- it 'adds adjustments' do
269
- params = { :adjustments_attributes => [
270
- { "label" => "Shipping Discount", "amount" => "-4.99" },
271
- { "label" => "Promotion Discount", "amount" => "-3.00" }] }
272
-
273
- order = Order.build_from_api(user, params)
274
- order.adjustments.all?(&:locked).should be_true
275
- order.adjustments.first.label.should eq 'Shipping Discount'
276
- order.adjustments.first.amount.should eq -4.99
277
- end
278
-
279
- it 'handles adjustment building exceptions' do
280
- params = { :adjustments_attributes => [
281
- { "amount" => "XXX" },
282
- { "label" => "Promotion Discount", "amount" => "-3.00" }] }
283
-
284
- expect {
285
- order = Order.build_from_api(user, params)
286
- }.to raise_error /XXX/
287
- end
288
-
289
- it 'builds a payment' do
290
- params = { :payments_attributes => [{ :amount => '4.99',
291
- :payment_method => payment_method.name }] }
292
- order = Order.build_from_api(user, params)
293
- order.payments.first.amount.should eq 4.99
294
- end
295
-
296
- it 'handles payment building exceptions' do
297
- params = { :payments_attributes => [{ :amount => '4.99',
298
- :payment_method => 'XXX' }] }
299
- expect {
300
- order = Order.build_from_api(user, params)
301
- }.to raise_error /XXX/
302
- end
303
-
304
- context "raises error" do
305
- it "clears out order from db" do
306
- params = { :payments_attributes => [{ payment_method: "XXX" }] }
307
- count = Order.count
308
-
309
- expect { order = Order.build_from_api(user, params) }.to raise_error
310
- expect(Order.count).to eq count
311
- end
312
- end
313
-
314
- context "import param and tax adjustments" do
315
- let!(:tax_rate) { create(:tax_rate, amount: 0.05, calculator: Calculator::DefaultTax.create) }
316
- let(:other_variant) { create(:variant) }
317
-
318
- let(:line_item_attributes) do
319
- line_items.merge({ "1" => { :variant_id => other_variant.id, :quantity => 5 }})
320
- end
321
-
322
- before { Zone.stub default_tax: tax_rate.zone }
323
-
324
- it "doesnt create any tax ajustments when importing order" do
325
- params = { import: true, line_items_attributes: line_item_attributes }
326
- expect {
327
- Order.build_from_api(user, params)
328
- }.not_to change { Adjustment.count }
329
- end
330
-
331
- it "does create tax adjustments if not importing order" do
332
- params = { import: false, line_items_attributes: line_item_attributes }
333
- expect {
334
- Order.build_from_api(user, params)
335
- }.to change { Adjustment.count }
336
- end
15
+ line_item.variant_id.should == variant_id
337
16
  end
338
17
  end
339
18
  end