spree_api 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/app/controllers/spree/api/base_controller.rb +10 -1
  4. data/app/controllers/spree/api/countries_controller.rb +2 -0
  5. data/app/controllers/spree/api/orders_controller.rb +9 -2
  6. data/app/controllers/spree/api/payments_controller.rb +13 -2
  7. data/app/controllers/spree/api/states_controller.rb +2 -0
  8. data/app/controllers/spree/api/stock_items_controller.rb +2 -1
  9. data/app/controllers/spree/api/stock_locations_controller.rb +1 -0
  10. data/app/controllers/spree/api/stock_movements_controller.rb +1 -0
  11. data/app/helpers/spree/api/api_helpers.rb +2 -3
  12. data/app/models/spree/option_value_decorator.rb +4 -0
  13. data/app/models/spree/order_decorator.rb +25 -18
  14. data/app/views/spree/api/payments/credit_over_limit.v1.rabl +1 -1
  15. data/app/views/spree/api/payments/new.v1.rabl +0 -1
  16. data/app/views/spree/api/payments/update_forbidden.v1.rabl +2 -0
  17. data/config/locales/en.yml +3 -1
  18. data/config/routes.rb +1 -1
  19. data/lib/spree/api/responders/rabl_template.rb +1 -1
  20. data/spec/controllers/spree/api/base_controller_spec.rb +1 -1
  21. data/spec/controllers/spree/api/checkouts_controller_spec.rb +14 -0
  22. data/spec/controllers/spree/api/orders_controller_spec.rb +84 -17
  23. data/spec/controllers/spree/api/payments_controller_spec.rb +118 -77
  24. data/spec/controllers/spree/api/stock_items_controller_spec.rb +62 -26
  25. data/spec/controllers/spree/api/stock_locations_controller_spec.rb +56 -29
  26. data/spec/controllers/spree/api/stock_movements_controller_spec.rb +51 -27
  27. data/spec/controllers/spree/api/zones_controller_spec.rb +2 -2
  28. data/spec/models/spree/order_spec.rb +71 -4
  29. metadata +5 -4
@@ -13,41 +13,65 @@ module Spree
13
13
  stub_authentication!
14
14
  end
15
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
16
+ context 'as a user' do
17
+ it 'cannot see a list of stock movements' do
18
+ api_get :index, stock_location_id: stock_location.to_param
19
+ response.status.should == 404
20
+ end
27
21
 
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
22
+ it 'cannot see a stock movement' do
23
+ api_get :show, stock_location_id: stock_location.to_param, id: stock_movement.id
24
+ response.status.should == 404
25
+ end
35
26
 
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
27
+ it 'cannot create a stock movement' do
28
+ params = {
29
+ stock_location_id: stock_location.to_param,
30
+ stock_movement: {
31
+ stock_item_id: stock_item.to_param
32
+ }
33
+ }
41
34
 
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
35
+ api_post :create, params
36
+ response.status.should == 404
37
+ end
46
38
  end
47
39
 
48
40
  context 'as an admin' do
49
41
  sign_in_as_admin!
50
42
 
43
+ it 'gets list of stock movements' do
44
+ api_get :index, stock_location_id: stock_location.to_param
45
+ json_response['stock_movements'].first.should have_attributes(attributes)
46
+ json_response['stock_movements'].first['stock_item']['count_on_hand'].should eq 11
47
+ end
48
+
49
+ it 'requires a stock_location_id to be passed as a parameter' do
50
+ api_get :index
51
+ json_response['error'].should =~ /stock_location_id parameter must be provided/
52
+ response.status.should == 422
53
+ end
54
+
55
+ it 'can control the page size through a parameter' do
56
+ create(:stock_movement, stock_item: stock_item)
57
+ api_get :index, stock_location_id: stock_location.to_param, per_page: 1
58
+ json_response['count'].should == 1
59
+ json_response['current_page'].should == 1
60
+ json_response['pages'].should == 2
61
+ end
62
+
63
+ it 'can query the results through a paramter' do
64
+ expected_result = create(:stock_movement, :received, quantity: 10, stock_item: stock_item)
65
+ api_get :index, stock_location_id: stock_location.to_param, q: { quantity_eq: '10' }
66
+ json_response['count'].should == 1
67
+ end
68
+
69
+ it 'gets a stock movement' do
70
+ api_get :show, stock_location_id: stock_location.to_param, id: stock_movement.to_param
71
+ json_response.should have_attributes(attributes)
72
+ json_response['stock_item_id'].should eq stock_movement.stock_item_id
73
+ end
74
+
51
75
  it 'can create a new stock movement' do
