spree_api 2.1.12 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +51 -1
- data/app/controllers/spree/api/addresses_controller.rb +25 -10
- data/app/controllers/spree/api/base_controller.rb +18 -29
- data/app/controllers/spree/api/checkouts_controller.rb +23 -29
- data/app/controllers/spree/api/classifications_controller.rb +18 -0
- data/app/controllers/spree/api/line_items_controller.rb +13 -1
- data/app/controllers/spree/api/option_types_controller.rb +2 -2
- data/app/controllers/spree/api/option_values_controller.rb +1 -1
- data/app/controllers/spree/api/orders_controller.rb +12 -34
- data/app/controllers/spree/api/products_controller.rb +22 -23
- data/app/controllers/spree/api/taxons_controller.rb +9 -0
- data/app/controllers/spree/api/variants_controller.rb +1 -1
- data/app/helpers/spree/api/api_helpers.rb +5 -3
- data/app/models/spree/order_decorator.rb +2 -11
- data/app/views/spree/api/line_items/show.v1.rabl +4 -0
- data/app/views/spree/api/orders/order.v1.rabl +0 -1
- data/app/views/spree/api/products/show.v1.rabl +7 -10
- data/app/views/spree/api/promotions/handler.v1.rabl +4 -0
- data/app/views/spree/api/variants/index.v1.rabl +1 -1
- data/app/views/spree/api/variants/show.v1.rabl +2 -3
- data/app/views/spree/api/variants/variant.v1.rabl +2 -0
- data/app/views/spree/api/variants/{big_variant.v1.rabl → variant_full.v1.rabl} +0 -0
- data/config/routes.rb +6 -4
- data/lib/spree/api/controller_setup.rb +0 -1
- data/lib/spree/api/testing_support/helpers.rb +1 -0
- data/spec/controllers/spree/api/addresses_controller_spec.rb +57 -31
- data/spec/controllers/spree/api/base_controller_spec.rb +0 -36
- data/spec/controllers/spree/api/checkouts_controller_spec.rb +8 -41
- data/spec/controllers/spree/api/classifications_controller_spec.rb +38 -0
- data/spec/controllers/spree/api/line_items_controller_spec.rb +10 -21
- data/spec/controllers/spree/api/option_values_controller_spec.rb +0 -7
- data/spec/controllers/spree/api/orders_controller_spec.rb +24 -121
- data/spec/controllers/spree/api/products_controller_spec.rb +26 -48
- data/spec/controllers/spree/api/promotion_application_spec.rb +48 -0
- data/spec/controllers/spree/api/shipments_controller_spec.rb +1 -22
- data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +1 -1
- data/spec/controllers/spree/api/users_controller_spec.rb +3 -2
- data/spec/controllers/spree/api/variants_controller_spec.rb +13 -7
- data/spec/models/spree/order_spec.rb +3 -28
- metadata +12 -7
- data/app/views/spree/api/orders/apply_coupon_code.v1.rabl +0 -7
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'spree/promo/coupon_applicator'
|
3
2
|
|
4
3
|
module Spree
|
5
4
|
describe Api::CheckoutsController do
|
@@ -15,23 +14,13 @@ module Spree
|
|
15
14
|
create(:stock_location)
|
16
15
|
|
17
16
|
@shipping_method = create(:shipping_method, :zones => [country_zone])
|
18
|
-
@payment_method = create(:
|
17
|
+
@payment_method = create(:credit_card_payment_method)
|
19
18
|
end
|
20
19
|
|
21
20
|
after do
|
22
21
|
Spree::Config[:track_inventory_levels] = true
|
23
22
|
end
|
24
23
|
|
25
|
-
context "GET 'show'" do
|
26
|
-
let(:order) { create(:order) }
|
27
|
-
|
28
|
-
it "redirects to Orders#show" do
|
29
|
-
api_get :show, :id => order.number
|
30
|
-
response.status.should == 301
|
31
|
-
response.should redirect_to("/api/orders/#{order.number}")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
24
|
context "POST 'create'" do
|
36
25
|
it "creates a new order when no parameters are passed" do
|
37
26
|
api_post :create
|
@@ -56,28 +45,12 @@ module Spree
|
|
56
45
|
order
|
57
46
|
end
|
58
47
|
|
48
|
+
|
59
49
|
before(:each) do
|
60
50
|
Order.any_instance.stub(:confirmation_required? => true)
|
61
51
|
Order.any_instance.stub(:payment_required? => true)
|
62
52
|
end
|
63
53
|
|
64
|
-
it 'should not allow users to change the price of line items' do
|
65
|
-
line_item = order.line_items.first
|
66
|
-
price_was = line_item.price
|
67
|
-
api_put(
|
68
|
-
:update,
|
69
|
-
id: order.to_param,
|
70
|
-
order_token: order.token,
|
71
|
-
order: {
|
72
|
-
line_items: {0 => {id: line_item.id, price: '0.1', quantity: '3'}}
|
73
|
-
}
|
74
|
-
)
|
75
|
-
response.status.should == 200
|
76
|
-
line_item.reload
|
77
|
-
expect(line_item.price).to eq price_was
|
78
|
-
expect(line_item.price).to_not eq 0.1
|
79
|
-
end
|
80
|
-
|
81
54
|
it "should transition a recently created order from cart to address" do
|
82
55
|
order.state.should eq "cart"
|
83
56
|
order.email.should_not be_nil
|
@@ -174,7 +147,8 @@ module Spree
|
|
174
147
|
"number" => "4111111111111111",
|
175
148
|
"month" => 1.month.from_now.month,
|
176
149
|
"year" => 1.month.from_now.year,
|
177
|
-
"verification_value" => "123"
|
150
|
+
"verification_value" => "123",
|
151
|
+
"name" => "Spree Commerce"
|
178
152
|
}
|
179
153
|
|
180
154
|
api_put :update, :id => order.to_param, :order_token => order.token,
|
@@ -189,8 +163,7 @@ module Spree
|
|
189
163
|
order.update_column(:state, "payment")
|
190
164
|
api_put :update, :id => order.to_param, :order_token => order.token,
|
191
165
|
:order => { :payments_attributes => [{ :payment_method_id => @payment_method.id.to_s }],
|
192
|
-
:payment_source => { @payment_method.id.to_s => {
|
193
|
-
|
166
|
+
:payment_source => { @payment_method.id.to_s => { } } }
|
194
167
|
response.status.should == 422
|
195
168
|
cc_errors = json_response['errors']['payments.Credit Card']
|
196
169
|
cc_errors.should include("Number can't be blank")
|
@@ -242,17 +215,11 @@ module Spree
|
|
242
215
|
end
|
243
216
|
|
244
217
|
it "can apply a coupon code to an order" do
|
245
|
-
order
|
246
|
-
Spree::Promo::CouponApplicator.should_receive(:new).with(order).and_call_original
|
247
|
-
Spree::Promo::CouponApplicator.any_instance.should_receive(:apply).and_return({:coupon_applied? => true})
|
248
|
-
api_put :update, :id => order.to_param, :order_token => order.token, :order => { :coupon_code => "foobar" }
|
249
|
-
end
|
218
|
+
pending "ensure that the order totals are properly updated, see frontend orders_controller or checkout_controller as example"
|
250
219
|
|
251
|
-
it "can apply a coupon code to an order" do
|
252
220
|
order.update_column(:state, "payment")
|
253
|
-
|
254
|
-
|
255
|
-
Spree::Promo::CouponApplicator.any_instance.should_receive(:apply).and_return(coupon_result)
|
221
|
+
PromotionHandler::Coupon.should_receive(:new).with(order).and_call_original
|
222
|
+
PromotionHandler::Coupon.any_instance.should_receive(:apply).and_return({:coupon_applied? => true})
|
256
223
|
api_put :update, :id => order.to_param, :order_token => order.token, :order => { :coupon_code => "foobar" }
|
257
224
|
end
|
258
225
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
describe Api::ClassificationsController do
|
5
|
+
let(:taxon) do
|
6
|
+
taxon = create(:taxon)
|
7
|
+
3.times do
|
8
|
+
product = create(:product)
|
9
|
+
product.taxons << taxon
|
10
|
+
end
|
11
|
+
taxon
|
12
|
+
end
|
13
|
+
|
14
|
+
before do
|
15
|
+
stub_authentication!
|
16
|
+
end
|
17
|
+
|
18
|
+
context "as a user" do
|
19
|
+
it "cannot change the order of a product" do
|
20
|
+
api_put :update, :taxon_id => taxon, :product_id => taxon.products.first, :position => 1
|
21
|
+
response.status.should == 401
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "as an admin" do
|
26
|
+
sign_in_as_admin!
|
27
|
+
|
28
|
+
it "can change the order a product" do
|
29
|
+
last_product = taxon.products.last
|
30
|
+
classification = taxon.classifications.find_by(:product_id => last_product.id)
|
31
|
+
classification.position.should == 3
|
32
|
+
api_put :update, :taxon_id => taxon, :product_id => last_product, :position => 0
|
33
|
+
response.status.should == 200
|
34
|
+
classification.reload.position.should == 1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -10,8 +10,11 @@ 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
|
+
|
13
17
|
it "can learn how to create a new line item" do
|
14
|
-
controller.stub :try_spree_current_user => current_api_user
|
15
18
|
api_get :new
|
16
19
|
json_response["attributes"].should == ["quantity", "price", "variant_id"]
|
17
20
|
required_attributes = json_response["required_attributes"]
|
@@ -37,7 +40,6 @@ module Spree
|
|
37
40
|
|
38
41
|
context "as the order owner" do
|
39
42
|
before do
|
40
|
-
controller.stub :try_spree_current_user => current_api_user
|
41
43
|
Order.any_instance.stub :user => current_api_user
|
42
44
|
end
|
43
45
|
|
@@ -60,15 +62,20 @@ module Spree
|
|
60
62
|
|
61
63
|
it "can update a line item on the order" do
|
62
64
|
line_item = order.line_items.first
|
63
|
-
api_put :update, :id => line_item.id, :line_item => { :quantity =>
|
65
|
+
api_put :update, :id => line_item.id, :line_item => { :quantity => 101 }
|
64
66
|
response.status.should == 200
|
67
|
+
order.reload
|
68
|
+
order.total.should == 1050 # 50 original due to factory, + 1000 in this test
|
65
69
|
json_response.should have_attributes(attributes)
|
70
|
+
json_response["quantity"].should == 101
|
66
71
|
end
|
67
72
|
|
68
73
|
it "can delete a line item on the order" do
|
69
74
|
line_item = order.line_items.first
|
70
75
|
api_delete :destroy, :id => line_item.id
|
71
76
|
response.status.should == 204
|
77
|
+
order.reload
|
78
|
+
order.line_items.count.should == 4 # 5 original due to factory, - 1 in this test
|
72
79
|
lambda { line_item.reload }.should raise_error(ActiveRecord::RecordNotFound)
|
73
80
|
end
|
74
81
|
|
@@ -95,28 +102,10 @@ module Spree
|
|
95
102
|
api_delete :destroy, :id => line_item.id
|
96
103
|
expect(order.reload.shipments).to be_empty
|
97
104
|
end
|
98
|
-
|
99
|
-
context "order is completed" do
|
100
|
-
before do
|
101
|
-
order.stub completed?: true
|
102
|
-
Order.stub find_by!: order
|
103
|
-
end
|
104
|
-
|
105
|
-
it "doesn't destroy shipments or restart checkout flow" do
|
106
|
-
expect(order.reload.shipments).not_to be_empty
|
107
|
-
api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }
|
108
|
-
expect(order.reload.shipments).not_to be_empty
|
109
|
-
end
|
110
|
-
end
|
111
105
|
end
|
112
106
|
end
|
113
107
|
|
114
108
|
context "as just another user" do
|
115
|
-
before do
|
116
|
-
user = create(:user)
|
117
|
-
controller.stub :try_spree_current_user => user
|
118
|
-
end
|
119
|
-
|
120
109
|
it "cannot add a new line item to the order" do
|
121
110
|
api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }
|
122
111
|
assert_unauthorized!
|
@@ -111,13 +111,6 @@ module Spree
|
|
111
111
|
option_value.name.should == "Option Value"
|
112
112
|
end
|
113
113
|
|
114
|
-
it "permits the correct attributes" do
|
115
|
-
controller.should_receive(:permitted_option_value_attributes)
|
116
|
-
api_put :update, :id => option_value.id, :option_value => {
|
117
|
-
:name => ""
|
118
|
-
}
|
119
|
-
end
|
120
|
-
|
121
114
|
it "cannot update an option value with invalid attributes" do
|
122
115
|
api_put :update, :id => option_value.id, :option_value => {
|
123
116
|
:name => ""
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'spree/testing_support/bar_ability'
|
3
2
|
|
4
3
|
module Spree
|
5
4
|
describe Api::OrdersController do
|
@@ -14,7 +13,7 @@ module Spree
|
|
14
13
|
:user_id, :created_at, :updated_at,
|
15
14
|
:completed_at, :payment_total, :shipment_state,
|
16
15
|
:payment_state, :email, :special_instructions,
|
17
|
-
:total_quantity, :display_item_total
|
16
|
+
:total_quantity, :display_item_total] }
|
18
17
|
|
19
18
|
let(:address_params) { { :country_id => Country.first.id, :state_id => State.first.id } }
|
20
19
|
|
@@ -26,7 +25,7 @@ module Spree
|
|
26
25
|
:city => "Sao Paulo", :zipcode => "1234567", :phone => "12345678",
|
27
26
|
:country_id => Country.first.id, :state_id => State.first.id} }
|
28
27
|
|
29
|
-
let!(:payment_method) { create(:
|
28
|
+
let!(:payment_method) { create(:check_payment_method) }
|
30
29
|
|
31
30
|
let(:current_api_user) do
|
32
31
|
user = Spree.user_class.new(:email => "spree@example.com")
|
@@ -117,20 +116,6 @@ module Spree
|
|
117
116
|
response.status.should == 200
|
118
117
|
end
|
119
118
|
|
120
|
-
context "with BarAbility registered" do
|
121
|
-
before { Spree::Ability.register_ability(::BarAbility) }
|
122
|
-
after { Spree::Ability.remove_ability(::BarAbility) }
|
123
|
-
|
124
|
-
it "can view an order" do
|
125
|
-
user = mock_model(Spree::LegacyUser)
|
126
|
-
user.stub(:has_spree_role?).with('bar').and_return(true)
|
127
|
-
user.stub(:has_spree_role?).with('admin').and_return(false)
|
128
|
-
controller.stub try_spree_current_user: user
|
129
|
-
api_get :show, :id => order.to_param
|
130
|
-
response.status.should == 200
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
119
|
it "cannot cancel an order that doesn't belong to them" do
|
135
120
|
order.update_attribute(:completed_at, Time.now)
|
136
121
|
order.update_attribute(:shipment_state, "ready")
|
@@ -188,45 +173,13 @@ module Spree
|
|
188
173
|
expect(Order.last.line_items.first.price.to_f).to eq(variant.price)
|
189
174
|
end
|
190
175
|
|
191
|
-
context "
|
192
|
-
|
193
|
-
let(:other_variant) { create(:variant) }
|
194
|
-
|
195
|
-
let(:order_params) do
|
196
|
-
{
|
197
|
-
:line_items => {
|
198
|
-
"0" => { :variant_id => variant.to_param, :quantity => 5 },
|
199
|
-
"1" => { :variant_id => other_variant.to_param, :quantity => 5 }
|
200
|
-
}
|
201
|
-
}
|
202
|
-
end
|
203
|
-
|
204
|
-
before do
|
205
|
-
Zone.stub default_tax: tax_rate.zone
|
206
|
-
current_api_user.stub has_spree_role?: true
|
207
|
-
end
|
176
|
+
context "admin user imports order" do
|
177
|
+
before { current_api_user.stub has_spree_role?: true }
|
208
178
|
|
209
179
|
it "sets channel" do
|
210
180
|
api_post :create, :order => { channel: "amazon" }
|
211
181
|
expect(json_response['channel']).to eq "amazon"
|
212
182
|
end
|
213
|
-
|
214
|
-
it "doesnt persist any automatic tax adjustment" do
|
215
|
-
expect {
|
216
|
-
api_post :create, :order => order_params.merge(:import => true)
|
217
|
-
}.not_to change { Adjustment.count }
|
218
|
-
|
219
|
-
expect(response.status).to eq 201
|
220
|
-
end
|
221
|
-
|
222
|
-
it "doesnt blow up when passing a sku into line items hash" do
|
223
|
-
order_params[:line_items]["0"][:sku] = variant.sku
|
224
|
-
order_params[:line_items]["0"][:variant_id] = nil
|
225
|
-
order_params[:line_items]["1"][:sku] = other_variant.sku
|
226
|
-
|
227
|
-
api_post :create, :order => order_params
|
228
|
-
expect(response.status).to eq 201
|
229
|
-
end
|
230
183
|
end
|
231
184
|
|
232
185
|
# Regression test for #3404
|
@@ -255,7 +208,7 @@ module Spree
|
|
255
208
|
|
256
209
|
let(:variant) { create(:variant) }
|
257
210
|
let!(:line_item) { order.contents.add(variant, 1) }
|
258
|
-
let!(:payment_method) { create(:
|
211
|
+
let!(:payment_method) { create(:check_payment_method) }
|
259
212
|
|
260
213
|
let(:address_params) { { :country_id => Country.first.id, :state_id => State.first.id } }
|
261
214
|
let(:billing_address) { { :firstname => "Tiago", :lastname => "Motta", :address1 => "Av Paulista",
|
@@ -413,8 +366,23 @@ module Spree
|
|
413
366
|
it "includes the tax_total in the response" do
|
414
367
|
api_get :show, :id => order.to_param
|
415
368
|
|
416
|
-
json_response['
|
417
|
-
json_response['
|
369
|
+
json_response['included_tax_total'].should == '0.0'
|
370
|
+
json_response['additional_tax_total'].should == '0.0'
|
371
|
+
json_response['display_included_tax_total'].should == '$0.00'
|
372
|
+
json_response['display_additional_tax_total'].should == '$0.00'
|
373
|
+
end
|
374
|
+
|
375
|
+
it "lists line item adjustments" do
|
376
|
+
adjustment = create(:adjustment,
|
377
|
+
:label => "10% off!",
|
378
|
+
:order => order,
|
379
|
+
:adjustable => order.line_items.first)
|
380
|
+
adjustment.update_column(:amount, 5)
|
381
|
+
api_get :show, :id => order.to_param
|
382
|
+
|
383
|
+
adjustment = json_response['line_items'].first['adjustments'].first
|
384
|
+
adjustment['label'].should == "10% off!"
|
385
|
+
adjustment['amount'].should == "5.0"
|
418
386
|
end
|
419
387
|
|
420
388
|
context "when in delivery" do
|
@@ -434,8 +402,8 @@ module Spree
|
|
434
402
|
it "includes the ship_total in the response" do
|
435
403
|
api_get :show, :id => order.to_param
|
436
404
|
|
437
|
-
json_response['ship_total'].should == '
|
438
|
-
json_response['display_ship_total'].should == '$
|
405
|
+
json_response['ship_total'].should == '0.0'
|
406
|
+
json_response['display_ship_total'].should == '$0.00'
|
439
407
|
end
|
440
408
|
|
441
409
|
it "returns available shipments for an order" do
|
@@ -564,71 +532,6 @@ module Spree
|
|
564
532
|
end
|
565
533
|
end
|
566
534
|
end
|
567
|
-
|
568
|
-
describe 'apply_promo_code' do
|
569
|
-
# Borrowed from:
|
570
|
-
# https://github.com/spree/spree/blob/master/api/spec/controllers/spree/api/promotion_application_spec.rb
|
571
|
-
|
572
|
-
let(:order) do
|
573
|
-
order = create(
|
574
|
-
:order_with_line_items,
|
575
|
-
state: 'payment',
|
576
|
-
user: current_api_user,
|
577
|
-
line_items_count: 1
|
578
|
-
)
|
579
|
-
end
|
580
|
-
|
581
|
-
subject do
|
582
|
-
api_put :apply_coupon_code, id: order.to_param, coupon_code: coupon_code, :order_token => order.token
|
583
|
-
end
|
584
|
-
|
585
|
-
context 'when valid promo code' do
|
586
|
-
let(:coupon_code) { 'some_code' }
|
587
|
-
|
588
|
-
# Create a promotion for testing.
|
589
|
-
# https://github.com/spree/spree/blob/2-1-stable/core/spec/lib/spree/promo/coupon_applicator_spec.rb
|
590
|
-
before(:each) do
|
591
|
-
flat_percent_calc = Spree::Calculator::FlatPercentItemTotal.create(:preferred_flat_percent => "10")
|
592
|
-
promo = Spree::Promotion.create(:name => "Discount", :event_name => "spree.checkout.coupon_code_added", :code => coupon_code, :usage_limit => "10", :starts_at => DateTime.yesterday, :expires_at => DateTime.tomorrow)
|
593
|
-
promo_rule = Spree::Promotion::Rules::ItemTotal.create(:preferred_operator => "gt", :preferred_amount => "1")
|
594
|
-
promo_rule.update_attribute(:activator_id, promo.id)
|
595
|
-
promo_action = Spree::Promotion::Actions::CreateAdjustment.create(:calculator_type => "Spree::Calculator::FlatPercentItemTotal")
|
596
|
-
promo_action.update_attribute(:activator_id, promo.id)
|
597
|
-
flat_percent_calc.update_attribute(:calculable_id, promo.id)
|
598
|
-
Spree::Order.any_instance.stub(:payment_required? => false)
|
599
|
-
Spree::Adjustment.any_instance.stub(:eligible => true)
|
600
|
-
end
|
601
|
-
|
602
|
-
it 'should should be_ok' do
|
603
|
-
subject
|
604
|
-
response.should be_ok
|
605
|
-
end
|
606
|
-
|
607
|
-
it 'should have success JSON' do
|
608
|
-
subject
|
609
|
-
result = JSON.parse(response.body)
|
610
|
-
result['successful'].should be_true
|
611
|
-
result['success'].should == "The coupon code was successfully applied to your order."
|
612
|
-
result['error'].should be_nil
|
613
|
-
end
|
614
|
-
end
|
615
|
-
|
616
|
-
context 'when invalid promo code' do
|
617
|
-
let(:coupon_code) { 'xxxxx' }
|
618
|
-
it 'should should be_ok ' do
|
619
|
-
subject
|
620
|
-
response.code.to_i.should == 422
|
621
|
-
end
|
622
|
-
|
623
|
-
it 'should have error JSON' do
|
624
|
-
subject
|
625
|
-
result = JSON.parse(response.body)
|
626
|
-
result['successful'].should be_false
|
627
|
-
result['error'].should == "The coupon code you entered doesn't exist. Please try again."
|
628
|
-
result['success'].should be_nil
|
629
|
-
end
|
630
|
-
end
|
631
|
-
end
|
632
535
|
end
|
633
536
|
end
|
634
537
|
|
@@ -7,7 +7,10 @@ module Spree
|
|
7
7
|
|
8
8
|
let!(:product) { create(:product) }
|
9
9
|
let!(:inactive_product) { create(:product, :available_on => Time.now.tomorrow, :name => "inactive") }
|
10
|
-
let(:
|
10
|
+
let(:base_attributes) { [:id, :name, :description, :price, :display_price, :available_on, :slug, :meta_description, :meta_keywords, :shipping_category_id, :taxon_ids] }
|
11
|
+
let(:show_attributes) { base_attributes.dup.push(:has_variants) }
|
12
|
+
let(:new_attributes) { base_attributes }
|
13
|
+
|
11
14
|
let(:product_data) do
|
12
15
|
{ name: "The Other Product",
|
13
16
|
price: 19.99,
|
@@ -28,27 +31,9 @@ module Spree
|
|
28
31
|
end
|
29
32
|
|
30
33
|
context "as a normal user" do
|
31
|
-
context "with caching enabled" do
|
32
|
-
let!(:product_2) { create(:product) }
|
33
|
-
|
34
|
-
before do
|
35
|
-
ActionController::Base.perform_caching = true
|
36
|
-
end
|
37
|
-
|
38
|
-
it "returns unique products" do
|
39
|
-
api_get :index
|
40
|
-
product_ids = json_response["products"].map { |p| p["id"] }
|
41
|
-
expect(product_ids.uniq.count).to eq(product_ids.count)
|
42
|
-
end
|
43
|
-
|
44
|
-
after do
|
45
|
-
ActionController::Base.perform_caching = false
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
34
|
it "retrieves a list of products" do
|
50
35
|
api_get :index
|
51
|
-
json_response["products"].first.should have_attributes(
|
36
|
+
json_response["products"].first.should have_attributes(show_attributes)
|
52
37
|
json_response["total_count"].should == 1
|
53
38
|
json_response["current_page"].should == 1
|
54
39
|
json_response["pages"].should == 1
|
@@ -57,7 +42,7 @@ module Spree
|
|
57
42
|
|
58
43
|
it "retrieves a list of products by id" do
|
59
44
|
api_get :index, :ids => [product.id]
|
60
|
-
json_response["products"].first.should have_attributes(
|
45
|
+
json_response["products"].first.should have_attributes(show_attributes)
|
61
46
|
json_response["total_count"].should == 1
|
62
47
|
json_response["current_page"].should == 1
|
63
48
|
json_response["pages"].should == 1
|
@@ -76,8 +61,8 @@ module Spree
|
|
76
61
|
it "retrieves a list of products by ids string" do
|
77
62
|
second_product = create(:product)
|
78
63
|
api_get :index, :ids => [product.id, second_product.id].join(",")
|
79
|
-
json_response["products"].first.should have_attributes(
|
80
|
-
json_response["products"][1].should have_attributes(
|
64
|
+
json_response["products"].first.should have_attributes(show_attributes)
|
65
|
+
json_response["products"][1].should have_attributes(show_attributes)
|
81
66
|
json_response["total_count"].should == 2
|
82
67
|
json_response["current_page"].should == 1
|
83
68
|
json_response["pages"].should == 1
|
@@ -98,7 +83,7 @@ module Spree
|
|
98
83
|
it "can select the next page of products" do
|
99
84
|
second_product = create(:product)
|
100
85
|
api_get :index, :page => 2, :per_page => 1
|
101
|
-
json_response["products"].first.should have_attributes(
|
86
|
+
json_response["products"].first.should have_attributes(show_attributes)
|
102
87
|
json_response["total_count"].should == 2
|
103
88
|
json_response["current_page"].should == 2
|
104
89
|
json_response["pages"].should == 2
|
@@ -132,7 +117,7 @@ module Spree
|
|
132
117
|
it "can search for products" do
|
133
118
|
create(:product, :name => "The best product in the world")
|
134
119
|
api_get :index, :q => { :name_cont => "best" }
|
135
|
-
json_response["products"].first.should have_attributes(
|
120
|
+
json_response["products"].first.should have_attributes(show_attributes)
|
136
121
|
json_response["count"].should == 1
|
137
122
|
end
|
138
123
|
|
@@ -142,11 +127,12 @@ module Spree
|
|
142
127
|
product.variants.first.images.create!(:attachment => image("thinking-cat.jpg"))
|
143
128
|
product.set_property("spree", "rocks")
|
144
129
|
api_get :show, :id => product.to_param
|
145
|
-
json_response.should have_attributes(
|
130
|
+
json_response.should have_attributes(show_attributes)
|
146
131
|
json_response['variants'].first.should have_attributes([:name,
|
147
132
|
:is_master,
|
148
133
|
:price,
|
149
|
-
:images
|
134
|
+
:images,
|
135
|
+
:in_stock])
|
150
136
|
|
151
137
|
json_response['variants'].first['images'].first.should have_attributes([:attachment_file_name,
|
152
138
|
:attachment_width,
|
@@ -163,20 +149,20 @@ module Spree
|
|
163
149
|
end
|
164
150
|
|
165
151
|
|
166
|
-
context "finds a product by
|
167
|
-
let!(:other_product) { create(:product, :
|
152
|
+
context "finds a product by slug first then by id" do
|
153
|
+
let!(:other_product) { create(:product, :slug => "these-are-not-the-droids-you-are-looking-for") }
|
168
154
|
|
169
155
|
before do
|
170
|
-
product.
|
156
|
+
product.update_column(:slug, "#{other_product.id}-and-1-ways")
|
171
157
|
end
|
172
158
|
|
173
159
|
specify do
|
174
160
|
api_get :show, :id => product.to_param
|
175
|
-
json_response["
|
161
|
+
json_response["slug"].should =~ /and-1-ways/
|
176
162
|
product.destroy
|
177
163
|
|
178
164
|
api_get :show, :id => other_product.id
|
179
|
-
json_response["
|
165
|
+
json_response["slug"].should =~ /droids/
|
180
166
|
end
|
181
167
|
end
|
182
168
|
|
@@ -192,7 +178,7 @@ module Spree
|
|
192
178
|
|
193
179
|
it "can learn how to create a new product" do
|
194
180
|
api_get :new
|
195
|
-
json_response["attributes"].should ==
|
181
|
+
json_response["attributes"].should == new_attributes.map(&:to_s)
|
196
182
|
required_attributes = json_response["required_attributes"]
|
197
183
|
required_attributes.should include("name")
|
198
184
|
required_attributes.should include("price")
|
@@ -238,7 +224,7 @@ module Spree
|
|
238
224
|
api_post :create, :product => { :name => "The Other Product",
|
239
225
|
:price => 19.99,
|
240
226
|
:shipping_category_id => create(:shipping_category).id }
|
241
|
-
json_response.should have_attributes(
|
227
|
+
json_response.should have_attributes(base_attributes)
|
242
228
|
response.status.should == 201
|
243
229
|
end
|
244
230
|
|
@@ -249,9 +235,9 @@ module Spree
|
|
249
235
|
|
250
236
|
api_post :create, :product => product_data
|
251
237
|
expect(response.status).to eq 201
|
252
|
-
expect(json_response['variants'].count).to eq(3) # 1 master + 2 variants
|
253
238
|
|
254
|
-
variants = json_response['variants']
|
239
|
+
variants = json_response['variants']
|
240
|
+
expect(variants.count).to eq(2)
|
255
241
|
expect(variants.last['option_values'][0]['name']).to eq('small')
|
256
242
|
expect(variants.last['option_values'][0]['option_type_name']).to eq('size')
|
257
243
|
|
@@ -281,14 +267,6 @@ module Spree
|
|
281
267
|
expect(json_response['option_types'].count).to eq(2)
|
282
268
|
end
|
283
269
|
|
284
|
-
it "can create a new product" do
|
285
|
-
api_post :create, :product => { :name => "The Other Product",
|
286
|
-
:price => 19.99,
|
287
|
-
:shipping_category_id => create(:shipping_category).id }
|
288
|
-
json_response.should have_attributes(attributes)
|
289
|
-
response.status.should == 201
|
290
|
-
end
|
291
|
-
|
292
270
|
it "creates with shipping categories" do
|
293
271
|
hash = { :name => "The Other Product",
|
294
272
|
:price => 19.99,
|
@@ -326,7 +304,7 @@ module Spree
|
|
326
304
|
|
327
305
|
it "can still create a product" do
|
328
306
|
api_post :create, :product => product_data, :token => "fake"
|
329
|
-
json_response.should have_attributes(
|
307
|
+
json_response.should have_attributes(show_attributes)
|
330
308
|
response.status.should == 201
|
331
309
|
end
|
332
310
|
end
|
@@ -336,7 +314,7 @@ module Spree
|
|
336
314
|
response.status.should == 422
|
337
315
|
json_response["error"].should == "Invalid resource. Please fix errors and try again."
|
338
316
|
errors = json_response["errors"]
|
339
|
-
errors.delete("
|
317
|
+
errors.delete("slug") # Don't care about this one.
|
340
318
|
errors.keys.should =~ ["name", "price", "shipping_category_id"]
|
341
319
|
end
|
342
320
|
end
|
@@ -355,7 +333,7 @@ module Spree
|
|
355
333
|
it "can create new variants on a product" do
|
356
334
|
api_put :update, :id => product.to_param, :product => { :variants => [attributes_for_variant, attributes_for_variant] }
|
357
335
|
expect(response.status).to eq 200
|
358
|
-
expect(json_response['variants'].count).to eq(
|
336
|
+
expect(json_response['variants'].count).to eq(2) # 2 variants
|
359
337
|
|
360
338
|
variants = json_response['variants'].select { |v| !v['is_master'] }
|
361
339
|
expect(variants.last['option_values'][0]['name']).to eq('small')
|
@@ -380,7 +358,7 @@ module Spree
|
|
380
358
|
]
|
381
359
|
}
|
382
360
|
|
383
|
-
expect(json_response['variants'].count).to eq(
|
361
|
+
expect(json_response['variants'].count).to eq(1)
|
384
362
|
variants = json_response['variants'].select { |v| !v['is_master'] }
|
385
363
|
expect(variants.last['option_values'][0]['name']).to eq('large')
|
386
364
|
expect(variants.last['sku']).to eq('456')
|