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
@@ -21,31 +21,6 @@ module Spree
21
21
  Spree::Config[:track_inventory_levels] = true
22
22
  end
23
23
 
24
- context "GET 'show'" do
25
- let(:order) { create(:order) }
26
-
27
- it "redirects to Orders#show" do
28
- api_get :show, :id => order.number
29
- response.status.should == 301
30
- response.should redirect_to("/api/orders/#{order.number}")
31
- end
32
- end
33
-
34
- context "POST 'create'" do
35
- it "creates a new order when no parameters are passed" do
36
- api_post :create
37
-
38
- json_response['number'].should be_present
39
- response.status.should == 201
40
- end
41
-
42
- it "assigns email when creating a new order" do
43
- api_post :create, :order => { :email => "guest@spreecommerce.com" }
44
- expect(json_response['email']).not_to eq controller.current_api_user
45
- expect(json_response['email']).to eq "guest@spreecommerce.com"
46
- end
47
- end
48
-
49
24
  context "PUT 'update'" do
50
25
  let(:order) do
51
26
  order = create(:order_with_line_items)
@@ -60,41 +35,24 @@ module Spree
60
35
  Order.any_instance.stub(:payment_required? => true)
61
36
  end
62
37
 
63
- it 'should not allow users to change the price of line items' do
64
- line_item = order.line_items.first
65
- price_was = line_item.price
66
- api_put(
67
- :update,
68
- id: order.to_param,
69
- order_token: order.token,
70
- order: {
71
- line_items: {0 => {id: line_item.id, price: '0.1', quantity: '3'}}
72
- }
73
- )
74
- response.status.should == 200
75
- line_item.reload
76
- expect(line_item.price).to eq price_was
77
- expect(line_item.price).to_not eq 0.1
78
- end
79
-
80
38
  it "should transition a recently created order from cart to address" do
81
39
  order.state.should eq "cart"
82
40
  order.email.should_not be_nil
83
- api_put :update, :id => order.to_param, :order_token => order.token
41
+ api_put :update, :id => order.to_param, :order_token => order.guest_token
84
42
  order.reload.state.should eq "address"
85
43
  end
86
44
 
87
45
  it "should transition a recently created order from cart to address with order token in header" do
88
46
  order.state.should eq "cart"
89
47
  order.email.should_not be_nil
90
- request.headers["X-Spree-Order-Token"] = order.token
48
+ request.headers["X-Spree-Order-Token"] = order.guest_token
91
49
  api_put :update, :id => order.to_param
92
50
  order.reload.state.should eq "address"
93
51
  end
94
52
 
95
53
  it "can take line_items_attributes as a parameter" do
96
54
  line_item = order.line_items.first
97
- api_put :update, :id => order.to_param, :order_token => order.token,
55
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
98
56
  :order => { :line_items_attributes => { 0 => { :id => line_item.id, :quantity => 1 } } }
99
57
  response.status.should == 200
100
58
  order.reload.state.should eq "address"
@@ -102,41 +60,66 @@ module Spree
102
60
 
103
61
  it "can take line_items as a parameter" do
104
62
  line_item = order.line_items.first
105
- api_put :update, :id => order.to_param, :order_token => order.token,
63
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
106
64
  :order => { :line_items => { 0 => { :id => line_item.id, :quantity => 1 } } }
107
65
  response.status.should == 200
108
66
  order.reload.state.should eq "address"
109
67
  end
110
68
 
111
69
  it "will return an error if the order cannot transition" do
112
- skip "not sure if this test is valid"
70
+ pending "not sure if this test is valid"
113
71
  order.bill_address = nil
114
72
  order.save
115
73
  order.update_column(:state, "address")
116
- api_put :update, :id => order.to_param, :order_token => order.token
74
+ api_put :update, :id => order.to_param, :order_token => order.guest_token
117
75
  # Order has not transitioned
118
76
  response.status.should == 422
119
77
  end
120
78
 
