spree_api 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,6 +10,7 @@ module Spree
10
10
  before_filter :check_for_api_key, :if => :requires_authentication?
11
11
  before_filter :authenticate_user
12
12
 
13
+ rescue_from Exception, :with => :error_during_processing
13
14
  rescue_from CanCan::AccessDenied, :with => :unauthorized
14
15
  rescue_from ActiveRecord::RecordNotFound, :with => :not_found
15
16
 
@@ -63,6 +64,11 @@ module Spree
63
64
  render "spree/api/v1/errors/not_found", :status => 404 and return
64
65
  end
65
66
 
67
+ def error_during_processing(exception)
68
+ render :text => { :exception => exception.message }.to_json,
69
+ :status => 422 and return
70
+ end
71
+
66
72
  def current_ability
67
73
  Spree::Ability.new(current_api_user)
68
74
  end
@@ -0,0 +1,50 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class InventoryUnitsController < Spree::Api::V1::BaseController
5
+ before_filter :prepare_event, :only => :update
6
+
7
+ def show
8
+ @inventory_unit = inventory_unit
9
+ end
10
+
11
+ def update
12
+ authorize! :update, Order
13
+
14
+ inventory_unit.transaction do
15
+ if inventory_unit.update_attributes(params[:inventory_unit])
16
+ fire
17
+ render :show, :status => 200
18
+ else
19
+ invalid_resource!(inventory_unit)
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def inventory_unit
27
+ @inventory_unit ||= InventoryUnit.find(params[:id])
28
+ end
29
+
30
+ def prepare_event
31
+ return unless @event = params[:fire]
32
+
33
+ can_event = "can_#{@event}?"
34
+
35
+ unless inventory_unit.respond_to?(can_event) &&
36
+ inventory_unit.send(can_event)
37
+ render :text => { exception: "cannot transition to #{@event}" }.to_json,
38
+ :status => 200
39
+ false
40
+ end
41
+ end
42
+
43
+ def fire
44
+ inventory_unit.send("#{@event}!") if @event
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -7,8 +7,8 @@ module Spree
7
7
 
8
8
  def index
9
9
  @product_properties = @product.product_properties.
10
- ransack(params[:q]).result
11
- .page(params[:page]).per(params[:per_page])
10
+ ransack(params[:q]).result.
11
+ page(params[:page]).per(params[:per_page])
12
12
  end
13
13
 
14
14
  def show
@@ -8,7 +8,11 @@ module Spree
8
8
  def ready
9
9
  authorize! :read, Shipment
10
10
  unless @shipment.ready?
11
- @shipment.ready!
11
+ if @shipment.can_ready?
12
+ @shipment.ready!
13
+ else
14
+ render "spree/api/v1/shipments/cannot_ready_shipment", :status => 422 and return
15
+ end
12
16
  end
13
17
  render :show
14
18
  end
@@ -64,6 +64,11 @@ module Spree
64
64
  [:id, :name, :permalink, :position, :parent_id, :taxonomy_id]
65
65
  end
66
66
 
67
+ def inventory_unit_attributes
68
+ [:id, :lock_version, :state, :variant_id, :order_id,
69
+ :shipment_id, :return_authorization_id]
70
+ end
71
+
67
72
  def return_authorization_attributes
68
73
  [:id, :number, :state, :amount, :order_id, :reason, :created_at, :updated_at]
69
74
  end
@@ -0,0 +1,2 @@
1
+ object @inventory_unit
2
+ attributes *inventory_unit_attributes
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:cannot_ready, :scope => "spree.api.shipment") }
@@ -19,3 +19,5 @@ en:
19
19
  order:
20
20
  could_not_transition: "The order could not be transitioned. Please fix the errors and try again."
21
21
  invalid_shipping_method: "Invalid shipping method specified."
22
+ shipment:
23
+ cannot_ready: "Cannot ready shipment."
data/config/routes.rb CHANGED
@@ -53,6 +53,7 @@ Spree::Core::Engine.routes.prepend do
53
53
  resources :taxonomies do
54
54
  resources :taxons
55
55
  end
56
+ resources :inventory_units, :only => [:show, :update]
56
57
  end
57
58
  end
58
59
  end
@@ -28,6 +28,13 @@ describe Spree::Api::V1::BaseController do
28
28
  end
29
29
  end
30
30
 
