spree_api 2.3.13 → 2.4.0.rc1

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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -43
  3. data/Gemfile +1 -1
  4. data/app/controllers/spree/api/addresses_controller.rb +1 -1
  5. data/app/controllers/spree/api/base_controller.rb +22 -9
  6. data/app/controllers/spree/api/checkouts_controller.rb +2 -2
  7. data/app/controllers/spree/api/countries_controller.rb +2 -2
  8. data/app/controllers/spree/api/credit_cards_controller.rb +1 -1
  9. data/app/controllers/spree/api/images_controller.rb +5 -0
  10. data/app/controllers/spree/api/inventory_units_controller.rb +1 -1
  11. data/app/controllers/spree/api/line_items_controller.rb +7 -7
  12. data/app/controllers/spree/api/option_types_controller.rb +2 -2
  13. data/app/controllers/spree/api/orders_controller.rb +17 -6
  14. data/app/controllers/spree/api/payments_controller.rb +3 -11
  15. data/app/controllers/spree/api/product_properties_controller.rb +2 -2
  16. data/app/controllers/spree/api/promotions_controller.rb +26 -0
  17. data/app/controllers/spree/api/properties_controller.rb +1 -1
  18. data/app/controllers/spree/api/return_authorizations_controller.rb +0 -10
  19. data/app/controllers/spree/api/shipments_controller.rb +71 -4
  20. data/app/controllers/spree/api/states_controller.rb +3 -3
  21. data/app/controllers/spree/api/stock_items_controller.rb +1 -1
  22. data/app/controllers/spree/api/stock_movements_controller.rb +1 -1
  23. data/app/controllers/spree/api/taxons_controller.rb +1 -1
  24. data/app/controllers/spree/api/variants_controller.rb +1 -2
  25. data/app/helpers/spree/api/api_helpers.rb +19 -5
  26. data/app/views/spree/api/images/index.v1.rabl +4 -0
  27. data/app/views/spree/api/products/show.v1.rabl +4 -1
  28. data/app/views/spree/api/promotions/handler.v1.rabl +2 -1
  29. data/app/views/spree/api/promotions/show.v1.rabl +2 -0
  30. data/app/views/spree/api/shipments/big.v1.rabl +48 -0
  31. data/app/views/spree/api/shipments/mine.v1.rabl +9 -0
  32. data/app/views/spree/api/shipments/small.v1.rabl +1 -5
  33. data/app/views/spree/api/shipping_rates/show.v1.rabl +2 -2
  34. data/app/views/spree/api/taxonomies/show.v1.rabl +2 -2
  35. data/app/views/spree/api/variants/big.v1.rabl +6 -1
  36. data/app/views/spree/api/variants/show.v1.rabl +1 -1
  37. data/app/views/spree/api/variants/small.v1.rabl +2 -3
  38. data/config/routes.rb +9 -0
  39. data/lib/spree/api/responders/rabl_template.rb +1 -1
  40. data/lib/spree/api/testing_support/helpers.rb +6 -6
  41. data/lib/spree/api/testing_support/setup.rb +2 -2
  42. data/spec/controllers/spree/api/addresses_controller_spec.rb +7 -7
  43. data/spec/controllers/spree/api/base_controller_spec.rb +66 -19
  44. data/spec/controllers/spree/api/checkouts_controller_spec.rb +54 -54
  45. data/spec/controllers/spree/api/classifications_controller_spec.rb +5 -5
  46. data/spec/controllers/spree/api/config_controller_spec.rb +9 -9
  47. data/spec/controllers/spree/api/countries_controller_spec.rb +11 -11
  48. data/spec/controllers/spree/api/credit_cards_controller_spec.rb +16 -16
  49. data/spec/controllers/spree/api/images_controller_spec.rb +37 -11
  50. data/spec/controllers/spree/api/inventory_units_controller_spec.rb +6 -6
  51. data/spec/controllers/spree/api/line_items_controller_spec.rb +34 -35
  52. data/spec/controllers/spree/api/option_types_controller_spec.rb +18 -18
  53. data/spec/controllers/spree/api/option_values_controller_spec.rb +21 -21
  54. data/spec/controllers/spree/api/orders_controller_spec.rb +177 -161
  55. data/spec/controllers/spree/api/payments_controller_spec.rb +52 -89
  56. data/spec/controllers/spree/api/product_properties_controller_spec.rb +21 -21
  57. data/spec/controllers/spree/api/products_controller_spec.rb +103 -60
  58. data/spec/controllers/spree/api/promotion_application_spec.rb +14 -12
  59. data/spec/controllers/spree/api/promotions_controller_spec.rb +64 -0
  60. data/spec/controllers/spree/api/properties_controller_spec.rb +25 -25
  61. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +42 -66
  62. data/spec/controllers/spree/api/shipments_controller_spec.rb +69 -21
  63. data/spec/controllers/spree/api/states_controller_spec.rb +18 -18
  64. data/spec/controllers/spree/api/stock_items_controller_spec.rb +26 -26
  65. data/spec/controllers/spree/api/stock_locations_controller_spec.rb +22 -22
  66. data/spec/controllers/spree/api/stock_movements_controller_spec.rb +16 -16
  67. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +24 -24
  68. data/spec/controllers/spree/api/taxons_controller_spec.rb +39 -39
  69. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +5 -5
  70. data/spec/controllers/spree/api/users_controller_spec.rb +41 -41
  71. data/spec/controllers/spree/api/variants_controller_spec.rb +47 -37
  72. data/spec/controllers/spree/api/zones_controller_spec.rb +40 -18
  73. data/spec/models/spree/legacy_user_spec.rb +5 -5
  74. data/spec/requests/rabl_cache_spec.rb +9 -9
  75. data/spec/spec_helper.rb +1 -0
  76. metadata +14 -9
  77. data/spec/requests/ransackable_attributes_spec.rb +0 -79