121
- it "can update addresses and transition from address to delivery" do
122
- order.update_column(:state, "address")
123
- shipping_address = billing_address = {
124
- :firstname => 'John',
125
- :lastname => 'Doe',
126
- :address1 => '7735 Old Georgetown Road',
127
- :city => 'Bethesda',
128
- :phone => '3014445002',
129
- :zipcode => '20814',
130
- :state_id => @state.id,
131
- :country_id => @country.id
132
- }
133
- api_put :update,
134
- :id => order.to_param, :order_token => order.token,
135
- :order => { :bill_address_attributes => billing_address, :ship_address_attributes => shipping_address }
136
- json_response['state'].should == 'delivery'
137
- json_response['bill_address']['firstname'].should == 'John'
138
- json_response['ship_address']['firstname'].should == 'John'
139
- response.status.should == 200
79
+ context "transitioning to delivery" do
80
+ before do
81
+ order.update_column(:state, "address")
82
+ end
83
+
84
+ let(:address) do
85
+ {
86
+ :firstname => 'John',
87
+ :lastname => 'Doe',
88
+ :address1 => '7735 Old Georgetown Road',
89
+ :city => 'Bethesda',
90
+ :phone => '3014445002',
91
+ :zipcode => '20814',
92
+ :state_id => @state.id,
93
+ :country_id => @country.id
94
+ }
95
+ end
96
+
97
+ it "can update addresses and transition from address to delivery" do
98
+ api_put :update,
99
+ :id => order.to_param, :order_token => order.guest_token,
100
+ :order => {
101
+ :bill_address_attributes => address,
102
+ :ship_address_attributes => address
103
+ }
104
+ json_response['state'].should == 'delivery'
105
+ json_response['bill_address']['firstname'].should == 'John'
106
+ json_response['ship_address']['firstname'].should == 'John'
107
+ response.status.should == 200
108
+ end
109
+
110
+ # Regression test for #4498
111
+ it "does not contain duplicate variant data in delivery return" do
112
+ api_put :update,
113
+ :id => order.to_param, :order_token => order.guest_token,
114
+ :order => {
115
+ :bill_address_attributes => address,
116
+ :ship_address_attributes => address
117
+ }
118
+ # Shipments manifests should not return the ENTIRE variant
119
+ # This information is already present within the order's line items
120
+ expect(json_response['shipments'].first['manifest'].first['variant']).to be_nil
121
+ expect(json_response['shipments'].first['manifest'].first['variant_id']).to_not be_nil
122
+ end
140
123
  end
141
124
 
142
125
  it "can update shipping method and transition from delivery to payment" do
@@ -144,7 +127,7 @@ module Spree
144
127
  shipment = create(:shipment, :order => order)
145
128
  shipment.refresh_rates
146
129
  shipping_rate = shipment.shipping_rates.where(:selected => false).first
147
- api_put :update, :id => order.to_param, :order_token => order.token,
130
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
148
131
  :order => { :shipments_attributes => { "0" => { :selected_shipping_rate_id => shipping_rate.id, :id => shipment.id } } }
149
132
  response.status.should == 200
150
133
  # Find the correct shipment...
@@ -152,14 +135,14 @@ module Spree
152
135
  # Find the correct shipping rate for that shipment...
153
136
  json_shipping_rate = json_shipment['shipping_rates'].detect { |sr| sr["id"] == shipping_rate.id }
154
137
  # ... And finally ensure that it's selected
155
- json_shipping_rate['selected'].should be true
138
+ json_shipping_rate['selected'].should be_true
156
139
  # Order should automatically transfer to payment because all criteria are met
157
140
  json_response['state'].should == 'payment'
158
141
  end
159
142
 
160
143
  it "can update payment method and transition from payment to confirm" do
161
144
  order.update_column(:state, "payment")
162
- api_put :update, :id => order.to_param, :order_token => order.token,
145
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
163
146
  :order => { :payments_attributes => [{ :payment_method_id => @payment_method.id }] }
164
147
  json_response['state'].should == 'confirm'
165
148
  json_response['payments'][0]['payment_method']['name'].should == @payment_method.name
@@ -177,7 +160,7 @@ module Spree
177
160
  "name" => "Spree Commerce"
178
161
  }
179
162
 
180
- api_put :update, :id => order.to_param, :order_token => order.token,
163
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
181
164
  :order => { :payments_attributes => [{ :payment_method_id => @payment_method.id.to_s }],
182
165
  :payment_source => { @payment_method.id.to_s => source_attributes } }