52
76
  params = {
53
77
  stock_location_id: stock_location.to_param,
@@ -48,13 +48,13 @@ module Spree
48
48
  end
49
49
 
50
50
  it "uses the specified template" do
51
- request.headers.env['X-Spree-Template'] = 'show'
51
+ request.headers['X-Spree-Template'] = 'show'
52
52
  api_get :custom_show, :id => @zone.id
53
53
  response.should render_template('spree/api/zones/show')
54
54
  end
55
55
 
56
56
  it "falls back to the default template if the specified template does not exist" do
57
- request.headers.env['X-Spree-Template'] = 'invoice'
57
+ request.headers['X-Spree-Template'] = 'invoice'
58
58
  api_get :show, :id => @zone.id
59
59
  response.should render_template('spree/api/zones/show')
60
60
  end
@@ -2,22 +2,25 @@ 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!(:stock_location) { FactoryGirl.create(:stock_location) }
5
+ let!(:country) { create(:country) }
6
+ let!(:state) { country.states.first || create(:state, :country => country) }
7
+ let!(:stock_location) { create(:stock_location) }
8
8
 
9
9
  let(:user) { stub_model(LegacyUser, :email => 'fox@mudler.com') }
10
10
  let(:shipping_method) { create(:shipping_method) }
11
11
  let(:payment_method) { create(:payment_method) }
12
+
12
13
  let(:product) { product = Spree::Product.create(:name => 'Test',
13
14
  :sku => 'TEST-1',
14
15
  :price => 33.22)
15
- product.shipping_category = FactoryGirl.create(:shipping_category)
16
+ product.shipping_category = create(:shipping_category)
16
17
  product.save
17
18
  product }
19
+
18
20
  let(:variant) { variant = product.master
19
21
  variant.stock_items.each { |si| si.update_attribute(:count_on_hand, 10) }
20
22
  variant }
23
+
21
24
  let(:sku) { variant.sku }
22
25
  let(:variant_id) { variant.id }
23
26
 
@@ -49,6 +52,12 @@ module Spree
49
52
  order.state.should eq 'complete'
50
53
  end
51
54
 
55
+ it "assigns order[email] over user email to order" do
56
+ params = { email: 'wooowww@test.com' }
57
+ order = Order.build_from_api(user, params)
58
+ expect(order.email).to eq params[:email]
59
+ end
60
+
52
61
  it 'can build an order from API with just line items' do
53
62
  params = { :line_items_attributes => line_items }
54
63
 
@@ -164,6 +173,13 @@ module Spree
164
173
  end
165
174
  end
166
175
 
176
+ it "raises with proper message when cant find country" do
177
+ address = { :country => { "name" => "NoNoCountry" } }
178
+ expect {
179
+ Order.ensure_country_id_from_api(address)
180
+ }.to raise_error /NoNoCountry/
181
+ end
182
+
167
183
  it 'ensures_state_id for state fields' do
168
184
  [:name, :abbr].each do |field|
169
185
  address = { :state => { field => state.send(field) }}
@@ -172,12 +188,20 @@ module Spree
172
188
  end
173
189
  end
174
190
 
191
+ it "raises with proper message when cant find state" do
192
+ address = { :state => { "name" => "NoNoState" } }
193
+ expect {
194
+ Order.ensure_state_id_from_api(address)
195
+ }.to raise_error /NoNoState/
196
+ end
197
+
175
198
  context "shippments" do
176
199
  let(:params) do
177
200
  { :shipments_attributes => [
178
201
  { :tracking => '123456789',
179
202
  :cost => '4.99',
180
203
  :shipping_method => shipping_method.name,
204
+ :stock_location => stock_location.name,
181
205
  :inventory_units => [{ :sku => sku }]
182
206
  }
183
207
  ] }
@@ -195,6 +219,14 @@ module Spree
195
219
  shipment.inventory_units.first.variant_id.should eq product.master.id
196
220
  shipment.tracking.should eq '123456789'
197
221
  shipment.shipping_rates.first.cost.should eq 4.99
222
+ shipment.stock_location.should eq stock_location
223
+ end
224
+
225
+ it "raises if cant find stock location" do
226
+ params[:shipments_attributes][0][:stock_location] = "doesnt exist"
227
+ expect {
228
+ order = Order.build_from_api(user, params)
229
+ }.to raise_error
198
230
  end
199
231
  end
200
232
 
@@ -244,5 +276,40 @@ module Spree
244
276
  order = Order.build_from_api(user, params)
245
277
  }.to raise_error /XXX/
246
278
  end
279
+
280
+ context "raises error" do
281
+ it "clears out order from db" do
282
+ params = { :payments_attributes => [{ payment_method: "XXX" }] }
283
+ count = Order.count
284
+
285
+ expect { order = Order.build_from_api(user, params) }.to raise_error
286
+ expect(Order.count).to eq count
287
+ end
288
+ end
289
+
290
+ context "import param and tax adjustments" do
291
+ let!(:tax_rate) { create(:tax_rate, amount: 0.05, calculator: Calculator::DefaultTax.create) }
292
+ let(:other_variant) { create(:variant) }
293
+
294
+ let(:line_item_attributes) do
295
+ line_items.merge({ "1" => { :variant_id => other_variant.id, :quantity => 5 }})
296
+ end
297
+
298
+ before { Zone.stub default_tax: tax_rate.zone }
299
+
300
+ it "doesnt create any tax ajustments when importing order" do
301
+ params = { import: true, line_items_attributes: line_item_attributes }
302
+ expect {
303
+ Order.build_from_api(user, params)
304
+ }.not_to change { Adjustment.count }
305
+ end
306
+
307
+ it "does create tax adjustments if not importing order" do
308
+ params = { import: false, line_items_attributes: line_item_attributes }
309
+ expect {
310
+ Order.build_from_api(user, params)
311
+ }.to change { Adjustment.count }
312
+ end
313
+ end
247
314
  end
248
315
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bigg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-17 00:00:00.000000000 Z
11
+ date: 2013-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree_core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.1
19
+ version: 2.1.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 2.1.1
26
+ version: 2.1.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rabl
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -134,6 +134,7 @@ files:
134
134
  - app/views/spree/api/payments/index.v1.rabl
135
135
  - app/views/spree/api/payments/new.v1.rabl
136
136
  - app/views/spree/api/payments/show.v1.rabl
137
+ - app/views/spree/api/payments/update_forbidden.v1.rabl
137
138
  - app/views/spree/api/product_properties/index.v1.rabl
138
139
  - app/views/spree/api/product_properties/new.v1.rabl
139
140
  - app/views/spree/api/product_properties/show.v1.rabl