spree_api 2.2.14 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +43 -1
  3. data/app/controllers/spree/api/base_controller.rb +8 -12
  4. data/app/controllers/spree/api/checkouts_controller.rb +4 -58
  5. data/app/controllers/spree/api/inventory_units_controller.rb +0 -1
  6. data/app/controllers/spree/api/line_items_controller.rb +3 -3
  7. data/app/controllers/spree/api/option_types_controller.rb +2 -2
  8. data/app/controllers/spree/api/orders_controller.rb +11 -36
  9. data/app/controllers/spree/api/payments_controller.rb +2 -2
  10. data/app/controllers/spree/api/products_controller.rb +6 -36
  11. data/app/controllers/spree/api/shipments_controller.rb +6 -25
  12. data/app/controllers/spree/api/taxonomies_controller.rb +8 -6
  13. data/app/controllers/spree/api/taxons_controller.rb +1 -1
  14. data/app/controllers/spree/api/variants_controller.rb +16 -19
  15. data/app/helpers/spree/api/api_helpers.rb +6 -1
  16. data/app/views/spree/api/errors/invalid_resource.v1.rabl +1 -1
  17. data/app/views/spree/api/orders/could_not_transition.v1.rabl +1 -1
  18. data/app/views/spree/api/orders/order.v1.rabl +1 -1
  19. data/app/views/spree/api/orders/show.v1.rabl +10 -4
  20. data/app/views/spree/api/shipments/small.v1.rabl +33 -0
  21. data/app/views/spree/api/taxonomies/show.v1.rabl +2 -2
  22. data/app/views/spree/api/users/show.v1.rabl +7 -0
  23. data/config/routes.rb +19 -28
  24. data/lib/spree/api/engine.rb +3 -3
  25. data/lib/spree/api/responders/rabl_template.rb +1 -1
  26. data/lib/spree/api/testing_support/helpers.rb +8 -3
  27. data/spec/controllers/spree/api/base_controller_spec.rb +15 -12
  28. data/spec/controllers/spree/api/checkouts_controller_spec.rb +78 -90
  29. data/spec/controllers/spree/api/line_items_controller_spec.rb +13 -9
  30. data/spec/controllers/spree/api/orders_controller_spec.rb +53 -31
  31. data/spec/controllers/spree/api/payments_controller_spec.rb +9 -8
  32. data/spec/controllers/spree/api/products_controller_spec.rb +1 -1
  33. data/spec/controllers/spree/api/promotion_application_spec.rb +5 -5
  34. data/spec/controllers/spree/api/shipments_controller_spec.rb +1 -11
  35. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +2 -2
  36. data/spec/controllers/spree/api/users_controller_spec.rb +23 -23
  37. data/spec/controllers/spree/api/zones_controller_spec.rb +22 -0
  38. data/spec/requests/rabl_cache_spec.rb +2 -2
  39. data/spec/spec_helper.rb +0 -1
  40. data/spec/support/controller_hacks.rb +1 -1
  41. data/spree_api.gemspec +2 -2
  42. metadata +12 -15
  43. data/app/views/spree/api/orders/delivery.v1.rabl +0 -3
  44. data/lib/spree/api/version.rb +0 -5
  45. data/spec/requests/ransackable_attributes_spec.rb +0 -79
@@ -24,6 +24,7 @@ module Spree
24
24
  :inventory_unit_attributes,
25
25
  :return_authorization_attributes,
26
26
  :creditcard_attributes,
27
+ :payment_source_attributes,
27
28
  :user_attributes,
28
29
  :property_attributes,
29
30
  :stock_location_attributes,
@@ -84,7 +85,7 @@ module Spree
84
85
 
85
86
  @@payment_attributes = [
86
87
  :id, :source_type, :source_id, :amount, :display_amount,
87
- :payment_method_id, :state, :avs_response, :created_at,
88
+ :payment_method_id, :response_code, :state, :avs_response, :created_at,
88
89
  :updated_at
89
90
  ]
90
91
 
@@ -130,6 +131,10 @@ module Spree
130
131
  :gateway_customer_profile_id, :gateway_payment_profile_id
131
132
  ]
132
133
 