183
166
  json_response['payments'][0]['payment_method']['name'].should == @payment_method.name
@@ -187,9 +170,13 @@ module Spree
187
170
 
188
171
  it "returns errors when source is missing attributes" do
189
172
  order.update_column(:state, "payment")
190
- api_put :update, :id => order.to_param, :order_token => order.token,
191
- :order => { :payments_attributes => [{ :payment_method_id => @payment_method.id.to_s }],
192
- :payment_source => { @payment_method.id.to_s => { name: "Spree" } } }
173
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
174
+ :order => {
175
+ :payments_attributes => [{ :payment_method_id => @payment_method.id.to_s }]
176
+ },
177
+ :payment_source => {
178
+ @payment_method.id.to_s => { name: "Spree" }
179
+ }
193
180
 
194
181
  response.status.should == 422
195
182
  cc_errors = json_response['errors']['payments.Credit Card']
@@ -202,14 +189,14 @@ module Spree
202
189
  it "can transition from confirm to complete" do
203
190
  order.update_column(:state, "confirm")
204
191
  Spree::Order.any_instance.stub(:payment_required? => false)
205
- api_put :update, :id => order.to_param, :order_token => order.token
192
+ api_put :update, :id => order.to_param, :order_token => order.guest_token
206
193
  json_response['state'].should == 'complete'
207
194
  response.status.should == 200
208
195
  end
209
196
 
210
197
  it "returns the order if the order is already complete" do
211
198
  order.update_column(:state, "complete")
212
- api_put :update, :id => order.to_param, :order_token => order.token
199
+ api_put :update, :id => order.to_param, :order_token => order.guest_token
213
200
  json_response['number'].should == order.number
214
201
  response.status.should == 200
215
202
  end
@@ -217,7 +204,7 @@ module Spree
217
204
  # Regression test for #3784
218
205
  it "can update the special instructions for an order" do
219
206
  instructions = "Don't drop it. (Please)"
220
- api_put :update, :id => order.to_param, :order_token => order.token,
207
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
221
208
  :order => { :special_instructions => instructions }
222
209
  expect(json_response['special_instructions']).to eql(instructions)
223
210
  end
@@ -227,7 +214,7 @@ module Spree
227
214
  it "can assign a user to the order" do
228
215
  user = create(:user)
229
216
  # Need to pass email as well so that validations succeed
230
- api_put :update, :id => order.to_param, :order_token => order.token,
217
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
231
218
  :order => { :user_id => user.id, :email => "guest@spreecommerce.com" }
232
219
  response.status.should == 200
233
220
  json_response['user_id'].should == user.id
@@ -235,19 +222,19 @@ module Spree
235
222
  end
236
223
 
237
224
  it "can assign an email to the order" do
238
- api_put :update, :id => order.to_param, :order_token => order.token,
225
+ api_put :update, :id => order.to_param, :order_token => order.guest_token,
239
226
  :order => { :email => "guest@spreecommerce.com" }
240
227
  json_response['email'].should == "guest@spreecommerce.com"
241
228
  response.status.should == 200
242
229
  end
243
230
 
244
231
  it "can apply a coupon code to an order" do
245
- skip "ensure that the order totals are properly updated, see frontend orders_controller or checkout_controller as example"
232
+ pending "ensure that the order totals are properly updated, see frontend orders_controller or checkout_controller as example"
246
233
 
247
234
  order.update_column(:state, "payment")
248
235
  PromotionHandler::Coupon.should_receive(:new).with(order).and_call_original
249
236
  PromotionHandler::Coupon.any_instance.should_receive(:apply).and_return({:coupon_applied? => true})
250
- api_put :update, :id => order.to_param, :order_token => order.token, :order => { :coupon_code => "foobar" }
237
+ api_put :update, :id => order.to_param, :order_token => order.guest_token, :order => { :coupon_code => "foobar" }
251
238
  end
252
239
  end
253
240
 
@@ -256,7 +243,7 @@ module Spree
256
243
  it "cannot transition to address without a line item" do
257
244
  order.line_items.delete_all
258
245
  order.update_column(:email, "spree@example.com")
259
- api_put :next, :id => order.to_param, :order_token => order.token
246
+ api_put :next, :id => order.to_param, :order_token => order.guest_token
260
247
  response.status.should == 422
