spree_api 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -49
  3. data/LICENSE +1 -1
  4. data/app/controllers/spree/api/addresses_controller.rb +0 -4
  5. data/app/controllers/spree/api/base_controller.rb +20 -18
  6. data/app/controllers/spree/api/checkouts_controller.rb +2 -7
  7. data/app/controllers/spree/api/images_controller.rb +11 -3
  8. data/app/controllers/spree/api/line_items_controller.rb +9 -7
  9. data/app/controllers/spree/api/orders_controller.rb +1 -5
  10. data/app/controllers/spree/api/products_controller.rb +1 -2
  11. data/app/helpers/spree/api/api_helpers.rb +1 -1
  12. data/app/views/spree/api/line_items/show.v1.rabl +2 -2
  13. data/app/views/spree/api/products/show.v1.rabl +3 -3
  14. data/app/views/spree/api/shipments/show.v1.rabl +1 -1
  15. data/app/views/spree/api/stock_items/show.v1.rabl +1 -1
  16. data/app/views/spree/api/variants/{variant_full.v1.rabl → big.v1.rabl} +4 -8
  17. data/app/views/spree/api/variants/index.v1.rabl +1 -1
  18. data/app/views/spree/api/variants/show.v1.rabl +2 -3
  19. data/app/views/spree/api/variants/{variant.v1.rabl → small.v1.rabl} +5 -0
  20. data/config/routes.rb +4 -2
  21. data/lib/spree/api/controller_setup.rb +0 -12
  22. data/lib/spree/api/responders/rabl_template.rb +9 -0
  23. data/lib/spree/api/testing_support/caching.rb +10 -0
  24. data/lib/spree/api/testing_support/helpers.rb +0 -1
  25. data/spec/controllers/spree/api/base_controller_spec.rb +23 -0
  26. data/spec/controllers/spree/api/checkouts_controller_spec.rb +10 -0
  27. data/spec/controllers/spree/api/images_controller_spec.rb +7 -6
  28. data/spec/controllers/spree/api/line_items_controller_spec.rb +20 -4
  29. data/spec/controllers/spree/api/orders_controller_spec.rb +1 -1
  30. data/spec/controllers/spree/api/products_controller_spec.rb +18 -0
  31. data/spec/controllers/spree/api/users_controller_spec.rb +2 -3
  32. data/spec/controllers/spree/api/variants_controller_spec.rb +9 -0
  33. data/spec/requests/rabl_cache_spec.rb +29 -0
  34. data/spec/spec_helper.rb +1 -0
  35. metadata +10 -13
  36. data/app/models/spree/order_decorator.rb +0 -171
  37. data/app/models/spree/user_decorator.rb +0 -13
  38. data/app/overrides/api_admin_user_edit_form.rb +0 -7
  39. data/app/views/spree/admin/users/_api_fields.html.erb +0 -31
  40. data/spec/models/spree/order_spec.rb +0 -301