134
+ @@payment_source_attributes = [
135
+ :id, :month, :year, :cc_type, :last_digits, :name
136
+ ]
137
+
133
138
  @@user_attributes = [:id, :email, :created_at, :updated_at]
134
139
 
135
140
  @@property_attributes = [:id, :name, :presentation]
@@ -1,3 +1,3 @@
1
1
  object false
2
2
  node(:error) { I18n.t(:invalid_resource, :scope => "spree.api") }
3
- node(:errors) { @resource.errors }
3
+ node(:errors) { @resource.errors.to_hash }
@@ -1,3 +1,3 @@
1
1
  object false
2
2
  node(:error) { I18n.t(:could_not_transition, :scope => "spree.api.order") }
3
- node(:errors) { @order.errors }
3
+ node(:errors) { @order.errors.to_hash }
@@ -5,5 +5,5 @@ node(:total_quantity) { |o| o.line_items.sum(:quantity) }
5
5
  node(:display_total) { |o| o.display_total.to_s }
6
6
  node(:display_ship_total) { |o| o.display_ship_total }
7
7
  node(:display_tax_total) { |o| o.display_tax_total }
8
- node(:token) { |o| o.token }
8
+ node(:token) { |o| o.guest_token }
9
9
  node(:checkout_steps) { |o| o.checkout_steps }
@@ -18,20 +18,26 @@ child :line_items => :line_items do
18
18
  end
19
19
 
20
20
  child :payments => :payments do
21
- attributes :id, :amount, :state, :payment_method_id
21
+ attributes *payment_attributes
22
+
22
23
  child :payment_method => :payment_method do
23
24
  attributes :id, :name, :environment
24
25
  end
26
+
27
+ child :source => :source do
28
+ attributes *payment_source_attributes
29
+ end
25
30
  end
26
31
 
27
32
  child :shipments => :shipments do
28
- extends "spree/api/shipments/show"
33
+ extends "spree/api/shipments/small"
29
34
  end
30
35
 
31
36
  child :adjustments => :adjustments do
32
37
  extends "spree/api/adjustments/show"
33
38
  end
34
39
 
35
- child :credit_cards => :credit_cards do
36
- extends "spree/api/credit_cards/show"
40
+ # Necessary for backend's order interface
41
+ node :permissions do
42
+ { can_update: current_ability.can?(:update, root_object) }
37
43
  end
@@ -0,0 +1,33 @@
1
+ object @shipment
2
+ cache [I18n.locale, 'small_shipment', root_object]
3
+
4
+ attributes *shipment_attributes
5
+ node(:order_id) { |shipment| shipment.order.number }
6
+ node(:stock_location_name) { |shipment| shipment.stock_location.name }
7
+
8
+ child :shipping_rates => :shipping_rates do
9
+ extends "spree/api/shipping_rates/show"
10
+ end
11
+
12
+ child :selected_shipping_rate => :selected_shipping_rate do
13
+ extends "spree/api/shipping_rates/show"
14
+ end
15
+
16
+ child :shipping_methods => :shipping_methods do
17
+ attributes :id, :name
18
+ child :zones => :zones do
19
+ attributes :id, :name, :description
20
+ end
21
+
22
+ child :shipping_categories => :shipping_categories do
23
+ attributes :id, :name
24
+ end
25
+ end
26
+
27
+ child :manifest => :manifest do
28
+ glue(:variant) do
29
+ attribute :id => :variant_id
30
+ end
31
+ node(:quantity) { |m| m.quantity }
32
+ node(:states) { |m| m.states }
33
+ end
@@ -1,7 +1,7 @@
1
1
  object @taxonomy
2
2
 
3
- if params[:set] == 'nested'
4
- extends "spree/api/taxonomies/nested"
3
+ if set = params[:set]
4
+ extends "spree/api/taxonomies/#{set}"
5
5
  else
6
6
  attributes *taxonomy_attributes
7
7
 
@@ -1,3 +1,10 @@
1
1
  object @user
2
2
 
3
3
  attributes *user_attributes
4
+ child(:bill_address => :bill_address) do
5
+ extends "spree/api/addresses/show"
6
+ end
7
+
8
+ child(:ship_address => :ship_address) do
9
+ extends "spree/api/addresses/show"
10
+ end
data/config/routes.rb CHANGED
@@ -8,14 +8,14 @@ Spree::Core::Engine.add_routes do
8
8
  end