@@ -0,0 +1,2 @@
1
+ object @promotion
2
+ attributes *promotion_attributes
@@ -0,0 +1,48 @@
1
+ object @shipment
2
+ cache @shipment
3
+ attributes *shipment_attributes
4
+
5
+ child selected_shipping_rate: :selected_shipping_rate do
6
+ extends "spree/api/shipping_rates/show"
7
+ end
8
+
9
+ child inventory_units: :inventory_units do
10
+ object @inventory_unit
11
+ attributes *inventory_unit_attributes
12
+
13
+ child :variant do
14
+ extends "spree/api/variants/small"
15
+ attributes :product_id
16
+ child(images: :images) { extends "spree/api/images/show" }
17
+ end
18
+
19
+ child :line_item do
20
+ attributes *line_item_attributes
21
+ node(:single_display_amount) { |li| li.single_display_amount.to_s }
22
+ node(:display_amount) { |li| li.display_amount.to_s }
23
+ node(:total) { |li| li.total }
24
+ end
25
+ end
26
+
27
+ child order: :order do
28
+ extends "spree/api/orders/order"
29
+
30
+ child billing_address: :bill_address do
31
+ extends "spree/api/addresses/show"
32
+ end
33
+
34
+ child shipping_address: :ship_address do
35
+ extends "spree/api/addresses/show"
36
+ end
37
+
38
+ child adjustments: :adjustments do
39
+ extends "spree/api/adjustments/show"
40
+ end
41
+
42
+ child payments: :payments do
43
+ attributes :id, :amount, :display_amount, :state
44
+ child payment_method: :payment_method do
45
+ attributes :id, :name
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ object false
2
+
3
+ node(:count) { @shipments.count }
4
+ node(:current_page) { params[:page] || 1 }
5
+ node(:pages) { @shipments.num_pages }
6
+
7
+ child(@shipments => :shipments) do
8
+ extends "spree/api/shipments/big"
9
+ end
@@ -14,7 +14,7 @@ child :selected_shipping_rate => :selected_shipping_rate do
14
14
  end
15
15
 
16
16
  child :shipping_methods => :shipping_methods do
17
- attributes :id, :name
17
+ attributes :id, :code, :name
18
18
  child :zones => :zones do
19
19
  attributes :id, :name, :description
20
20
  end
@@ -31,7 +31,3 @@ child :manifest => :manifest do
31
31
  node(:quantity) { |m| m.quantity }
32
32
  node(:states) { |m| m.states }
33
33
  end