261
248
  json_response["errors"]["base"].should include(Spree.t(:there_are_no_items_for_this_order))
262
249
  end
@@ -264,7 +251,7 @@ module Spree
264
251
  it "can transition an order to the next state" do
265
252
  order.update_column(:email, "spree@example.com")
266
253
 
267
- api_put :next, :id => order.to_param, :order_token => order.token
254
+ api_put :next, :id => order.to_param, :order_token => order.guest_token
268
255
  response.status.should == 200
269
256
  json_response['state'].should == 'address'
270
257
  end
@@ -275,29 +262,30 @@ module Spree
275
262
  email: nil
276
263
  )
277
264
 
278
- api_put :next, :id => order.to_param, :order_token => order.token
265
+ api_put :next, :id => order.to_param, :order_token => order.guest_token
279
266
  response.status.should == 422
280
267
  json_response['error'].should =~ /could not be transitioned/
281
268
  end
282
269
 
283
- it "returns a sensible error when no payment method is specified" do
270
+ it "doesnt advance payment state if order has no payment" do
284
271
  order.update_column(:state, "payment")
285
- api_put :next, :id => order.to_param, :order_token => order.token, :order => {}
286
- json_response["errors"]["base"].should include(Spree.t(:no_pending_payments))
272
+ api_put :next, :id => order.to_param, :order_token => order.guest_token, :order => {}
273
+ json_response["errors"]["base"].should include(Spree.t(:no_payment_found))
287
274
  end
288
275
  end
289
276
 
290
277
  context "PUT 'advance'" do
291
278
  let!(:order) { create(:order_with_line_items) }
279
+
292
280
  it 'continues to advance advances an order while it can move forward' do
293
281
  Spree::Order.any_instance.should_receive(:next).exactly(3).times.and_return(true, true, false)
294
- api_put :advance, :id => order.to_param, :order_token => order.token
282
+ api_put :advance, :id => order.to_param, :order_token => order.guest_token
295
283
  end
284
+
296
285
  it 'returns the order' do
297
- api_put :advance, :id => order.to_param, :order_token => order.token
286
+ api_put :advance, :id => order.to_param, :order_token => order.guest_token
298
287
  json_response['id'].should == order.id
299
288
  end
300
-
301
289
  end
302
290
  end
303
291
  end
@@ -10,11 +10,8 @@ module Spree
10
10
  let(:attributes) { [:id, :quantity, :price, :variant, :total, :display_amount, :single_display_amount] }
11
11
  let(:resource_scoping) { { :order_id => order.to_param } }
12
12
 
13
- before do
14
- stub_authentication!
15
- end
16
-
17
13
  it "can learn how to create a new line item" do
14
+ controller.stub :try_spree_current_user => current_api_user
18
15
  api_get :new
19
16
  json_response["attributes"].should == ["quantity", "price", "variant_id"]
20
17
  required_attributes = json_response["required_attributes"]
@@ -23,14 +20,14 @@ module Spree
23
20
 
24
21
  context "authenticating with a token" do
25
22
  it "can add a new line item to an existing order" do
26
- api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }, :order_token => order.token
23
+ api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }, :order_token => order.guest_token
27
24
  response.status.should == 201
28
25
  json_response.should have_attributes(attributes)
29
26
  json_response["variant"]["name"].should_not be_blank
30
27
  end
31
28
 
32
29
  it "can add a new line item to an existing order with token in header" do
33
- request.headers["X-Spree-Order-Token"] = order.token
30
+ request.headers["X-Spree-Order-Token"] = order.guest_token
34
31
  api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }
35
32
  response.status.should == 201
36
33
  json_response.should have_attributes(attributes)
@@ -40,7 +37,8 @@ module Spree
40
37
 
41
38
  context "as the order owner" do
42
39
  before do
43
- allow_any_instance_of(Order).to receive_messages :user => current_api_user
40
+ controller.stub :try_spree_current_user => current_api_user
41
+ Order.any_instance.stub :user => current_api_user
44
42
  end
45
43
 
46
44
  it "can add a new line item to an existing order" do
@@ -50,6 +48,13 @@ module Spree
50
48
  json_response["variant"]["name"].should_not be_blank
51
49
  end