9
9
  end
10
10
 
11
- namespace :api, :defaults => { :format => 'json' } do
11
+ namespace :api, defaults: { format: 'json' } do
12
12
  resources :products do
13
13
  resources :images
14
14
  resources :variants
15
15
  resources :product_properties
16
16
  end
17
17
 
18
- order_routes = lambda {
18
+ concern :order_routes do
19
19
  member do
20
20
  put :cancel
21
21
  put :empty
@@ -32,17 +32,8 @@ Spree::Core::Engine.add_routes do
32
32
  put :credit
33
33
  end
34
34
  end
35
- # TODO Remove after shipment api is no longer handled through order nesting.
36
- resources :shipments, :only => [:create, :update] do
37
- member do
38
- put :ready
39
- put :ship
40
- put :add
41
- put :remove
42
- end
43
- end
44
35
 
45
- resources :addresses, :only => [:show, :update]
36
+ resources :addresses, only: [:show, :update]
46
37
 
47
38
  resources :return_authorizations do
48
39
  member do
@@ -51,17 +42,16 @@ Spree::Core::Engine.add_routes do
51
42
  put :receive
52
43
  end
53
44
  end
54
- }
45
+ end
55
46
 
56
- resources :checkouts do
47
+ resources :checkouts, only: [:update], concerns: :order_routes do
57
48
  member do
58
49
  put :next
59
50
  put :advance
60
51
  end
61
- order_routes.call
62
52
  end
63
53
 
64
- resources :variants, :only => [:index, :show] do
54
+ resources :variants, only: [:index, :show] do
65
55
  resources :images
66
56
  end
67
57
 
@@ -69,15 +59,16 @@ Spree::Core::Engine.add_routes do
69
59
  resources :option_values
70
60
  end
71
61
 
72
- get '/orders/mine', :to => 'orders#mine', :as => 'my_orders'
62
+ get '/orders/mine', to: 'orders#mine', as: 'my_orders'
73
63
 
74
- resources :orders, &order_routes
64
+ resources :orders, concerns: :order_routes
75
65
 
76
66
  resources :zones
77
- resources :countries, :only => [:index, :show] do
78
- resources :states, :only => [:index, :show]
67
+ resources :countries, only: [:index, :show] do
68
+ resources :states, only: [:index, :show]
79
69
  end
80
- resources :shipments, :only => [:create, :update] do
70
+
71
+ resources :shipments, only: [:create, :update] do
81
72
  member do
82
73
  put :ready
83
74
  put :ship
@@ -85,7 +76,7 @@ Spree::Core::Engine.add_routes do
85
76
  put :remove
86
77
  end
87
78
  end
88
- resources :states, :only => [:index, :show]
79
+ resources :states, only: [:index, :show]
89
80
 
90
81
  resources :taxonomies do
91
82
  member do
@@ -98,9 +89,9 @@ Spree::Core::Engine.add_routes do
98
89
  end
99
90
  end
100
91
 
101
- resources :taxons, :only => [:index]
92
+ resources :taxons, only: [:index]
102
93
 
103
- resources :inventory_units, :only => [:show, :update]
94
+ resources :inventory_units, only: [:show, :update]
104
95
  resources :users
105
96
  resources :properties
106
97
  resources :stock_locations do
@@ -108,10 +99,10 @@ Spree::Core::Engine.add_routes do
108
99
  resources :stock_items
109
100
  end
110
101
 
111
- get '/config/money', :to => 'config#money'
112
- get '/config', :to => 'config#show'
102
+ get '/config/money', to: 'config#money'
103
+ get '/config', to: 'config#show'
113
104
 
114
- put '/classifications', :to => 'classifications#update', :as => :classifications
115
- get '/taxons/products', :to => 'taxons#products', :as => :taxon_products
105
+ put '/classifications', to: 'classifications#update', as: :classifications
106
+ get '/taxons/products', to: 'taxons#products', as: :taxon_products
116
107
  end
117
108
  end
@@ -10,9 +10,9 @@ module Spree
10
10
  config.include_json_root = false
11
11
  config.include_child_root = false
12
12
 
