epopia-stripe-ruby-mock 2.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. checksums.yaml +7 -0
  2. data/.env +2 -0
  3. data/.gitignore +8 -0
  4. data/.rspec +1 -0
  5. data/.travis.yml +28 -0
  6. data/Gemfile +12 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +413 -0
  9. data/Rakefile +14 -0
  10. data/bin/stripe-mock-server +19 -0
  11. data/lib/stripe_mock.rb +95 -0
  12. data/lib/stripe_mock/api/account_balance.rb +14 -0
  13. data/lib/stripe_mock/api/bank_tokens.rb +13 -0
  14. data/lib/stripe_mock/api/card_tokens.rb +13 -0
  15. data/lib/stripe_mock/api/client.rb +41 -0
  16. data/lib/stripe_mock/api/conversion_rate.rb +14 -0
  17. data/lib/stripe_mock/api/debug.rb +11 -0
  18. data/lib/stripe_mock/api/errors.rb +65 -0
  19. data/lib/stripe_mock/api/global_id_prefix.rb +22 -0
  20. data/lib/stripe_mock/api/instance.rb +38 -0
  21. data/lib/stripe_mock/api/live.rb +15 -0
  22. data/lib/stripe_mock/api/server.rb +39 -0
  23. data/lib/stripe_mock/api/test_helpers.rb +24 -0
  24. data/lib/stripe_mock/api/webhooks.rb +88 -0
  25. data/lib/stripe_mock/client.rb +127 -0
  26. data/lib/stripe_mock/data.rb +1193 -0
  27. data/lib/stripe_mock/data/list.rb +73 -0
  28. data/lib/stripe_mock/error_queue.rb +27 -0
  29. data/lib/stripe_mock/errors/closed_client_connection_error.rb +9 -0
  30. data/lib/stripe_mock/errors/server_timeout_error.rb +12 -0
  31. data/lib/stripe_mock/errors/stripe_mock_error.rb +15 -0
  32. data/lib/stripe_mock/errors/uninitialized_instance_error.rb +9 -0
  33. data/lib/stripe_mock/errors/unstarted_state_error.rb +9 -0
  34. data/lib/stripe_mock/errors/unsupported_request_error.rb +4 -0
  35. data/lib/stripe_mock/instance.rb +237 -0
  36. data/lib/stripe_mock/request_handlers/accounts.rb +86 -0
  37. data/lib/stripe_mock/request_handlers/balance.rb +17 -0
  38. data/lib/stripe_mock/request_handlers/balance_transactions.rb +37 -0
  39. data/lib/stripe_mock/request_handlers/cards.rb +35 -0
  40. data/lib/stripe_mock/request_handlers/charges.rb +177 -0
  41. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  42. data/lib/stripe_mock/request_handlers/coupons.rb +35 -0
  43. data/lib/stripe_mock/request_handlers/customers.rb +137 -0
  44. data/lib/stripe_mock/request_handlers/disputes.rb +35 -0
  45. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  46. data/lib/stripe_mock/request_handlers/events.rb +21 -0
  47. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  48. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +14 -0
  49. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +127 -0
  50. data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
  51. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +17 -0
  52. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  53. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +119 -0
  54. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +44 -0
  55. data/lib/stripe_mock/request_handlers/invoice_items.rb +45 -0
  56. data/lib/stripe_mock/request_handlers/invoices.rb +177 -0
  57. data/lib/stripe_mock/request_handlers/orders.rb +80 -0
  58. data/lib/stripe_mock/request_handlers/payment_intents.rb +203 -0
  59. data/lib/stripe_mock/request_handlers/payment_methods.rb +112 -0
  60. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  61. data/lib/stripe_mock/request_handlers/plans.rb +42 -0
  62. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  63. data/lib/stripe_mock/request_handlers/recipients.rb +60 -0
  64. data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
  65. data/lib/stripe_mock/request_handlers/sources.rb +55 -0
  66. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  67. data/lib/stripe_mock/request_handlers/subscriptions.rb +296 -0
  68. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  69. data/lib/stripe_mock/request_handlers/tokens.rb +75 -0
  70. data/lib/stripe_mock/request_handlers/transfers.rb +65 -0
  71. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +32 -0
  72. data/lib/stripe_mock/server.rb +93 -0
  73. data/lib/stripe_mock/test_strategies/base.rb +81 -0
  74. data/lib/stripe_mock/test_strategies/live.rb +40 -0
  75. data/lib/stripe_mock/test_strategies/mock.rb +27 -0
  76. data/lib/stripe_mock/util.rb +44 -0
  77. data/lib/stripe_mock/version.rb +4 -0
  78. data/lib/stripe_mock/webhook_fixtures/account.application.deauthorized.json +12 -0
  79. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  80. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  81. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  82. data/lib/stripe_mock/webhook_fixtures/account.updated.json +26 -0
  83. data/lib/stripe_mock/webhook_fixtures/balance.available.json +25 -0
  84. data/lib/stripe_mock/webhook_fixtures/charge.dispute.closed.json +22 -0
  85. data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +22 -0
  86. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  87. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  88. data/lib/stripe_mock/webhook_fixtures/charge.dispute.updated.json +25 -0
  89. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +56 -0
  90. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +69 -0
  91. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +55 -0
  92. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  93. data/lib/stripe_mock/webhook_fixtures/coupon.created.json +23 -0
  94. data/lib/stripe_mock/webhook_fixtures/coupon.deleted.json +23 -0
  95. data/lib/stripe_mock/webhook_fixtures/customer.created.json +54 -0
  96. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +42 -0
  97. data/lib/stripe_mock/webhook_fixtures/customer.discount.created.json +28 -0
  98. data/lib/stripe_mock/webhook_fixtures/customer.discount.deleted.json +28 -0
  99. data/lib/stripe_mock/webhook_fixtures/customer.discount.updated.json +43 -0
  100. data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +32 -0
  101. data/lib/stripe_mock/webhook_fixtures/customer.source.deleted.json +32 -0
  102. data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -0
  103. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +66 -0
  104. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +65 -0
  105. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +65 -0
  106. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +78 -0
  107. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +57 -0
  108. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +71 -0
  109. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +105 -0
  110. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +112 -0
  111. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +74 -0
  112. data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +21 -0
  113. data/lib/stripe_mock/webhook_fixtures/invoiceitem.deleted.json +21 -0
  114. data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +24 -0
  115. data/lib/stripe_mock/webhook_fixtures/plan.created.json +20 -0
  116. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +20 -0
  117. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +23 -0
  118. data/lib/stripe_mock/webhook_fixtures/transfer.created.json +89 -0
  119. data/lib/stripe_mock/webhook_fixtures/transfer.failed.json +89 -0
  120. data/lib/stripe_mock/webhook_fixtures/transfer.paid.json +89 -0
  121. data/lib/stripe_mock/webhook_fixtures/transfer.updated.json +92 -0
  122. data/lib/trollop.rb +782 -0
  123. data/spec/_dummy/webhooks/dummy.event.json +6 -0
  124. data/spec/api/instance_spec.rb +30 -0
  125. data/spec/fixtures/create_refund.yml +126 -0
  126. data/spec/fixtures/stripe_webhooks/account.updated.json +7 -0
  127. data/spec/fixtures/stripe_webhooks/custom.account.updated.json +5 -0
  128. data/spec/instance_spec.rb +100 -0
  129. data/spec/integration_examples/charge_token_examples.rb +51 -0
  130. data/spec/integration_examples/customer_card_examples.rb +42 -0
  131. data/spec/integration_examples/prepare_error_examples.rb +38 -0
  132. data/spec/list_spec.rb +140 -0
  133. data/spec/readme_spec.rb +75 -0
  134. data/spec/server_spec.rb +139 -0
  135. data/spec/shared_stripe_examples/account_examples.rb +96 -0
  136. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  137. data/spec/shared_stripe_examples/balance_transaction_examples.rb +63 -0
  138. data/spec/shared_stripe_examples/bank_examples.rb +229 -0
  139. data/spec/shared_stripe_examples/bank_token_examples.rb +59 -0
  140. data/spec/shared_stripe_examples/card_examples.rb +307 -0
  141. data/spec/shared_stripe_examples/card_token_examples.rb +185 -0
  142. data/spec/shared_stripe_examples/charge_examples.rb +510 -0
  143. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  144. data/spec/shared_stripe_examples/coupon_examples.rb +85 -0
  145. data/spec/shared_stripe_examples/customer_examples.rb +453 -0
  146. data/spec/shared_stripe_examples/dispute_examples.rb +98 -0
  147. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  148. data/spec/shared_stripe_examples/error_mock_examples.rb +162 -0
  149. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  150. data/spec/shared_stripe_examples/extra_features_examples.rb +36 -0
  151. data/spec/shared_stripe_examples/invoice_examples.rb +524 -0
  152. data/spec/shared_stripe_examples/invoice_item_examples.rb +69 -0
  153. data/spec/shared_stripe_examples/payment_intent_examples.rb +131 -0
  154. data/spec/shared_stripe_examples/payment_method_examples.rb +175 -0
  155. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  156. data/spec/shared_stripe_examples/plan_examples.rb +194 -0
  157. data/spec/shared_stripe_examples/product_example.rb +65 -0
  158. data/spec/shared_stripe_examples/recipient_examples.rb +118 -0
  159. data/spec/shared_stripe_examples/refund_examples.rb +472 -0
  160. data/spec/shared_stripe_examples/subscription_examples.rb +1148 -0
  161. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  162. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  163. data/spec/shared_stripe_examples/transfer_examples.rb +130 -0
  164. data/spec/shared_stripe_examples/validation_examples.rb +19 -0
  165. data/spec/shared_stripe_examples/webhook_event_examples.rb +261 -0
  166. data/spec/spec_helper.rb +58 -0
  167. data/spec/stripe_mock_spec.rb +123 -0
  168. data/spec/support/stripe_examples.rb +42 -0
  169. data/spec/util_spec.rb +121 -0
  170. data/stripe-ruby-mock.gemspec +27 -0
  171. metadata +344 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 46adc737bee684a2e8268e50e8009adb026d52c61448df9b7f3b490b2390d1fa