52
50
 
51
+ it "default quantity to 1 if none is given" do
52
+ api_post :create, :line_item => { :variant_id => product.master.to_param }
53
+ response.status.should == 201
54
+ json_response.should have_attributes(attributes)
55
+ expect(json_response[:quantity]).to eq 1
56
+ end
57
+
53
58
  it "increases a line item's quantity if it exists already" do
54
59
  order.line_items.create(:variant_id => product.master.id, :quantity => 10)
55
60
  api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }
@@ -121,8 +126,7 @@ module Spree
121
126
  context "as just another user" do
122
127
  before do
123
128
  user = create(:user)
124
- allow(Spree.user_class).to receive(:find_by).
125
- and_return(user)
129
+ controller.stub :try_spree_current_user => user
126
130
  end
127
131
 
128
132
  it "cannot add a new line item to the order" do
@@ -26,8 +26,6 @@ module Spree
26
26
  :city => "Sao Paulo", :zipcode => "1234567", :phone => "12345678",
27
27
  :country_id => Country.first.id, :state_id => State.first.id} }
28
28
 
29
- let!(:payment_method) { create(:check_payment_method) }
30
-
31
29
  let(:current_api_user) do
32
30
  user = Spree.user_class.new(:email => "spree@example.com")
33
31
  user.generate_spree_api_key!
@@ -75,18 +73,6 @@ module Spree
75
73
  response.status.should == 200
76
74
  json_response["orders"].length.should == 0
77
75
  end
78
-
79
- it "returns orders in reverse chronological order" do
80
- order2 = create(:order, line_items: [line_item], user: order.user)
81
- order2.created_at.should > order.created_at
82
-
83
- api_get :mine
84
- response.status.should == 200
85
- json_response["pages"].should == 1
86
- json_response["orders"].length.should == 2
87
- json_response["orders"][0]["number"].should == order2.number
88
- json_response["orders"][1]["number"].should == order.number
89
- end
90
76
  end
91
77
 
92
78
  it "can view their own order" do
@@ -95,7 +81,6 @@ module Spree
95
81
  response.status.should == 200
96
82
  json_response.should have_attributes(attributes)
97
83
  json_response["adjustments"].should be_empty
98
- json_response["credit_cards"].should be_empty
99
84
  end
100
85
 
101
86
  it "orders contain the basic checkout steps" do
@@ -119,12 +104,12 @@ module Spree
119
104
  end
120
105
 
121
106
  it "can view an order if the token is known" do
122
- api_get :show, :id => order.to_param, :order_token => order.token
107
+ api_get :show, :id => order.to_param, :order_token => order.guest_token
123
108
  response.status.should == 200
124
109
  end
125
110
 
126
111
  it "can view an order if the token is passed in header" do
127
- request.headers["X-Spree-Order-Token"] = order.token
112
+ request.headers["X-Spree-Order-Token"] = order.guest_token
128
113
  api_get :show, :id => order.to_param
129
114
  response.status.should == 200
130
115
  end
@@ -135,10 +120,9 @@ module Spree
135
120
 
136
121
  it "can view an order" do
137
122
  user = mock_model(Spree::LegacyUser)
138
- allow(user).to receive_message_chain(:spree_roles, :pluck).and_return(["bar"])
139
- allow(user).to receive(:has_spree_role?).with('bar').and_return(true)
140
- allow(user).to receive(:has_spree_role?).with('admin').and_return(false)
141
- allow(Spree.user_class).to receive_messages find_by: user
123
+ user.stub(:has_spree_role?).with('bar').and_return(true)
124
+ user.stub(:has_spree_role?).with('admin').and_return(false)
125
+ controller.stub try_spree_current_user: user
142
126
  api_get :show, :id => order.to_param
143
127
  response.status.should == 200
144
128
  end
@@ -159,10 +143,13 @@ module Spree
159
143
  it "can create an order" do
160
144
  api_post :create, :order => { :line_items => { "0" => { :variant_id => variant.to_param, :quantity => 5 } } }
161
145
  response.status.should == 201
146
+
162
147
  order = Order.last
163
148
  order.line_items.count.should == 1
164
149
  order.line_items.first.variant.should == variant
165
150
  order.line_items.first.quantity.should == 5
