stripe-ruby-mock 2.3.1 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/stripe_mock.rb +2 -0
  4. data/lib/stripe_mock/api/errors.rb +2 -1
  5. data/lib/stripe_mock/api/instance.rb +10 -0
  6. data/lib/stripe_mock/api/webhooks.rb +3 -0
  7. data/lib/stripe_mock/data.rb +196 -17
  8. data/lib/stripe_mock/instance.rb +14 -3
  9. data/lib/stripe_mock/request_handlers/accounts.rb +7 -0
  10. data/lib/stripe_mock/request_handlers/charges.rb +24 -21
  11. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  12. data/lib/stripe_mock/request_handlers/customers.rb +3 -3
  13. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +5 -2
  14. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +2 -1
  15. data/lib/stripe_mock/request_handlers/recipients.rb +12 -0
  16. data/lib/stripe_mock/request_handlers/refunds.rb +88 -0
  17. data/lib/stripe_mock/request_handlers/subscriptions.rb +7 -6
  18. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +5 -0
  19. data/lib/stripe_mock/test_strategies/base.rb +7 -4
  20. data/lib/stripe_mock/version.rb +1 -1
  21. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  22. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  23. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  24. data/spec/api/instance_spec.rb +30 -0
  25. data/spec/list_spec.rb +11 -7
  26. data/spec/server_spec.rb +1 -1
  27. data/spec/shared_stripe_examples/account_examples.rb +11 -0
  28. data/spec/shared_stripe_examples/card_examples.rb +13 -2
  29. data/spec/shared_stripe_examples/card_token_examples.rb +1 -0
  30. data/spec/shared_stripe_examples/charge_examples.rb +64 -15
  31. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  32. data/spec/shared_stripe_examples/customer_examples.rb +38 -0
  33. data/spec/shared_stripe_examples/error_mock_examples.rb +12 -2
  34. data/spec/shared_stripe_examples/plan_examples.rb +11 -0
  35. data/spec/shared_stripe_examples/recipient_examples.rb +7 -1
  36. data/spec/shared_stripe_examples/refund_examples.rb +447 -84
  37. data/spec/shared_stripe_examples/subscription_examples.rb +15 -0
  38. data/spec/support/stripe_examples.rb +1 -0
  39. metadata +12 -3
