spree_api 2.3.13 → 2.4.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Spree
4
- describe Spree::Api::PaymentsController, :type => :controller do
4
+ describe Spree::Api::PaymentsController do
5
5
  render_views
6
6
  let!(:order) { create(:order) }
7
7
  let!(:payment) { create(:payment, :order => order) }
8
8
  let!(:attributes) { [:id, :source_type, :source_id, :amount, :display_amount,
9
- :payment_method_id, :state, :avs_response,
9
+ :payment_method_id, :response_code, :state, :avs_response,
10
10
  :created_at, :updated_at] }
11
11
 
12
12
  let(:resource_scoping) { { :order_id => order.to_param } }
@@ -18,30 +18,30 @@ module Spree
18
18
  context "as a user" do
19
19
  context "when the order belongs to the user" do
20
20
  before do
21
- allow_any_instance_of(Order).to receive_messages :user => current_api_user
21
+ Order.any_instance.stub :user => current_api_user
22
22
  end
23
23
 
24
24
  it "can view the payments for their order" do
25
25
  api_get :index
26
- expect(json_response["payments"].first).to have_attributes(attributes)
26
+ json_response["payments"].first.should have_attributes(attributes)
27
27
  end
28
28
 
29
29
  it "can learn how to create a new payment" do
30
30
  api_get :new
31
- expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
32
- expect(json_response["payment_methods"]).not_to be_empty
33
- expect(json_response["payment_methods"].first).to have_attributes([:id, :name, :description])
31
+ json_response["attributes"].should == attributes.map(&:to_s)
32
+ json_response["payment_methods"].should_not be_empty
33
+ json_response["payment_methods"].first.should have_attributes([:id, :name, :description])
34
34
  end
35
35
 
36
36
  it "can create a new payment" do
37
37
  api_post :create, :payment => { :payment_method_id => PaymentMethod.first.id, :amount => 50 }
38
- expect(response.status).to eq(201)
39
- expect(json_response).to have_attributes(attributes)
38
+ response.status.should == 201
39
+ json_response.should have_attributes(attributes)
40
40
  end
41
41
 
42
42
  it "can view a pre-existing payment's details" do
43
43
  api_get :show, :id => payment.to_param
44
- expect(json_response).to have_attributes(attributes)
44
+ json_response.should have_attributes(attributes)
45
45
  end
46
46
 
47
47
  it "cannot update a payment" do
@@ -57,7 +57,7 @@ module Spree
57
57
 
58
58
  context "when the order does not belong to the user" do
59
59
  before do
60
- allow_any_instance_of(Order).to receive_messages :user => stub_model(LegacyUser)
60
+ Order.any_instance.stub :user => stub_model(LegacyUser)
61
61
  end
62
62
 
63
63
  it "cannot view payments for somebody else's order" do
@@ -67,7 +67,7 @@ module Spree
67
67
 
68
68
  it "can view the payments for an order given the order token" do
69
69
  api_get :index, :order_id => order.to_param, :order_token => order.guest_token
70
- expect(json_response["payments"].first).to have_attributes(attributes)
70
+ json_response["payments"].first.should have_attributes(attributes)
71
71
  end
72
72
  end
73
73
  end
@@ -77,59 +77,54 @@ module Spree
77
77
 
78
78
  it "can view the payments on any order" do
79
79
  api_get :index
80
- expect(response.status).to eq(200)
81
- expect(json_response["payments"].first).to have_attributes(attributes)
80
+ response.status.should == 200
81
+ json_response["payments"].first.should have_attributes(attributes)
82
82
  end
83
83
 
84
84
  context "multiple payments" do
85
- before { @payment = create(:payment, :order => order) }
85
+ before { @payment = create(:payment, :order => order, :response_code => '99999') }
86
86
 
87
87
  it "can view all payments on an order" do
88
88
  api_get :index
89
- expect(json_response["count"]).to eq(2)
89
+ json_response["count"].should == 2
90
90
  end
91
91
 
92
92
  it 'can control the page size through a parameter' do
93
93
  api_get :index, :per_page => 1