@@ -1,301 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Spree
4
- describe Order do
5
- let!(:country) { create(:country) }
6
- let!(:state) { country.states.first || create(:state, :country => country) }
7
- let!(:stock_location) { create(:stock_location) }
8
-
9
- let(:user) { stub_model(LegacyUser, :email => 'fox@mudler.com') }
10
- let(:shipping_method) { create(:shipping_method) }
11
- let(:payment_method) { create(:check_payment_method) }
12
-
13
- let(:product) { product = Spree::Product.create(:name => 'Test',
14
- :sku => 'TEST-1',
15
- :price => 33.22)
16
- product.shipping_category = create(:shipping_category)
17
- product.save
18
- product }
19
-
20
- let(:variant) { variant = product.master
21
- variant.stock_items.each { |si| si.update_attribute(:count_on_hand, 10) }
22
- variant }
23
-
24
- let(:sku) { variant.sku }
25
- let(:variant_id) { variant.id }
26
-
27
- let(:line_items) {{ "0" => { :variant_id => variant.id, :quantity => 5 }}}
28
- let(:ship_address) {{
29
- :address1 => '123 Testable Way',
30
- :firstname => 'Fox',
31
- :lastname => 'Mulder',
32
- :city => 'Washington',
33
- :country_id => country.id,
34
- :state_id => state.id,
35
- :zipcode => '666',
36
- :phone => '666-666-6666'
37
- }}
38
-
39
- it 'can import an order number' do
40
- params = { number: '123-456-789' }
41
- order = Order.build_from_api(user, params)
42
- order.number.should eq '123-456-789'
43
- end
44
-
45
- it 'optionally add completed at' do
46
- params = { email: 'test@test.com',
47
- completed_at: Time.now,
48
- line_items_attributes: line_items }
49
-
50
- order = Order.build_from_api(user, params)
51
- order.should be_completed
52
- order.state.should eq 'complete'
53
- end
54
-
55
- it "assigns order[email] over user email to order" do
56
- params = { email: 'wooowww@test.com' }
57
- order = Order.build_from_api(user, params)
58
- expect(order.email).to eq params[:email]
59
- end
60
-
61
- it 'can build an order from API with just line items' do
62
- params = { :line_items_attributes => line_items }
63
-
64
- Order.should_receive(:ensure_variant_id_from_api)
65
- order = Order.build_from_api(user, params)
66
- order.user.should == nil
67
- line_item = order.line_items.first
68
- line_item.quantity.should == 5
69
- line_item.variant_id.should == variant_id
70
- end
71
-
72
- it 'handles line_item building exceptions' do
73
- line_items['0'][:variant_id] = 'XXX'
74
- params = { :line_items_attributes => line_items }
75
-
76
- expect {
77
- order = Order.build_from_api(user, params)
78
- }.to raise_error /XXX/
79
- end
80
-
81
- it 'can build an order from API with variant sku' do
82
- params = { :line_items_attributes => {
83
- "0" => { :sku => sku, :quantity => 5 } }}
84
-
85
- order = Order.build_from_api(user, params)
86
-
87
- line_item = order.line_items.first
88
- line_item.variant_id.should == variant_id
89
- line_item.quantity.should == 5
90
- end
91
-
92
- it 'handles exceptions when sku is not found' do
93
- params = { :line_items_attributes => {
94
- "0" => { :sku => 'XXX', :quantity => 5 } }}
95
- expect {
96
- order = Order.build_from_api(user, params)
97
- }.to raise_error /XXX/
98
- end
99
-
100
- it 'can build an order from API shipping address' do
101
- params = { :ship_address_attributes => ship_address,
102
- :line_items_attributes => line_items }
103
-
104
- order = Order.build_from_api(user, params)
105
- order.ship_address.address1.should eq '123 Testable Way'
106
- end
107
-
108
- it 'can build an order from API with country attributes' do
109
- ship_address.delete(:country_id)
110
- ship_address[:country] = { 'iso' => 'US' }
111
- params = { :ship_address_attributes => ship_address,
112
- :line_items_attributes => line_items }
113
-
114
- order = Order.build_from_api(user, params)
115
- order.ship_address.country.iso.should eq 'US'
116
- end
117
-
118
- it 'handles country lookup exceptions' do
119
- ship_address.delete(:country_id)
120
- ship_address[:country] = { 'iso' => 'XXX' }
121
- params = { :ship_address_attributes => ship_address,
122
- :line_items_attributes => line_items }
123
-
124
- expect {
125
- order = Order.build_from_api(user, params)
126
- }.to raise_error /XXX/
127
- end
128
-
129
- it 'can build an order from API with state attributes' do
130
- ship_address.delete(:state_id)
131
- ship_address[:state] = { 'name' => state.name }
132
- params = { :ship_address_attributes => ship_address,
133
- :line_items_attributes => line_items }
134
-
135
- order = Order.build_from_api(user, params)
136
- order.ship_address.state.name.should eq 'Alabama'
137
- end
138
-
139
- context "state passed is not associated with country" do
140
- let(:params) do
141
- params = { :ship_address_attributes => ship_address,
142
- :line_items_attributes => line_items }
143
- end
144
-
145
- let(:other_state) { create(:state, name: "Uhuhuh", country: create(:country)) }
146
-
147
- before do
148
- ship_address.delete(:state_id)
149
- ship_address[:state] = { 'name' => other_state.name }
150
- end
151
-
152
- it 'sets states name instead of state id' do
153
- order = Order.build_from_api(user, params)
154
- expect(order.ship_address.state_name).to eq other_state.name
155
- end
156
- end
157
-
158
- it 'sets state name if state record not found' do
159
- ship_address.delete(:state_id)
160
- ship_address[:state] = { 'name' => 'XXX' }
161
- params = { :ship_address_attributes => ship_address,
162
- :line_items_attributes => line_items }
163
-
164
- order = Order.build_from_api(user, params)
165
- expect(order.ship_address.state_name).to eq 'XXX'
166
- end
167
-
168
- context 'variant not deleted' do
169
- it 'ensures variant id from api' do
170
- hash = { sku: variant.sku }
171
- Order.ensure_variant_id_from_api(hash)
172
- expect(hash[:variant_id]).to eq variant.id
173
- end
174
- end
175
-
176
- context 'variant was deleted' do
177
- it 'raise error as variant shouldnt be found' do
178
- variant.product.destroy
179
- hash = { sku: variant.sku }
180
- expect {
181
- Order.ensure_variant_id_from_api(hash)
182
- }.to raise_error
183
- end
184
- end
185
-
186
- it 'ensures_country_id for country fields' do
187
- [:name, :iso, :iso_name, :iso3].each do |field|
188
- address = { :country => { field => country.send(field) }}
189
- Order.ensure_country_id_from_api(address)
190
- address[:country_id].should eq country.id
191
- end
192
- end
193
-
194
- it "raises with proper message when cant find country" do
195
- address = { :country => { "name" => "NoNoCountry" } }
196
- expect {
197
- Order.ensure_country_id_from_api(address)
198
- }.to raise_error /NoNoCountry/
199
- end
200
-
201
- it 'ensures_state_id for state fields' do
202
- [:name, :abbr].each do |field|
203
- address = { country_id: country.id, :state => { field => state.send(field) }}
204
- Order.ensure_state_id_from_api(address)
205
- address[:state_id].should eq state.id
206
- end
207
- end
208
-
209
- context "shipments" do
210
- let(:params) do
211
- { :shipments_attributes => [
212
- { :tracking => '123456789',
213
- :cost => '4.99',
214
- :shipping_method => shipping_method.name,
215
- :stock_location => stock_location.name,
216
- :inventory_units => [{ :sku => sku }]
217
- }
218
- ] }
219
- end
220
-
221
- it 'ensures variant exists and is not deleted' do
222
- Order.should_receive(:ensure_variant_id_from_api)
223
- order = Order.build_from_api(user, params)
224
- end
225
-
226
- it 'builds them properly' do
227
- order = Order.build_from_api(user, params)
228
-
229
- shipment = order.shipments.first
230
- shipment.inventory_units.first.variant_id.should eq product.master.id
231
- shipment.tracking.should eq '123456789'
232
- shipment.shipping_rates.first.cost.should eq 4.99
233
- shipment.stock_location.should eq stock_location
234
- end
235
-
236
- it "raises if cant find stock location" do
237
- params[:shipments_attributes][0][:stock_location] = "doesnt exist"
238
- expect {
239
- order = Order.build_from_api(user, params)
240
- }.to raise_error
241
- end
242
- end
243
-
244
- it 'handles shipment building exceptions' do
245
- params = { :shipments_attributes => [{ tracking: '123456789',
246
- cost: '4.99',
247
- shipping_method: 'XXX',
248
- inventory_units: [{ sku: sku }]
249
- }] }
250
- expect {
251
- order = Order.build_from_api(user, params)
252
- }.to raise_error /XXX/
253
- end
254
-
255
- it 'adds adjustments' do
256
- params = { :adjustments_attributes => [
257
- { label: 'Shipping Discount', amount: -4.99 },
258
- { label: 'Promotion Discount', amount: -3.00 }] }
259
-
260
- order = Order.build_from_api(user, params)
261
- order.adjustments.all?(&:closed?).should be_true
262
- order.adjustments.first.label.should eq 'Shipping Discount'
263
- order.adjustments.first.amount.should eq -4.99
264
- end
265
-
266
- it 'handles adjustment building exceptions' do
267
- params = { :adjustments_attributes => [
268
- { amount: 'XXX' },
269
- { label: 'Promotion Discount', amount: '-3.00' }] }
270
-
271
- expect {
272
- order = Order.build_from_api(user, params)
273
- }.to raise_error /XXX/
274
- end
275
-
276
- it 'builds a payment' do
277
- params = { :payments_attributes => [{ amount: '4.99',
278
- payment_method: payment_method.name }] }
279
- order = Order.build_from_api(user, params)
280
- order.payments.first.amount.should eq 4.99
281
- end
282
-
283
- it 'handles payment building exceptions' do
284
- params = { :payments_attributes => [{ amount: '4.99',
285
- payment_method: 'XXX' }] }
286
- expect {
287
- order = Order.build_from_api(user, params)
288
- }.to raise_error /XXX/
289
- end
290
-
291
- context "raises error" do
292
- it "clears out order from db" do
293
- params = { :payments_attributes => [{ payment_method: "XXX" }] }
294
- count = Order.count
295
-
296
- expect { order = Order.build_from_api(user, params) }.to raise_error
297
- expect(Order.count).to eq count
298
- end
299
- end
300
- end
301
- end