31
+ it 'handles exceptions' do
32
+ subject.should_receive(:authenticate_user).and_return(true)
33
+ subject.should_receive(:index).and_raise(Exception.new("no joy"))
34
+ get :index, :token => "fake_key"
35
+ json_response.should == { "exception" => "no joy" }
36
+ end
37
+
31
38
  it "maps symantec keys to nested_attributes keys" do
32
39
  klass = stub(:nested_attributes_options => { :line_items => {},
33
40
  :bill_address => {} })
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::InventoryUnitsController do
5
+ render_views
6
+
7
+ before do
8
+ stub_authentication!
9
+ @inventory_unit = create(:inventory_unit)
10
+ end
11
+
12
+ it "gets an inventory unit" do
13
+ api_get :show, :id => @inventory_unit.id
14
+ json_response['inventory_unit']['state'].should eq @inventory_unit.state
15
+ end
16
+
17
+ it "updates an inventory unit (only shipment is accessable by default)" do
18
+ api_put :update, :id => @inventory_unit.id,
19
+ :inventory_unit => { :shipment => nil }
20
+ json_response['inventory_unit']['shipment_id'].should be_nil
21
+ end
22
+
23
+ context 'fires state event' do
24
+ it 'if supplied with :fire param' do
25
+ api_put :update, :id => @inventory_unit.id,
26
+ :fire => 'ship',
27
+ :inventory_unit => { :shipment => nil }
28
+
29
+ json_response['inventory_unit']['state'].should eq 'shipped'
30
+ end
31
+
32
+ it 'and returns exception if cannot fire' do
33
+ api_put :update, :id => @inventory_unit.id,
34
+ :fire => 'return'
35
+ json_response['exception'].should match /cannot transition to return/
36
+ end
37
+
38
+ it 'and returns exception bad state' do
39
+ api_put :update, :id => @inventory_unit.id,
40
+ :fire => 'bad'
41
+ json_response['exception'].should match /cannot transition to bad/
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -126,7 +126,7 @@ module Spree
126
126
  json_response["error"].should == "There was a problem with the payment gateway: Insufficient funds"
127
127
 
128
128
  payment.reload
129
- payment.state.should == "pending"
129
+ payment.state.should == "failed"
130
130
  end
131
131
 
132
132
  it "can purchase" do
@@ -9,8 +9,8 @@ module Spree
9
9
  order.line_items << create(:line_item)
10
10
  order.shipments << create(:shipment, :state => 'shipped')
11
11
  order.finalize!
12
- order.shipments.each(&:ready!)
13
- order.shipments.each(&:ship!)
12
+ order.shipments.update_all(:state => 'shipped')
13
+ order.inventory_units.update_all(:state => 'shipped')
14
14
  order
15
15
  end
16
16
 
@@ -62,18 +62,18 @@ module Spree
62
62
  sign_in_as_admin!
63
63
 
64
64
  it "can show return authorization" do
65
- order.return_authorizations << create(:return_authorization)
65
+ FactoryGirl.create(:return_authorization, :order => order)
66
66
  return_authorization = order.return_authorizations.first
67
- api_get :show, :order_id => order.id, :id => return_authorization.id
67
+ api_get :show, :order_id => order.number, :id => return_authorization.id
68
68
  response.status.should == 200
69
69
  json_response.should have_attributes(attributes)
70
70
  json_response["return_authorization"]["state"].should_not be_blank
71
71
  end
72
72
 
73
73
  it "can get a list of return authorizations" do
74
- order.return_authorizations << create(:return_authorization)
75
- order.return_authorizations << create(:return_authorization)
76
- api_get :index, { :order_id => order.id }
74
+ FactoryGirl.create(:return_authorization, :order => order)
75
+ FactoryGirl.create(:return_authorization, :order => order)
76
+ api_get :index, { :order_id => order.number }
77
77
  response.status.should == 200
78
78
  return_authorizations = json_response["return_authorizations"]
79
79
  return_authorizations.first.should have_attributes(attributes)
@@ -81,16 +81,16 @@ module Spree
81
81
  end
82
82
 
83
83
  it 'can control the page size through a parameter' do
84
- order.return_authorizations << create(:return_authorization)
85
- order.return_authorizations << create(:return_authorization)
86
- api_get :index, :order_id => order.id, :per_page => 1
84
+ FactoryGirl.create(:return_authorization, :order => order)
85
+ FactoryGirl.create(:return_authorization, :order => order)
86
+ api_get :index, :order_id => order.number, :per_page => 1
87
87
  json_response['count'].should == 1