94
- expect(json_response['count']).to eq(1)
95
- expect(json_response['current_page']).to eq(1)
96
- expect(json_response['pages']).to eq(2)
94
+ json_response['count'].should == 1
95
+ json_response['current_page'].should == 1
96
+ json_response['pages'].should == 2
97
+ end
98
+
99
+ it 'can query the results through a paramter' do
100
+ api_get :index, :q => { :response_code_cont => '999' }
101
+ json_response['count'].should == 1
102
+ json_response['payments'].first['response_code'].should eq @payment.response_code
97
103
  end
98
104
  end
99
105
 
100
106
  context "for a given payment" do
101
107
  context "updating" do
102
- context "when the state is checkout" do
103
- it "can update" do
104
- payment.update_attributes(state: 'checkout')
105
- api_put(:update, id: payment.to_param, payment: { amount: 2.01 })
106
- expect(response.status).to be(200)
107
- expect(payment.reload.amount).to eq(2.01)
108
- end
109
- end
110
-
111
- context "when the state is pending" do
112
- it "can update" do
113
- payment.update_attributes(state: 'pending')
114
- api_put(:update, id: payment.to_param, payment: { amount: 2.01 })
115
- expect(response.status).to be(200)
116
- expect(payment.reload.amount).to eq(2.01)
117
- end
108
+ it "can update" do
109
+ payment.update_attributes(:state => 'pending')
110
+ api_put :update, :id => payment.to_param, :payment => { :amount => 2.01 }
111
+ response.status.should == 200
112
+ payment.reload.amount.should == 2.01
118
113
  end
119
114
 
120
115
  context "update fails" do
121
116
  it "returns a 422 status when the amount is invalid" do
122
- payment.update_attributes(state: 'pending')
123
- api_put(:update, id: payment.to_param, payment: { amount: 'invalid' })
124
- expect(response.status).to be(422)
125
- expect(json_response['error']).to eql('Invalid resource. Please fix errors and try again.')
117
+ payment.update_attributes(:state => 'pending')
118
+ api_put :update, :id => payment.to_param, :payment => { :amount => 'invalid' }
119
+ response.status.should == 422
120
+ json_response["error"].should == "Invalid resource. Please fix errors and try again."
126
121
  end
127
122
 
128
123
  it "returns a 403 status when the payment is not pending" do
129
- payment.update_attributes(state: 'completed')
130
- api_put(:update, id: payment.to_param, payment: { amount: 2.01 })
131
- expect(response.status).to be(403)
132
- expect(json_response['error']).to eql('This payment cannot be updated because it is completed.')
124
+ payment.update_attributes(:state => 'completed')
125
+ api_put :update, :id => payment.to_param, :payment => { :amount => 2.01 }
126
+ response.status.should == 403
127
+ json_response["error"].should == "This payment cannot be updated because it is completed."
133
128
  end
134
129
  end
135
130
  end
@@ -137,26 +132,26 @@ module Spree
137
132
  context "authorizing" do
138
133
  it "can authorize" do
139
134
  api_put :authorize, :id => payment.to_param
140
- expect(response.status).to eq(200)
141
- expect(payment.reload.state).to eq("pending")
135
+ response.status.should == 200
136
+ payment.reload.state.should == "pending"
142
137
  end
143
138
 
144
139
  context "authorization fails" do
145
140
  before do
146
141
  fake_response = double(:success? => false, :to_s => "Could not authorize card")
147
- expect_any_instance_of(Spree::Gateway::Bogus).to receive(:authorize).and_return(fake_response)
142
+ Spree::Gateway::Bogus.any_instance.should_receive(:authorize).and_return(fake_response)
148
143
  api_put :authorize, :id => payment.to_param
149
144
  end
150
145
 
151
146
  it "returns a 422 status" do
152
- expect(response.status).to eq(422)
147
+ response.status.should == 422
153
148
  expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
154
149
  expect(json_response["errors"]["base"][0]).to eq "Could not authorize card"
155
150
  end
156
151
 
157
152
  it "does not raise a stack level error" do
158
153
  skip "Investigate why a payment.reload after the request raises 'stack level too deep'"
159
- expect(payment.reload.state).to eq("failed")
154
+ payment.reload.state.should == "failed"
160
155
  end
161
156
  end
162
157
  end
@@ -164,19 +159,19 @@ module Spree
164
159
  context "capturing" do
165
160
  it "can capture" do
166
161
  api_put :capture, :id => payment.to_param
167
- expect(response.status).to eq(200)
168
- expect(payment.reload.state).to eq("completed")
162
+ response.status.should == 200
163
+ payment.reload.state.should == "completed"
169
164
  end
