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
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Subscription Items API' do
4
+ let(:stripe_helper) { StripeMock.create_test_helper }
5
+ let(:plan) { stripe_helper.create_plan }
6
+ let(:plan2) { stripe_helper.create_plan(amount: 100, id: 'one_more_1_plan') }
7
+ let(:customer) { Stripe::Customer.create(source: stripe_helper.generate_card_token) }
8
+ let(:subscription) { Stripe::Subscription.create(customer: customer.id, items: [{ plan: plan.id }]) }
9
+
10
+ context 'creates an item' do
11
+ it 'when required params only' do
12
+ item = Stripe::SubscriptionItem.create(plan: plan.id, subscription: subscription.id)
13
+
14
+ expect(item.id).to match(/^test_si/)
15
+ expect(item.plan.id).to eq(plan.id)
16
+ expect(item.subscription).to eq(subscription.id)
17
+ end
18
+ it 'when no subscription params' do
19
+ expect { Stripe::SubscriptionItem.create(plan: plan.id) }.to raise_error { |e|
20
+ expect(e).to be_a(Stripe::InvalidRequestError)
21
+ expect(e.param).to eq('subscription')
22
+ expect(e.message).to eq('Missing required param: subscription.')
23
+ }
24
+ end
25
+ it 'when no plan params' do
26
+ expect { Stripe::SubscriptionItem.create(subscription: subscription.id) }.to raise_error { |e|
27
+ expect(e).to be_a(Stripe::InvalidRequestError)
28
+ expect(e.param).to eq('plan')
29
+ expect(e.message).to eq('Missing required param: plan.')
30
+ }
31
+ end
32
+ end
33
+
34
+ context 'updates an item' do
35
+ let(:item) { Stripe::SubscriptionItem.create(plan: plan.id, subscription: subscription.id, quantity: 2 ) }
36
+
37
+ it 'updates plan' do
38
+ updated_item = Stripe::SubscriptionItem.update(item.id, plan: plan2.id)
39
+
40
+ expect(updated_item.plan.id).to eq(plan2.id)
41
+ end
42
+ it 'updates quantity' do
43
+ updated_item = Stripe::SubscriptionItem.update(item.id, quantity: 23)
44
+
45
+ expect(updated_item.quantity).to eq(23)
46
+ end
47
+ it 'when no existing item' do
48
+ expect { Stripe::SubscriptionItem.update('some_id') }.to raise_error { |e|
49
+ expect(e).to be_a(Stripe::InvalidRequestError)
50
+ expect(e.param).to eq('subscription_item')
51
+ expect(e.message).to eq('No such subscription_item: some_id')
52
+ }
53
+ end
54
+ end
55
+
56
+ context 'retrieves a list of items' do
57
+ before do
58
+ Stripe::SubscriptionItem.create(plan: plan.id, subscription: subscription.id, quantity: 2 )
59
+ Stripe::SubscriptionItem.create(plan: plan2.id, subscription: subscription.id, quantity: 20)
60
+ end
61
+
62
+ it 'retrieves all subscription items' do
63
+ all = Stripe::SubscriptionItem.list(subscription: subscription.id)
64
+
65
+ expect(all.count).to eq(2)
66
+ end
67
+ it 'when no subscription param' do
68
+ expect { Stripe::SubscriptionItem.list }.to raise_error { |e|
69
+ expect(e).to be_a(Stripe::InvalidRequestError)
70
+ expect(e.param).to eq('subscription')
71
+ expect(e.message).to eq('Missing required param: subscription.')
72
+ }
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'TaxRate API' do
4
+ context 'with created tax rate' do
5
+ let!(:rate) { Stripe::TaxRate.create }
6
+
7
+ it 'returns list of tax rates' do
8
+ rates = Stripe::TaxRate.list
9
+ expect(rates.count).to eq(1)
10
+ end
11
+
12
+ it 'retrieves tax rate' do
13
+ ret_rate = Stripe::TaxRate.retrieve(rate.id)
14
+ expect(ret_rate.id).not_to be_nil
15
+ expect(ret_rate.object).to eq('tax_rate')
16
+ expect(ret_rate.display_name).to eq('VAT')
17
+ expect(ret_rate.percentage).to eq(21.0)
18
+ expect(ret_rate.jurisdiction).to eq('EU')
19
+ expect(ret_rate.inclusive).to eq(false)
20
+ end
21
+
22
+ it 'updates tax rate' do
23
+ ret_rate = Stripe::TaxRate.update(rate.id, percentage: 30.5)
24
+ expect(ret_rate.id).not_to be_nil
25
+ expect(ret_rate.object).to eq('tax_rate')
26
+ expect(ret_rate.display_name).to eq('VAT')
27
+ expect(ret_rate.percentage).to eq(30.5)
28
+ expect(ret_rate.jurisdiction).to eq('EU')
29
+ expect(ret_rate.inclusive).to eq(false)
30
+ end
31
+ end
32
+
33
+ it 'creates tax rate' do
34
+ rate = Stripe::TaxRate.create
35
+ expect(rate.id).not_to be_nil
36
+ expect(rate.object).to eq('tax_rate')
37
+ expect(rate.display_name).to eq('VAT')
38
+ expect(rate.percentage).to eq(21.0)
39
+ expect(rate.jurisdiction).to eq('EU')
40
+ expect(rate.inclusive).to eq(false)
41
+ end
42
+ end
@@ -3,22 +3,39 @@ require 'spec_helper'
3
3
  shared_examples 'Transfer API' do