88
88
  json_response['current_page'].should == 1
89
89
  json_response['pages'].should == 2
90
90
  end
91
91
 
92
92
  it 'can query the results through a paramter' do
93
- order.return_authorizations << create(:return_authorization)
93
+ FactoryGirl.create(:return_authorization, :order => order)
94
94
  expected_result = create(:return_authorization, :reason => 'damaged')
95
95
  order.return_authorizations << expected_result
96
96
  api_get :index, :q => { :reason_cont => 'damage' }
@@ -106,7 +106,7 @@ module Spree
106
106
  end
107
107
 
108
108
  it "can update a return authorization on the order" do
109
- order.return_authorizations << create(:return_authorization)
109
+ FactoryGirl.create(:return_authorization, :order => order)
110
110
  return_authorization = order.return_authorizations.first
111
111
  api_put :update, :id => return_authorization.id, :return_authorization => { :amount => 19.99 }
112
112
  response.status.should == 200
@@ -114,7 +114,7 @@ module Spree
114
114
  end
115
115
 
116
116
  it "can delete a return authorization on the order" do
117
- order.return_authorizations << create(:return_authorization)
117
+ FactoryGirl.create(:return_authorization, :order => order)
118
118
  return_authorization = order.return_authorizations.first
119
119
  api_delete :destroy, :id => return_authorization.id
120
120
  response.status.should == 204
@@ -122,7 +122,7 @@ module Spree
122
122
  end
123
123
 
124
124
  it "can add a new return authorization to an existing order" do
125
- api_post :create, :return_autorization => { :order_id => order.id, :amount => 14.22, :reason => "Defective" }
125
+ api_post :create, :return_autorization => { :order_id => order.number, :amount => 14.22, :reason => "Defective" }
126
126
  response.status.should == 201
127
127
  json_response.should have_attributes(attributes)
128
128
  json_response["return_authorization"]["state"].should_not be_blank
@@ -131,12 +131,12 @@ module Spree
131
131
 
132
132
  context "as just another user" do
133
133
  it "cannot add a return authorization to the order" do
134
- api_post :create, :return_autorization => { :order_id => order.id, :amount => 14.22, :reason => "Defective" }
134
+ api_post :create, :return_autorization => { :order_id => order.number, :amount => 14.22, :reason => "Defective" }
135
135
  assert_unauthorized!
136
136
  end
137
137
 
138
138
  it "cannot update a return authorization on the order" do
139
- order.return_authorizations << create(:return_authorization)
139
+ FactoryGirl.create(:return_authorization, :order => order)
140
140
  return_authorization = order.return_authorizations.first
141
141
  api_put :update, :id => return_authorization.id, :return_authorization => { :amount => 19.99 }
142
142
  assert_unauthorized!
@@ -144,7 +144,7 @@ module Spree
144
144
  end
145
145
 
146
146
  it "cannot delete a return authorization on the order" do
147
- order.return_authorizations << create(:return_authorization)
147
+ FactoryGirl.create(:return_authorization, :order => order)
148
148
  return_authorization = order.return_authorizations.first
149
149
  api_delete :destroy, :id => return_authorization.id
150
150
  assert_unauthorized!
@@ -6,7 +6,6 @@ describe Spree::Api::V1::ShipmentsController do
6
6
  let!(:attributes) { [:id, :tracking, :number, :cost, :shipped_at] }
7
7
 
8
8
  before do
9
- Spree::Order.any_instance.stub(:paid? => true, :complete? => true)
10
9
  stub_authentication!
11
10
  end
12
11
 
@@ -28,14 +27,22 @@ describe Spree::Api::V1::ShipmentsController do
28
27
  sign_in_as_admin!
29
28
 
30
29
  it "can make a shipment ready" do
30
+ Spree::Order.any_instance.stub(:paid? => true, :complete? => true)
31
31
  api_put :ready
32
32
  json_response.should have_attributes(attributes)
33
33
  json_response["shipment"]["state"].should == "ready"
34
34
  shipment.reload.state.should == "ready"
35
35
  end
36
36
 
37
+ it "cannot make a shipment ready if the order is unpaid" do
38
+ Spree::Order.any_instance.stub(:paid? => false)
39
+ api_put :ready
40
+ json_response["error"].should == "Cannot ready shipment."
41
+ end
42
+
37
43
  context "can transition a shipment from ready to ship" do
38
44
  before do
