stripe-ruby-mock 2.3.1 → 2.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/.env +2 -0
  3. data/.travis.yml +8 -4
  4. data/README.md +12 -5
  5. data/lib/stripe_mock/api/account_balance.rb +14 -0
  6. data/lib/stripe_mock/api/client.rb +4 -4
  7. data/lib/stripe_mock/api/conversion_rate.rb +14 -0
  8. data/lib/stripe_mock/api/errors.rb +25 -13
  9. data/lib/stripe_mock/api/instance.rb +16 -6
  10. data/lib/stripe_mock/api/webhooks.rb +8 -1
  11. data/lib/stripe_mock/client.rb +18 -2
  12. data/lib/stripe_mock/data/list.rb +14 -2
  13. data/lib/stripe_mock/data.rb +398 -58
  14. data/lib/stripe_mock/instance.rb +105 -9
  15. data/lib/stripe_mock/request_handlers/accounts.rb +41 -2
  16. data/lib/stripe_mock/request_handlers/balance.rb +17 -0
  17. data/lib/stripe_mock/request_handlers/balance_transactions.rb +18 -2
  18. data/lib/stripe_mock/request_handlers/charges.rb +44 -33
  19. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  20. data/lib/stripe_mock/request_handlers/coupons.rb +5 -4
  21. data/lib/stripe_mock/request_handlers/customers.rb +29 -11
  22. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  23. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  24. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +1 -1
  25. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +9 -7
  26. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -6
  27. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  28. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +59 -16
  29. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +3 -2
  30. data/lib/stripe_mock/request_handlers/invoices.rb +93 -14
  31. data/lib/stripe_mock/request_handlers/orders.rb +5 -5
  32. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  33. data/lib/stripe_mock/request_handlers/plans.rb +1 -0
  34. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  35. data/lib/stripe_mock/request_handlers/recipients.rb +12 -0
  36. data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
  37. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  38. data/lib/stripe_mock/request_handlers/subscriptions.rb +101 -39
  39. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  40. data/lib/stripe_mock/request_handlers/tokens.rb +9 -3
  41. data/lib/stripe_mock/request_handlers/transfers.rb +11 -5
  42. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +15 -1
  43. data/lib/stripe_mock/server.rb +14 -1
  44. data/lib/stripe_mock/test_strategies/base.rb +10 -5
  45. data/lib/stripe_mock/test_strategies/live.rb +5 -0
  46. data/lib/stripe_mock/test_strategies/mock.rb +8 -0
  47. data/lib/stripe_mock/util.rb +8 -2
  48. data/lib/stripe_mock/version.rb +1 -1
  49. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  50. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  51. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  52. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  53. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  54. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  55. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  56. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +40 -10
  57. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +39 -10
  58. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +39 -10
  59. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +40 -11
  60. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  61. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  62. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +92 -85
  63. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  64. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  65. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  66. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  67. data/lib/stripe_mock.rb +15 -0
  68. data/spec/api/instance_spec.rb +30 -0
  69. data/spec/instance_spec.rb +54 -4
  70. data/spec/integration_examples/prepare_error_examples.rb +6 -6
  71. data/spec/list_spec.rb +27 -10
  72. data/spec/readme_spec.rb +2 -0
  73. data/spec/server_spec.rb +7 -3
  74. data/spec/shared_stripe_examples/account_examples.rb +46 -0
  75. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  76. data/spec/shared_stripe_examples/balance_transaction_examples.rb +28 -0
  77. data/spec/shared_stripe_examples/bank_examples.rb +28 -1
  78. data/spec/shared_stripe_examples/card_examples.rb +23 -5
  79. data/spec/shared_stripe_examples/card_token_examples.rb +1 -0
  80. data/spec/shared_stripe_examples/charge_examples.rb +131 -26
  81. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  82. data/spec/shared_stripe_examples/coupon_examples.rb +8 -2
  83. data/spec/shared_stripe_examples/customer_examples.rb +90 -0
  84. data/spec/shared_stripe_examples/dispute_examples.rb +19 -8
  85. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  86. data/spec/shared_stripe_examples/error_mock_examples.rb +15 -5
  87. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  88. data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
  89. data/spec/shared_stripe_examples/invoice_examples.rb +314 -51
  90. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  91. data/spec/shared_stripe_examples/plan_examples.rb +47 -4
  92. data/spec/shared_stripe_examples/product_example.rb +65 -0
  93. data/spec/shared_stripe_examples/recipient_examples.rb +13 -7
  94. data/spec/shared_stripe_examples/refund_examples.rb +453 -84
  95. data/spec/shared_stripe_examples/subscription_examples.rb +477 -32
  96. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  97. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  98. data/spec/shared_stripe_examples/transfer_examples.rb +72 -23
  99. data/spec/shared_stripe_examples/webhook_event_examples.rb +74 -5
  100. data/spec/spec_helper.rb +7 -6
  101. data/spec/stripe_mock_spec.rb +16 -3
  102. data/spec/support/stripe_examples.rb +8 -1
  103. data/spec/util_spec.rb +35 -1
  104. data/stripe-ruby-mock.gemspec +1 -1
  105. metadata +44 -8
  106. data/ChangeLog.rdoc +0 -4