34
-
35
- child :adjustments => :adjustments do
36
- extends "spree/api/adjustments/show"
37
- end
@@ -1,2 +1,2 @@
1
- attributes :id, :name, :cost, :selected, :shipping_method_id
2
- node(:display_cost) { |sr| sr.display_cost.to_s }
1
+ attributes :id, :name, :cost, :selected, :shipping_method_id, :shipping_method_code
2
+ node(:display_cost) { |sr| sr.display_cost.to_s }
@@ -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,10 +1,15 @@
1
1
  object @variant
2
2
  attributes *variant_attributes
3
3
 
4
- cache [I18n.locale, 'big_variant', root_object]
4
+ cache [I18n.locale, @current_user_roles.include?('admin'), 'big_variant', root_object]
5
5
 
6
6
  extends "spree/api/variants/small"
7
7
 
8
+ node :total_on_hand do
9
+ root_object.total_on_hand
10
+ end
11
+
12
+
8
13
  child(:stock_items => :stock_items) do
9
14
  attributes :id, :count_on_hand, :stock_location_id, :backorderable
10
15
  attribute :available? => :available
@@ -1,3 +1,3 @@
1
1
  object @variant
2
- cache [I18n.locale, 'show', root_object]
2
+ cache [I18n.locale, @current_user_roles.include?('admin'), 'show', root_object]
3
3
  extends "spree/api/variants/big"
@@ -1,11 +1,10 @@
1
+ cache [I18n.locale, @current_user_roles.include?('admin'), 'small_variant', root_object]
2
+
1
3
  attributes *variant_attributes
2
- cache [I18n.locale, 'small_variant', root_object]
3
4
 
4
5
  node(:display_price) { |p| p.display_price.to_s }
5
6
  node(:options_text) { |v| v.options_text }
6
7
  node(:in_stock) { |v| v.in_stock? }
7
- node(:is_backorderable) { |v| v.is_backorderable? }
8
- node(:total_on_hand) { |v| v.total_on_hand }
9
8
 
10
9
  child :option_values => :option_values do
11
10
  attributes *option_value_attributes
data/config/routes.rb CHANGED
@@ -9,6 +9,8 @@ Spree::Core::Engine.add_routes do
9
9
  end
10
10
 
11
11
  namespace :api, defaults: { format: 'json' } do
12
+ resources :promotions, only: [:show]
13
+
12
14
  resources :products do
13
15
  resources :images
14
16
  resources :variants
@@ -60,6 +62,7 @@ Spree::Core::Engine.add_routes do
60
62
  end
61
63
 
62
64
  get '/orders/mine', to: 'orders#mine', as: 'my_orders'
65
+ get "/orders/current", to: "orders#current", to: "orders#current", as: "current_order"
63
66
 
64
67
  resources :orders, concerns: :order_routes
65
68
 
@@ -69,6 +72,12 @@ Spree::Core::Engine.add_routes do
69
72
  end
70
73
 
71
74
  resources :shipments, only: [:create, :update] do
75
+ collection do
76
+ post 'transfer_to_location'
77
+ post 'transfer_to_shipment'
78
+ get :mine
79
+ end
80
+
72
81
  member do
73
82
  put :ready
74
83
  put :ship
@@ -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)
@@ -12,23 +12,23 @@ module Spree
12
12
  end
13
13
 
14
14
  def assert_not_found!
15
- expect(json_response).to eq({ "error" => "The resource you were looking for could not be found." })
16
- expect(response.status).to eq 404
15
+ json_response.should == { "error" => "The resource you were looking for could not be found." }
16
+ response.status.should == 404
17
17
  end
18
18
 
19
19
  def assert_unauthorized!
20
- expect(json_response).to eq({ "error" => "You are not authorized to perform that action." })
21
- expect(response.status).to eq 401
20
+ json_response.should == { "error" => "You are not authorized to perform that action." }
21
+ response.status.should == 401
22
22
  end
23
23
 
24
24
  def stub_authentication!
25
- 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 }
26
26
  end
27
27
 
28
28
  # This method can be overriden (with a let block) inside a context
29
29
  # For instance, if you wanted to have an admin user instead.
30
30
  def current_api_user