4
4
 
5
5
  it "creates a stripe transfer" do
6
- recipient = Stripe::Recipient.create(type: "corporation", name: "MyCo")
7
- transfer = Stripe::Transfer.create(amount: "100", currency: "usd", recipient: recipient.id)
6
+ destination = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", id: "acct_12345")
7
+ transfer = Stripe::Transfer.create(amount: 100, currency: "usd", destination: destination.id)
8
8
 
9
9
  expect(transfer.id).to match /^test_tr/
10
- expect(transfer.amount).to eq('100')
10
+ expect(transfer.amount).to eq(100)
11
+ expect(transfer.amount_reversed).to eq(0)
12
+ expect(transfer.balance_transaction).to eq('txn_2dyYXXP90MN26R')
13
+ expect(transfer.created).to eq(1304114826)
11
14
  expect(transfer.currency).to eq('usd')
12
- expect(transfer.recipient).to eq recipient.id
15
+ expect(transfer.description).to eq('Transfer description')
16
+ expect(transfer.destination).to eq('acct_12345')
17
+ expect(transfer.destination_payment).to eq("py_164xRvKbnvuxQXGuVFV2pZo1")
18
+ expect(transfer.livemode).to eq(false)
19
+ expect(transfer.metadata).to eq(Stripe::StripeObject.new)
20
+ expect(transfer.reversals).to eq(Stripe::ListObject.construct_from({
21
+ object: "list",
22
+ data: [],
23
+ total_count: 0,
24
+ has_more: false,
25
+ url: "/v1/transfers/#{transfer.id}/reversals"
26
+ }))
13
27
  expect(transfer.reversed).to eq(false)
28
+ expect(transfer.source_transaction).to eq("ch_164xRv2eZvKYlo2Clu1sIJWB")
29
+ expect(transfer.source_type).to eq("card")
30
+ expect(transfer.transfer_group).to eq("group_ch_164xRv2eZvKYlo2Clu1sIJWB")
14
31
  end
15
32
 
16
33
  describe "listing transfers" do