@@ -3,6 +3,8 @@ require 'pp'
3
3
 
4
4
  shared_examples 'Dispute API' do
5
5
 
6
+ let(:stripe_helper) { StripeMock.create_test_helper }
7
+
6
8
  it "returns an error if dispute does not exist" do
7
9
  dispute_id = 'dp_xxxxxxxxxxxxxxxxxxxxxxxx'
8
10
 
@@ -18,7 +20,7 @@ shared_examples 'Dispute API' do
18
20
  dispute_id = 'dp_05RsQX2eZvKYlo2C0FRTGSSA'
19
21
  dispute = Stripe::Dispute.retrieve(dispute_id)
20
22
 
21
- expect(dispute).to be_a(Stripe::Dispute)
23
+ expect(dispute).to be_a(Stripe::Dispute)
22
24
  expect(dispute.id).to eq(dispute_id)
23
25
  end
24
26
 
@@ -36,7 +38,7 @@ shared_examples 'Dispute API' do
36
38
  :customer_name => 'Rebel Idealist',
37
39
  :product_description => 'Lorem ipsum dolor sit amet.',
38
40
  :shipping_documentation => 'fil_15BZxW2eZvKYlo2CvQbrn9dc',
39
- }
41
+ }
40
42
  dispute.save
41
43
 
42
44
  dispute = Stripe::Dispute.retrieve(dispute_id)
@@ -50,13 +52,13 @@ shared_examples 'Dispute API' do
50
52
 
51
53
  it "closes a dispute" do
52
54
  dispute_id = 'dp_75RsQX2eZvKYlo2C0EDCXSWQ'
53
-
55
+
54
56
  dispute = Stripe::Dispute.retrieve(dispute_id)
55
-
57
+
56
58
  expect(dispute).to be_a(Stripe::Dispute)
57
59
  expect(dispute.id).to eq(dispute_id)
58
60
  expect(dispute.status).to eq('under_review')
59
-
61
+
60
62
  dispute.close
61
63
 
62
64
  dispute = Stripe::Dispute.retrieve(dispute_id)
@@ -67,7 +69,7 @@ shared_examples 'Dispute API' do
67
69
  end
68
70
 
69
71
  describe "listing disputes" do
70
-
72
+
71
73
  it "retrieves all disputes" do
72
74
  disputes = Stripe::Dispute.all
73
75
 
@@ -79,9 +81,18 @@ shared_examples 'Dispute API' do
79
81
  disputes = Stripe::Dispute.all(limit: 3)
80
82
 
81
83
  expect(disputes.count).to eq(3)
82
- expect(disputes.map &:id).to include('dp_05RsQX2eZvKYlo2C0FRTGSSA','dp_15RsQX2eZvKYlo2C0ERTYUIA', 'dp_25RsQX2eZvKYlo2C0ZXCVBNM')
84
+ expected = ['dp_95RsQX2eZvKYlo2C0EDFRYUI','dp_85RsQX2eZvKYlo2C0UJMCDET', 'dp_75RsQX2eZvKYlo2C0EDCXSWQ']
85
+ expect(disputes.map &:id).to include(*expected)
83
86
  end
84
87
 
85
88
  end
86
-
89
+
90
+ it "creates a dispute" do
91
+ card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
92
+ charge = Stripe::Charge.create(amount: 1000, currency: "usd", source: card_token)
93
+ stripe_dispute_id = stripe_helper.upsert_stripe_object(:dispute, {amount: charge.amount, charge: charge.id})
94
+ stripe_dispute = Stripe::Dispute.retrieve(stripe_dispute_id)
95
+ expect(stripe_dispute.charge).to eq(charge.id)
96
+ end
97
+
87
98
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'EphemeralKey API' do
4
+ describe 'Create a new key' do
5
+ let(:customer) { Stripe::Customer.create email: 'test@example.com' }
6
+ let(:version) { '2016-07-06' }
7
+
8
+ it 'creates a new key' do
9
+ key = Stripe::EphemeralKey.create(
10
+ { customer: customer.id },
11
+ { stripe_version: version }
12
+ )
13
+
14
+ expect(key[:associated_objects][0][:id]).to eq customer.id
15
+ end
16
+ end
17
+ end
@@ -12,7 +12,7 @@ end
12
12
  shared_examples 'Stripe Error Mocking' do