31
- @current_api_user ||= stub_model(Spree::LegacyUser, email: "spree@example.com")
31
+ @current_api_user ||= stub_model(Spree::LegacyUser, :email => "spree@example.com")
32
32
  end
33
33
 
34
34
  def image(filename)
@@ -5,8 +5,8 @@ module Spree
5
5
  def sign_in_as_admin!
6
6
  let!(:current_api_user) do
7
7
  user = stub_model(Spree::LegacyUser)
8
- allow(user).to receive_message_chain(:spree_roles, :pluck).and_return(["admin"])
9
- allow(user).to receive(:has_spree_role?).with("admin").and_return(true)
8
+ user.stub_chain(:spree_roles, :pluck).and_return(["admin"])
9
+ user.stub(:has_spree_role?).with("admin").and_return(true)
10
10
  user
11
11
  end
12
12
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Spree
4
- describe Api::AddressesController, :type => :controller do
4
+ describe Api::AddressesController do
5
5
  render_views
6
6
 
7
7
  before do
@@ -12,27 +12,27 @@ module Spree
12
12
 
13
13
  context "with their own address" do
14
14
  before do
15
- allow_any_instance_of(Order).to receive_messages :user => current_api_user
15
+ Order.any_instance.stub :user => current_api_user
16
16
  end
17
17
 
18
18
  it "gets an address" do
19
19
  api_get :show, :id => @address.id, :order_id => @order.number
20
- expect(json_response['address1']).to eq @address.address1
20
+ json_response['address1'].should eq @address.address1
21
21
  end
22
22
 
23
23
  it "updates an address" do
24
24
  api_put :update, :id => @address.id, :order_id => @order.number,
25
25
  :address => { :address1 => "123 Test Lane" }
26
- expect(json_response['address1']).to eq '123 Test Lane'
26
+ json_response['address1'].should eq '123 Test Lane'
27
27
  end
28
28
 
29
29
  it "receives the errors object if address is invalid" do
30
30
  api_put :update, :id => @address.id, :order_id => @order.number,
31
31
  :address => { :address1 => "" }
32
32
 
33
- expect(json_response['error']).not_to be_nil
34
- expect(json_response['errors']).not_to be_nil
35
- expect(json_response['errors']['address1'].first).to eq "can't be blank"
33
+ json_response['error'].should_not be_nil
34
+ json_response['errors'].should_not be_nil
35
+ json_response['errors']['address1'].first.should eq "can't be blank"
36
36
  end
37
37
  end
38
38
 
@@ -1,6 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Spree::Api::BaseController, :type => :controller do
3
+ class FakesController < Spree::Api::BaseController
4
+ end
5
+
6
+ describe Spree::Api::BaseController do
4
7
  render_views
5
8
  controller(Spree::Api::BaseController) do
6
9
  def index
@@ -8,9 +11,17 @@ describe Spree::Api::BaseController, :type => :controller do
8
11
  end
9
12
  end
10
13
 
11
- before do
12
- @routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
13
- r.draw { get 'index', to: 'spree/api/base#index' }
14
+ context "signed in as a user using an authentication extension" do
15
+ before do
16
+ user = double(:email => "spree@example.com")
17
+ user.stub_chain :spree_roles, pluck: []
18
+ controller.stub :try_spree_current_user => user
19
+ end
20
+
21
+ it "can make a request" do
22
+ api_get :index
23
+ json_response.should == { "products" => [] }
24
+ response.status.should == 200
14
25
  end
15
26
  end
16
27
 
@@ -20,19 +31,19 @@ describe Spree::Api::BaseController, :type => :controller do
20
31
  context "with a correct order token" do
21
32
  it "succeeds" do
22
33
  api_get :index, order_token: order.guest_token, order_id: order.number
23
- expect(response.status).to eq(200)
34
+ response.status.should == 200
24
35
  end
25
36
 
26
37
  it "succeeds with an order_number parameter" do
27
38
  api_get :index, order_token: order.guest_token, order_number: order.number
28
- expect(response.status).to eq(200)
39
+ response.status.should == 200
29
40
  end
30
41
  end
31
42
 
32
43
  context "with an incorrect order token" do
33
44
  it "returns unauthorized" do
34
45
  api_get :index, order_token: "NOT_A_TOKEN", order_id: order.number