170
165
 
171
166
  context "capturing fails" do
172
167
  before do
173
168
  fake_response = double(:success? => false, :to_s => "Insufficient funds")
174
- expect_any_instance_of(Spree::Gateway::Bogus).to receive(:capture).and_return(fake_response)
169
+ Spree::Gateway::Bogus.any_instance.should_receive(:capture).and_return(fake_response)
175
170
  end
176
171
 
177
172
  it "returns a 422 status" do
178
173
  api_put :capture, :id => payment.to_param
179
- expect(response.status).to eq(422)
174
+ response.status.should == 422
180
175
  expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
181
176
  expect(json_response["errors"]["base"][0]).to eq "Insufficient funds"
182
177
  end
@@ -186,19 +181,19 @@ module Spree
186
181
  context "purchasing" do
187
182
  it "can purchase" do
188
183
  api_put :purchase, :id => payment.to_param
189
- expect(response.status).to eq(200)
190
- expect(payment.reload.state).to eq("completed")
184
+ response.status.should == 200
185
+ payment.reload.state.should == "completed"
191
186
  end
192
187
 
193
188
  context "purchasing fails" do
194
189
  before do
195
190
  fake_response = double(:success? => false, :to_s => "Insufficient funds")
196
- expect_any_instance_of(Spree::Gateway::Bogus).to receive(:purchase).and_return(fake_response)
191
+ Spree::Gateway::Bogus.any_instance.should_receive(:purchase).and_return(fake_response)
197
192
  end
198
193
 
199
194
  it "returns a 422 status" do
200
195
  api_put :purchase, :id => payment.to_param
201
- expect(response.status).to eq(422)
196
+ response.status.should == 422
202
197
  expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
203
198
  expect(json_response["errors"]["base"][0]).to eq "Insufficient funds"
204
199
  end
@@ -215,7 +210,7 @@ module Spree
215
210
  context "voiding fails" do
216
211
  before do
217
212
  fake_response = double(success?: false, to_s: "NO REFUNDS")
218
- expect_any_instance_of(Spree::Gateway::Bogus).to receive(:void).and_return(fake_response)
213
+ Spree::Gateway::Bogus.any_instance.should_receive(:void).and_return(fake_response)
219
214
  end
220
215
 
221
216
  it "returns a 422 status" do
@@ -228,38 +223,6 @@ module Spree
228
223
  end
229
224
  end
230
225
 
231
- context "crediting" do
232
- before do
233
- payment.purchase!
234
- end
235
-
236
- it "can credit" do
237
- api_put :credit, :id => payment.to_param
238
- expect(response.status).to eq(200)
239
- expect(payment.reload.state).to eq("completed")
240
-
241
- # Ensure that a credit payment was created, and it has correct credit amount
242
- credit_payment = Payment.where(:source_type => 'Spree::Payment', :source_id => payment.id).last
243
- expect(credit_payment.amount.to_f).to eq(-45.75)
244
- end
245
-
246
- context "crediting fails" do
247
- it "returns a 422 status" do
248
- fake_response = double(:success? => false, :to_s => "NO CREDIT FOR YOU")
249
- expect_any_instance_of(Spree::Gateway::Bogus).to receive(:credit).and_return(fake_response)
250
- api_put :credit, :id => payment.to_param
251
- expect(response.status).to eq(422)
252
- expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
253
- expect(json_response["errors"]["base"][0]).to eq "NO CREDIT FOR YOU"
254
- end
255
-
256
- it "cannot credit over credit_allowed limit" do
257
- api_put :credit, :id => payment.to_param, :amount => 1000000
258
- expect(response.status).to eq(422)
259
- expect(json_response["error"]).to eq("This payment can only be credited up to 45.75. Please specify an amount less than or equal to this number.")
260
- end
261
- end
262
- end
263
226
  end
264
227
  end
265
228
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'shared_examples/protect_product_actions'
3
3
 
4
4
  module Spree
5
- describe Spree::Api::ProductPropertiesController, :type => :controller do
5
+ describe Spree::Api::ProductPropertiesController do
6
6
  render_views
7
7
 
8
8
  let!(:product) { create(:product) }
@@ -23,40 +23,40 @@ module Spree
23
23
 
24
24
  it "can not see a list of product properties" do