17
- let(:recipient) { Stripe::Recipient.create(type: "corporation", name: "MyCo") }
34
+ let(:destination) { Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", business_name: "MyCo") }
18
35
 
19
36
  before do
20
37
  3.times do
21
- Stripe::Transfer.create(amount: "100", currency: "usd", recipient: recipient.id)
38
+ Stripe::Transfer.create(amount: "100", currency: "usd", destination: destination.id)
22
39
  end
23
40
  end
24
41
 
@@ -30,30 +47,52 @@ shared_examples 'Transfer API' do
30
47
  expect(Stripe::Transfer.all(limit: 2).count).to eq(2)
31
48
  end
32
49
 
33
- it "filters the search to a specific recipient" do
34
- r2 = Stripe::Recipient.create(type: "corporation", name: "MyCo")
35
- Stripe::Transfer.create(amount: "100", currency: "usd", recipient: r2.id)
50
+ it "filters the search to a specific destination" do
51
+ d2 = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", business_name: "MyCo")
52
+ Stripe::Transfer.create(amount: "100", currency: "usd", destination: d2.id)
36
53
 
37
- expect(Stripe::Transfer.all(recipient: r2.id).count).to eq(1)
54
+ expect(Stripe::Transfer.all(destination: d2.id).count).to eq(1)
55
+ end
56
+
57
+ it "disallows unknown parameters" do
58
+ expect { Stripe::Transfer.all(recipient: "foo") }.to raise_error {|e|
59
+ expect(e).to be_a Stripe::InvalidRequestError
60
+ expect(e.param).to eq("recipient")
61
+ expect(e.message).to eq("Received unknown parameter: recipient")
62
+ expect(e.http_status).to eq(400)
63
+ }
38
64
  end
39
65
  end
40
66
 
41
67
 
42
68
  it "retrieves a stripe transfer" do
43
- original = Stripe::Transfer.create(amount: "100", currency: "usd")
69
+ original = Stripe::Transfer.create(amount: "100", currency: "usd")
44
70
  transfer = Stripe::Transfer.retrieve(original.id)
45
71
 
46
72
  expect(transfer.id).to eq(original.id)
73
+ expect(transfer.object).to eq(original.object)
47
74
  expect(transfer.amount).to eq(original.amount)
75
+ expect(transfer.amount_reversed).to eq(original.amount_reversed)
76
+ expect(transfer.balance_transaction).to eq(original.balance_transaction)
77
+ expect(transfer.created).to eq(original.created)
48
78
  expect(transfer.currency).to eq(original.currency)
49
- expect(transfer.recipient).to eq(original.recipient)
79
+ expect(transfer.description).to eq(original.description)
80
+ expect(transfer.destination).to eq(original.destination)
81
+ expect(transfer.destination_payment).to eq(original.destination_payment)
82
+ expect(transfer.livemode).to eq(original.livemode)
83
+ expect(transfer.metadata).to eq(original.metadata)
84
+ expect(transfer.reversals).to eq(original.reversals)
85
+ expect(transfer.reversed).to eq(original.reversed)
86
+ expect(transfer.source_transaction).to eq(original.source_transaction)
87
+ expect(transfer.source_type).to eq(original.source_type)
88
+ expect(transfer.transfer_group).to eq(original.transfer_group)
50
89
  end
51
90
 
52
- it "canceles a stripe transfer " do
91
+ it "cancels a stripe transfer" do
53
92
  original = Stripe::Transfer.create(amount: "100", currency: "usd")
54
- res, api_key = Stripe.request(:post, "/v1/transfers/#{original.id}/cancel", 'api_key', {})
93
+ res, api_key = Stripe::StripeClient.active_client.execute_request(:post, "/v1/transfers/#{original.id}/cancel", api_key: 'api_key')
55
94
 
56
- expect(res[:status]).to eq("canceled")
95
+ expect(res.data[:status]).to eq("canceled")
57
96
  end
58
97
 
59
98
  it "cannot retrieve a transfer that doesn't exist" do
@@ -64,17 +103,27 @@ shared_examples 'Transfer API' do
64
103
  }
65
104
  end
66
105
 
