spree_api 2.3.4 → 2.3.5
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.
- checksums.yaml +4 -4
- data/app/controllers/spree/api/base_controller.rb +2 -2
- data/app/controllers/spree/api/option_types_controller.rb +2 -2
- data/app/controllers/spree/api/shipments_controller.rb +2 -2
- data/app/controllers/spree/api/taxons_controller.rb +1 -1
- data/app/helpers/spree/api/api_helpers.rb +0 -2
- data/app/views/spree/api/shipments/small.v1.rabl +4 -0
- data/app/views/spree/api/variants/small.v1.rabl +2 -0
- data/lib/spree/api/testing_support/helpers.rb +6 -6
- data/lib/spree/api/testing_support/setup.rb +2 -2
- data/spec/controllers/spree/api/addresses_controller_spec.rb +7 -7
- data/spec/controllers/spree/api/base_controller_spec.rb +20 -20
- data/spec/controllers/spree/api/checkouts_controller_spec.rb +53 -53
- data/spec/controllers/spree/api/classifications_controller_spec.rb +5 -5
- data/spec/controllers/spree/api/config_controller_spec.rb +9 -9
- data/spec/controllers/spree/api/countries_controller_spec.rb +11 -11
- data/spec/controllers/spree/api/credit_cards_controller_spec.rb +16 -16
- data/spec/controllers/spree/api/images_controller_spec.rb +11 -11
- data/spec/controllers/spree/api/inventory_units_controller_spec.rb +6 -6
- data/spec/controllers/spree/api/line_items_controller_spec.rb +33 -33
- data/spec/controllers/spree/api/option_types_controller_spec.rb +18 -18
- data/spec/controllers/spree/api/option_values_controller_spec.rb +21 -21
- data/spec/controllers/spree/api/orders_controller_spec.rb +170 -132
- data/spec/controllers/spree/api/payments_controller_spec.rb +46 -46
- data/spec/controllers/spree/api/product_properties_controller_spec.rb +21 -21
- data/spec/controllers/spree/api/products_controller_spec.rb +67 -67
- data/spec/controllers/spree/api/promotion_application_spec.rb +11 -11
- data/spec/controllers/spree/api/properties_controller_spec.rb +25 -25
- data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +40 -40
- data/spec/controllers/spree/api/shipments_controller_spec.rb +53 -30
- data/spec/controllers/spree/api/states_controller_spec.rb +18 -18
- data/spec/controllers/spree/api/stock_items_controller_spec.rb +26 -26
- data/spec/controllers/spree/api/stock_locations_controller_spec.rb +22 -22
- data/spec/controllers/spree/api/stock_movements_controller_spec.rb +16 -16
- data/spec/controllers/spree/api/taxonomies_controller_spec.rb +24 -24
- data/spec/controllers/spree/api/taxons_controller_spec.rb +39 -39
- data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +5 -5
- data/spec/controllers/spree/api/users_controller_spec.rb +25 -25
- data/spec/controllers/spree/api/variants_controller_spec.rb +36 -36
- data/spec/controllers/spree/api/zones_controller_spec.rb +20 -20
- data/spec/models/spree/legacy_user_spec.rb +5 -5
- data/spec/requests/rabl_cache_spec.rb +9 -9
- data/spec/spec_helper.rb +0 -1
- metadata +4 -4
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module Spree
|
4
|
-
describe Spree::Api::PaymentsController do
|
4
|
+
describe Spree::Api::PaymentsController, :type => :controller do
|
5
5
|
render_views
|
6
6
|
let!(:order) { create(:order) }
|
7
7
|
let!(:payment) { create(:payment, :order => order) }
|
@@ -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
|
-
Order.
|
21
|
+
allow_any_instance_of(Order).to receive_messages :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
|
-
json_response["payments"].first.
|
26
|
+
expect(json_response["payments"].first).to 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
|
-
json_response["attributes"].
|
32
|
-
json_response["payment_methods"].
|
33
|
-
json_response["payment_methods"].first.
|
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])
|
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
|
-
response.status.
|
39
|
-
json_response.
|
38
|
+
expect(response.status).to eq(201)
|
39
|
+
expect(json_response).to 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
|
-
json_response.
|
44
|
+
expect(json_response).to 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
|
-
Order.
|
60
|
+
allow_any_instance_of(Order).to receive_messages :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
|
-
json_response["payments"].first.
|
70
|
+
expect(json_response["payments"].first).to have_attributes(attributes)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -77,8 +77,8 @@ module Spree
|
|
77
77
|
|
78
78
|
it "can view the payments on any order" do
|
79
79
|
api_get :index
|
80
|
-
response.status.
|
81
|
-
json_response["payments"].first.
|
80
|
+
expect(response.status).to eq(200)
|
81
|
+
expect(json_response["payments"].first).to have_attributes(attributes)
|
82
82
|
end
|
83
83
|
|
84
84
|
context "multiple payments" do
|
@@ -86,20 +86,20 @@ module Spree
|
|
86
86
|
|
87
87
|
it "can view all payments on an order" do
|
88
88
|
api_get :index
|
89
|
-
json_response["count"].
|
89
|
+
expect(json_response["count"]).to eq(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
|
-
json_response['count'].
|
95
|
-
json_response['current_page'].
|
96
|
-
json_response['pages'].
|
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)
|
97
97
|
end
|
98
98
|
|
99
99
|
it 'can query the results through a paramter' do
|
100
100
|
api_get :index, :q => { :response_code_cont => '999' }
|
101
|
-
json_response['count'].
|
102
|
-
json_response['payments'].first['response_code'].
|
101
|
+
expect(json_response['count']).to eq(1)
|
102
|
+
expect(json_response['payments'].first['response_code']).to eq @payment.response_code
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
@@ -108,23 +108,23 @@ module Spree
|
|
108
108
|
it "can update" do
|
109
109
|
payment.update_attributes(:state => 'pending')
|
110
110
|
api_put :update, :id => payment.to_param, :payment => { :amount => 2.01 }
|
111
|
-
response.status.
|
112
|
-
payment.reload.amount.
|
111
|
+
expect(response.status).to eq(200)
|
112
|
+
expect(payment.reload.amount).to eq(2.01)
|
113
113
|
end
|
114
114
|
|
115
115
|
context "update fails" do
|
116
116
|
it "returns a 422 status when the amount is invalid" do
|
117
117
|
payment.update_attributes(:state => 'pending')
|
118
118
|
api_put :update, :id => payment.to_param, :payment => { :amount => 'invalid' }
|
119
|
-
response.status.
|
120
|
-
json_response["error"].
|
119
|
+
expect(response.status).to eq(422)
|
120
|
+
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
|
121
121
|
end
|
122
122
|
|
123
123
|
it "returns a 403 status when the payment is not pending" do
|
124
124
|
payment.update_attributes(:state => 'completed')
|
125
125
|
api_put :update, :id => payment.to_param, :payment => { :amount => 2.01 }
|
126
|
-
response.status.
|
127
|
-
json_response["error"].
|
126
|
+
expect(response.status).to eq(403)
|
127
|
+
expect(json_response["error"]).to eq("This payment cannot be updated because it is completed.")
|
128
128
|
end
|
129
129
|
end
|
130
130
|
end
|
@@ -132,26 +132,26 @@ module Spree
|
|
132
132
|
context "authorizing" do
|
133
133
|
it "can authorize" do
|
134
134
|
api_put :authorize, :id => payment.to_param
|
135
|
-
response.status.
|
136
|
-
payment.reload.state.
|
135
|
+
expect(response.status).to eq(200)
|
136
|
+
expect(payment.reload.state).to eq("pending")
|
137
137
|
end
|
138
138
|
|
139
139
|
context "authorization fails" do
|
140
140
|
before do
|
141
141
|
fake_response = double(:success? => false, :to_s => "Could not authorize card")
|
142
|
-
Spree::Gateway::Bogus.
|
142
|
+
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:authorize).and_return(fake_response)
|
143
143
|
api_put :authorize, :id => payment.to_param
|
144
144
|
end
|
145
145
|
|
146
146
|
it "returns a 422 status" do
|
147
|
-
response.status.
|
147
|
+
expect(response.status).to eq(422)
|
148
148
|
expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
|
149
149
|
expect(json_response["errors"]["base"][0]).to eq "Could not authorize card"
|
150
150
|
end
|
151
151
|
|
152
152
|
it "does not raise a stack level error" do
|
153
153
|
skip "Investigate why a payment.reload after the request raises 'stack level too deep'"
|
154
|
-
payment.reload.state.
|
154
|
+
expect(payment.reload.state).to eq("failed")
|
155
155
|
end
|
156
156
|
end
|
157
157
|
end
|
@@ -159,19 +159,19 @@ module Spree
|
|
159
159
|
context "capturing" do
|
160
160
|
it "can capture" do
|
161
161
|
api_put :capture, :id => payment.to_param
|
162
|
-
response.status.
|
163
|
-
payment.reload.state.
|
162
|
+
expect(response.status).to eq(200)
|
163
|
+
expect(payment.reload.state).to eq("completed")
|
164
164
|
end
|
165
165
|
|
166
166
|
context "capturing fails" do
|
167
167
|
before do
|
168
168
|
fake_response = double(:success? => false, :to_s => "Insufficient funds")
|
169
|
-
Spree::Gateway::Bogus.
|
169
|
+
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:capture).and_return(fake_response)
|
170
170
|
end
|
171
171
|
|
172
172
|
it "returns a 422 status" do
|
173
173
|
api_put :capture, :id => payment.to_param
|
174
|
-
response.status.
|
174
|
+
expect(response.status).to eq(422)
|
175
175
|
expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
|
176
176
|
expect(json_response["errors"]["base"][0]).to eq "Insufficient funds"
|
177
177
|
end
|
@@ -181,19 +181,19 @@ module Spree
|
|
181
181
|
context "purchasing" do
|
182
182
|
it "can purchase" do
|
183
183
|
api_put :purchase, :id => payment.to_param
|
184
|
-
response.status.
|
185
|
-
payment.reload.state.
|
184
|
+
expect(response.status).to eq(200)
|
185
|
+
expect(payment.reload.state).to eq("completed")
|
186
186
|
end
|
187
187
|
|
188
188
|
context "purchasing fails" do
|
189
189
|
before do
|
190
190
|
fake_response = double(:success? => false, :to_s => "Insufficient funds")
|
191
|
-
Spree::Gateway::Bogus.
|
191
|
+
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:purchase).and_return(fake_response)
|
192
192
|
end
|
193
193
|
|
194
194
|
it "returns a 422 status" do
|
195
195
|
api_put :purchase, :id => payment.to_param
|
196
|
-
response.status.
|
196
|
+
expect(response.status).to eq(422)
|
197
197
|
expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
|
198
198
|
expect(json_response["errors"]["base"][0]).to eq "Insufficient funds"
|
199
199
|
end
|
@@ -210,7 +210,7 @@ module Spree
|
|
210
210
|
context "voiding fails" do
|
211
211
|
before do
|
212
212
|
fake_response = double(success?: false, to_s: "NO REFUNDS")
|
213
|
-
Spree::Gateway::Bogus.
|
213
|
+
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:void).and_return(fake_response)
|
214
214
|
end
|
215
215
|
|
216
216
|
it "returns a 422 status" do
|
@@ -230,28 +230,28 @@ module Spree
|
|
230
230
|
|
231
231
|
it "can credit" do
|
232
232
|
api_put :credit, :id => payment.to_param
|
233
|
-
response.status.
|
234
|
-
payment.reload.state.
|
233
|
+
expect(response.status).to eq(200)
|
234
|
+
expect(payment.reload.state).to eq("completed")
|
235
235
|
|
236
236
|
# Ensure that a credit payment was created, and it has correct credit amount
|
237
237
|
credit_payment = Payment.where(:source_type => 'Spree::Payment', :source_id => payment.id).last
|
238
|
-
credit_payment.amount.to_f.
|
238
|
+
expect(credit_payment.amount.to_f).to eq(-45.75)
|
239
239
|
end
|
240
240
|
|
241
241
|
context "crediting fails" do
|
242
242
|
it "returns a 422 status" do
|
243
243
|
fake_response = double(:success? => false, :to_s => "NO CREDIT FOR YOU")
|
244
|
-
Spree::Gateway::Bogus.
|
244
|
+
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:credit).and_return(fake_response)
|
245
245
|
api_put :credit, :id => payment.to_param
|
246
|
-
response.status.
|
246
|
+
expect(response.status).to eq(422)
|
247
247
|
expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
|
248
248
|
expect(json_response["errors"]["base"][0]).to eq "NO CREDIT FOR YOU"
|
249
249
|
end
|
250
250
|
|
251
251
|
it "cannot credit over credit_allowed limit" do
|
252
252
|
api_put :credit, :id => payment.to_param, :amount => 1000000
|
253
|
-
response.status.
|
254
|
-
json_response["error"].
|
253
|
+
expect(response.status).to eq(422)
|
254
|
+
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.")
|
255
255
|
end
|
256
256
|
end
|
257
257
|
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 do
|
5
|
+
describe Spree::Api::ProductPropertiesController, :type => :controller 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
|
-
response.status.
|
26
|
+
expect(response.status).to eq(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
|
-
json_response["product_properties"].count.
|
33
|
-
json_response["product_properties"].first.
|
32
|
+
expect(json_response["product_properties"].count).to eq 2
|
33
|
+
expect(json_response["product_properties"].first).to 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
|
-
json_response['product_properties'].count.
|
39
|
-
json_response['current_page'].
|
40
|
-
json_response['pages'].
|
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)
|
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
|
-
json_response['count'].
|
48
|
-
json_response['product_properties'].first['value'].
|
47
|
+
expect(json_response['count']).to eq(1)
|
48
|
+
expect(json_response['product_properties'].first['value']).to 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
|
-
json_response.
|
53
|
+
expect(json_response).to 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
|
-
json_response["attributes"].
|
59
|
-
json_response["required_attributes"].
|
58
|
+
expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
|
59
|
+
expect(json_response["required_attributes"]).to 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
|
-
|
75
|
+
expect { property_1.reload }.not_to 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
|
-
json_response.
|
86
|
-
response.status.
|
85
|
+
expect(json_response).to have_attributes(attributes)
|
86
|
+
expect(response.status).to eq(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
|
-
response.status.
|
91
|
+
expect(response.status).to eq(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
|
-
response.status.
|
97
|
-
|
96
|
+
expect(response.status).to eq(204)
|
97
|
+
expect { property_1.reload }.to 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
|
-
json_response["product_properties"].count.
|
106
|
-
json_response["product_properties"].first.
|
105
|
+
expect(json_response["product_properties"].count).to eq 2
|
106
|
+
expect(json_response["product_properties"].first).to 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
|
-
json_response.
|
111
|
+
expect(json_response).to have_attributes(attributes)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
@@ -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::ProductsController do
|
5
|
+
describe Spree::Api::ProductsController, :type => :controller do
|
6
6
|
render_views
|
7
7
|
|
8
8
|
let!(:product) { create(:product) }
|
@@ -51,20 +51,20 @@ module Spree
|
|
51
51
|
|
52
52
|
it "retrieves a list of products" do
|
53
53
|
api_get :index
|
54
|
-
json_response["products"].first.
|
55
|
-
json_response["total_count"].
|
56
|
-
json_response["current_page"].
|
57
|
-
json_response["pages"].
|
58
|
-
json_response["per_page"].
|
54
|
+
expect(json_response["products"].first).to have_attributes(show_attributes)
|
55
|
+
expect(json_response["total_count"]).to eq(1)
|
56
|
+
expect(json_response["current_page"]).to eq(1)
|
57
|
+
expect(json_response["pages"]).to eq(1)
|
58
|
+
expect(json_response["per_page"]).to eq(Kaminari.config.default_per_page)
|
59
59
|
end
|
60
60
|
|
61
61
|
it "retrieves a list of products by id" do
|
62
62
|
api_get :index, :ids => [product.id]
|
63
|
-
json_response["products"].first.
|
64
|
-
json_response["total_count"].
|
65
|
-
json_response["current_page"].
|
66
|
-
json_response["pages"].
|
67
|
-
json_response["per_page"].
|
63
|
+
expect(json_response["products"].first).to have_attributes(show_attributes)
|
64
|
+
expect(json_response["total_count"]).to eq(1)
|
65
|
+
expect(json_response["current_page"]).to eq(1)
|
66
|
+
expect(json_response["pages"]).to eq(1)
|
67
|
+
expect(json_response["per_page"]).to eq(Kaminari.config.default_per_page)
|
68
68
|
end
|
69
69
|
|
70
70
|
context "specifying a rabl template for a custom action" do
|
@@ -80,18 +80,18 @@ module Spree
|
|
80
80
|
it "uses the specified custom template through the request header" do
|
81
81
|
request.headers['X-Spree-Template'] = 'show'
|
82
82
|
api_get :custom_show, :id => product.id
|
83
|
-
response.
|
83
|
+
expect(response).to render_template('spree/api/products/show')
|
84
84
|
end
|
85
85
|
|
86
86
|
it "uses the specified custom template through the template URL parameter" do
|
87
87
|
api_get :custom_show, :id => product.id, :template => 'show'
|
88
|
-
response.
|
88
|
+
expect(response).to render_template('spree/api/products/show')
|
89
89
|
end
|
90
90
|
|
91
91
|
it "falls back to the default template if the specified template does not exist" do
|
92
92
|
request.headers['X-Spree-Template'] = 'invoice'
|
93
93
|
api_get :show, :id => product.id
|
94
|
-
response.
|
94
|
+
expect(response).to render_template('spree/api/products/show')
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -107,64 +107,64 @@ module Spree
|
|
107
107
|
it "retrieves a list of products by ids string" do
|
108
108
|
second_product = create(:product)
|
109
109
|
api_get :index, :ids => [product.id, second_product.id].join(",")
|
110
|
-
json_response["products"].first.
|
111
|
-
json_response["products"][1].
|
112
|
-
json_response["total_count"].
|
113
|
-
json_response["current_page"].
|
114
|
-
json_response["pages"].
|
115
|
-
json_response["per_page"].
|
110
|
+
expect(json_response["products"].first).to have_attributes(show_attributes)
|
111
|
+
expect(json_response["products"][1]).to have_attributes(show_attributes)
|
112
|
+
expect(json_response["total_count"]).to eq(2)
|
113
|
+
expect(json_response["current_page"]).to eq(1)
|
114
|
+
expect(json_response["pages"]).to eq(1)
|
115
|
+
expect(json_response["per_page"]).to eq(Kaminari.config.default_per_page)
|
116
116
|
end
|
117
117
|
|
118
118
|
it "does not return inactive products when queried by ids" do
|
119
119
|
api_get :index, :ids => [inactive_product.id]
|
120
|
-
json_response["count"].
|
120
|
+
expect(json_response["count"]).to eq(0)
|
121
121
|
end
|
122
122
|
|
123
123
|
it "does not list unavailable products" do
|
124
124
|
api_get :index
|
125
|
-
json_response["products"].first["name"].
|
125
|
+
expect(json_response["products"].first["name"]).not_to eq("inactive")
|
126
126
|
end
|
127
127
|
|
128
128
|
context "pagination" do
|
129
129
|
it "can select the next page of products" do
|
130
130
|
second_product = create(:product)
|
131
131
|
api_get :index, :page => 2, :per_page => 1
|
132
|
-
json_response["products"].first.
|
133
|
-
json_response["total_count"].
|
134
|
-
json_response["current_page"].
|
135
|
-
json_response["pages"].
|
132
|
+
expect(json_response["products"].first).to have_attributes(show_attributes)
|
133
|
+
expect(json_response["total_count"]).to eq(2)
|
134
|
+
expect(json_response["current_page"]).to eq(2)
|
135
|
+
expect(json_response["pages"]).to eq(2)
|
136
136
|
end
|
137
137
|
|
138
138
|
it 'can control the page size through a parameter' do
|
139
139
|
create(:product)
|
140
140
|
api_get :index, :per_page => 1
|
141
|
-
json_response['count'].
|
142
|
-
json_response['total_count'].
|
143
|
-
json_response['current_page'].
|
144
|
-
json_response['pages'].
|
141
|
+
expect(json_response['count']).to eq(1)
|
142
|
+
expect(json_response['total_count']).to eq(2)
|
143
|
+
expect(json_response['current_page']).to eq(1)
|
144
|
+
expect(json_response['pages']).to eq(2)
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
148
148
|
context "jsonp" do
|
149
149
|
it "retrieves a list of products of jsonp" do
|
150
150
|
api_get :index, {:callback => 'callback'}
|
151
|
-
response.body.
|
152
|
-
response.header['Content-Type'].
|
151
|
+
expect(response.body).to match(/^callback\(.*\)$/)
|
152
|
+
expect(response.header['Content-Type']).to include('application/javascript')
|
153
153
|
end
|
154
154
|
|
155
155
|
# Regression test for #4332
|
156
156
|
it "does not escape quotes" do
|
157
157
|
api_get :index, {:callback => 'callback'}
|
158
|
-
response.body.
|
159
|
-
response.header['Content-Type'].
|
158
|
+
expect(response.body).to match(/^callback\({"count":1,"total_count":1/)
|
159
|
+
expect(response.header['Content-Type']).to include('application/javascript')
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
163
163
|
it "can search for products" do
|
164
164
|
create(:product, :name => "The best product in the world")
|
165
165
|
api_get :index, :q => { :name_cont => "best" }
|
166
|
-
json_response["products"].first.
|
167
|
-
json_response["count"].
|
166
|
+
expect(json_response["products"].first).to have_attributes(show_attributes)
|
167
|
+
expect(json_response["count"]).to eq(1)
|
168
168
|
end
|
169
169
|
|
170
170
|
it "gets a single product" do
|
@@ -173,14 +173,14 @@ module Spree
|
|
173
173
|
product.variants.first.images.create!(:attachment => image("thinking-cat.jpg"))
|
174
174
|
product.set_property("spree", "rocks")
|
175
175
|
api_get :show, :id => product.to_param
|
176
|
-
json_response.
|
177
|
-
json_response['variants'].first.
|
176
|
+
expect(json_response).to have_attributes(show_attributes)
|
177
|
+
expect(json_response['variants'].first).to have_attributes([:name,
|
178
178
|
:is_master,
|
179
179
|
:price,
|
180
180
|
:images,
|
181
181
|
:in_stock])
|
182
182
|
|
183
|
-
json_response['variants'].first['images'].first.
|
183
|
+
expect(json_response['variants'].first['images'].first).to have_attributes([:attachment_file_name,
|
184
184
|
:attachment_width,
|
185
185
|
:attachment_height,
|
186
186
|
:attachment_content_type,
|
@@ -189,7 +189,7 @@ module Spree
|
|
189
189
|
:product_url,
|
190
190
|
:large_url])
|
191
191
|
|
192
|
-
json_response["product_properties"].first.
|
192
|
+
expect(json_response["product_properties"].first).to have_attributes([:value,
|
193
193
|
:product_id,
|
194
194
|
:property_name])
|
195
195
|
end
|
@@ -215,11 +215,11 @@ module Spree
|
|
215
215
|
|
216
216
|
specify do
|
217
217
|
api_get :show, :id => product.to_param
|
218
|
-
json_response["slug"].
|
218
|
+
expect(json_response["slug"]).to match(/and-1-ways/)
|
219
219
|
product.destroy
|
220
220
|
|
221
221
|
api_get :show, :id => other_product.id
|
222
|
-
json_response["slug"].
|
222
|
+
expect(json_response["slug"]).to match(/droids/)
|
223
223
|
end
|
224
224
|
end
|
225
225
|
|
@@ -235,11 +235,11 @@ module Spree
|
|
235
235
|
|
236
236
|
it "can learn how to create a new product" do
|
237
237
|
api_get :new
|
238
|
-
json_response["attributes"].
|
238
|
+
expect(json_response["attributes"]).to eq(new_attributes.map(&:to_s))
|
239
239
|
required_attributes = json_response["required_attributes"]
|
240
|
-
required_attributes.
|
241
|
-
required_attributes.
|
242
|
-
required_attributes.
|
240
|
+
expect(required_attributes).to include("name")
|
241
|
+
expect(required_attributes).to include("price")
|
242
|
+
expect(required_attributes).to include("shipping_category_id")
|
243
243
|
end
|
244
244
|
|
245
245
|
it_behaves_like "modifying product actions are restricted"
|
@@ -253,10 +253,10 @@ module Spree
|
|
253
253
|
|
254
254
|
it "can see all products" do
|
255
255
|
api_get :index
|
256
|
-
json_response["products"].count.
|
257
|
-
json_response["count"].
|
258
|
-
json_response["current_page"].
|
259
|
-
json_response["pages"].
|
256
|
+
expect(json_response["products"].count).to eq(2)
|
257
|
+
expect(json_response["count"]).to eq(2)
|
258
|
+
expect(json_response["current_page"]).to eq(1)
|
259
|
+
expect(json_response["pages"]).to eq(1)
|
260
260
|
end
|
261
261
|
|
262
262
|
# Regression test for #1626
|
@@ -267,12 +267,12 @@ module Spree
|
|
267
267
|
|
268
268
|
it "does not include deleted products" do
|
269
269
|
api_get :index
|
270
|
-
json_response["products"].count.
|
270
|
+
expect(json_response["products"].count).to eq(2)
|
271
271
|
end
|
272
272
|
|
273
273
|
it "can include deleted products" do
|
274
274
|
api_get :index, :show_deleted => 1
|
275
|
-
json_response["products"].count.
|
275
|
+
expect(json_response["products"].count).to eq(3)
|
276
276
|
end
|
277
277
|
end
|
278
278
|
|
@@ -281,8 +281,8 @@ module Spree
|
|
281
281
|
api_post :create, :product => { :name => "The Other Product",
|
282
282
|
:price => 19.99,
|
283
283
|
:shipping_category_id => create(:shipping_category).id }
|
284
|
-
json_response.
|
285
|
-
response.status.
|
284
|
+
expect(json_response).to have_attributes(base_attributes)
|
285
|
+
expect(response.status).to eq(201)
|
286
286
|
end
|
287
287
|
|
288
288
|
it "creates with embedded variants" do
|
@@ -361,25 +361,25 @@ module Spree
|
|
361
361
|
|
362
362
|
it "can still create a product" do
|
363
363
|
api_post :create, :product => product_data, :token => "fake"
|
364
|
-
json_response.
|
365
|
-
response.status.
|
364
|
+
expect(json_response).to have_attributes(show_attributes)
|
365
|
+
expect(response.status).to eq(201)
|
366
366
|
end
|
367
367
|
end
|
368
368
|
|
369
369
|
it "cannot create a new product with invalid attributes" do
|
370
|
-
api_post :create, :
|
371
|
-
response.status.
|
372
|
-
json_response["error"].
|
370
|
+
api_post :create, product: {}
|
371
|
+
expect(response.status).to eq(422)
|
372
|
+
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
|
373
373
|
errors = json_response["errors"]
|
374
374
|
errors.delete("slug") # Don't care about this one.
|
375
|
-
errors.keys.
|
375
|
+
expect(errors.keys).to match_array(["name", "price", "shipping_category_id"])
|
376
376
|
end
|
377
377
|
end
|
378
378
|
|
379
379
|
context 'updating a product' do
|
380
380
|
it "can update a product" do
|
381
381
|
api_put :update, :id => product.to_param, :product => { :name => "New and Improved Product!" }
|
382
|
-
response.status.
|
382
|
+
expect(response.status).to eq(200)
|
383
383
|
end
|
384
384
|
|
385
385
|
it "can create new option types on a product" do
|
@@ -424,9 +424,9 @@ module Spree
|
|
424
424
|
|
425
425
|
it "cannot update a product with an invalid attribute" do
|
426
426
|
api_put :update, :id => product.to_param, :product => { :name => "" }
|
427
|
-
response.status.
|
428
|
-
json_response["error"].
|
429
|
-
json_response["errors"]["name"].
|
427
|
+
expect(response.status).to eq(422)
|
428
|
+
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
|
429
|
+
expect(json_response["errors"]["name"]).to eq(["can't be blank"])
|
430
430
|
end
|
431
431
|
|
432
432
|
# Regression test for #4123
|
@@ -443,10 +443,10 @@ module Spree
|
|
443
443
|
end
|
444
444
|
|
445
445
|
it "can delete a product" do
|
446
|
-
product.deleted_at.
|
446
|
+
expect(product.deleted_at).to be_nil
|
447
447
|
api_delete :destroy, :id => product.to_param
|
448
|
-
response.status.
|
449
|
-
product.reload.deleted_at.
|
448
|
+
expect(response.status).to eq(204)
|
449
|
+
expect(product.reload.deleted_at).not_to be_nil
|
450
450
|
end
|
451
451
|
end
|
452
452
|
end
|