4
+ data.tar.gz: d5d97bc6f6bb5706e5ca1563b5e0ba08c3d567c6d24991c17eb534c9b6e0c2e7
5
+ SHA512:
6
+ metadata.gz: ce438549ff84637a34b83bd2655714cad46717d8a630fe19743e56d6ca153082a9bfc37dd821c3d71fc7be11d965ffbe3c9a7a4b0e49abc9b2b229a850ac7168
7
+ data.tar.gz: a47a7d44988a8439b3ac0125427dce66c9ec0ed30e5e2b0fb3a2d69664ac55aef34cb3acfcc242da359a148c2119af3c521dcde652d97c86be6bb70a82f51638
data/.env ADDED
@@ -0,0 +1,2 @@
1
+ STRIPE_TEST_SECRET_KEY=sk_test_eFvAvN5rz4GqAbsWxg63Jx79
2
+ STRIPE_TEST_OAUTH_ACCESS_TOKEN=sk_test_WnZmEBIHhMcDltNe98sqWN7z
@@ -0,0 +1,8 @@
1
+ html/
2
+ pkg/
3
+ .DS_Store
4
+ stripe-mock-server.pid
5
+ Gemfile.lock
6
+ stripe-mock-server.log
7
+ .idea
8
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
@@ -0,0 +1,28 @@
1
+ dist: trusty
2
+ group: deprecated-2017Q2
3
+ sudo: required
4
+ language: ruby
5
+ rvm:
6
+ - 2.0.0
7
+ - 2.1.10
8
+ - 2.2.7
9
+ - 2.3.4
10
+ before_install:
11
+ - rvm 2.1.10 do gem install mime-types -v 2.6.2
12
+ - gem install bundler
13
+ before_script:
14
+ - "sudo touch /var/log/stripe-mock-server.log"
15
+ - "sudo chown travis /var/log/stripe-mock-server.log"
16
+ script: "bundle exec rspec && bundle exec rspec -t live"
17
+
18
+ env:
19
+ global:
20
+ - IS_TRAVIS=true STRIPE_TEST_SECRET_KEY_A=sk_test_Ut2MSlZANdT3iDALdGhyLymy STRIPE_TEST_SECRET_KEY_B=sk_test_JXtzss9tHOG1ofIyEZgoUP4Q STRIPE_TEST_SECRET_KEY_C=sk_test_ZR5nVz9p3ivsqVa7mYB0sFep STRIPE_TEST_SECRET_KEY_D=sk_test_ZR5nVz9p3ivsqVa7mYB0sFep
21
+
22
+ notifications:
23
+ webhooks:
24
+ urls:
25
+ - https://webhooks.gitter.im/e/44a1f4718ae2efb67eac
26
+ on_success: change # options: [always|never|change] default: always
27
+ on_failure: always # options: [always|never|change] default: always
28
+ on_start: false # default: false
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ platforms :ruby_19 do
4
+ gem 'mime-types', '~> 2.6'
5
+ gem 'rest-client', '~> 1.8'
6
+ end
7
+
8
+ group :test do
9
+ gem 'rake'
10
+ end
11
+
12
+ gemspec
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2013 Gilbert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,413 @@
1
+ # epopia-stripe-ruby-mock
2
+
3
+ This gem is a custom version of stripe-ruby-mock. We needed some PR to be merged, but the upstream was taking too much time to do it. Thus we created this fork, and publish it under the name epopia-stripe-ruby-mock for the time being.
4
+
5
+ I would recommend NOT using this gem, because it could be deleted at any moment's notice.
6
+
7
+ # stripe-ruby-mock [![Build Status](https://travis-ci.org/rebelidealist/stripe-ruby-mock.png?branch=master)](https://travis-ci.org/rebelidealist/stripe-ruby-mock) [![Gitter chat](https://badges.gitter.im/rebelidealist/stripe-ruby-mock.png)](https://gitter.im/rebelidealist/stripe-ruby-mock)
8
+
9
+ * Homepage: https://github.com/rebelidealist/stripe-ruby-mock
10
+ * Issues: https://github.com/rebelidealist/stripe-ruby-mock/issues
11
+ * **CHAT**: https://gitter.im/rebelidealist/stripe-ruby-mock
12
+
13
+ # REQUEST: Looking for More Core Contributors
14
+
15
+ This gem has unexpectedly grown in popularity and I've gotten pretty busy, so I'm currently looking for more core contributors to help me out. If you're interested, there is only one requirement: submit a significant enough pull request and have it merged into master (many of you have already done this). Afterwards, ping me in [chat](https://gitter.im/rebelidealist/stripe-ruby-mock) and I will add you as a collaborator.
16
+
17
+ ## Install
18
+
19
+ In your gemfile:
20
+
21
+ gem 'stripe-ruby-mock', '~> 2.5.8', :require => 'stripe_mock'
22
+
23
+ ## Features
24
+
25
+ * No stripe server access required
26
+ * Easily test against stripe errors
27
+ * Mock and customize stripe webhooks
28
+ * Flip a switch to run your tests against Stripe's **live test servers**
29
+
30
+ ### Requirements
31
+
32
+ * ruby >= 2.0.0
33
+ * stripe >= 2.0.3
34
+
35
+ ### Specifications
36
+
37
+ **STRIPE API TARGET VERSION:** 2017-06-05 (master)
38
+
39
+ Older API version branches:
40
+
41
+ - api-2015-09-08 - use gem version 2.4.1
42
+ - [api-2014-06-17](https://github.com/rebelidealist/stripe-ruby-mock/tree/api-2014-06-17)
43
+
44
+ ### Versioning System
45
+
46
+ Since StripeMock tries to keep up with Stripe's API version, its version system is a little different:
47
+
48
+ - The **major** number (1.x.x) is for breaking changes involving how you use StripeMock itself
49
+ - The **minor** number (x.1.x) is for breaking changes involving Stripe's API
50
+ - The **patch** number (x.x.0) is for non-breaking changes/fixes involving Stripe's API, or for non-breaking changes/fixes/features for StripeMock itself.
51
+
52
+ ## Description
53
+
54
+ ** *WARNING: This library does not cover all Stripe API endpoints. If you need one that's missing, please create an issue for it, or [see this wiki page](https://github.com/rebelidealist/stripe-ruby-mock/wiki/Implementing-a-New-Behavior) if you're interested in contributing* **
55
+
56
+ At its core, this library overrides [stripe-ruby's](https://github.com/stripe/stripe-ruby)
57
+ request method to skip all http calls and
58
+ instead directly return test data. This allows you to write and run tests
59
+ without the need to actually hit stripe's servers.
60
+
61
+ You can use stripe-ruby-mock with any ruby testing library. Here's a quick dummy example with RSpec:
62
+
63
+ ```ruby
64
+ require 'stripe_mock'
65
+
66
+ describe MyApp do
67
+ let(:stripe_helper) { StripeMock.create_test_helper }
68
+ before { StripeMock.start }
69
+ after { StripeMock.stop }
70
+
71
+ it "creates a stripe customer" do
72
+
73
+ # This doesn't touch stripe's servers nor the internet!
74
+ # Specify :source in place of :card (with same value) to return customer with source data
75
+ customer = Stripe::Customer.create({
76
+ email: 'johnny@appleseed.com',
77
+ source: stripe_helper.generate_card_token
78
+ })
79
+ expect(customer.email).to eq('johnny@appleseed.com')
80
+ end
81
+ end
82
+ ```
83
+
84
+ ## Test Helpers
85
+
86
+ Some Stripe API calls require several parameters. StripeMock helps you keep your test brief with some helpers:
87
+
88
+ ```ruby
89
+ describe MyApp do
90
+ let(:stripe_helper) { StripeMock.create_test_helper }
91
+
92
+ it "creates a stripe plan" do
93
+ plan = stripe_helper.create_plan(:id => 'my_plan', :amount => 1500)
94
+
95
+ # The above line replaces the following:
96
+ # plan = Stripe::Plan.create(
97
+ # :id => 'my_plan',
98
+ # :name => 'StripeMock Default Plan ID',
99
+ # :amount => 1500,
100
+ # :currency => 'usd',
101
+ # :interval => 'month'
102
+ # )
103
+ expect(plan.id).to eq('my_plan')
104
+ expect(plan.amount).to eq(1500)
105
+ end
106
+ end
107
+ ```
108
+
109
+ The [available helpers](lib/stripe_mock/test_strategies/) are:
110
+
111
+ ```ruby
112
+ stripe_helper.create_plan(my_plan_params)
113
+ stripe_helper.delete_plan(my_plan_params)
114
+ stripe_helper.generate_card_token(my_card_params)
115
+ ```
116
+
117
+ For everything else, use Stripe as you normally would (i.e. use Stripe as if you were not using StripeMock).
118
+
119
+ ## Live Testing
120
+
121
+ Every once in a while you want to make sure your tests are actually valid. StripeMock has a switch that allows you to run your test suite (or a subset thereof) against Stripe's live test servers.
122
+
123
+ Here is an example of setting up your RSpec (2.x) test suite to run live with a command line switch:
124
+
125
+ ```ruby
126
+ # RSpec 2.x
127
+ RSpec.configure do |c|
128
+ if c.filter_manager.inclusions.keys.include?(:live)
129
+ StripeMock.toggle_live(true)
130
+ puts "Running **live** tests against Stripe..."
131
+ end
132
+ end
133
+ ```
134
+
135
+ With this you can run live tests by running `rspec -t live`
136
+
137
+ Here is an example of setting up your RSpec (3.x) test suite to run live with the same command line switch:
138
+
139
+ ```ruby
140
+ # RSpec 3.x
141
+ RSpec.configure do |c|
142
+ if c.filter_manager.inclusions.rules.include?(:live)
143
+ StripeMock.toggle_live(true)
144
+ puts "Running **live** tests against Stripe..."
145
+ end
146
+ end
147
+ ```
148
+
149
+ ## Mocking Card Errors
150
+
151
+ Tired of manually inputting fake credit card numbers to test against errors? Tire no more!
152
+
153
+ ```ruby
154
+ it "mocks a declined card error" do
155
+ # Prepares an error for the next create charge request
156
+ StripeMock.prepare_card_error(:card_declined)
157
+
158
+ expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to raise_error {|e|
159
+ expect(e).to be_a Stripe::CardError
160
+ expect(e.http_status).to eq(402)
161
+ expect(e.code).to eq('card_declined')
162
+ }
163
+ end
164
+ ```
165
+
166
+ ### Built-In Card Errors
167
+
168
+ ```ruby
169
+ StripeMock.prepare_card_error(:incorrect_number)
170
+ StripeMock.prepare_card_error(:invalid_number)
171
+ StripeMock.prepare_card_error(:invalid_expiry_month)
172
+ StripeMock.prepare_card_error(:invalid_expiry_year)
173
+ StripeMock.prepare_card_error(:invalid_cvc)
174
+ StripeMock.prepare_card_error(:expired_card)
175
+ StripeMock.prepare_card_error(:incorrect_cvc)
176
+ StripeMock.prepare_card_error(:card_declined)
177
+ StripeMock.prepare_card_error(:missing)
178
+ StripeMock.prepare_card_error(:processing_error)
179
+ StripeMock.prepare_card_error(:incorrect_zip)
180
+ ```
181
+
182
+ You can see the details of each error in [lib/stripe_mock/api/errors.rb](lib/stripe_mock/api/errors.rb)
183
+
184
+ ### Specifying Card Errors
185
+
186
+ By default, `prepare_card_error` only triggers for `:new_charge`, the event that happens when you run `Charge.create`. More explicitly, this is what happens by default:
187
+
188
+ ```ruby
189
+ StripeMock.prepare_card_error(:card_declined, :new_charge)
190
+ ```
191
+
192
+ If you want the error to trigger on a different event, you need to replace `:new_charge` with a different event. For example:
193
+
194
+ ```ruby
195
+ StripeMock.prepare_card_error(:card_declined, :create_card)
196
+ customer = Stripe::Customer.create
197
+ # This line throws the card error
198
+ customer.cards.create
199
+ ```
200
+
201
+ `:new_charge` and `:create_card` are names of methods in the [StripeMock request handlers](lib/stripe_mock/request_handlers). You can also set `StripeMock.toggle_debug(true)` to see the event name for each Stripe request made in your tests.
202
+
203
+ ### Custom Errors
204
+
205
+ To raise an error on a specific type of request, take a look at the [request handlers folder](lib/stripe_mock/request_handlers/) and pass a method name to `StripeMock.prepare_error`.
206
+
207
+ If you wanted to raise an error for creating a new customer, for instance, you would do the following:
208
+
209
+ ```ruby
210
+ it "raises a custom error for specific actions" do
211
+ custom_error = StandardError.new("Please knock first.")
212
+
213
+ StripeMock.prepare_error(custom_error, :new_customer)
214
+
215
+ expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to_not raise_error
216
+ expect { Stripe::Customer.create }.to raise_error {|e|
217
+ expect(e).to be_a StandardError
218
+ expect(e.message).to eq("Please knock first.")
219
+ }
220
+ end
221
+ ```
222
+
223
+ In the above example, `:new_customer` is the name of a method from [customers.rb](lib/stripe_mock/request_handlers/customers.rb).
224
+
225
+ ## Running the Mock Server
226
+
227
+ Sometimes you want your test stripe data to persist for a bit, such as during integration tests
228
+ running on different processes. In such cases you'll want to start the stripe mock server:
229
+
230
+ # spec_helper.rb
231
+ #
232
+ # The mock server will automatically be killed when your tests are done running.
233
+ #
234
+ require 'thin'
235
+ StripeMock.spawn_server
236
+
237
+ Then, instead of `StripeMock.start`, you'll want to use `StripeMock.start_client`:
238
+
239
+ ```ruby
240
+ describe MyApp do
241
+ before do
242
+ @client = StripeMock.start_client
243
+ end
244
+
245
+ after do
246
+ StripeMock.stop_client
247
+ # Alternatively:
248
+ # @client.close!
249
+ # -- Or --
250
+ # StripeMock.stop_client(:clear_server_data => true)
251
+ end
252
+ end
253
+ ```
254
+
255
+ This is all essentially the same as using `StripeMock.start`, except that the stripe test
256
+ data is held in its own server process.
257
+
258
+ Here are some other neat things you can do with the client:
259
+
260
+ ```ruby
261
+ @client.state #=> 'ready'
262
+
263
+ @client.get_server_data(:customers) # Also works for :charges, :plans, etc.
264
+ @client.clear_server_data
265
+
266
+ @client.close!
267
+ @client.state #=> 'closed'
268
+ ```
269
+
270
+ ### Mock Server Options
271
+
272
+ ```ruby
273
+ # NOTE: Shown below are the default options
274
+ StripeMock.default_server_pid_path = './stripe-mock-server.pid'
275
+
276
+ StripeMock.spawn_server(
277
+ :pid_path => StripeMock.default_server_pid_path,
278
+ :host => '0.0.0.0',
279
+ :port => 4999,
280
+ :server => :thin
281
+ )
282
+
283
+ StripeMock.kill_server(StripeMock.default_server_pid_path)
284
+ ```
285
+
286
+ ### Mock Server Command
287
+
288
+ If you need the mock server to continue running even after your tests are done,
289
+ you'll want to use the executable:
290
+
291
+ $ stripe-mock-server -p 4000
292
+ $ stripe-mock-server --help
293
+
294
+ ## Mocking Webhooks
295
+
296
+ If your application handles stripe webhooks, you are most likely retrieving the event from
297
+ stripe and passing the result to a handler. StripeMock helps you by easily mocking that event:
298
+
299
+ ```ruby
300
+ it "mocks a stripe webhook" do
301
+ event = StripeMock.mock_webhook_event('customer.created')
302
+
303
+ customer_object = event.data.object
304
+ expect(customer_object.id).to_not be_nil
305
+ expect(customer_object.default_card).to_not be_nil
306
+ # etc.
307
+ end
308
+
309
+ it "mocks stripe connect webhooks" do
310
+ event = StripeMock.mock_webhook_event('customer.created', account: 'acc_123123')
311
+
312
+ expect(event.account).to eq('acc_123123')
313
+ end
314
+ ```
315
+
316
+ ### Customizing Webhooks
317
+
318
+ By default, StripeMock searches in your `spec/fixtures/stripe_webhooks/` folder for your own, custom webhooks.
319
+ If it finds nothing, it falls back to [test events generated through stripe's webhooktester](lib/stripe_mock/webhook_fixtures/).
320
+
321
+ For example, you could create a file in `spec/fixtures/stripe_webhooks/invoice.created.with-sub.json`, copy/paste the default from [the default invoice.created.json](lib/stripe_mock/webhook_fixtures/invoice.created.json), and customize it to your needs.
322
+
323
+ Then you can use that webook directly in your specs:
324
+
325
+ ```ruby
326
+ it "can use a custom webhook fixture" do
327
+ event = StripeMock.mock_webhook_event('invoice.created.with-sub')
328
+ # etc.
329
+ end
330
+ ```
331
+
332
+ You can alse override values on the fly:
333
+
334
+ ```ruby
335
+ it "can override webhook values" do
336
+ # NOTE: given hash values get merged directly into event.data.object
337
+ event = StripeMock.mock_webhook_event('customer.created', {
338
+ :id => 'cus_my_custom_value',
339
+ :email => 'joe@example.com'
340
+ })
341
+ # Alternatively:
342
+ # event.data.object.id = 'cus_my_custom_value'
343
+ # event.data.object.email = 'joe@example.com'
344
+ expect(event.data.object.id).to eq('cus_my_custom_value')
345
+ expect(event.data.object.email).to eq('joe@example.com')
346
+ end
347
+ ```
348
+
349
+ You can name events whatever you like in your `spec/fixtures/stripe_webhooks/` folder. However, if you try to call a non-standard event that's doesn't exist in that folder, StripeMock will throw an error.
350
+
351
+ If you wish to use a different fixture path, you can set it yourself:
352
+
353
+ StripeMock.webhook_fixture_path = './spec/other/folder/'
354
+
355
+ ## Generating Card Tokens
356
+
357
+ Sometimes you need to check if your code reads a stripe card correctly. If so, you can specifically
358
+ assign card data to a generated card token:
359
+
360
+ ```ruby
361
+ it "generates a stripe card token" do
362
+ card_token = StripeMock.generate_card_token(last4: "9191", exp_year: 1984)
363
+
364
+ cus = Stripe::Customer.create(source: card_token)
365
+ card = cus.sources.data.first
366
+ expect(card.last4).to eq("9191")
367
+ expect(card.exp_year).to eq(1984)
368
+ end
369
+ ```
370
+
371
+ ## Debugging
372
+
373
+ To enable debug messages:
374
+
375
+ StripeMock.toggle_debug(true)
376
+
377
+ This will **only last for the session**; Once you call `StripeMock.stop` or `StripeMock.stop_client`,
378
+ debug will be toggled off.
379
+
380
+ If you always want debug to be on (it's quite verbose), you should put this in a `before` block.
381
+
382
+ ## Miscellaneous Features
383
+
384
+ You may have noticed that all generated Stripe ids start with `test_`. If you want to remove this:
385
+
386
+ ```ruby
387
+ # Turns off test_ prefix
388
+ StripeMock.global_id_prefix = false
389
+
390
+ # Or you can set your own
391
+ StripeMock.global_id_prefix = 'my_app_'
392
+ ```
393
+
394
+ ## TODO
395
+
396
+ * Cover all stripe urls/methods
397
+ * Throw useful errors that emulate Stripe's requirements
398
+ * For example: "You must supply either a card or a customer id" for `Stripe::Charge`
399
+ * Fingerprinting for other resources besides Cards
400
+
401
+ ## Developing stripe-ruby-mock
402
+
403
+ [Please see this wiki page](https://github.com/rebelidealist/stripe-ruby-mock/wiki/Implementing-a-New-Behavior)
404
+
405
+ Patches are welcome and greatly appreciated! If you're contributing to fix a problem,
406
+ be sure to write tests that illustrate the problem being fixed.
407
+ This will help ensure that the problem remains fixed in future updates.
408
+
409
+ ## Copyright
410
+
411
+ Copyright (c) 2013 Gilbert
412
+
413
+ See LICENSE.txt for details.