67
- it 'when amount is not integer', live: true do
68
- rec = Stripe::Recipient.create({
69
- type: 'individual',
70
- name: 'Alex Smith',
71
- })
106
+ it "when amount is not integer", live: true do
107
+ dest = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", business_name: "Alex Smith")
72
108
  expect { Stripe::Transfer.create(amount: '400.2',
73
- currency: 'usd',
74
- recipient: rec.id,
75
- description: 'Transfer for test@example.com') }.to raise_error { |e|
109
+ currency: 'usd',
110
+ destination: dest.id,
111
+ description: 'Transfer for test@example.com') }.to raise_error { |e|
112
+ expect(e).to be_a Stripe::InvalidRequestError
113
+ expect(e.param).to eq('amount')
114
+ expect(e.http_status).to eq(400)
115
+ }
116
+ end
117
+
118
+ it "when amount is negative", live: true do
119
+ dest = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", business_name: "Alex Smith")
120
+ expect { Stripe::Transfer.create(amount: '-400',
121
+ currency: 'usd',
122
+ destination: dest.id,
123
+ description: 'Transfer for test@example.com') }.to raise_error { |e|
76
124
  expect(e).to be_a Stripe::InvalidRequestError
77
125
  expect(e.param).to eq('amount')
126
+ expect(e.message).to match(/^Invalid.*integer/)
78
127
  expect(e.http_status).to eq(400)
79
128
  }
80
129
  end
@@ -70,10 +70,10 @@ shared_examples 'Webhook Events API' do
70
70
  expect(data[event_b.id][:id]).to eq(event_b.id)
71
71
  end
72
72
 
73
- it "handles stripe connect event when user_id is present" do
73
+ it "handles stripe connect event when account is present" do
74
74
  acc_12314 = 'acc_12314'
75
- event = StripeMock.mock_webhook_event('customer.created', user_id: acc_12314)
76
- expect(event[:user_id]).to eq(acc_12314)
75
+ event = StripeMock.mock_webhook_event('customer.created', account: acc_12314)
76
+ expect(event[:account]).to eq(acc_12314)
77
77
  end
78
78
 
79
79
  it "retrieves an eveng using the event resource" do
@@ -129,6 +129,22 @@ shared_examples 'Webhook Events API' do
129
129
  }.to raise_error StripeMock::UnsupportedRequestError
130
130
  end
131
131
 
132
+ it 'has actual created attribute' do
133
+ time = Time.now.to_i
134
+
135
+ evnt = StripeMock.mock_webhook_event('customer.subscription.updated')
136
+
137
+ expect(evnt.created).to eq time
138
+ end
139
+
140
+ it 'when created param can be overrided' do
141
+ time = Time.now.to_i - 1000
142
+
143
+ evnt = StripeMock.mock_webhook_event('customer.subscription.updated', created: time)
144
+
145
+ expect(evnt.created).to eq time
146
+ end
147
+
132
148
  describe "listing events" do
133
149
 
134
150
  it "retrieves all events" do
@@ -183,10 +199,63 @@ shared_examples 'Webhook Events API' do
183
199
  events = Stripe::Event.all(limit: 3)
184
200
 
185
201
  expect(events.count).to eq(3)
186
- expect(events.map &:id).to include(customer_created_event.id, plan_created_event.id, coupon_created_event.id)
187
- expect(events.map &:type).to include('customer.created', 'plan.created', 'coupon.created')
202
+ expect(events.map &:id).to include(invoice_item_created_event.id, invoice_created_event.id, coupon_created_event.id)
203
+ expect(events.map &:type).to include('invoiceitem.created', 'invoice.created', 'coupon.created')
188
204
  end
189
205
 
190
206
  end