25
25
  api_get :index
26
- expect(response.status).to eq(404)
26
+ response.status.should == 404
27
27
  end
28
28
  end
29
29
 
30
30
  it "can see a list of all product properties" do
31
31
  api_get :index
32
- expect(json_response["product_properties"].count).to eq 2
33
- expect(json_response["product_properties"].first).to have_attributes(attributes)
32
+ json_response["product_properties"].count.should eq 2
33
+ json_response["product_properties"].first.should have_attributes(attributes)
34
34
  end
35
35
 
36
36
  it "can control the page size through a parameter" do
37
37
  api_get :index, :per_page => 1
38
- expect(json_response['product_properties'].count).to eq(1)
39
- expect(json_response['current_page']).to eq(1)
40
- expect(json_response['pages']).to eq(2)
38
+ json_response['product_properties'].count.should == 1
39
+ json_response['current_page'].should == 1
40
+ json_response['pages'].should == 2
41
41
  end
42
42
 
43
43
  it 'can query the results through a parameter' do
44
44
  Spree::ProductProperty.last.update_attribute(:value, 'loose')
45
45
  property = Spree::ProductProperty.last
46
46
  api_get :index, :q => { :value_cont => 'loose' }
47
- expect(json_response['count']).to eq(1)
48
- expect(json_response['product_properties'].first['value']).to eq property.value
47
+ json_response['count'].should == 1
48
+ json_response['product_properties'].first['value'].should eq property.value
49
49
  end
50
50
 
51
51
  it "can see a single product_property" do
52
52
  api_get :show, :id => property_1.property_name
53
- expect(json_response).to have_attributes(attributes)
53
+ json_response.should have_attributes(attributes)
54
54
  end
55
55
 
56
56
  it "can learn how to create a new product property" do
57
57
  api_get :new
58
- expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
59
- expect(json_response["required_attributes"]).to be_empty
58
+ json_response["attributes"].should == attributes.map(&:to_s)
59
+ json_response["required_attributes"].should be_empty
60
60
  end
61
61
 
62
62
  it "cannot create a new product property if not an admin" do
@@ -72,7 +72,7 @@ module Spree
72
72
  it "cannot delete a product property" do
73
73
  api_delete :destroy, :property_name => property_1.property_name
74
74
  assert_unauthorized!
75
- expect { property_1.reload }.not_to raise_error
75
+ lambda { property_1.reload }.should_not raise_error
76
76
  end
77
77
 
78
78
  context "as an admin" do
@@ -82,19 +82,19 @@ module Spree
82
82
  expect do
83
83
  api_post :create, :product_property => { :property_name => "My Property 3", :value => "my value 3" }
84
84
  end.to change(product.product_properties, :count).by(1)
85
- expect(json_response).to have_attributes(attributes)
86
- expect(response.status).to eq(201)
85
+ json_response.should have_attributes(attributes)
86
+ response.status.should == 201
87
87
  end
88
88
 
89
89
  it "can update a product property" do
90
90
  api_put :update, :id => property_1.property_name, :product_property => { :value => "my value 456" }
91
- expect(response.status).to eq(200)
91
+ response.status.should == 200
92
92
  end
93
93
 
94
94
  it "can delete a product property" do
95
95
  api_delete :destroy, :id => property_1.property_name
96
- expect(response.status).to eq(204)
97
- expect { property_1.reload }.to raise_error(ActiveRecord::RecordNotFound)
96
+ response.status.should == 204
97
+ lambda { property_1.reload }.should raise_error(ActiveRecord::RecordNotFound)
98
98
  end
99
99
  end
100
100
 
@@ -102,13 +102,13 @@ module Spree
102
102
  let(:resource_scoping) { { :product_id => product.id } }
103
103
  it "can see a list of all product properties" do
104
104
  api_get :index
105
- expect(json_response["product_properties"].count).to eq 2
106
- expect(json_response["product_properties"].first).to have_attributes(attributes)
105
+ json_response["product_properties"].count.should eq 2
106
+ json_response["product_properties"].first.should have_attributes(attributes)
107
107
  end
108
108
 
109
109
  it "can see a single product_property by id" do
110
110
  api_get :show, :id => property_1.id
111
- expect(json_response).to have_attributes(attributes)
111
+ json_response.should have_attributes(attributes)
112
112
  end
113
113
  end
114
114