35
- expect(response.status).to eq(401)
46
+ response.status.should == 401
36
47
  end
37
48
  end
38
49
  end
@@ -40,29 +51,29 @@ describe Spree::Api::BaseController, :type => :controller do
40
51
  context "cannot make a request to the API" do
41
52
  it "without an API key" do
42
53
  api_get :index
43
- expect(json_response).to eq({ "error" => "You must specify an API key." })
44
- expect(response.status).to eq(401)
54
+ json_response.should == { "error" => "You must specify an API key." }
55
+ response.status.should == 401
45
56
  end
46
57
 
47
58
  it "with an invalid API key" do
48
59
  request.headers["X-Spree-Token"] = "fake_key"
49
60
  get :index, {}
50
- expect(json_response).to eq({ "error" => "Invalid API key (fake_key) specified." })
51
- expect(response.status).to eq(401)
61
+ json_response.should == { "error" => "Invalid API key (fake_key) specified." }
62
+ response.status.should == 401
52
63
  end
53
64
 
54
65
  it "using an invalid token param" do
55
66
  get :index, :token => "fake_key"
56
- expect(json_response).to eq({ "error" => "Invalid API key (fake_key) specified." })
67
+ json_response.should == { "error" => "Invalid API key (fake_key) specified." }
57
68
  end
58
69
  end
59
70
 
60
71
  it 'handles exceptions' do
61
- expect(subject).to receive(:authenticate_user).and_return(true)
62
- expect(subject).to receive(:load_user_roles).and_return(true)
63
- expect(subject).to receive(:index).and_raise(Exception.new("no joy"))
72
+ subject.should_receive(:authenticate_user).and_return(true)
73
+ subject.should_receive(:load_user_roles).and_return(true)
74
+ subject.should_receive(:index).and_raise(Exception.new("no joy"))
64
75
  get :index, :token => "fake_key"
65
- expect(json_response).to eq({ "exception" => "no joy" })
76
+ json_response.should == { "exception" => "no joy" }
66
77
  end
67
78
 
68
79
  it "maps semantic keys to nested_attributes keys" do
@@ -73,11 +84,47 @@ describe Spree::Api::BaseController, :type => :controller do
73
84
  'name' => 'test order' }
74
85
 
75
86
  mapped = subject.map_nested_attributes_keys(klass, attributes)
76
- expect(mapped.has_key?('line_items_attributes')).to be true
77
- expect(mapped.has_key?('name')).to be true
87
+ mapped.has_key?('line_items_attributes').should be true
88
+ mapped.has_key?('name').should be true
78
89
  end
79
90
 
80
91
  it "lets a subclass override the product associations that are eager-loaded" do
81
- expect(controller.respond_to?(:product_includes, true)).to be
92
+ controller.respond_to?(:product_includes, true).should be
93
+ end
94
+
95
+ describe '#error_during_processing' do
96
+ controller(FakesController) do
97
+ # GET /foo
98
+ # Simulates a failed API call.
99
+ def foo
100
+ raise StandardError
101
+ end
102
+ end
103
+
104
+ # What would be placed in config/initializers/spree.rb
105
+ Spree::Api::BaseController.error_notifier = Proc.new do |e, controller|
106
+ MockHoneybadger.notify_or_ignore(e, rack_env: controller.request.env)
107
+ end
108
+
109
+ ##
110
+ # Fake HB alert class
111
+ class MockHoneybadger
112
+ # https://github.com/honeybadger-io/honeybadger-ruby/blob/master/lib/honeybadger.rb#L136
113
+ def self.notify_or_ignore(exception, opts = {})
114
+ end
115
+ end
116
+
117
+ before do
118
+ user = double(email: "spree@example.com")
119
+ user.stub_chain :spree_roles, pluck: []
120
+ controller.stub try_spree_current_user: user
121
+ routes.draw { get 'foo' => 'fakes#foo' }
122
+ end
123
+
124
+ it 'should notify notify_error_during_processing' do
125
+ expect(MockHoneybadger).to receive(:notify_or_ignore).once.with(kind_of(Exception), rack_env: kind_of(Hash))
126
+ api_get :foo
127
+ expect(response.status).to eq(422)
128
+ end
82
129
  end
83
130
  end