207
+
208
+ describe 'Subscription events' do
209
+ it "Checks for billing items in customer.subscription.created" do
210
+ subscription_created_event = StripeMock.mock_webhook_event('customer.subscription.created')
211
+ expect(subscription_created_event).to be_a(Stripe::Event)
212
+ expect(subscription_created_event.id).to_not be_nil
213
+ expect(subscription_created_event.data.object.items.data.class).to be Array
214
+ expect(subscription_created_event.data.object.items.data.length).to be 2
215
+ expect(subscription_created_event.data.object.items.data.first).to respond_to(:plan)
216
+ expect(subscription_created_event.data.object.items.data.first.id).to eq('si_00000000000000')
217
+ end
218
+
219
+ it "Checks for billing items in customer.subscription.deleted" do
220
+ subscription_deleted_event = StripeMock.mock_webhook_event('customer.subscription.deleted')
221
+ expect(subscription_deleted_event).to be_a(Stripe::Event)
222
+ expect(subscription_deleted_event.id).to_not be_nil
223
+ expect(subscription_deleted_event.data.object.items.data.class).to be Array
224
+ expect(subscription_deleted_event.data.object.items.data.length).to be 2
225
+ expect(subscription_deleted_event.data.object.items.data.first).to respond_to(:plan)
226
+ expect(subscription_deleted_event.data.object.items.data.first.id).to eq('si_00000000000000')
227
+ end
228
+
229
+ it "Checks for billing items in customer.subscription.updated" do
230
+ subscription_updated_event = StripeMock.mock_webhook_event('customer.subscription.updated')
231
+ expect(subscription_updated_event).to be_a(Stripe::Event)
232
+ expect(subscription_updated_event.id).to_not be_nil
233
+ expect(subscription_updated_event.data.object.items.data.class).to be Array
234
+ expect(subscription_updated_event.data.object.items.data.length).to be 2
235
+ expect(subscription_updated_event.data.object.items.data.first).to respond_to(:plan)
236
+ expect(subscription_updated_event.data.object.items.data.first.id).to eq('si_00000000000000')
237
+ end
238
+
239
+ it "Checks for billing items in customer.subscription.trial_will_end" do
240
+ subscription_trial_will_end_event = StripeMock.mock_webhook_event('customer.subscription.trial_will_end')
241
+ expect(subscription_trial_will_end_event).to be_a(Stripe::Event)
242
+ expect(subscription_trial_will_end_event.id).to_not be_nil
243
+ expect(subscription_trial_will_end_event.data.object.items.data.class).to be Array
244
+ expect(subscription_trial_will_end_event.data.object.items.data.length).to be 2
245
+ expect(subscription_trial_will_end_event.data.object.items.data.first).to respond_to(:plan)
246
+ expect(subscription_trial_will_end_event.data.object.items.data.first.id).to eq('si_00000000000000')
247
+ end
248
+ end
191
249
 
250
+ describe 'Invoices events' do
251
+ it "Checks for billing items in invoice.payment_succeeded" do
252
+ invoice_payment_succeeded = StripeMock.mock_webhook_event('invoice.payment_succeeded')
253
+ expect(invoice_payment_succeeded).to be_a(Stripe::Event)
254
+ expect(invoice_payment_succeeded.id).to_not be_nil
255
+ expect(invoice_payment_succeeded.data.object.lines.data.class).to be Array
256
+ expect(invoice_payment_succeeded.data.object.lines.data.length).to be 2
257
+ expect(invoice_payment_succeeded.data.object.lines.data.first).to respond_to(:plan)
258
+ expect(invoice_payment_succeeded.data.object.lines.data.first.id).to eq('sub_00000000000000')
259
+ end
260
+ end
192
261
  end
data/spec/spec_helper.rb CHANGED
@@ -30,9 +30,10 @@ RSpec.configure do |c|
30
30
  if ENV['IS_TRAVIS']
31
31
  puts "Travis ruby version: #{RUBY_VERSION}"
32
32
  api_key = case RUBY_VERSION