151
+
152
+ json_response['number'].should be_present
166
153
  json_response["token"].should_not be_blank
167
154
  json_response["state"].should == "cart"
168
155
  order.user.should == current_api_user
@@ -170,12 +157,18 @@ module Spree
170
157
  json_response["user_id"].should == current_api_user.id
171
158
  end
172
159
 
160
+ it "assigns email when creating a new order" do
161
+ api_post :create, :order => { :email => "guest@spreecommerce.com" }
162
+ expect(json_response['email']).not_to eq controller.current_api_user
163
+ expect(json_response['email']).to eq "guest@spreecommerce.com"
164
+ end
165
+
173
166
  # Regression test for #3404
174
167
  it "can specify additional parameters for a line item" do
175
168
  Order.should_receive(:create!).and_return(order = Spree::Order.new)
176
169
  order.stub(:associate_user!)
177
170
  order.stub_chain(:contents, :add).and_return(line_item = double('LineItem'))
178
- line_item.should_receive(:update_attributes!).with("special" => true)
171
+ line_item.should_receive(:update_attributes).with("special" => true)
179
172
 
180
173
  controller.stub(permitted_line_item_attributes: [:id, :variant_id, :quantity, :special])
181
174
  api_post :create, :order => {
@@ -274,7 +267,7 @@ module Spree
274
267
  it "updates quantities of existing line items" do
275
268
  api_put :update, :id => order.to_param, :order => {
276
269
  :line_items => {
277
- 0 => { :id => line_item.id, :quantity => 10 }
270
+ "0" => { :id => line_item.id, :quantity => 10 }
278
271
  }
279
272
  }
280
273
 
@@ -287,8 +280,8 @@ module Spree
287
280
  variant2 = create(:variant)
288
281
  api_put :update, :id => order.to_param, :order => {
289
282
  :line_items => {
290
- 0 => { :id => line_item.id, :quantity => 10 },
291
- 1 => { :variant_id => variant2.id, :quantity => 1}
283
+ "0" => { :id => line_item.id, :quantity => 10 },
284
+ "1" => { :variant_id => variant2.id, :quantity => 1}
292
285
  }
293
286
  }
294
287
 
@@ -347,6 +340,14 @@ module Spree
347
340
  json_response['errors']['ship_address.firstname'].first.should eq "can't be blank"
348
341
  end
349
342
 
343
+ it "cannot set the user_id for the order" do
344
+ user = Spree.user_class.create
345
+ original_id = order.user_id
346
+ api_post :update, :id => order.to_param, :order => { user_id: user.id }
347
+ expect(response.status).to eq 200
348
+ json_response["user_id"].should == original_id
349
+ end
350
+
350
351
  context "order has shipments" do
351
352
  before { order.create_proposed_shipments }
352
353
 
@@ -413,6 +414,18 @@ module Spree
413
414
  adjustment['amount'].should == "5.0"
414
415
  end
415
416
 
417
+ it "lists payments source" do
418
+ order.payments.push payment = create(:payment)
419
+ api_get :show, :id => order.to_param
420
+
421
+ source = json_response[:payments].first[:source]
422
+ expect(source[:name]).to eq payment.source.name
423
+ expect(source[:cc_type]).to eq payment.source.cc_type
424
+ expect(source[:last_digits]).to eq payment.source.last_digits
425
+ expect(source[:month].to_i).to eq payment.source.month
426
+ expect(source[:year].to_i).to eq payment.source.year
427
+ end
428
+
416
429
  context "when in delivery" do
417
430
  let!(:shipping_method) do
418
431
  FactoryGirl.create(:shipping_method).tap do |shipping_method|
@@ -454,13 +467,13 @@ module Spree
454
467
  shipping_rate = shipment["shipping_rates"][0]
455
468
  shipping_rate["name"].should == json_shipping_method["name"]
456
469
  shipping_rate["cost"].should == "10.0"
457
- shipping_rate["selected"].should be true
470
+ shipping_rate["selected"].should be_true
458
471
  shipping_rate["display_cost"].should == "$10.00"
459
472
 
460
473
  shipment["stock_location_name"].should_not be_blank
461
474
  manifest_item = shipment["manifest"][0]
462
475
  manifest_item["quantity"].should == 1
463
- manifest_item["variant"].should have_attributes([:id, :name, :sku, :price])
476
+ manifest_item["variant_id"].should == order.line_items.first.variant_id
464
477
  end
465
478
  end
466
479
  end
@@ -479,9 +492,9 @@ module Spree
479
492
 
480
493
  it "responds with orders updated_at with miliseconds precision" do
481
494
  if ActiveRecord::Base.connection.adapter_name == "Mysql2"
482
- skip "MySQL does not support millisecond timestamps."
495
+ pending "MySQL does not support millisecond timestamps."
483
496
  else
484
- skip "Probable need to make it call as_json. See https://github.com/rails/rails/commit/0f33d70e89991711ff8b3dde134a61f4a5a0ec06"
497
+ pending "Probable need to make it call as_json. See https://github.com/rails/rails/commit/0f33d70e89991711ff8b3dde134a61f4a5a0ec06"
485
498
  end
486
499
 
487
500
  api_get :index
@@ -499,9 +512,9 @@ module Spree
499
512
  it "returns unique orders" do
500
513
  api_get :index
501
514
 
502
- orders = json_response["orders"]
515
+ orders = json_response[:orders]
503
516
  expect(orders.count).to be >= 3
504
- expect(orders.map { |o| o["id"] }.sort).to eq(Order.order(:id).pluck(:id))
517
+ expect(orders.map { |o| o[:id] }).to match_array Order.pluck(:id)
505
518
  end
506
519
 
507
520
  after { ActionController::Base.perform_caching = false }
@@ -563,6 +576,15 @@ module Spree
563
576
  end
564
577
  end
565
578
 
579
+ context "updating" do
580
+ it "can set the user_id for the order" do
581
+ user = Spree.user_class.create
582
+ api_post :update, :id => order.number, :order => { user_id: user.id }
583
+ expect(response.status).to eq 200
584
+ json_response["user_id"].should == user.id
585
+ end
586
+ end
587
+
566
588
  context "can cancel an order" do
567
589
  before do
568
590
  Spree::Config[:mails_from] = "spree@example.com"
@@ -6,7 +6,7 @@ module Spree
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 } }
@@ -64,11 +64,6 @@ module Spree
64
64
  api_get :index, :order_id => order.to_param
