rafter-fulfillment 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,426 @@
1
+ require 'spec_helper'
2
+
3
+ def create_add_response_json(client_reference_id, post_data)
4
+ response_json = <<-END
5
+ {
6
+ "public_id":"FO1234567890",
7
+ "created_at":"2012-12-12T16:42:57-08:00",
8
+ "updated_at":"2012-12-12T16:42:57-08:00",
9
+ "ship_date":"#{post_data['ship_date']}",
10
+ "client_reference_id":"#{client_reference_id}",
11
+ "started_processing":null,
12
+ "fulfillment_provider":"#{post_data['fulfillment_provider']}",
13
+ "fulfillment_tags":[
14
+ ],
15
+ "status":"new",
16
+ "rejected_code":null,
17
+ "fulfillment_shipping_address":{
18
+ "name":"#{post_data['fulfillment_shipping_address']['name']}",
19
+ "surname":"#{post_data['fulfillment_shipping_address']['surname']}",
20
+ "street_address_1":"#{post_data['fulfillment_shipping_address']['street_address_1']}",
21
+ "street_address_2":"#{post_data['fulfillment_shipping_address']['street_address_2']}",
22
+ "city":"#{post_data['fulfillment_shipping_address']['city']}",
23
+ "state":"#{post_data['fulfillment_shipping_address']['state']}",
24
+ "postal_code":"#{post_data['fulfillment_shipping_address']['postal_code']}",
25
+ "country":"#{post_data['fulfillment_shipping_address']['country']}"
26
+ },
27
+ "fulfillment_order_items":{
28
+ "total":0,
29
+ "shipped":0,
30
+ "rejected":0,
31
+ "location":"/orders/FO1234567890/items"
32
+ },
33
+ "fulfillment_shipments":{
34
+ "total":0,
35
+ "location":"/orders/FO1234567890/shipments"
36
+ }
37
+ }
38
+ END
39
+ response_json
40
+ end
41
+
42
+ describe Fulfillment::Order do
43
+ before { @client = Fulfillment::Client.new(:api_key => '12345', :host => 'localhost:3000', :scheme => 'http') }
44
+
45
+ describe ".show" do
46
+ before :each do
47
+ @curl = stub
48
+ @public_id = '1'
49
+ @response_json = <<-END
50
+ {
51
+ "public_id":"#{@public_id}",
52
+ "fulfillment_provider":"Ingram",
53
+ "expiration_date":"20130515",
54
+ "client_reference_id":"FirstTestOrder",
55
+ "fulfillment_tags":[],
56
+ "status":"ready",
57
+ "fulfillment_shipping_address":{
58
+ "name":"#{Faker::Company.name}",
59
+ "street_address_1":"#{Faker::Address.street_address}",
60
+ "street_address_2":"#{Faker::Address.secondary_address}",
61
+ "city":"#{Faker::Address.city}",
62
+ "state":"#{Faker::Address.state_abbr}",
63
+ "postal_code":"#{Faker::Address.zip_code}",
64
+ "country":"USA"
65
+ }
66
+ }
67
+ END
68
+ @curl = double( 'curl', :response_code => 200,
69
+ :body_str => @response_json,
70
+ :header_str => "HTTP/1.1 200 OK\r\nX-API-PAGINATION: {\"per_page\":100,\"total_pages\":1}")
71
+ end
72
+
73
+ it 'should return the order' do
74
+ Curl::Easy.should_receive(:http_get).with("http://localhost:3000/orders/#{@public_id}").and_return(@curl)
75
+ @response = Fulfillment::Order.show(@client,"1")
76
+ @response.should be_a Fulfillment::Order
77
+ @response.status.should eq 'ready'
78
+ end
79
+ end
80
+
81
+ describe '#ready' do
82
+ before do
83
+ @curl = stub
84
+
85
+ @response_json = <<-END
86
+ [{
87
+ "public_id":"FO-gibberish0123456",
88
+ "fulfillment_provider":"Ingram",
89
+ "expiration_date":"20130515",
90
+ "client_reference_id":"FirstTestOrder",
91
+ "fulfillment_tags":[],
92
+ "status":"ready",
93
+ "fulfillment_shipping_address":{
94
+ "name":"#{Faker::Company.name}",
95
+ "street_address_1":"#{Faker::Address.street_address}",
96
+ "street_address_2":"#{Faker::Address.secondary_address}",
97
+ "city":"#{Faker::Address.city}",
98
+ "state":"#{Faker::Address.state_abbr}",
99
+ "postal_code":"#{Faker::Address.zip_code}",
100
+ "country":"USA"
101
+ }
102
+ }]
103
+ END
104
+ @curl.stub(:perform)
105
+ @curl.stub(:response_code => 200)
106
+ @curl.stub(:header_str).and_return("HTTP/1.1 200 OK\r\nX-API-PAGINATION: {\"per_page\":100,\"total_pages\":1}")
107
+ @curl.stub(:body_str => @response_json)
108
+ end
109
+
110
+ context 'with one order' do
111
+ it 'returns one order' do
112
+ Curl::Easy.should_receive(:new).with("http://localhost:3000/orders/ready").and_return(@curl)
113
+ fulfillment_orders = Fulfillment::Order.ready(@client).results
114
+ fulfillment_orders.first.should be_a Fulfillment::Order
115
+ fulfillment_orders.size.should eq 1
116
+ end
117
+ end
118
+
119
+ context "with no orders" do
120
+ before do
121
+ @response_json = "[]"
122
+ @curl.stub(:body_str => @response_json)
123
+ end
124
+
125
+ it 'returns 0 orders' do
126
+ Curl::Easy.should_receive(:new).with("http://localhost:3000/orders/ready").and_return(@curl)
127
+ fulfillment_orders = Fulfillment::Order.ready(@client).results
128
+ fulfillment_orders.size.should eq 0
129
+ end
130
+ end
131
+
132
+
133
+ context "with two orders" do
134
+ before do
135
+ @response_json = <<-END
136
+ [{
137
+ "public_id":"FO-gibberish0123456",
138
+ "fulfillment_provider":"Ingram",
139
+ "expiration_date":"20130515",
140
+ "client_reference_id":"FirstTestOrder",
141
+ "fulfillment_tags":[],
142
+ "status":"ready",
143
+ "fulfillment_shipping_address":{
144
+ "name":"#{Faker::Company.name}",
145
+ "street_address_1":"#{Faker::Address.street_address}",
146
+ "street_address_2":"#{Faker::Address.secondary_address}",
147
+ "city":"#{Faker::Address.city}",
148
+ "state":"#{Faker::Address.state_abbr}",
149
+ "postal_code":"#{Faker::Address.zip_code}",
150
+ "country":"USA"
151
+ }
152
+ },
153
+ {
154
+ "public_id":"FO-gobbledeg0123456",
155
+ "fulfillment_provider":"Ingram",
156
+ "expiration_date":"20130515",
157
+ "client_reference_id":"SecondTestOrder",
158
+ "fulfillment_tags":[],
159
+ "status":"ready",
160
+ "fulfillment_shipping_address":{
161
+ "name":"#{Faker::Company.name}",
162
+ "street_address_1":"#{Faker::Address.street_address}",
163
+ "street_address_2":"#{Faker::Address.secondary_address}",
164
+ "city":"#{Faker::Address.city}",
165
+ "state":"#{Faker::Address.state_abbr}",
166
+ "postal_code":"#{Faker::Address.zip_code}",
167
+ "country":"USA"
168
+ }
169
+ }]
170
+ END
171
+ @curl.stub(:body_str => @response_json)
172
+ end
173
+
174
+ it 'returns 2 ready orders' do
175
+ Curl::Easy.should_receive(:new).with("http://localhost:3000/orders/ready").and_return(@curl)
176
+ fulfillment_orders = Fulfillment::Order.ready(@client).results
177
+ fulfillment_orders[0].should be_a Fulfillment::Order
178
+ fulfillment_orders[1].should be_a Fulfillment::Order
179
+ fulfillment_orders.size.should eq 2
180
+ end
181
+ end
182
+ end
183
+
184
+ describe "transitions" do
185
+ context 'processing transition' do
186
+ before :each do
187
+ @public_id = 1
188
+ @response_json = <<-END
189
+ {
190
+ "public_id":"1",
191
+ "fulfillment_provider":"Ingram",
192
+ "expiration_date":"20130515",
193
+ "client_reference_id":"FirstTestOrder",
194
+ "fulfillment_tags":[],
195
+ "status":"processing",
196
+ "fulfillment_shipping_address":{
197
+ "name":"#{Faker::Company.name}",
198
+ "street_address_1":"#{Faker::Address.street_address}",
199
+ "street_address_2":"#{Faker::Address.secondary_address}",
200
+ "city":"#{Faker::Address.city}",
201
+ "state":"#{Faker::Address.state_abbr}",
202
+ "postal_code":"#{Faker::Address.zip_code}",
203
+ "country":"USA"
204
+ }
205
+ }
206
+ END
207
+ @curl = stub
208
+ @curl.stub(:response_code => 200)
209
+ @curl.stub(:perform)
210
+ @curl.stub(:body_str => @response_json)
211
+ @curl.stub(:header_str).and_return("HTTP/1.1 200 OK\r\nX-API-PAGINATION: {\"per_page\":100,\"total_pages\":1}")
212
+ end
213
+
214
+ it "should respond with a processing Order" do
215
+ Curl::Easy.should_receive(:http_put).with("http://localhost:3000/orders/#{@public_id}/process", {}).and_return(@curl)
216
+ @fo = Fulfillment::Order.new(@client, {:public_id => @public_id})
217
+ @response = @fo.process
218
+ @response.should be_a Fulfillment::Order
219
+ @response.status.should eq 'processing'
220
+ end
221
+ end
222
+
223
+ context 'processed transition' do
224
+ before :each do
225
+ @public_id = 1
226
+ @response_json = <<-END
227
+ {
228
+ "public_id":"1",
229
+ "fulfillment_provider":"Ingram",
230
+ "expiration_date":"20130515",
231
+ "client_reference_id":"FirstTestOrder",
232
+ "fulfillment_tags":[],
233
+ "status":"processed",
234
+ "fulfillment_shipping_address":{
235
+ "name":"#{Faker::Company.name}",
236
+ "street_address_1":"#{Faker::Address.street_address}",
237
+ "street_address_2":"#{Faker::Address.secondary_address}",
238
+ "city":"#{Faker::Address.city}",
239
+ "state":"#{Faker::Address.state_abbr}",
240
+ "postal_code":"#{Faker::Address.zip_code}",
241
+ "country":"USA"
242
+ }
243
+ }
244
+ END
245
+ @curl = stub
246
+ @curl.stub(:response_code => 200)
247
+ @curl.stub(:perform)
248
+ @curl.stub(:body_str => @response_json)
249
+ @curl.stub(:header_str).and_return("HTTP/1.1 200 OK\r\nX-API-PAGINATION: {\"per_page\":100,\"total_pages\":1}")
250
+ end
251
+
252
+ it "should respond with a processing Order" do
253
+ Curl::Easy.should_receive(:http_put).with("http://localhost:3000/orders/#{@public_id}/processed", {}).and_return(@curl)
254
+ @fo = Fulfillment::Order.new(@client, {:public_id => @public_id})
255
+ @response = @fo.processed
256
+ @response.should be_a Fulfillment::Order
257
+ @response.status.should eq 'processed'
258
+ end
259
+ end
260
+
261
+ context 'shipped transition' do
262
+ before :each do
263
+ @public_id = 1
264
+ @response_json = <<-END
265
+ {
266
+ "public_id":"1",
267
+ "fulfillment_provider":"Ingram",
268
+ "expiration_date":"20130515",
269
+ "client_reference_id":"FirstTestOrder",
270
+ "fulfillment_tags":[],
271
+ "status":"shipped",
272
+ "fulfillment_shipping_address":{
273
+ "name":"#{Faker::Company.name}",
274
+ "street_address_1":"#{Faker::Address.street_address}",
275
+ "street_address_2":"#{Faker::Address.secondary_address}",
276
+ "city":"#{Faker::Address.city}",
277
+ "state":"#{Faker::Address.state_abbr}",
278
+ "postal_code":"#{Faker::Address.zip_code}",
279
+ "country":"USA"
280
+ }
281
+ }
282
+ END
283
+ @curl = stub
284
+ @curl.stub(:response_code => 200)
285
+ @curl.stub(:perform)
286
+ @curl.stub(:body_str => @response_json)
287
+ end
288
+
289
+ it "should respond with a shipped order" do
290
+ Curl::Easy.should_receive(:http_put).with("http://localhost:3000/orders/#{@public_id}/shipped", {}.to_json).and_return(@curl)
291
+ order = Fulfillment::Order.new(@client, {:public_id => @public_id})
292
+ shipped_order = order.shipped
293
+ shipped_order.should be_a Fulfillment::Order
294
+ shipped_order.status.should eq 'shipped'
295
+ end
296
+ end
297
+
298
+ context 'shipping transition' do
299
+ before :each do
300
+ @public_id = 1
301
+ @response_json = <<-END
302
+ {
303
+ "public_id":"1",
304
+ "fulfillment_provider":"Ingram",
305
+ "expiration_date":"20130515",
306
+ "client_reference_id":"FirstTestOrder",
307
+ "fulfillment_tags":[],
308
+ "status":"shipping",
309
+ "fulfillment_shipping_address":{
310
+ "name":"#{Faker::Company.name}",
311
+ "street_address_1":"#{Faker::Address.street_address}",
312
+ "street_address_2":"#{Faker::Address.secondary_address}",
313
+ "city":"#{Faker::Address.city}",
314
+ "state":"#{Faker::Address.state_abbr}",
315
+ "postal_code":"#{Faker::Address.zip_code}",
316
+ "country":"USA"
317
+ }
318
+ }
319
+ END
320
+ @curl = stub
321
+ @curl.stub(:response_code => 200)
322
+ @curl.stub(:perform)
323
+ @curl.stub(:body_str => @response_json)
324
+ end
325
+
326
+ it "should respond with a shipping order" do
327
+ Curl::Easy.should_receive(:http_put).with("http://localhost:3000/orders/#{@public_id}/shipping", {}.to_json).and_return(@curl)
328
+ order = Fulfillment::Order.new(@client, {:public_id => @public_id})
329
+ shipping_order = order.shipping
330
+ shipping_order.should be_a Fulfillment::Order
331
+ shipping_order.status.should eq 'shipping'
332
+ end
333
+ end
334
+
335
+ end
336
+
337
+ describe ".search" do
338
+ before :each do
339
+ @response_json = <<-END
340
+ [{
341
+ "public_id":"FO-gibberish0123456",
342
+ "fulfillment_provider":"Ingram",
343
+ "expiration_date":"20130515",
344
+ "client_reference_id":"FirstTestOrder",
345
+ "fulfillment_tags":[],
346
+ "status":"ready",
347
+ "fulfillment_shipping_address":{
348
+ "name":"#{Faker::Company.name}",
349
+ "street_address_1":"#{Faker::Address.street_address}",
350
+ "street_address_2":"#{Faker::Address.secondary_address}",
351
+ "city":"#{Faker::Address.city}",
352
+ "state":"#{Faker::Address.state_abbr}",
353
+ "postal_code":"#{Faker::Address.zip_code}",
354
+ "country":"USA"
355
+ }
356
+ },
357
+ {
358
+ "public_id":"FO-gobbledeg0123456",
359
+ "fulfillment_provider":"Ingram",
360
+ "expiration_date":"20130515",
361
+ "client_reference_id":"SecondTestOrder",
362
+ "fulfillment_tags":[],
363
+ "status":"ready",
364
+ "fulfillment_shipping_address":{
365
+ "name":"#{Faker::Company.name}",
366
+ "street_address_1":"#{Faker::Address.street_address}",
367
+ "street_address_2":"#{Faker::Address.secondary_address}",
368
+ "city":"#{Faker::Address.city}",
369
+ "state":"#{Faker::Address.state_abbr}",
370
+ "postal_code":"#{Faker::Address.zip_code}",
371
+ "country":"USA"
372
+ }
373
+ }]
374
+ END
375
+ @curl = double( 'curl', :response_code => 200,
376
+ :body_str => @response_json,
377
+ :header_str => "HTTP/1.1 200 OK\r\nX-API-PAGINATION: {\"per_page\":100,\"total_pages\":1}")
378
+ @search_options = {:fulfillment_status=>'ready'}
379
+ end
380
+
381
+ it 'should return the order' do
382
+ Curl::Easy.should_receive(:http_get).with("http://localhost:3000/orders/search").and_yield(@curl)
383
+ @client.should_receive(:configure_http).with(@curl)
384
+ @client.should_receive(:set_request_page).with(@curl, 1)
385
+ @client.should_receive(:add_query_parameter).with(@curl, :fulfillment_status, 'ready')
386
+ @response = Fulfillment::Order.search(@client, @search_options)
387
+ @response.should be_a Fulfillment::PagedResult
388
+ @response.results.first.status.should eq 'ready'
389
+ end
390
+ end
391
+
392
+ describe ".reject" do
393
+ before :each do
394
+ @public_id = 'FO-gobbledeg0123456'
395
+ @response_json = <<-END
396
+ {
397
+ "public_id":"#{@public_id}",
398
+ "fulfillment_provider":"Ingram",
399
+ "expiration_date":"20130515",
400
+ "client_reference_id":"SecondTestOrder",
401
+ "fulfillment_tags":[],
402
+ "status":"rejected",
403
+ "fulfillment_shipping_address":{
404
+ "name":"#{Faker::Company.name}",
405
+ "street_address_1":"#{Faker::Address.street_address}",
406
+ "street_address_2":"#{Faker::Address.secondary_address}",
407
+ "city":"#{Faker::Address.city}",
408
+ "state":"#{Faker::Address.state_abbr}",
409
+ "postal_code":"#{Faker::Address.zip_code}",
410
+ "country":"USA"
411
+ }
412
+ }
413
+ END
414
+
415
+ @curl = double( 'curl', :response_code => 200,
416
+ :body_str => @response_json)
417
+ end
418
+ it 'should return the order with a rejected status' do
419
+ Curl::Easy.should_receive(:http_put).with("http://localhost:3000/orders/#{@public_id}/reject", {'rejected_code' => 1 }.to_json).and_return(@curl)
420
+ order = Fulfillment::Order.reject(@client, @public_id, 1)
421
+ order.should be_an Fulfillment::Order
422
+ order.status.should eq 'rejected'
423
+ end
424
+ end
425
+
426
+ end
@@ -0,0 +1,259 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fulfillment::Shipment do
4
+ before { @client = Fulfillment::Client.new(:api_key => '12345', :host => 'localhost:3000', :scheme => 'http') }
5
+
6
+ before :each do
7
+ @order_public_id = "FO1234567890"
8
+ end
9
+
10
+ context '.create' do
11
+ before :each do
12
+ @post_data = mock_shipment_data(["FI123","FI456"])
13
+ @post_data['shipment_reference_id'] = 'FAKE_SHIP_1067'
14
+ @post_json = @post_data.to_json
15
+ @response_json = <<-END
16
+ {
17
+ "public_id":"FS5ca30b22132d7bc9",
18
+ "created_at":"2012-12-10T21:19:42-08:00",
19
+ "updated_at":"2012-12-10T21:19:42-08:00",
20
+ "shipment_reference_id":"FAKE_SHIP_1067",
21
+ "tracking_number":"1Z1234567",
22
+ "carrier":"UPS",
23
+ "carrier_code":"Second Day",
24
+ "fulfillment_order":"#{@order_public_id}",
25
+ "fulfillment_order_items":{
26
+ "total":1,
27
+ "location":"/shipments/FS5ca30b22132d7bc9/items"
28
+ }
29
+ }
30
+ END
31
+ @response_code = 201
32
+ @curl = double('curl', :response_code => @response_code, :body_str => @response_json)
33
+ end
34
+
35
+ it 'creates a shipment for an order' do
36
+ Curl::Easy.should_receive(:http_post).with("http://localhost:3000/orders/#{@order_public_id}/shipments", @post_json).and_return(@curl)
37
+ shipment = Fulfillment::Shipment.create(@client, @order_public_id, @post_data)
38
+ shipment.should be_a Fulfillment::Shipment
39
+ shipment.shipment_reference_id.should eq @post_data['shipment_reference_id']
40
+ shipment.fulfillment_order.should eq @order_public_id
41
+ shipment.fulfillment_order_items['total'].should eq 1
42
+ end
43
+
44
+ end
45
+
46
+ context '.add' do
47
+
48
+ before :each do
49
+ @shipment_public_id = "FS1234567890"
50
+ @post_data = mock_shipment_item_data(["FI123","FI456"])
51
+ @post_json = {"fulfillment_order_items" => @post_data}.to_json
52
+ @response_json = <<-END
53
+ {
54
+ "public_id":"#{@shipment_public_id}",
55
+ "created_at":"2012-12-10T21:19:42-08:00",
56
+ "updated_at":"2012-12-10T21:19:42-08:00",
57
+ "shipment_reference_id":"FAKE_SHIP_1067",
58
+ "tracking_number":"1Z1234567",
59
+ "carrier":"UPS",
60
+ "carrier_code":"Second Day",
61
+ "fulfillment_order":"#{@order_public_id}",
62
+ "fulfillment_order_items":{
63
+ "total":2,
64
+ "location":"/shipments/FS5ca30b22132d7bc9/items"
65
+ }
66
+ }
67
+ END
68
+ @response_code = 200
69
+ @curl = double('curl', :response_code => @response_code, :body_str => @response_json)
70
+
71
+ end
72
+
73
+ it 'adds to an existing shipment' do
74
+ Curl::Easy.should_receive(:http_put).with("http://localhost:3000/orders/#{@order_public_id}/shipments/#{@shipment_public_id}/add", @post_json).and_return(@curl)
75
+ shipment = Fulfillment::Shipment.add(@client, @order_public_id, @shipment_public_id, @post_data)
76
+ shipment.should be_a Fulfillment::Shipment
77
+ shipment.shipment_reference_id.should eq 'FAKE_SHIP_1067'
78
+ shipment.fulfillment_order.should eq @order_public_id
79
+ shipment.fulfillment_order_items['total'].should eq 2
80
+ end
81
+
82
+ end
83
+
84
+ context '.shipment_items' do
85
+
86
+ before :each do
87
+ @curl = stub
88
+ @order_item_public_id_1 = "FO111"
89
+ @order_item_public_id_2 = "FO222"
90
+ @shipment_public_id = "FS1234567890"
91
+ @response_json = <<-END
92
+ [
93
+ {
94
+ "created_at":"2012-12-12T15:02:11-08:00",
95
+ "updated_at":"2012-12-12T15:02:11-08:00",
96
+ "fulfillment_shipment":{
97
+ "public_id":"#{@shipment_public_id}",
98
+ "location":"/shipments/#{@shipment_public_id}"
99
+ },
100
+ "fulfillment_order":{
101
+ "public_id":"#{@order_public_id}",
102
+ "location":"/orders/#{@order_public_id}"
103
+ },
104
+ "fulfillment_order_item":{
105
+ "public_id":"#{@order_item_public_id_1}",
106
+ "location":"/orders/#{@order_public_id}/items/#{@order_item_public_id_1}"
107
+ }
108
+ },
109
+ {
110
+ "created_at":"2012-12-12T15:02:11-08:00",
111
+ "updated_at":"2012-12-12T15:02:11-08:00",
112
+ "fulfillment_shipment":{
113
+ "public_id":"#{@shipment_public_id}",
114
+ "location":"/shipments/#{@shipment_public_id}"
115
+ },
116
+ "fulfillment_order":{
117
+ "public_id":"#{@order_public_id}",
118
+ "location":"/orders/#{@order_public_id}"
119
+ },
120
+ "fulfillment_order_item":{
121
+ "public_id":"#{@order_item_public_id_2}",
122
+ "location":"/orders/#{@order_public_id}/items/#{@order_item_public_id_2}"
123
+ }
124
+ }
125
+ ]
126
+ END
127
+ @response_code = 200
128
+ @curl = double('curl', :response_code => @response_code, :body_str => @response_json, :header_str => "HTTP/1.1 200 OK\r\nX-API-PAGINATION: {\"per_page\":100,\"total_pages\":1}")
129
+ end
130
+
131
+ it 'presents a list of shipment items' do
132
+ Curl::Easy.should_receive(:http_get).with("http://localhost:3000/shipments/#{@shipment_public_id}/items").and_return(@curl)
133
+ shipment_items = Fulfillment::Shipment.shipment_items(@client, @shipment_public_id)
134
+ shipment_items.should be_a Fulfillment::PagedResult
135
+ shipment_items_results = shipment_items.results
136
+ shipment_items_results.size.should eq 2
137
+ shipment_items_results.first.should be_a Fulfillment::ShipmentItem
138
+ shipment_items_results.first.fulfillment_shipment['public_id'].should eq @shipment_public_id
139
+ shipment_items_results.first.fulfillment_order_item['public_id'].should eq @order_item_public_id_1
140
+ shipment_items_results.first.fulfillment_order['public_id'].should eq @order_public_id
141
+ end
142
+ end
143
+
144
+ context '.show' do
145
+ before :each do
146
+ @shipment_public_id = 'FS1234567890'
147
+ @response_json = <<-END
148
+ {
149
+ "public_id":"#{@shipment_public_id}",
150
+ "created_at":"2012-12-10T21:33:24-08:00",
151
+ "updated_at":"2012-12-10T21:41:53-08:00",
152
+ "shipment_reference_id":"FAKE_SHIP_7289",
153
+ "tracking_number":"1Z1234567",
154
+ "carrier":"UPS",
155
+ "carrier_code":"Second Day",
156
+ "fulfillment_order":"#{@order_public_id}",
157
+ "fulfillment_order_items":{
158
+ "total":2,
159
+ "location":"/shipments/FSfadfaf2aee509789/items"
160
+ }
161
+ }
162
+ END
163
+ @response_code = 200
164
+ @curl = double('curl', :response_code => @response_code, :body_str => @response_json)
165
+ end
166
+
167
+ it 'can show an order by shipment id' do
168
+ Curl::Easy.should_receive(:http_get).with("http://localhost:3000/shipments/#{@shipment_public_id}").and_return(@curl)
169
+ shipment = Fulfillment::Shipment.show(@client, @shipment_public_id)
170
+ shipment.should be_a Fulfillment::Shipment
171
+ shipment.public_id.should eq @shipment_public_id
172
+ shipment.fulfillment_order.should eq @order_public_id
173
+ end
174
+ end
175
+
176
+ context '.list' do
177
+ before :each do
178
+ @shipment_public_id_1 = "FS1234567890"
179
+ @shipment_public_id_2 = "FS0987654321"
180
+ @response_json = <<-END
181
+ [
182
+ {
183
+ "public_id":"#{@shipment_public_id_1}",
184
+ "created_at":"2012-12-10T21:33:24-08:00",
185
+ "updated_at":"2012-12-10T21:41:53-08:00",
186
+ "shipment_reference_id":"FAKE_SHIP_7289",
187
+ "tracking_number":"1Z1234567",
188
+ "carrier":"UPS",
189
+ "carrier_code":"Second Day",
190
+ "fulfillment_order":"FO111",
191
+ "fulfillment_order_items":{
192
+ "total":2,
193
+ "location":"/shipments/#{@shipment_public_id_1}/items"
194
+ }
195
+ },
196
+ {
197
+ "public_id":"#{@shipment_public_id_2}",
198
+ "created_at":"2012-12-10T21:33:24-08:00",
199
+ "updated_at":"2012-12-10T21:41:53-08:00",
200
+ "shipment_reference_id":"FAKE_SHIP_7290",
201
+ "tracking_number":"1Z1234567",
202
+ "carrier":"UPS",
203
+ "carrier_code":"Second Day",
204
+ "fulfillment_order":"FO222",
205
+ "fulfillment_order_items":{
206
+ "total":2,
207
+ "location":"/shipments/#{@shipment_public_id_2}/items"
208
+ }
209
+ }
210
+ ]
211
+ END
212
+ @response_code = 200
213
+ @curl = double('curl', :response_code => @response_code, :body_str => @response_json, :header_str => "HTTP/1.1 200 OK\r\nX-API-PAGINATION: {\"per_page\":100,\"total_pages\":1}")
214
+ end
215
+
216
+ it 'can list all shipments' do
217
+ Curl::Easy.should_receive(:http_get).with("http://localhost:3000/shipments").and_return(@curl)
218
+ shipments = Fulfillment::Shipment.list(@client)
219
+ shipments.should be_a Fulfillment::PagedResult
220
+ shipments_results = shipments.results
221
+ shipments_results.first.should be_a Fulfillment::Shipment
222
+ shipments_results.first.public_id.should eq @shipment_public_id_1
223
+ shipments_results.last.public_id.should eq @shipment_public_id_2
224
+ end
225
+ end
226
+
227
+ context '.close' do
228
+ before :each do
229
+ @order_public_id = 'FO123'
230
+ @shipment_public_id = 'FS456'
231
+ @response_json = <<-END
232
+ {
233
+ "public_id":"#{@shipment_public_id}",
234
+ "created_at":"2012-12-10T21:33:24-08:00",
235
+ "updated_at":"2012-12-10T21:41:53-08:00",
236
+ "shipment_reference_id":"FAKE_SHIP_7289",
237
+ "tracking_number":"1Z1234567",
238
+ "status":"closed",
239
+ "carrier":"UPS",
240
+ "carrier_code":"Second Day",
241
+ "fulfillment_order":"#{@order_public_id}",
242
+ "fulfillment_order_items":{
243
+ "total":100,
244
+ "location":"/shipments/FSfadfaf2aee509789/items"
245
+ }
246
+ }
247
+ END
248
+ @response_code = 200
249
+ @curl = double('curl', :response_code => @response_code, :body_str => @response_json)
250
+ end
251
+
252
+ it "should return a ready Order" do
253
+ Curl::Easy.should_receive(:http_put).with("http://localhost:3000/orders/#{@order_public_id}/shipments/#{@shipment_public_id}/close", {}.to_json).and_return(@curl)
254
+ shipment = Fulfillment::Shipment.close(@client, @order_public_id, @shipment_public_id)
255
+ shipment.should be_a Fulfillment::Shipment
256
+ shipment.status.should eq 'closed'
257
+ end
258
+ end
259
+ end