33
- when '1.9.3' then ENV['STRIPE_TEST_SECRET_KEY_A']
34
- when '2.0.0' then ENV['STRIPE_TEST_SECRET_KEY_B']
35
- when '2.1.6' then ENV['STRIPE_TEST_SECRET_KEY_C']
33
+ when '2.0.0' then ENV['STRIPE_TEST_SECRET_KEY_A']
34
+ when '2.1.10' then ENV['STRIPE_TEST_SECRET_KEY_B']
35
+ when '2.2.7' then ENV['STRIPE_TEST_SECRET_KEY_C']
36
+ when '2.3.4' then ENV['STRIPE_TEST_SECRET_KEY_D']
36
37
  end
37
38
  else
38
39
  api_key = ENV['STRIPE_TEST_SECRET_KEY']
@@ -42,11 +43,11 @@ RSpec.configure do |c|
42
43
  end
43
44
 
44
45
  c.before(:each) do
45
- StripeMock.stub(:start).and_return(nil)
46
- StripeMock.stub(:stop).and_return(nil)
46
+ allow(StripeMock).to receive(:start).and_return(nil)
47
+ allow(StripeMock).to receive(:stop).and_return(nil)
47
48
  Stripe.api_key = api_key
48
49
  end
49
- c.after(:each) { sleep 1 }
50
+ c.after(:each) { sleep 0.01 }
50
51
  else
51
52
  c.filter_run_excluding :oauth => true
52
53
  Stripe.api_key ||= ''
@@ -4,15 +4,28 @@ describe StripeMock do
4
4
 
5
5
  it "overrides stripe's request method" do
6
6
  StripeMock.start
7
- Stripe.request(:xtest, '/', 'abcde') # no error
7
+ Stripe::StripeClient.active_client.execute_request(:xtest, '/', api_key: 'abcde') # no error
8
+ StripeMock.stop
9
+ end
10
+
11
+ it "overrides stripe's execute_request method in other threads" do
12
+ StripeMock.start
13
+ Thread.new { Stripe::StripeClient.active_client.execute_request(:xtest, '/', api_key: 'abcde') }.join # no error
8
14
  StripeMock.stop
9
15
  end
10
16
 
11
17
  it "reverts overriding stripe's request method" do
12
18
  StripeMock.start
13
- Stripe.request(:xtest, '/', 'abcde') # no error
19
+ Stripe::StripeClient.active_client.execute_request(:xtest, '/', api_key: 'abcde') # no error
20
+ StripeMock.stop
21
+ expect { Stripe::StripeClient.active_client.execute_request(:x, '/', api_key: 'abcde') }.to raise_error ArgumentError
22
+ end
23
+
24
+ it "reverts overriding stripe's execute_request method in other threads" do
25
+ StripeMock.start
26
+ Thread.new { Stripe::StripeClient.active_client.execute_request(:xtest, '/', api_key: 'abcde') }.join # no error
14
27
  StripeMock.stop
15
- expect { Stripe.request(:x, '/', 'abcde') }.to raise_error
28
+ expect { Thread.new { Stripe::StripeClient.active_client.execute_request(:x, '/', api_key: 'abcde') }.join }.to raise_error ArgumentError
16
29
  end
17
30
 
18
31
  it "does not persist data between mock sessions" do
@@ -1,4 +1,3 @@
1
-
2
1
  def require_stripe_examples
3
2
  Dir["./spec/shared_stripe_examples/**/*.rb"].each {|f| require f}
4
3
  Dir["./spec/integration_examples/**/*.rb"].each {|f| require f}
@@ -6,12 +5,14 @@ end
6
5
 
7
6
  def it_behaves_like_stripe(&block)
8
7
  it_behaves_like 'Account API', &block
8
+ it_behaves_like 'Balance API', &block
9
9
  it_behaves_like 'Balance Transaction API', &block
10
10
  it_behaves_like 'Bank Account Token Mocking', &block
11
11
  it_behaves_like 'Card Token Mocking', &block
12
12
  it_behaves_like 'Card API', &block
13
13
  it_behaves_like 'Charge API', &block
14
14
  it_behaves_like 'Bank API', &block
15
+ it_behaves_like 'External Account API', &block
15
16
  it_behaves_like 'Coupon API', &block
