stripe-ruby-mock 2.3.0 → 2.4.0

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -0
  3. data/Gemfile +5 -0
  4. data/README.md +1 -1
  5. data/lib/stripe_mock/api/errors.rb +2 -1
  6. data/lib/stripe_mock/api/instance.rb +10 -0
  7. data/lib/stripe_mock/api/webhooks.rb +3 -0
  8. data/lib/stripe_mock/data.rb +203 -21
  9. data/lib/stripe_mock/instance.rb +14 -3
  10. data/lib/stripe_mock/request_handlers/accounts.rb +7 -0
  11. data/lib/stripe_mock/request_handlers/charges.rb +29 -21
  12. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  13. data/lib/stripe_mock/request_handlers/customers.rb +3 -3
  14. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +5 -2
  15. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +2 -1
  16. data/lib/stripe_mock/request_handlers/recipients.rb +12 -0
  17. data/lib/stripe_mock/request_handlers/refunds.rb +88 -0
  18. data/lib/stripe_mock/request_handlers/subscriptions.rb +48 -23
  19. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +5 -0
  20. data/lib/stripe_mock/test_strategies/base.rb +7 -4
  21. data/lib/stripe_mock/version.rb +1 -1
  22. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  23. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  24. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  25. data/lib/stripe_mock.rb +2 -0
  26. data/spec/api/instance_spec.rb +30 -0
  27. data/spec/list_spec.rb +11 -7
  28. data/spec/server_spec.rb +1 -1
  29. data/spec/shared_stripe_examples/account_examples.rb +11 -0
  30. data/spec/shared_stripe_examples/card_examples.rb +13 -2
  31. data/spec/shared_stripe_examples/card_token_examples.rb +1 -0
  32. data/spec/shared_stripe_examples/charge_examples.rb +90 -14
  33. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  34. data/spec/shared_stripe_examples/customer_examples.rb +38 -0
  35. data/spec/shared_stripe_examples/error_mock_examples.rb +12 -2
  36. data/spec/shared_stripe_examples/plan_examples.rb +19 -0
  37. data/spec/shared_stripe_examples/recipient_examples.rb +7 -1
  38. data/spec/shared_stripe_examples/refund_examples.rb +447 -84
  39. data/spec/shared_stripe_examples/subscription_examples.rb +28 -0
  40. data/spec/support/stripe_examples.rb +1 -0
  41. data/stripe-ruby-mock.gemspec +3 -3
  42. metadata +24 -21
@@ -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
 
@@ -375,4 +423,32 @@ shared_examples 'Charge API' do
375
423
  end
376
424
  end
377
425
 
426
+ describe "idempotency" do
427
+ let(:customer) { Stripe::Customer.create(email: 'johnny@appleseed.com') }
428
+ let(:idempotent_charge_params) {{
429
+ amount: 777,
430
+ currency: 'USD',
431
+ customer: customer.id,
432
+ capture: true,
433
+ idempotency_key: 'onceisenough'
434
+ }}
435
+
436
+ it "returns the original charge if the same idempotency_key is passed in" do
437
+ charge1 = Stripe::Charge.create(idempotent_charge_params)
438
+ charge2 = Stripe::Charge.create(idempotent_charge_params)
439
+
440
+ expect(charge1).to eq(charge2)
441
+ end
442
+
443
+ it "returns different charges if different idempotency_keys are used for each charge" do
444
+ idempotent_charge_params2 = idempotent_charge_params.clone
445
+ idempotent_charge_params2[:idempotency_key] = 'thisoneisdifferent'
446
+
447
+ charge1 = Stripe::Charge.create(idempotent_charge_params)
448
+ charge2 = Stripe::Charge.create(idempotent_charge_params2)
449
+
450
+ expect(charge1).not_to eq(charge2)
451
+ end
452
+ end
453
+
378
454
  end
@@ -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
@@ -64,7 +64,10 @@ shared_examples 'Stripe Error Mocking' do
64
64
  custom_error = StandardError.new("Please knock first.")
65
65
  StripeMock.prepare_error(custom_error, :new_customer)
66
66
 
67
- expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to_not raise_error
67
+ expect {
68
+ Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
69
+ }.to_not raise_error
70
+
68
71
  expect { Stripe::Customer.create }.to raise_error {|e|
69
72
  expect(e).to be_a StandardError
70
73
  expect(e.message).to eq("Please knock first.")
@@ -89,7 +92,9 @@ shared_examples 'Stripe Error Mocking' do
89
92
 
90
93
  it "mocks a card error with a given handler" do
91
94
  StripeMock.prepare_card_error(:incorrect_cvc, :new_customer)
92
- expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to_not raise_error
95
+ expect {
96
+ Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
97
+ }.to_not raise_error
93
98
 
94
99
  expect { Stripe::Customer.create() }.to raise_error {|e|
95
100
  expect(e).to be_a(Stripe::CardError)
@@ -149,4 +154,9 @@ shared_examples 'Stripe Error Mocking' do
149
154
  expect_card_error 'processing_error', nil
150
155
  end
151
156
 
157
+ it "mocks an incorrect zip code card error" do
158
+ StripeMock.prepare_card_error(:incorrect_zip)
159
+ expect_card_error 'incorrect_zip', 'address_zip'
160
+ end
161
+
152
162
  end
@@ -9,6 +9,10 @@ shared_examples 'Plan API' do
9
9
  :amount => 9900,
10
10
  :currency => 'USD',
11
11
  :interval => 1,
12
+ :metadata => {
13
+ :description => "desc text",
14
+ :info => "info text"
15
+ },
12
16
  :trial_period_days => 30
13
17
  )
14
18
 
@@ -18,6 +22,10 @@ shared_examples 'Plan API' do
18
22
 
19
23
  expect(plan.currency).to eq('USD')
20
24
  expect(plan.interval).to eq(1)
25
+
26
+ expect(plan.metadata.description).to eq('desc text')
27
+ expect(plan.metadata.info).to eq('info text')
28
+
21
29
  expect(plan.trial_period_days).to eq(30)
22
30
  end
23
31
 
@@ -110,6 +118,17 @@ shared_examples 'Plan API' do
110
118
  expect(all.count).to eq(100)
111
119
  end
112
120
 
121
+ it 'validates the amount' do
122
+ expect {
123
+ Stripe::Plan.create(
124
+ :id => 'pid_1',
125
+ :name => 'The Mock Plan',
126
+ :amount => 99.99,
127
+ :currency => 'USD',
128
+ :interval => 'month'
129
+ )
130
+ }.to raise_error(Stripe::InvalidRequestError, "Invalid integer: 99.99")
131
+ end
113
132
 
114
133
  describe "Validation", :live => true do
115
134
  let(:params) { stripe_helper.create_plan_params }
@@ -28,6 +28,13 @@ shared_examples 'Recipient API' do
28
28
  expect { recipient.card }.to raise_error
29
29
  end
30
30
 
31
+ it "raises a error if params are invalid" do
32
+ expect { Stripe::Recipient.create(name: "foo") }.to raise_error
33
+ expect { Stripe::Recipient.create(type: "individual") }.to raise_error
34
+ expect { Stripe::Recipient.create(name: "foo", type: "bar") }.to raise_error
35
+ expect { Stripe::Recipient.create(name: "foo", type: "individual") }.not_to raise_error
36
+ end
37
+
31
38
  it "creates a stripe recipient without a card" do
32
39
  recipient = Stripe::Recipient.create({
33
40
  type: "corporation",
@@ -109,4 +116,3 @@ shared_examples 'Recipient API' do
109
116
  end
110
117
 
111
118
  end
112
-