13
13
 
14
14
  it "mocks a manually given stripe card error" do
15
- error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', 444, 'body', 'json body')
15
+ error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', http_status: 444, http_body: 'body', json_body: 'json body')
16
16
  StripeMock.prepare_error(error)
17
17
 
18
18
  expect { Stripe::Customer.create() }.to raise_error {|e|
@@ -30,7 +30,7 @@ shared_examples 'Stripe Error Mocking' do
30
30
 
31
31
  it "mocks a manually gives stripe invalid request error" do
32
32
 
33
- error = Stripe::InvalidRequestError.new('Test Invalid', 'param', 987, 'ibody', 'json ibody')
33
+ error = Stripe::InvalidRequestError.new('Test Invalid', 'param', http_status: 987, http_body: 'ibody', json_body: 'json ibody')
34
34
  StripeMock.prepare_error(error)
35
35
 
36
36
  expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to raise_error {|e|
@@ -46,7 +46,7 @@ shared_examples 'Stripe Error Mocking' do
46
46
 
47
47
 
48
48
  it "mocks a manually gives stripe invalid auth error" do
49
- error = Stripe::AuthenticationError.new('Bad Auth', 499, 'abody', 'json abody')
49
+ error = Stripe::AuthenticationError.new('Bad Auth', http_status: 499, http_body: 'abody', json_body: 'json abody')
50
50
  StripeMock.prepare_error(error)
51
51
 
52
52
  expect { stripe_helper.create_plan() }.to raise_error {|e|
@@ -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
@@ -0,0 +1,170 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'External Account API' do
4
+
5
+ it 'creates/returns a bank when using account.external_accounts.create given a bank token' do
6
+ account = Stripe::Account.create(id: 'test_account', type: 'custom', country: "US")
7
+ bank_token = stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099)
8
+ bank = account.external_accounts.create(external_account: bank_token)
9
+
10
+ expect(bank.account).to eq('test_account')
11
+ expect(bank.last4).to eq("1123")
12
+ expect(bank.exp_month).to eq(11)
13
+ expect(bank.exp_year).to eq(2099)
14
+
15
+ account = Stripe::Account.retrieve('test_account')
16
+ expect(account.external_accounts.count).to eq(1)
17
+ bank = account.external_accounts.first
18
+ expect(bank.account).to eq('test_account')
19
+ expect(bank.last4).to eq("1123")
20
+ expect(bank.exp_month).to eq(11)
21
+ expect(bank.exp_year).to eq(2099)
22
+ end
23
+
24
+ it 'creates/returns a bank when using account.external_accounts.create given bank params' do
25
+ account = Stripe::Account.create(id: 'test_account', type: 'custom', country: "US")
26
+ bank = account.external_accounts.create(external_account: {
27
+ object: 'bank_account',
28
+ account_number: '000123456789',
29
+ routing_number: '110000000',
30
+ country: 'US',
31
+ currency: 'usd'
32
+ })
33
+
34
+ expect(bank.account).to eq('test_account')
35
+ expect(bank.routing_number).to eq('110000000')
36
+ expect(bank.country).to eq('US')
37
+ expect(bank.currency).to eq('usd')
38
+
39
+ account = Stripe::Account.retrieve('test_account')
40
+ expect(account.external_accounts.count).to eq(1)
41
+ bank = account.external_accounts.first
42
+ expect(bank.account).to eq('test_account')
43
+ expect(bank.routing_number).to eq('110000000')
44
+ expect(bank.country).to eq('US')
45
+ expect(bank.currency).to eq('usd')
46
+ end
47
+
48
+ it "creates a single bank with a generated bank token" do
49
+ account = Stripe::Account.create(type: 'custom', country: "US")
50
+ expect(account.external_accounts.count).to eq 0
51
+
52
+ account.external_accounts.create external_account: stripe_helper.generate_bank_token
53
+ # Yes, stripe-ruby does not actually add the new bank to the account instance
54
+ expect(account.external_accounts.count).to eq 0
55
+
56
+ account2 = Stripe::Account.retrieve(account.id)
57
+ expect(account2.external_accounts.count).to eq 1
58
+ end
59
+
60
+ describe "retrieval and deletion with accounts" do
61
+ let!(:account) { Stripe::Account.create(id: 'test_account', type: 'custom', country: "US") }
62
+ let!(:bank_token) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) }
63
+ let!(:bank) { account.external_accounts.create(external_account: bank_token) }
64
+
65
+ it "can retrieve all account's banks" do
66
+ retrieved = account.external_accounts.all
67
+ expect(retrieved.count).to eq(1)
68
+ end
69
+
70
+ it "retrieves an account bank" do
71
+ retrieved = account.external_accounts.retrieve(bank.id)
72
+ expect(retrieved.to_s).to eq(bank.to_s)
73
+ end
74
+
75
+ it "retrieves an account's bank after re-fetching the account" do
76
+ retrieved = Stripe::Account.retrieve(account.id).external_accounts.retrieve(bank.id)
77
+ expect(retrieved.id).to eq bank.id
78
+ end
79
+
80
+ it "deletes an accounts bank" do
81
+ bank.delete
82
+ retrieved_acct = Stripe::Account.retrieve(account.id)
83
+ expect(retrieved_acct.external_accounts.data).to be_empty
84
+ end
85
+
86
+ context "deletion when the user has two external accounts" do
87
+ let!(:bank_token_2) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) }
88
+ let!(:bank_2) { account.external_accounts.create(external_account: bank_token_2) }
89
+
90
+ it "has just one bank anymore" do
91
+ bank.delete
92
+ retrieved_acct = Stripe::Account.retrieve(account.id)
93
+ expect(retrieved_acct.external_accounts.data.count).to eq 1
94
+ expect(retrieved_acct.external_accounts.data.first.id).to eq bank_2.id
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "Errors" do
100
+ it "throws an error when the account does not have the retrieving bank id" do
101
+ account = Stripe::Account.create(type: 'custom', country: "US")
102
+ bank_id = "bank_123"
103
+ expect { account.external_accounts.retrieve(bank_id) }.to raise_error {|e|
104
+ expect(e).to be_a Stripe::InvalidRequestError
105
+ expect(e.message).to match /no.*source/i
106
+ expect(e.message).to include bank_id
107
+ expect(e.param).to eq 'id'
108
+ expect(e.http_status).to eq 404
109
+ }
110
+ end
111
+ end
112
+
113
+ context "update bank" do
114
+ let!(:account) { Stripe::Account.create(id: 'test_account', type: 'custom', country: "US") }
115
+ let!(:bank_token) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) }
116
+ let!(:bank) { account.external_accounts.create(external_account: bank_token) }
117
+
118
+ it "updates the bank" do
119
+ exp_month = 10
120
+ exp_year = 2098
121
+
122
+ bank.exp_month = exp_month
123
+ bank.exp_year = exp_year
124
+ bank.save
125
+
126
+ retrieved = account.external_accounts.retrieve(bank.id)
127
+
128
+ expect(retrieved.exp_month).to eq(exp_month)
129
+ expect(retrieved.exp_year).to eq(exp_year)
130
+ end
131
+ end
132
+
133
+ context "retrieve multiple banks" do
134
+
135
+ it "retrieves a list of multiple banks" do
136
+ account = Stripe::Account.create(id: 'test_account', type: 'custom', country: "US")
137
+
138
+ bank_token = stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099)
139
+ bank1 = account.external_accounts.create(external_accout: bank_token)
140
+ bank_token = stripe_helper.generate_bank_token(last4: "1124", exp_month: 12, exp_year: 2098)
141
+ bank2 = account.external_accounts.create(external_account: bank_token)
142
+
143
+ account = Stripe::Account.retrieve('test_account')
144
+
145
+ list = account.external_accounts.all
146
+
147
+ expect(list.object).to eq("list")
148
+ expect(list.count).to eq(2)
149
+ expect(list.data.length).to eq(2)
150
+
151
+ expect(list.data.first.object).to eq("bank_account")
152
+ expect(list.data.first.to_hash).to eq(bank1.to_hash)
153
+
154
+ expect(list.data.last.object).to eq("bank_account")
155
+ expect(list.data.last.to_hash).to eq(bank2.to_hash)
156
+ end
157
+
158
+ it "retrieves an empty list if there's no subscriptions" do
159
+ Stripe::Account.create(id: 'no_banks', type: 'custom', country: "US")
160
+ account = Stripe::Account.retrieve('no_banks')
161
+
162
+ list = account.external_accounts.all
163
+
164
+ expect(list.object).to eq("list")
165
+ expect(list.count).to eq(0)
166
+ expect(list.data.length).to eq(0)
167
+ end
168
+ end
169
+
170
+ end
@@ -8,6 +8,7 @@ shared_examples 'Extra Features' do
8
8
 
9
9
  customer = Stripe::Customer.create
10
10
  expect(customer.id).to match /^custom_prefix_cus/
11
+ StripeMock.global_id_prefix = nil
11
12
  end
12
13
 
13
14
  it "can set the global id prefix to nothing" do
@@ -23,6 +24,7 @@ shared_examples 'Extra Features' do
23
24
 
24
25
  customer = Stripe::Customer.create
25
26
  expect(customer.id).to match /^cus/
27
+ StripeMock.global_id_prefix = nil
26
28
  end
27
29
 
28
30
  it "has a default global id prefix" do