16
17
  it_behaves_like 'Customer API', &block
17
18
  it_behaves_like 'Dispute API', &block
@@ -19,12 +20,18 @@ def it_behaves_like_stripe(&block)
19
20
  it_behaves_like 'Invoice API', &block
20
21
  it_behaves_like 'Invoice Item API', &block
21
22
  it_behaves_like 'Plan API', &block
23
+ it_behaves_like 'Product API', &block
22
24
  it_behaves_like 'Recipient API', &block
23
25
  it_behaves_like 'Refund API', &block
24
26
  it_behaves_like 'Transfer API', &block
27
+ it_behaves_like 'Payout API', &block
25
28
  it_behaves_like 'Stripe Error Mocking', &block
26
29
  it_behaves_like 'Customer Subscriptions', &block
30
+ it_behaves_like 'Subscription Items API', &block
27
31
  it_behaves_like 'Webhook Events API', &block
32
+ it_behaves_like 'Country Spec API', &block
33
+ it_behaves_like 'EphemeralKey API', &block
34
+ it_behaves_like 'TaxRate API', &block
28
35
 
29
36
  # Integration tests
30
37
  it_behaves_like 'Multiple Customer Cards'
data/spec/util_spec.rb CHANGED
@@ -19,7 +19,7 @@ describe StripeMock::Util do
19
19
  expect(result).to eq({ x: { y: 999, z: { m: 44, n: 55 } } })
20
20
  end
21
21
 
22
- it "merges array elements" do
22
+ it "merges array elements (that are hashes)" do
23
23
  dest = { x: [ {a: 1}, {b: 2}, {c: 3} ] }
24
24
  source = { x: [ {a: 0}, {a: 0} ] }
25
25
  result = StripeMock::Util.rmerge(dest, source)
@@ -27,6 +27,40 @@ describe StripeMock::Util do
27
27
  expect(result).to eq({ x: [ {a: 0}, {a: 0, b: 2}, {c: 3} ] })
28
28
  end
29
29
 
30
+ context "array elements (that are simple values)" do
31
+ it "merges arrays" do
32
+ dest = { x: [ 1, 2 ] }
33
+ source = { x: [ 3, 4 ] }
34
+ result = StripeMock::Util.rmerge(dest, source)
35
+
36
+ expect(result).to eq({ x: [ 1, 3, 2, 4 ] })
37
+ end
38
+
39
+ it "ignores empty arrays" do
40
+ dest = { x: [] }
41
+ source = { x: [ 3, 4 ] }
42
+ result = StripeMock::Util.rmerge(dest, source)
43
+
44
+ expect(result).to eq({ x: [ 3, 4 ] })
45
+ end
46
+
47
+ it "removes nil values" do
48
+ dest = { x: [ 1, 2, nil ] }
49
+ source = { x: [ nil, 3, 4 ] }
50
+ result = StripeMock::Util.rmerge(dest, source)
51
+
52
+ expect(result).to eq({ x: [ 1, 2, 3, 4 ] })
53
+ end
54
+
55
+ it "respects duplicate values" do
56
+ dest = { x: [ 1, 2, 3 ] }
57
+ source = { x: [ 3, 4 ] }
58
+ result = StripeMock::Util.rmerge(dest, source)
59
+
60
+ expect(result).to eq({ x: [ 1, 3, 2, 4, 3 ] })
61
+ end
62
+ end
63
+
30
64
  it "does not truncate the array when merging" do
31
65
  dest = { x: [ {a: 1}, {b: 2} ] }
32
66
  source = { x: [ nil, nil, {c: 3} ] }
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
19
 
20
- gem.add_dependency 'stripe', '~> 1.31'
20
+ gem.add_dependency 'stripe', '>= 2.0.3'
21
21
  gem.add_dependency 'multi_json', '~> 1.0'
22
22
  gem.add_dependency 'dante', '>= 0.2.0'
23
23