13
- # Motivation here it make it call as_json when rendering. Otherwise it
14
- # would fall to JSON and get errors like the one described here https://github.com/spree/spree/issues/4589
15
- # where Float::INFINITY would resolve to Infinite (invalid json) istead of null
13
+ # Motivation here it make it call as_json when rendering timestamps
14
+ # and therefore display miliseconds. Otherwise it would fall to
15
+ # JSON.dump which doesn't display the miliseconds
16
16
  config.json_engine = ActiveSupport::JSON
17
17
  end
18
18
 
@@ -14,7 +14,7 @@ module Spree
14
14
  end
15
15
 
16
16
  def template
17
- options[:default_template]
17
+ request.headers['X-Spree-Template'] || controller.params[:template] || options[:default_template]
18
18
  end
19
19
 
20
20
  def api_behavior(error)
@@ -3,7 +3,12 @@ module Spree
3
3
  module TestingSupport
4
4
  module Helpers
5
5
  def json_response
6
- JSON.parse(response.body)
6
+ case body = JSON.parse(response.body)
7
+ when Hash
8
+ body.with_indifferent_access
9
+ when Array
10
+ body
11
+ end
7
12
  end
8
13
 
9
14
  def assert_not_found!
@@ -17,7 +22,7 @@ module Spree
17
22
  end
18
23
 
19
24
  def stub_authentication!
20
- allow(Spree.user_class).to receive(:find_by).with(hash_including(:spree_api_key)) { current_api_user }
25
+ Spree::LegacyUser.stub(:find_by).with(hash_including(:spree_api_key)) { current_api_user }
21
26
  end
22
27
 
23
28
  # This method can be overriden (with a let block) inside a context
@@ -31,7 +36,7 @@ module Spree
31
36
  end
32
37
 
33
38
  def upload_image(filename)
34
- fixture_file_upload(image(filename).path)
39
+ fixture_file_upload(image(filename).path, 'image/jpg')
35
40
  end
36
41
  end
37
42
  end
@@ -8,9 +8,16 @@ describe Spree::Api::BaseController do
8
8
  end
9
9
  end
10
10
 
11
- before do
12
- @routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
13
- r.draw { get 'index', to: 'spree/api/base#index' }
11
+ context "signed in as a user using an authentication extension" do
12
+ before do
13
+ controller.stub :try_spree_current_user => double(:email => "spree@example.com")
14
+ Spree::Api::Config[:requires_authentication] = true
15
+ end
16
+
17
+ it "can make a request" do
18
+ api_get :index
19
+ json_response.should == { "products" => [] }
20
+ response.status.should == 200
14
21
  end
15
22
  end
16
23
 
@@ -19,12 +26,12 @@ describe Spree::Api::BaseController do
19
26
 
20
27
  context "with a correct order token" do
21
28
  it "succeeds" do
22
- api_get :index, order_token: order.token, order_id: order.number
29
+ api_get :index, order_token: order.guest_token, order_id: order.number
23
30
  response.status.should == 200
24
31
  end
25
32
 
26
33
  it "succeeds with an order_number parameter" do
27
- api_get :index, order_token: order.token, order_number: order.number
34
+ api_get :index, order_token: order.guest_token, order_number: order.number
28
35
  response.status.should == 200
29
36
  end
30
37
  end
@@ -64,7 +71,7 @@ describe Spree::Api::BaseController do
64
71
  json_response.should == { "exception" => "no joy" }
65
72
  end
66
73
 
67
- it "maps semantic keys to nested_attributes keys" do
74
+ it "maps symantec keys to nested_attributes keys" do
68
75
  klass = double(:nested_attributes_options => { :line_items => {},
69
76
  :bill_address => {} })
70
77
  attributes = { 'line_items' => { :id => 1 },
@@ -72,11 +79,7 @@ describe Spree::Api::BaseController do
72
79
  'name' => 'test order' }
73
80
 
74
81
  mapped = subject.map_nested_attributes_keys(klass, attributes)
75
- mapped.has_key?('line_items_attributes').should be true
76
- mapped.has_key?('name').should be true
77
- end
78
-
79
- it "lets a subclass override the product associations that are eager-loaded" do
80
- controller.respond_to?(:product_includes, true).should be
82
+ mapped.has_key?('line_items_attributes').should be_true
83
+ mapped.has_key?('name').should be_true
81
84
  end
82
85
  end