65
65
  assert_unauthorized!
66
66
  end
67
-
68
- it "can view the payments for an order given the order token" do
69
- api_get :index, :order_id => order.to_param, :order_token => order.token
70
- json_response["payments"].first.should have_attributes(attributes)
71
- end
72
67
  end
73
68
  end
74
69
 
@@ -82,7 +77,7 @@ module Spree
82
77
  end
83
78
 
84
79
  context "multiple payments" do
85
- before { @payment = create(:payment, :order => order) }
80
+ before { @payment = create(:payment, :order => order, :response_code => '99999') }
86
81
 
87
82
  it "can view all payments on an order" do
88
83
  api_get :index
@@ -95,6 +90,12 @@ module Spree
95
90
  json_response['current_page'].should == 1
96
91
  json_response['pages'].should == 2
97
92
  end
93
+
94
+ it 'can query the results through a paramter' do
95
+ api_get :index, :q => { :response_code_cont => '999' }
96
+ json_response['count'].should == 1
97
+ json_response['payments'].first['response_code'].should eq @payment.response_code
98
+ end
98
99
  end
99
100
 
100
101
  context "for a given payment" do
@@ -143,7 +144,7 @@ module Spree
143
144
  end
144
145
 
145
146
  it "does not raise a stack level error" do
146
- skip "Investigate why a payment.reload after the request raises 'stack level too deep'"
147
+ pending "Investigate why a payment.reload after the request raises 'stack level too deep'"
147
148
  payment.reload.state.should == "failed"
148
149
  end
149
150
  end
@@ -169,7 +169,7 @@ module Spree
169
169
  context "tracking is disabled" do
170
170
  before { Config.track_inventory_levels = false }
171
171
 
172
- it "still displays valid json with total on hand" do
172
+ it "still displays valid json with total_on_hand Float::INFINITY" do
173
173
  api_get :show, :id => product.to_param
174
174
  expect(response).to be_ok
175
175
  expect(json_response[:total_on_hand]).to eq nil