45
+ Spree::Order.any_instance.stub(:paid? => true, :complete? => true)
39
46
  shipment.update!(shipment.order)
40
47
  shipment.state.should == "ready"
41
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,43 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-19 00:00:00.000000000 Z
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spree_core
16
- requirement: &70185208786720 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - =
19
+ - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.2.2
21
+ version: 1.2.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70185208786720
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.2.3
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec-rails
27
- requirement: &70185208785040 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
- - - =
35
+ - - '='
31
36
  - !ruby/object:Gem::Version
32
37
  version: 2.9.0
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70185208785040
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.9.0
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: database_cleaner
38
- requirement: &70185208804960 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70185208804960
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  description: Spree's API
48
63
  email:
49
64
  - ryan@spreecommerce.com
@@ -59,6 +74,7 @@ files:
59
74
  - app/controllers/spree/api/v1/base_controller.rb
60
75
  - app/controllers/spree/api/v1/countries_controller.rb
61
76
  - app/controllers/spree/api/v1/images_controller.rb
77
+ - app/controllers/spree/api/v1/inventory_units_controller.rb
62
78
  - app/controllers/spree/api/v1/line_items_controller.rb
63
79
  - app/controllers/spree/api/v1/orders_controller.rb
64
80
  - app/controllers/spree/api/v1/payments_controller.rb
@@ -88,6 +104,7 @@ files:
88
104
  - app/views/spree/api/v1/errors/not_found.rabl
89
105
  - app/views/spree/api/v1/errors/unauthorized.rabl
90
106
  - app/views/spree/api/v1/images/show.rabl
107
+ - app/views/spree/api/v1/inventory_units/show.rabl
91
108
  - app/views/spree/api/v1/line_items/new.rabl
92
109
  - app/views/spree/api/v1/line_items/show.rabl
93
110
  - app/views/spree/api/v1/orders/address.rabl
@@ -114,6 +131,7 @@ files:
114
131
  - app/views/spree/api/v1/return_authorizations/index.rabl
115
132
  - app/views/spree/api/v1/return_authorizations/new.rabl
116
133
  - app/views/spree/api/v1/return_authorizations/show.rabl
134
+ - app/views/spree/api/v1/shipments/cannot_ready_shipment.rabl
117
135
  - app/views/spree/api/v1/shipments/show.rabl
118
136
  - app/views/spree/api/v1/taxonomies/index.rabl
119
137
  - app/views/spree/api/v1/taxonomies/nested.rabl
@@ -147,6 +165,7 @@ files:
147
165
  - spec/controllers/spree/api/v1/base_controller_spec.rb
148
166
  - spec/controllers/spree/api/v1/countries_controller_spec.rb
149
167
  - spec/controllers/spree/api/v1/images_controller_spec.rb
168
+ - spec/controllers/spree/api/v1/inventory_units_controller_spec.rb
150
169
  - spec/controllers/spree/api/v1/line_items_controller_spec.rb
151
170
  - spec/controllers/spree/api/v1/orders_controller_spec.rb
152
171
  - spec/controllers/spree/api/v1/payments_controller_spec.rb
@@ -182,7 +201,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
201
  version: '0'
183
202
  segments:
184
203
  - 0
185
- hash: 2313245839499120062
204
+ hash: 4196845361076740967
186
205
  required_rubygems_version: !ruby/object:Gem::Requirement
187
206
  none: false
188
207
  requirements:
@@ -191,10 +210,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
210
  version: '0'
192
211
  segments:
193
212
  - 0
194
- hash: 2313245839499120062
213
+ hash: 4196845361076740967
195
214
  requirements: []
196
215
  rubyforge_project:
197
- rubygems_version: 1.8.10
216
+ rubygems_version: 1.8.23
198
217
  signing_key:
199
218
  specification_version: 3
200
219
  summary: Spree's API
@@ -203,6 +222,7 @@ test_files:
203
222
  - spec/controllers/spree/api/v1/base_controller_spec.rb
204
223
  - spec/controllers/spree/api/v1/countries_controller_spec.rb
205
224
  - spec/controllers/spree/api/v1/images_controller_spec.rb
225
+ - spec/controllers/spree/api/v1/inventory_units_controller_spec.rb
206
226
  - spec/controllers/spree/api/v1/line_items_controller_spec.rb
207
227
  - spec/controllers/spree/api/v1/orders_controller_spec.rb
208
228
  - spec/controllers/spree/api/v1/payments_controller_spec.rb