stripe 1.8.7 → 1.8.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,767 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.expand_path('../test_helper', __FILE__)
3
- require 'test/unit'
4
- require 'shoulda'
5
- require 'mocha/setup'
6
- require 'pp'
7
- require 'rest-client'
8
- require 'cgi'
9
- require 'uri'
10
-
11
- class TestStripeRuby < Test::Unit::TestCase
12
- include Mocha
13
-
14
- context "Util" do
15
- should "symbolize_names should convert names to symbols" do
16
- start = {
17
- 'foo' => 'bar',
18
- 'array' => [{ 'foo' => 'bar' }],
19
- 'nested' => {
20
- 1 => 2,
21
- :symbol => 9,
22
- 'string' => nil
23
- }
24
- }
25
- finish = {
26
- :foo => 'bar',
27
- :array => [{ :foo => 'bar' }],
28
- :nested => {
29
- 1 => 2,
30
- :symbol => 9,
31
- :string => nil
32
- }
33
- }
34
-
35
- symbolized = Stripe::Util.symbolize_names(start)
36
- assert_equal(finish, symbolized)
37
- end
38
- end
39
-
40
- context "API Bindings" do
41
- setup do
42
- @mock = mock
43
- Stripe.mock_rest_client = @mock
44
- end
45
-
46
- teardown do
47
- Stripe.mock_rest_client = nil
48
- end
49
-
50
- should "creating a new APIResource should not fetch over the network" do
51
- @mock.expects(:get).never
52
- c = Stripe::Customer.new("someid")
53
- end
54
-
55
- should "creating a new APIResource from a hash should not fetch over the network" do
56
- @mock.expects(:get).never
57
- c = Stripe::Customer.construct_from({
58
- :id => "somecustomer",
59
- :card => {:id => "somecard", :object => "card"},
60
- :object => "customer"
61
- })
62
- end
63
-
64
- should "setting an attribute should not cause a network request" do
65
- @mock.expects(:get).never
66
- @mock.expects(:post).never
67
- c = Stripe::Customer.new("test_customer");
68
- c.card = {:id => "somecard", :object => "card"}
69
- end
70
-
71
- should "accessing id should not issue a fetch" do
72
- @mock.expects(:get).never
73
- c = Stripe::Customer.new("test_customer");
74
- c.id
75
- end
76
-
77
- should "not specifying api credentials should raise an exception" do
78
- Stripe.api_key = nil
79
- assert_raises Stripe::AuthenticationError do
80
- Stripe::Customer.new("test_customer").refresh
81
- end
82
- end
83
-
84
- should "specifying api credentials containing whitespace should raise an exception" do
85
- Stripe.api_key = "key "
86
- assert_raises Stripe::AuthenticationError do
87
- Stripe::Customer.new("test_customer").refresh
88
- end
89
- end
90
-
91
- should "specifying invalid api credentials should raise an exception" do
92
- Stripe.api_key = "invalid"
93
- response = test_response(test_invalid_api_key_error, 401)
94
- assert_raises Stripe::AuthenticationError do
95
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
96
- Stripe::Customer.retrieve("failing_customer")
97
- end
98
- end
99
-
100
- should "AuthenticationErrors should have an http status, http body, and JSON body" do
101
- Stripe.api_key = "invalid"
102
- response = test_response(test_invalid_api_key_error, 401)
103
- begin
104
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
105
- Stripe::Customer.retrieve("failing_customer")
106
- rescue Stripe::AuthenticationError => e
107
- assert_equal(401, e.http_status)
108
- assert_equal(true, !!e.http_body)
109
- assert_equal(true, !!e.json_body[:error][:message])
110
- assert_equal(test_invalid_api_key_error['error']['message'], e.json_body[:error][:message])
111
- end
112
- end
113
-
114
- context "when specifying per-object credentials" do
115
- context "with no global API key set" do
116
- should "use the per-object credential when creating" do
117
- Stripe.expects(:execute_request).with do |opts|
118
- opts[:headers][:authorization] == 'Bearer sk_test_local'
119
- end.returns(test_response(test_charge))
120
-
121
- Stripe::Charge.create({:card => {:number => '4242424242424242'}},
122
- 'sk_test_local')
123
- end
124
- end
125
-
126
- context "with a global API key set" do
127
- setup do
128
- Stripe.api_key = "global"
129
- end
130
-
131
- teardown do
132
- Stripe.api_key = nil
133
- end
134
-
135
- should "use the per-object credential when creating" do
136
- Stripe.expects(:execute_request).with do |opts|
137
- opts[:headers][:authorization] == 'Bearer local'
138
- end.returns(test_response(test_charge))
139
-
140
- Stripe::Charge.create({:card => {:number => '4242424242424242'}},
141
- 'local')
142
- end
143
-
144
- should "use the per-object credential when retrieving and making other calls" do
145
- Stripe.expects(:execute_request).with do |opts|
146
- opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge" &&
147
- opts[:headers][:authorization] == 'Bearer local'
148
- end.returns(test_response(test_charge))
149
- Stripe.expects(:execute_request).with do |opts|
150
- opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge/refund" &&
151
- opts[:headers][:authorization] == 'Bearer local'
152
- end.returns(test_response(test_charge))
153
-
154
- ch = Stripe::Charge.retrieve('ch_test_charge', 'local')
155
- ch.refund
156
- end
157
- end
158
- end
159
-
160
- context "with valid credentials" do
161
- setup do
162
- Stripe.api_key="foo"
163
- end
164
-
165
- teardown do
166
- Stripe.api_key=nil
167
- end
168
-
169
- should "urlencode values in GET params" do
170
- response = test_response(test_charge_array)
171
- @mock.expects(:get).with("#{Stripe.api_base}/v1/charges?customer=test%20customer", nil, nil).returns(response)
172
- charges = Stripe::Charge.all(:customer => 'test customer').data
173
- assert charges.kind_of? Array
174
- end
175
-
176
- should "construct URL properly with base query parameters" do
177
- response = test_response(test_invoice_customer_array)
178
- @mock.expects(:get).with("#{Stripe.api_base}/v1/invoices?customer=test_customer", nil, nil).returns(response)
179
- invoices = Stripe::Invoice.all(:customer => 'test_customer')
180
-
181
- @mock.expects(:get).with("#{Stripe.api_base}/v1/invoices?customer=test_customer&paid=true", nil, nil).returns(response)
182
- invoices.all(:paid => true)
183
- end
184
-
185
- should "a 400 should give an InvalidRequestError with http status, body, and JSON body" do
186
- response = test_response(test_missing_id_error, 400)
187
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
188
- begin
189
- Stripe::Customer.retrieve("foo")
190
- rescue Stripe::InvalidRequestError => e
191
- assert_equal(400, e.http_status)
192
- assert_equal(true, !!e.http_body)
193
- assert_equal(true, e.json_body.kind_of?(Hash))
194
- end
195
- end
196
-
197
- should "a 401 should give an AuthenticationError with http status, body, and JSON body" do
198
- response = test_response(test_missing_id_error, 401)
199
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
200
- begin
201
- Stripe::Customer.retrieve("foo")
202
- rescue Stripe::AuthenticationError => e
203
- assert_equal(401, e.http_status)
204
- assert_equal(true, !!e.http_body)
205
- assert_equal(true, e.json_body.kind_of?(Hash))
206
- end
207
- end
208
-
209
- should "a 402 should give a CardError with http status, body, and JSON body" do
210
- response = test_response(test_missing_id_error, 402)
211
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
212
- begin
213
- Stripe::Customer.retrieve("foo")
214
- rescue Stripe::CardError => e
215
- assert_equal(402, e.http_status)
216
- assert_equal(true, !!e.http_body)
217
- assert_equal(true, e.json_body.kind_of?(Hash))
218
- end
219
- end
220
-
221
- should "a 404 should give an InvalidRequestError with http status, body, and JSON body" do
222
- response = test_response(test_missing_id_error, 404)
223
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
224
- begin
225
- Stripe::Customer.retrieve("foo")
226
- rescue Stripe::InvalidRequestError => e
227
- assert_equal(404, e.http_status)
228
- assert_equal(true, !!e.http_body)
229
- assert_equal(true, e.json_body.kind_of?(Hash))
230
- end
231
- end
232
-
233
- should "setting a nil value for a param should exclude that param from the request" do
234
- @mock.expects(:get).with do |url, api_key, params|
235
- uri = URI(url)
236
- query = CGI.parse(uri.query)
237
- (url =~ %r{^#{Stripe.api_base}/v1/charges?} &&
238
- query.keys.sort == ['offset', 'sad'])
239
- end.returns(test_response({ :count => 1, :data => [test_charge] }))
240
- c = Stripe::Charge.all(:count => nil, :offset => 5, :sad => false)
241
-
242
- @mock.expects(:post).with do |url, api_key, params|
243
- url == "#{Stripe.api_base}/v1/charges" &&
244
- api_key.nil? &&
245
- CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
246
- end.returns(test_response({ :count => 1, :data => [test_charge] }))
247
- c = Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
248
- end
249
-
250
- should "requesting with a unicode ID should result in a request" do
251
- response = test_response(test_missing_id_error, 404)
252
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/%E2%98%83", nil, nil).raises(RestClient::ExceptionWithResponse.new(response, 404))
253
- c = Stripe::Customer.new("☃")
254
- assert_raises(Stripe::InvalidRequestError) { c.refresh }
255
- end
256
-
257
- should "requesting with no ID should result in an InvalidRequestError with no request" do
258
- c = Stripe::Customer.new
259
- assert_raises(Stripe::InvalidRequestError) { c.refresh }
260
- end
261
-
262
- should "making a GET request with parameters should have a query string and no body" do
263
- params = { :limit => 1 }
264
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/charges?limit=1", nil, nil).returns(test_response([test_charge]))
265
- c = Stripe::Charge.all(params)
266
- end
267
-
268
- should "making a POST request with parameters should have a body and no query string" do
269
- params = { :amount => 100, :currency => 'usd', :card => 'sc_token' }
270
- @mock.expects(:post).once.with do |url, get, post|
271
- get.nil? && CGI.parse(post) == {'amount' => ['100'], 'currency' => ['usd'], 'card' => ['sc_token']}
272
- end.returns(test_response(test_charge))
273
- c = Stripe::Charge.create(params)
274
- end
275
-
276
- should "loading an object should issue a GET request" do
277
- @mock.expects(:get).once.returns(test_response(test_customer))
278
- c = Stripe::Customer.new("test_customer")
279
- c.refresh
280
- end
281
-
282
- should "using array accessors should be the same as the method interface" do
283
- @mock.expects(:get).once.returns(test_response(test_customer))
284
- c = Stripe::Customer.new("test_customer")
285
- c.refresh
286
- assert_equal c.created, c[:created]
287
- assert_equal c.created, c['created']
288
- c['created'] = 12345
289
- assert_equal c.created, 12345
290
- end
291
-
292
- should "accessing a property other than id or parent on an unfetched object should fetch it" do
293
- @mock.expects(:get).once.returns(test_response(test_customer))
294
- c = Stripe::Customer.new("test_customer")
295
- c.charges
296
- end
297
-
298
- should "updating an object should issue a POST request with only the changed properties" do
299
- @mock.expects(:post).with do |url, api_key, params|
300
- url == "#{Stripe.api_base}/v1/customers/c_test_customer" && api_key.nil? && CGI.parse(params) == {'description' => ['another_mn']}
301
- end.once.returns(test_response(test_customer))
302
- c = Stripe::Customer.construct_from(test_customer)
303
- c.description = "another_mn"
304
- c.save
305
- end
306
-
307
- should "updating should merge in returned properties" do
308
- @mock.expects(:post).once.returns(test_response(test_customer))
309
- c = Stripe::Customer.new("c_test_customer")
310
- c.description = "another_mn"
311
- c.save
312
- assert_equal false, c.livemode
313
- end
314
-
315
- should "deleting should send no props and result in an object that has no props other deleted" do
316
- @mock.expects(:get).never
317
- @mock.expects(:post).never
318
- @mock.expects(:delete).with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
319
-
320
- c = Stripe::Customer.construct_from(test_customer)
321
- c.delete
322
- assert_equal true, c.deleted
323
-
324
- assert_raises NoMethodError do
325
- c.livemode
326
- end
327
- end
328
-
329
- should "loading an object with properties that have specific types should instantiate those classes" do
330
- @mock.expects(:get).once.returns(test_response(test_charge))
331
- c = Stripe::Charge.retrieve("test_charge")
332
- assert c.card.kind_of?(Stripe::StripeObject) && c.card.object == 'card'
333
- end
334
-
335
- should "loading all of an APIResource should return an array of recursively instantiated objects" do
336
- @mock.expects(:get).once.returns(test_response(test_charge_array))
337
- c = Stripe::Charge.all.data
338
- assert c.kind_of? Array
339
- assert c[0].kind_of? Stripe::Charge
340
- assert c[0].card.kind_of?(Stripe::StripeObject) && c[0].card.object == 'card'
341
- end
342
-
343
- context "account tests" do
344
- should "account should be retrievable" do
345
- resp = {:email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
346
- @mock.expects(:get).once.returns(test_response(resp))
347
- a = Stripe::Account.retrieve
348
- assert_equal "test+bindings@stripe.com", a.email
349
- assert !a.charge_enabled
350
- assert !a.details_submitted
351
- end
352
- end
353
-
354
- context "balance tests" do
355
- should "balance should be retrievable" do
356
- @mock.expects(:get).once.returns(test_response(test_balance))
357
- b = Stripe::Balance.retrieve
358
- assert_equal 12345, b.pending.first.amount
359
- assert_equal 6789, b.available.first.amount
360
- end
361
- end
362
-
363
- context "balance transaction tests" do
364
- should "balance transactions should be listable" do
365
- @mock.expects(:get).once.returns(test_response(test_balance_transaction_array))
366
- bt = Stripe::BalanceTransaction.all
367
- assert bt.data.kind_of?(Array)
368
- bt.each do |balance_transaction|
369
- assert balance_transaction.kind_of?(Stripe::BalanceTransaction)
370
- end
371
- end
372
- end
373
-
374
- context "list tests" do
375
- should "be able to retrieve full lists given a listobject" do
376
- @mock.expects(:get).twice.returns(test_response(test_charge_array))
377
- c = Stripe::Charge.all
378
- assert c.kind_of?(Stripe::ListObject)
379
- assert_equal('/v1/charges', c.url)
380
- all = c.all
381
- assert all.kind_of?(Stripe::ListObject)
382
- assert_equal('/v1/charges', all.url)
383
- assert all.data.kind_of?(Array)
384
- end
385
- end
386
-
387
- context "charge tests" do
388
-
389
- should "charges should be listable" do
390
- @mock.expects(:get).once.returns(test_response(test_charge_array))
391
- c = Stripe::Charge.all
392
- assert c.data.kind_of? Array
393
- c.each do |charge|
394
- assert charge.kind_of?(Stripe::Charge)
395
- end
396
- end
397
-
398
- should "charges should be refundable" do
399
- @mock.expects(:get).never
400
- @mock.expects(:post).once.returns(test_response({:id => "ch_test_charge", :refunded => true}))
401
- c = Stripe::Charge.new("test_charge")
402
- c.refund
403
- assert c.refunded
404
- end
405
-
406
- should "charges should not be deletable" do
407
- assert_raises NoMethodError do
408
- @mock.expects(:get).once.returns(test_response(test_charge))
409
- c = Stripe::Charge.retrieve("test_charge")
410
- c.delete
411
- end
412
- end
413
-
414
- should "charges should be updateable" do
415
- @mock.expects(:get).once.returns(test_response(test_charge))
416
- @mock.expects(:post).once.returns(test_response(test_charge))
417
- c = Stripe::Charge.new("test_charge")
418
- c.refresh
419
- c.description = "New charge description"
420
- c.save
421
- end
422
-
423
- should "charge id should not be changeable" do
424
- @mock.expects(:get).once.returns(test_response(test_charge))
425
- c = Stripe::Charge.new("test_charge")
426
- c.refresh
427
- assert_raises NoMethodError do
428
- c.id = "my new id"
429
- end
430
- end
431
-
432
- should "charge descriptions should not be settable to an empty string" do
433
- @mock.expects(:get).once.returns(test_response(test_charge))
434
- c = Stripe::Charge.new("test_charge")
435
- c.refresh
436
- assert_raises ArgumentError do
437
- c.description = ""
438
- end
439
- end
440
-
441
- should "charges descriptions should pass nil as an empty string" do
442
- @mock.expects(:get).once.returns(test_response(test_charge))
443
- @mock.expects(:post).once.with do |url, api_key, params|
444
- params == 'description='
445
- end.returns(test_response(test_charge))
446
- c = Stripe::Charge.new("test_charge")
447
- c.refresh
448
- c.description = nil
449
- c.save
450
- end
451
-
452
- should "charges should have Card objects associated with their Card property" do
453
- @mock.expects(:get).once.returns(test_response(test_charge))
454
- c = Stripe::Charge.retrieve("test_charge")
455
- assert c.card.kind_of?(Stripe::StripeObject) && c.card.object == 'card'
456
- end
457
-
458
- should "execute should return a new, fully executed charge when passed correct parameters" do
459
- @mock.expects(:post).with do |url, api_key, params|
460
- url == "#{Stripe.api_base}/v1/charges" && api_key.nil? && CGI.parse(params) == {
461
- 'currency' => ['usd'], 'amount' => ['100'],
462
- 'card[exp_year]' => ['2012'],
463
- 'card[number]' => ['4242424242424242'],
464
- 'card[exp_month]' => ['11']
465
- }
466
- end.once.returns(test_response(test_charge))
467
-
468
- c = Stripe::Charge.create({
469
- :amount => 100,
470
- :card => {
471
- :number => "4242424242424242",
472
- :exp_month => 11,
473
- :exp_year => 2012,
474
- },
475
- :currency => "usd"
476
- })
477
- assert c.paid
478
- end
479
-
480
- end
481
-
482
- context "customer tests" do
483
-
484
- should "customers should be listable" do
485
- @mock.expects(:get).once.returns(test_response(test_customer_array))
486
- c = Stripe::Customer.all.data
487
- assert c.kind_of? Array
488
- assert c[0].kind_of? Stripe::Customer
489
- end
490
-
491
- should "customers should be deletable" do
492
- @mock.expects(:delete).once.returns(test_response(test_customer({:deleted => true})))
493
- c = Stripe::Customer.new("test_customer")
494
- c.delete
495
- assert c.deleted
496
- end
497
-
498
- should "customers should be updateable" do
499
- @mock.expects(:get).once.returns(test_response(test_customer({:description => "foo"})))
500
- @mock.expects(:post).once.returns(test_response(test_customer({:description => "bar"})))
501
- c = Stripe::Customer.new("test_customer").refresh
502
- assert_equal c.description, "foo"
503
- c.description = "bar"
504
- c.save
505
- assert_equal c.description, "bar"
506
- end
507
-
508
- should "create should return a new customer" do
509
- @mock.expects(:post).once.returns(test_response(test_customer))
510
- c = Stripe::Customer.create
511
- assert_equal "c_test_customer", c.id
512
- end
513
-
514
- should "be able to update a customer's subscription" do
515
- @mock.expects(:get).once.returns(test_response(test_customer))
516
- c = Stripe::Customer.retrieve("test_customer")
517
-
518
- @mock.expects(:post).once.with do |url, api_key, params|
519
- url == "#{Stripe.api_base}/v1/customers/c_test_customer/subscription" && api_key.nil? && CGI.parse(params) == {'plan' => ['silver']}
520
- end.returns(test_response(test_subscription('silver')))
521
- s = c.update_subscription({:plan => 'silver'})
522
-
523
- assert_equal 'subscription', s.object
524
- assert_equal 'silver', s.plan.identifier
525
- end
526
-
527
- should "be able to cancel a customer's subscription" do
528
- @mock.expects(:get).once.returns(test_response(test_customer))
529
- c = Stripe::Customer.retrieve("test_customer")
530
-
531
- # Not an accurate response, but whatever
532
-
533
- @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription?at_period_end=true", nil, nil).returns(test_response(test_subscription('silver')))
534
- s = c.cancel_subscription({:at_period_end => 'true'})
535
-
536
- @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription", nil, nil).returns(test_response(test_subscription('silver')))
537
- s = c.cancel_subscription
538
- end
539
-
540
- should "be able to delete a customer's discount" do
541
- @mock.expects(:get).once.returns(test_response(test_customer))
542
- c = Stripe::Customer.retrieve("test_customer")
543
-
544
- @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/discount", nil, nil).returns(test_response(test_delete_discount_response))
545
- s = c.delete_discount
546
- assert_equal nil, c.discount
547
- end
548
-
549
- should "be able to update a customer without refreshing it first" do
550
- @mock.expects(:post).once.with("#{Stripe.api_base}/v1/customers/test_customer", nil, 'description=bar').returns(test_response(test_customer({:description => "bar"})))
551
- c = Stripe::Customer.new("test_customer")
552
- c.description = "bar"
553
- c.save
554
- assert_equal c.description, "bar"
555
- end
556
-
557
- end
558
-
559
- context "card tests" do
560
- should "be able to create a new card for a customer" do
561
- @mock.expects(:get).once.returns(test_response(test_customer))
562
- customer = Stripe::Customer.retrieve("test_customer")
563
-
564
- @mock.expects(:post).once.returns(test_response(test_card))
565
- card = customer.cards.create(:card => 'card')
566
- assert card.kind_of? Stripe::Card
567
- end
568
-
569
- should "be able to retrieve a card for a customer" do
570
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
571
- customer = Stripe::Customer.retrieve("c_test_customer")
572
-
573
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, nil).returns(test_response(test_card))
574
- card = customer.cards.retrieve("cc_test_card")
575
- assert card.kind_of? Stripe::Card
576
- end
577
-
578
- should "be able to list all cards for a customer" do
579
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
580
- customer = Stripe::Customer.retrieve("c_test_customer")
581
-
582
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards", nil, nil).returns(test_response(test_card_array("c_test_customer")))
583
- cards = customer.cards.all()
584
- assert cards.data.kind_of? Array
585
- cards.each do |card|
586
- assert card.kind_of?(Stripe::Card)
587
- end
588
- end
589
-
590
- should "be able to update a card for a customer" do
591
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
592
- customer = Stripe::Customer.retrieve("c_test_customer")
593
- @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, nil).returns(test_response(test_card))
594
- card = customer.cards.retrieve("cc_test_card")
595
-
596
- @mock.expects(:post).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, "address_zip=zippy").returns(test_response(test_card(:address_zip => 'zippy')))
597
- card.address_zip = "zippy"
598
- card.save
599
-
600
- assert_equal "zippy", card.address_zip
601
- end
602
- end
603
-
604
-
605
- context "coupon tests" do
606
- should "create should return a new coupon" do
607
- @mock.expects(:post).once.returns(test_response(test_coupon))
608
- c = Stripe::Coupon.create
609
- assert_equal "co_test_coupon", c.id
610
- end
611
- end
612
-
613
- context "invoice tests" do
614
- should "retrieve should retrieve invoices" do
615
- @mock.expects(:get).once.returns(test_response(test_invoice))
616
- i = Stripe::Invoice.retrieve('in_test_invoice')
617
- assert_equal 'in_test_invoice', i.id
618
- end
619
-
620
- should "pay should pay an invoice" do
621
- @mock.expects(:get).once.returns(test_response(test_invoice))
622
- i = Stripe::Invoice.retrieve('in_test_invoice')
623
-
624
- @mock.expects(:post).once.with('https://api.stripe.com/v1/invoices/in_test_invoice/pay', nil, '').returns(test_response(test_paid_invoice))
625
- i.pay
626
- assert_equal i.next_payment_attempt, nil
627
- end
628
- end
629
-
630
- context "transfer tests" do
631
-
632
- should "transfers should be listable" do
633
- @mock.expects(:get).once.returns(test_response(test_transfer_array))
634
- t = Stripe::Transfer.all
635
- assert t.data.kind_of? Array
636
- t.each do |transfer|
637
- assert transfer.kind_of?(Stripe::Transfer)
638
- end
639
- end
640
-
641
- should "transfers should not be deletable" do
642
- assert_raises NoMethodError do
643
- @mock.expects(:get).once.returns(test_response(test_transfer))
644
- t = Stripe::Transfer.retrieve("test_transfer")
645
- t.delete
646
- end
647
- end
648
-
649
- should "transfers should have BankAccount objects associated with their account property" do
650
- @mock.expects(:get).once.returns(test_response(test_transfer))
651
- t = Stripe::Transfer.retrieve("test_transfer")
652
- assert t.account.kind_of?(Stripe::StripeObject) && t.account.object == 'bank_account'
653
- end
654
-
655
- should "create a transfer should return a new transfer when passed correct parameters" do
656
- @mock.expects(:post).with do |url, api_key, params|
657
- url == "#{Stripe.api_base}/v1/transfers" && api_key.nil? && CGI.parse(params) == {
658
- 'currency' => ['usd'], 'amount' => ['100'],
659
- 'recipient' => ['test_recipient']
660
- }
661
- end.once.returns(test_response(test_transfer))
662
-
663
- t = Stripe::Transfer.create({
664
- :amount => 100,
665
- :currency => "usd",
666
- :recipient => "test_recipient"
667
- })
668
-
669
- assert_equal 'pending', t.status
670
- end
671
-
672
- end
673
-
674
- context "recipient tests" do
675
-
676
- should "recipients should be listable" do
677
- @mock.expects(:get).once.returns(test_response(test_recipient_array))
678
- r = Stripe::Recipient.all.data
679
- assert r.kind_of? Array
680
- assert r[0].kind_of? Stripe::Recipient
681
- end
682
-
683
- should "recipients should be deletable" do
684
- @mock.expects(:delete).once.returns(test_response(test_recipient({:deleted => true})))
685
- r = Stripe::Recipient.new("test_recipient")
686
- r.delete
687
- assert r.deleted
688
- end
689
-
690
- should "recipients should be updateable" do
691
- @mock.expects(:get).once.returns(test_response(test_recipient({:description => "foo"})))
692
- @mock.expects(:post).once.returns(test_response(test_recipient({:description => "bar"})))
693
- r = Stripe::Recipient.new("test_recipient").refresh
694
- assert_equal r.description, "foo"
695
- r.description = "bar"
696
- r.save
697
- assert_equal r.description, "bar"
698
- end
699
-
700
- should "recipients should have BankAccount objects associated with their active_account property" do
701
- @mock.expects(:get).once.returns(test_response(test_recipient))
702
- r = Stripe::Recipient.retrieve("test_recipient")
703
- assert r.active_account.kind_of?(Stripe::StripeObject) && r.active_account.object == 'bank_account'
704
- end
705
-
706
- should "create should return a new recipient" do
707
- @mock.expects(:post).once.returns(test_response(test_recipient))
708
- r = Stripe::Recipient.create(:name => 'Stripe User', :type => 'individual')
709
- assert_equal "rp_test_recipient", r.id
710
- end
711
- end
712
-
713
- context "error checking" do
714
-
715
- should "404s should raise an InvalidRequestError" do
716
- response = test_response(test_missing_id_error, 404)
717
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
718
-
719
- begin
720
- Stripe::Customer.new("test_customer").refresh
721
- assert false #shouldn't get here either
722
- rescue Stripe::InvalidRequestError => e # we don't use assert_raises because we want to examine e
723
- assert e.kind_of? Stripe::InvalidRequestError
724
- assert_equal "id", e.param
725
- assert_equal "Missing id", e.message
726
- return
727
- end
728
-
729
- assert false #shouldn't get here
730
- end
731
-
732
- should "5XXs should raise an APIError" do
733
- response = test_response(test_api_error, 500)
734
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
735
-
736
- begin
737
- Stripe::Customer.new("test_customer").refresh
738
- assert false #shouldn't get here either
739
- rescue Stripe::APIError => e # we don't use assert_raises because we want to examine e
740
- assert e.kind_of? Stripe::APIError
741
- return
742
- end
743
-
744
- assert false #shouldn't get here
745
- end
746
-
747
- should "402s should raise a CardError" do
748
- response = test_response(test_invalid_exp_year_error, 402)
749
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 402))
750
-
751
- begin
752
- Stripe::Customer.new("test_customer").refresh
753
- assert false #shouldn't get here either
754
- rescue Stripe::CardError => e # we don't use assert_raises because we want to examine e
755
- assert e.kind_of? Stripe::CardError
756
- assert_equal "invalid_expiry_year", e.code
757
- assert_equal "exp_year", e.param
758
- assert_equal "Your card's expiration year is invalid", e.message
759
- return
760
- end
761
-
762
- assert false #shouldn't get here
763
- end
764
- end
765
- end
766
- end
767
- end