@@ -0,0 +1,27 @@
1
+ {
2
+ "created":1326853478,
3
+ "livemode":false,
4
+ "id":"evt_00000000000000",
5
+ "type":"account.external_account.deleted",
6
+ "object":"event",
7
+ "data":{
8
+ "object":{
9
+ "id":"ba_00000000000000",
10
+ "object":"bank_account",
11
+ "account":"acct_00000000000000",
12
+ "account_holder_name":"Jane Austen",
13
+ "account_holder_type":"individual",
14
+ "bank_name":"STRIPE TEST BANK",
15
+ "country":"US",
16
+ "currency":"eur",
17
+ "default_for_currency":false,
18
+ "fingerprint":"efGCBmiwp56O1lsN",
19
+ "last4":"6789",
20
+ "metadata":{
21
+
22
+ },
23
+ "routing_number":"110000000",
24
+ "status":"new"
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "created":1326853478,
3
+ "livemode":false,
4
+ "id":"evt_00000000000000",
5
+ "type":"account.external_account.updated",
6
+ "object":"event",
7
+ "data":{
8
+ "object":{
9
+ "id":"ba_00000000000000",
10
+ "object":"bank_account",
11
+ "account":"acct_00000000000000",
12
+ "account_holder_name":"Jane Austen",
13
+ "account_holder_type":"individual",
14
+ "bank_name":"STRIPE TEST BANK",
15
+ "country":"US",
16
+ "currency":"eur",
17
+ "default_for_currency":false,
18
+ "fingerprint":"efGCBmiwp56O1lsN",
19
+ "last4":"6789",
20
+ "metadata":{
21
+
22
+ },
23
+ "routing_number":"110000000",
24
+ "status":"new"
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe StripeMock do
4
+ describe ".mock" do
5
+ it "yields the given block between starting and stopping StripeMock" do
6
+ expect(StripeMock.instance).to be_nil
7
+ expect(StripeMock.state).to eq "ready"
8
+
9
+ StripeMock.mock do
10
+ expect(StripeMock.instance).to be_instance_of StripeMock::Instance
11
+ expect(StripeMock.state).to eq "local"
12
+ end
13
+
14
+ expect(StripeMock.instance).to be_nil
15
+ expect(StripeMock.state).to eq "ready"
16
+ end
17
+
18
+ it "stops StripeMock if the given block raises an exception" do
19
+ expect(StripeMock.instance).to be_nil
20
+ begin
21
+ StripeMock.mock do
22
+ raise "Uh-oh..."
23
+ end
24
+ rescue
25
+ expect(StripeMock.instance).to be_nil
26
+ expect(StripeMock.state).to eq "ready"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,6 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe StripeMock::Data::List do
4
+ let(:stripe_helper) { StripeMock.create_test_helper }
5
+
4
6
  before :all do
5
7
  StripeMock.start
6
8
  end
@@ -33,9 +35,9 @@ describe StripeMock::Data::List do
33
35
  end
34
36
 
35
37
  it "eventually gets turned into a hash" do
36
- charge1 = Stripe::Charge.create(amount: 1, currency: 'usd')
37
- charge2 = Stripe::Charge.create(amount: 1, currency: 'usd')
38
- charge3 = Stripe::Charge.create(amount: 1, currency: 'usd')
38
+ charge1 = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
39
+ charge2 = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
40
+ charge3 = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
39
41
  list = StripeMock::Data::List.new([charge1, charge2, charge3])
40
42
  hash = list.to_h
41
43
 
@@ -95,15 +97,17 @@ describe StripeMock::Data::List do
95
97
 
96
98
  context "pagination" do
97
99
  it "has a has_more field when it has more" do
98
- list = StripeMock::Data::List.new([Stripe::Charge.create(amount: 1, currency: 'usd')] * 256)
100
+ list = StripeMock::Data::List.new(
101
+ [Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)] * 256
102
+ )
99
103
 
100
104
  expect(list).to have_more
101
105
  end
102
106
 
103
107
  it "accepts a starting_after parameter" do
104
108
  data = []
105
- 255.times { data << Stripe::Charge.create(amount: 1, currency: 'usd') }
106
- new_charge = Stripe::Charge.create(amount: 1, currency: 'usd')
109
+ 255.times { data << Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
110
+ new_charge = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
107
111
  data[89] = new_charge
108
112
  list = StripeMock::Data::List.new(data, starting_after: new_charge.id)
109
113
  hash = list.to_h
@@ -114,7 +118,7 @@ describe StripeMock::Data::List do
114
118
 
115
119
  it "raises an error if starting_after cursor is not found" do
116
120
  data = []
117
- 255.times { data << Stripe::Charge.create(amount: 1, currency: 'usd') }
121
+ 255.times { data << Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
118
122
  list = StripeMock::Data::List.new(data, starting_after: "test_ch_unknown")
119
123
 
120
124
  expect { list.to_h }.to raise_error
@@ -24,7 +24,7 @@ describe 'StripeMock Server', :mock_server => true do
24
24
  charge = Stripe::Charge.create(
25
25
  amount: 987,
26
26
  currency: 'USD',
27
- card: stripe_helper.generate_card_token,
27
+ source: stripe_helper.generate_card_token,
28
28
  description: 'card charge'
29
29
  )
30
30
  expect(charge.amount).to eq(987)
@@ -34,6 +34,9 @@ shared_examples 'Account API' do
34
34
  expect(account.keys).not_to be_nil
35
35
  expect(account.keys.secret).to match /sk_(live|test)_[\d\w]+/
36
36
  expect(account.keys.publishable).to match /pk_(live|test)_[\d\w]+/
37
+ expect(account.external_accounts).not_to be_nil
38
+ expect(account.external_accounts.data).to be_an Array
39
+ expect(account.external_accounts.url).to match /\/v1\/accounts\/.*\/external_accounts/
37
40
  end
38
41
  end
39
42
  describe 'updates account' do
@@ -47,4 +50,12 @@ shared_examples 'Account API' do
47
50
  expect(account.support_phone).to eq '1234567'
48
51
  end
49
52
  end
53
+
54
+ it 'deauthorizes the stripe account', live: false do
55
+ account = Stripe::Account.retrieve
56
+ result = account.deauthorize('CLIENT_ID')
57
+
58
+ expect(result).to be_a Stripe::StripeObject
59
+ expect(result[:stripe_user_id]).to eq account[:id]
60
+ end
50
61
  end
@@ -22,7 +22,13 @@ shared_examples 'Card API' do
22
22
  end
23
23
 
24
24
  it 'creates/returns a card when using recipient.cards.create given a card token' do
25
- recipient = Stripe::Recipient.create(id: 'test_recipient_sub')
25
+ params = {
26
+ id: 'test_recipient_sub',
27
+ name: 'MyRec',
28
+ type: 'individual'
29
+ }
30
+
31
+ recipient = Stripe::Recipient.create(params)
26
32
  card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
27
33
  card = recipient.cards.create(card: card_token)
28
34
 
@@ -64,7 +70,12 @@ shared_examples 'Card API' do
64
70
  end
65
71
 
66
72
  it 'creates/returns a card when using recipient.cards.create given card params' do
67
- recipient = Stripe::Recipient.create(id: 'test_recipient_sub')
73
+ params = {
74
+ id: 'test_recipient_sub',
75
+ name: 'MyRec',
76
+ type: 'individual'
77
+ }
78
+ recipient = Stripe::Recipient.create(params)
68
79
  card = recipient.cards.create(card: {
69
80
  number: '4000056655665556',
70
81
  exp_month: '11',
@@ -12,6 +12,7 @@ shared_examples 'Card Token Mocking' do
12
12
  expect(card.last4).to eq("4242")
13
13
  expect(card.exp_month).to eq(4)
14
14
  expect(card.exp_year).to eq(2016)
15
+ expect(card.tokenization_method).to eq(nil)
15
16
  end
16
17
 
17
18
  it "generates and reads a card token for create charge" do
@@ -12,11 +12,20 @@ shared_examples 'Charge API' do
12
12
  }.to raise_error(Stripe::InvalidRequestError, /token/i)
13
13
  end
14
14
 
15
+ it "requires a valid customer or source", :live => true do
16
+ expect {
17
+ charge = Stripe::Charge.create(
18
+ amount: 99,
19
+ currency: 'usd',
20
+ )
21
+ }.to raise_error(Stripe::InvalidRequestError, /Must provide source or customer/i)
22
+ end
23
+
15
24
  it "requires presence of amount", :live => true do
16
25
  expect {
17
26
  charge = Stripe::Charge.create(
18
27
  currency: 'usd',
19
- card: stripe_helper.generate_card_token
28
+ source: stripe_helper.generate_card_token
20
29
  )
21
30
  }.to raise_error(Stripe::InvalidRequestError, /missing required param: amount/i)
22
31
  end
@@ -25,7 +34,7 @@ shared_examples 'Charge API' do
25
34
  expect {
26
35
  charge = Stripe::Charge.create(
27
36
  amount: 99,
28
- card: stripe_helper.generate_card_token
37
+ source: stripe_helper.generate_card_token
29
38
  )
30
39
  }.to raise_error(Stripe::InvalidRequestError, /missing required param: currency/i)
31
40
  end
@@ -35,7 +44,7 @@ shared_examples 'Charge API' do
35
44
  charge = Stripe::Charge.create(
36
45
  amount: -99,
37
46
  currency: 'usd',
38
- card: stripe_helper.generate_card_token
47
+ source: stripe_helper.generate_card_token
39
48
  )
40
49
  }.to raise_error(Stripe::InvalidRequestError, /invalid positive integer/i)
41
50
  end
@@ -45,7 +54,7 @@ shared_examples 'Charge API' do
45
54
  charge = Stripe::Charge.create(
46
55
  amount: 99.0,
47
56
  currency: 'usd',
48
- card: stripe_helper.generate_card_token
57
+ source: stripe_helper.generate_card_token
49
58
  )
50
59
  }.to raise_error(Stripe::InvalidRequestError, /invalid integer/i)
51
60
  end
@@ -65,6 +74,21 @@ shared_examples 'Charge API' do
65
74
  expect(charge.status).to eq('succeeded')
66
75
  end
67
76
 
77
+ it "creates a stripe charge item with a bank token" do
78
+ charge = Stripe::Charge.create(
79
+ amount: 999,
80
+ currency: 'USD',
81
+ source: stripe_helper.generate_bank_token,
82
+ description: 'bank charge'
83
+ )
84
+
85
+ expect(charge.id).to match(/^test_ch/)
86
+ expect(charge.amount).to eq(999)
87
+ expect(charge.description).to eq('bank charge')
88
+ expect(charge.captured).to eq(true)
89
+ expect(charge.status).to eq('succeeded')
90
+ end
91
+
68
92
  it 'creates a stripe charge item with a customer', :live => true do
69
93
  customer = Stripe::Customer.create({
70
94
  email: 'johnny@appleseed.com',
@@ -138,6 +162,28 @@ shared_examples 'Charge API' do
138
162
  expect(data[charge2.id][:amount]).to eq(777)
139
163
  end
140
164
 
165
+ it "creates a balance transaction" do
166
+ charge = Stripe::Charge.create({
167
+ amount: 300,
168
+ currency: 'USD',
169
+ source: stripe_helper.generate_card_token
170
+ })
171
+ bal_trans = Stripe::BalanceTransaction.retrieve(charge.balance_transaction)
172
+ expect(bal_trans.amount).to eq(charge.amount)
173
+ expect(bal_trans.fee).to eq(39)
174
+ expect(bal_trans.source).to eq(charge.source)
175
+ end
176
+
177
+ it "can expand balance transaction" do
178
+ charge = Stripe::Charge.create({
179
+ amount: 300,
180
+ currency: 'USD',
181
+ source: stripe_helper.generate_card_token,
182
+ expand: ['balance_transaction']
183
+ })
184
+ expect(charge.balance_transaction).to be_a(Stripe::BalanceTransaction)
185
+ end
186
+
141
187
  it "retrieves a stripe charge" do
142
188
  original = Stripe::Charge.create({
143
189
  amount: 777,
@@ -237,14 +283,14 @@ shared_examples 'Charge API' do
237
283
  charge1 = Stripe::Charge.create(
238
284
  amount: 999,
239
285
  currency: 'USD',
240
- card: stripe_helper.generate_card_token,
286
+ source: stripe_helper.generate_card_token,
241
287
  description: 'card charge'
242
288
  )
243
289
 
244
290
  charge2 = Stripe::Charge.create(
245
291
  amount: 999,
246
292
  currency: 'USD',
247
- card: stripe_helper.generate_card_token,
293
+ source: stripe_helper.generate_card_token,
248
294
  description: 'card charge'
249
295
  )
250
296
 
@@ -254,8 +300,9 @@ shared_examples 'Charge API' do
254
300
  context "retrieving a list of charges" do
255
301
  before do
256
302
  @customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
303
+ @customer2 = Stripe::Customer.create(email: 'johnny2@appleseed.com')
257
304
  @charge = Stripe::Charge.create(amount: 1, currency: 'usd', customer: @customer.id)
258
- @charge2 = Stripe::Charge.create(amount: 1, currency: 'usd')
305
+ @charge2 = Stripe::Charge.create(amount: 1, currency: 'usd', customer: @customer2.id)
259
306
  end
260
307
 
261
308
  it "stores charges for a customer in memory" do
@@ -267,12 +314,13 @@ shared_examples 'Charge API' do
267
314
  end
268
315
 
269
316
  it "defaults count to 10 charges" do
270
- 11.times { Stripe::Charge.create(amount: 1, currency: 'usd') }
317
+ 11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
318
+
271
319
  expect(Stripe::Charge.all.data.count).to eq(10)
272
320
  end
273
321
 
274
322
  it "is marked as having more when more objects exist" do
275
- 11.times { Stripe::Charge.create(amount: 1, currency: 'usd') }
323
+ 11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
276
324
 
277
325
  expect(Stripe::Charge.all.has_more).to eq(true)
278
326
  end
@@ -314,7 +362,7 @@ shared_examples 'Charge API' do
314
362
  charge = Stripe::Charge.create({
315
363
  amount: 777,
316
364
  currency: 'USD',
317
- card: stripe_helper.generate_card_token
365
+ source: stripe_helper.generate_card_token
318
366
  })
319
367
 
320
368
  expect(charge.captured).to eq(true)
@@ -324,7 +372,7 @@ shared_examples 'Charge API' do
324
372
  charge = Stripe::Charge.create({
325
373
  amount: 777,
326
374
  currency: 'USD',
327
- card: stripe_helper.generate_card_token,
375
+ source: stripe_helper.generate_card_token,
328
376
  capture: true
329
377
  })
330
378
 
@@ -335,7 +383,7 @@ shared_examples 'Charge API' do
335
383
  charge = Stripe::Charge.create({
336
384
  amount: 777,
337
385
  currency: 'USD',
338
- card: stripe_helper.generate_card_token,
386
+ source: stripe_helper.generate_card_token,
339
387
  capture: false
340
388
  })
341
389
 
@@ -348,7 +396,7 @@ shared_examples 'Charge API' do
348
396
  charge = Stripe::Charge.create({
349
397
  amount: 777,
350
398
  currency: 'USD',
351
- card: stripe_helper.generate_card_token,
399
+ source: stripe_helper.generate_card_token,
352
400
  capture: false
353
401
  })
354
402
 
@@ -362,7 +410,7 @@ shared_examples 'Charge API' do
362
410
  charge = Stripe::Charge.create({
363
411
  amount: 777,
364
412
  currency: 'USD',
365
- card: stripe_helper.generate_card_token,
413
+ source: stripe_helper.generate_card_token,
366
414
  capture: false
367
415
  })
368
416
 
@@ -376,10 +424,11 @@ shared_examples 'Charge API' do
376
424
  end
377
425
 
378
426
  describe "idempotency" do
427
+ let(:customer) { Stripe::Customer.create(email: 'johnny@appleseed.com') }
379
428
  let(:idempotent_charge_params) {{
380
429
  amount: 777,
381
430
  currency: 'USD',
382
- card: stripe_helper.generate_card_token,
431
+ customer: customer.id,
383
432
  capture: true,
384
433
  idempotency_key: 'onceisenough'
385
434
  }}
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Country Spec API' do
4
+ context 'retrieve country', live: true do
5
+ it 'retrieves a stripe country spec' do
6
+ country = Stripe::CountrySpec.retrieve('US')
7
+
8
+ expect(country).to be_a Stripe::CountrySpec
9
+ expect(country.id).to match /US/
10
+ end
11
+
12
+ it "cannot retrieve a stripe country that doesn't exist" do
13
+ expect { Stripe::CountrySpec.retrieve('nope') }
14
+ .to raise_error(Stripe::InvalidRequestError, /(nope is not currently supported by Stripe)|(Country 'nope' is unknown)/)
15
+
16
+ end
17
+ end
18
+ end
@@ -24,6 +24,27 @@ shared_examples 'Customer API' do
24
24
  expect { customer.source }.to raise_error
25
25
  end
26
26
 
27
+ it "creates a stripe customer with multiple cards and updates the default card" do
28
+ card_a = gen_card_tk
29
+ card_b = gen_card_tk
30
+ customer = Stripe::Customer.create({
31
+ email: 'johnny.multiple@appleseed.com',
32
+ source: card_a,
33
+ description: "a description"
34
+ })
35
+
36
+ original_card = customer.sources.data.first.id
37
+
38
+ customer.sources.create(source: card_b)
39
+ retrieved_customer = Stripe::Customer.retrieve(customer.id)
40
+
41
+ expect(retrieved_customer.sources.data.length).to eq(2)
42
+ retrieved_customer.default_source = retrieved_customer.sources.data.last.id
43
+ retrieved_customer.save
44
+ expect(Stripe::Customer.retrieve(customer.id).default_source).to eq(retrieved_customer.sources.data.last.id)
45
+ expect(Stripe::Customer.retrieve(customer.id).default_source).to_not eq(original_card)
46
+ end
47
+
27
48
  it "creates a stripe customer without a card" do
28
49
  customer = Stripe::Customer.create({
29
50
  email: 'cardless@appleseed.com',
@@ -344,9 +365,26 @@ shared_examples 'Customer API' do
344
365
  expect(original.subscriptions.total_count).to eq(1)
345
366
  end
346
367
 
368
+ it "should add a customer to a subscription" do
369
+ plan = stripe_helper.create_plan(id: 'silver')
370
+ customer = Stripe::Customer.create(source: gen_card_tk)
371
+ customer.subscriptions.create(plan: plan.id)
372
+
373
+ expect(Stripe::Customer.retrieve(customer.id).subscriptions.total_count).to eq(1)
374
+ end
375
+
347
376
  it "deletes a customer" do
348
377
  customer = Stripe::Customer.create(id: 'test_customer_sub')
349
378
  customer = customer.delete
350
379
  expect(customer.deleted).to eq(true)
351
380
  end
381
+
382
+ it 'works with the update_subscription method' do
383
+ stripe_helper.create_plan(id: 'silver')
384
+ cus = Stripe::Customer.create(source: gen_card_tk)
385
+ expect {
386
+ cus.update_subscription(plan: 'silver')
387
+ }.not_to raise_error
388
+ end
389
+
352
390
  end