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,6 @@
1
+ {
2
+ "val": "success",
3
+ "data": {
4
+ "object": {}
5
+ }
6
+ }
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe StripeMock do
4
+ describe ".mock" do
5
+ it "yields the given block between starting and stopping StripeMock" do
6
+ expect(StripeMock.instance).to be_nil
7
+ expect(StripeMock.state).to eq "ready"
8
+
9
+ StripeMock.mock do
10
+ expect(StripeMock.instance).to be_instance_of StripeMock::Instance
11
+ expect(StripeMock.state).to eq "local"
12
+ end
13
+
14
+ expect(StripeMock.instance).to be_nil
15
+ expect(StripeMock.state).to eq "ready"
16
+ end
17
+
18
+ it "stops StripeMock if the given block raises an exception" do
19
+ expect(StripeMock.instance).to be_nil
20
+ begin
21
+ StripeMock.mock do
22
+ raise "Uh-oh..."
23
+ end
24
+ rescue
25
+ expect(StripeMock.instance).to be_nil
26
+ expect(StripeMock.state).to eq "ready"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,126 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.stripe.com/v1/charges/ch_4fX3f4kErVLRcj
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*; q=0.5, application/xml'
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Stripe/v1 RubyBindings/1.15.0
16
+ Authorization:
17
+ - Bearer sk_test_sZQU6LreNksFlmewtO4JqBK1
18
+ Content-Type:
19
+ - application/x-www-form-urlencoded
20
+ X-Stripe-Client-User-Agent:
21
+ - ! '{"bindings_version":"1.15.0","lang":"ruby","lang_version":"1.9.3 p125 (2012-02-16)","platform":"x86_64-darwin10.8.0","publisher":"stripe","uname":"Darwin
22
+ airwater.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST
23
+ 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64"}'
24
+ response:
25
+ status:
26
+ code: 200
27
+ message: OK
28
+ headers:
29
+ Server:
30
+ - nginx
31
+ Date:
32
+ - Wed, 27 Aug 2014 19:21:23 GMT
33
+ Content-Type:
34
+ - application/json;charset=utf-8
35
+ Content-Length:
36
+ - '1158'
37
+ Access-Control-Allow-Credentials:
38
+ - 'true'
39
+ Access-Control-Allow-Methods:
40
+ - GET, POST, HEAD, OPTIONS, DELETE
41
+ Access-Control-Max-Age:
42
+ - '300'
43
+ Cache-Control:
44
+ - no-cache, no-store
45
+ Stripe-Version:
46
+ - '2014-08-04'
47
+ Strict-Transport-Security:
48
+ - max-age=31556926; includeSubDomains
49
+ body:
50
+ encoding: US-ASCII
51
+ string: ! "{\n \"id\": \"ch_4fX3f4kErVLRcj\",\n \"object\": \"charge\",\n
52
+ \ \"created\": 1409167282,\n \"livemode\": false,\n \"paid\": true,\n \"amount\":
53
+ 555,\n \"currency\": \"usd\",\n \"refunded\": false,\n \"card\": {\n \"id\":
54
+ \"card_4fX3v7mm7FZQxN\",\n \"object\": \"card\",\n \"last4\": \"4242\",\n
55
+ \ \"brand\": \"Visa\",\n \"funding\": \"credit\",\n \"exp_month\":
56
+ 9,\n \"exp_year\": 2018,\n \"fingerprint\": \"wXWJT135mEK107G8\",\n
57
+ \ \"country\": \"US\",\n \"name\": null,\n \"address_line1\": null,\n
58
+ \ \"address_line2\": null,\n \"address_city\": null,\n \"address_state\":
59
+ null,\n \"address_zip\": null,\n \"address_country\": null,\n \"cvc_check\":
60
+ \"pass\",\n \"address_line1_check\": null,\n \"address_zip_check\":
61
+ null,\n \"customer\": null\n },\n \"captured\": true,\n \"refunds\":
62
+ {\n \"object\": \"list\",\n \"total_count\": 0,\n \"has_more\": false,\n
63
+ \ \"url\": \"/v1/charges/ch_4fX3f4kErVLRcj/refunds\",\n \"data\": []\n
64
+ \ },\n \"balance_transaction\": \"txn_4fX3u6H5sC09M0\",\n \"failure_message\":
65
+ null,\n \"failure_code\": null,\n \"amount_refunded\": 0,\n \"customer\":
66
+ null,\n \"invoice\": null,\n \"description\": null,\n \"dispute\": null,\n
67
+ \ \"metadata\": {},\n \"statement_description\": null,\n \"receipt_email\":
68
+ null\n}\n"
69
+ http_version:
70
+ recorded_at: Wed, 27 Aug 2014 19:21:22 GMT
71
+ - request:
72
+ method: post
73
+ uri: https://api.stripe.com/v1/charges/ch_4fX3f4kErVLRcj/refunds
74
+ body:
75
+ encoding: US-ASCII
76
+ string: amount=555
77
+ headers:
78
+ Accept:
79
+ - ! '*/*; q=0.5, application/xml'
80
+ Accept-Encoding:
81
+ - gzip, deflate
82
+ User-Agent:
83
+ - Stripe/v1 RubyBindings/1.15.0
84
+ Authorization:
85
+ - Bearer sk_test_sZQU6LreNksFlmewtO4JqBK1
86
+ Content-Type:
87
+ - application/x-www-form-urlencoded
88
+ X-Stripe-Client-User-Agent:
89
+ - ! '{"bindings_version":"1.15.0","lang":"ruby","lang_version":"1.9.3 p125 (2012-02-16)","platform":"x86_64-darwin10.8.0","publisher":"stripe","uname":"Darwin
90
+ airwater.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST
91
+ 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64"}'
92
+ Content-Length:
93
+ - '10'
94
+ response:
95
+ status:
96
+ code: 200
97
+ message: OK
98
+ headers:
99
+ Server:
100
+ - nginx
101
+ Date:
102
+ - Wed, 27 Aug 2014 19:21:25 GMT
103
+ Content-Type:
104
+ - application/json;charset=utf-8
105
+ Content-Length:
106
+ - '215'
107
+ Access-Control-Allow-Credentials:
108
+ - 'true'
109
+ Access-Control-Allow-Methods:
110
+ - GET, POST, HEAD, OPTIONS, DELETE
111
+ Access-Control-Max-Age:
112
+ - '300'
113
+ Cache-Control:
114
+ - no-cache, no-store
115
+ Stripe-Version:
116
+ - '2014-08-04'
117
+ Strict-Transport-Security:
118
+ - max-age=31556926; includeSubDomains
119
+ body:
120
+ encoding: US-ASCII
121
+ string: ! "{\n \"id\": \"re_4fX3sdHZN1mfba\",\n \"amount\": 555,\n \"currency\":
122
+ \"usd\",\n \"created\": 1409167284,\n \"object\": \"refund\",\n \"balance_transaction\":
123
+ \"txn_4fX3vOQMq1HTQt\",\n \"metadata\": {},\n \"charge\": \"ch_4fX3f4kErVLRcj\"\n}\n"
124
+ http_version:
125
+ recorded_at: Wed, 27 Aug 2014 19:21:23 GMT
126
+ recorded_with: VCR 2.6.0
@@ -0,0 +1,7 @@
1
+ {
2
+ "id": "evt_123",
3
+ "type": "account.updated",
4
+ "data": {
5
+ "object": {}
6
+ }
7
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "data": {
3
+ "object": {}
4
+ }
5
+ }
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+ require_stripe_examples
3
+
4
+ describe StripeMock::Instance do
5
+
6
+ let(:stripe_helper) { StripeMock.create_test_helper }
7
+
8
+ it_behaves_like_stripe do
9
+ def test_data_source(type); StripeMock.instance.send(type); end
10
+ end
11
+
12
+ before { StripeMock.start }
13
+ after { StripeMock.stop }
14
+
15
+ it "handles both string and symbol hash keys" do
16
+ string_params = stripe_helper.create_plan_params(
17
+ "id" => "str_abcde",
18
+ :name => "String Plan"
19
+ )
20
+ res, api_key = StripeMock.instance.mock_request('post', '/v1/plans', api_key: 'api_key', params: string_params)
21
+ expect(res.data[:id]).to eq('str_abcde')
22
+ expect(res.data[:name]).to eq('String Plan')
23
+ end
24
+
25
+ it "exits gracefully on an unrecognized handler url" do
26
+ dummy_params = {
27
+ "id" => "str_12345",
28
+ "name" => "PLAN"
29
+ }
30
+
31
+ expect { res, api_key = StripeMock.instance.mock_request('post', '/v1/unrecongnized_method', api_key: 'api_key', params: dummy_params) }.to_not raise_error
32
+ end
33
+
34
+ it "can toggle debug" do
35
+ StripeMock.toggle_debug(true)
36
+ expect(StripeMock.instance.debug).to eq(true)
37
+ StripeMock.toggle_debug(false)
38
+ expect(StripeMock.instance.debug).to eq(false)
39
+ end
40
+
41
+ it "should toggle off debug when mock session ends" do
42
+ StripeMock.toggle_debug(true)
43
+
44
+ StripeMock.stop
45
+ expect(StripeMock.instance).to be_nil
46
+
47
+ StripeMock.start
48
+ expect(StripeMock.instance.debug).to eq(false)
49
+ end
50
+
51
+ it "can set a conversion rate" do
52
+ StripeMock.set_conversion_rate(1.25)
53
+ expect(StripeMock.instance.conversion_rate).to eq(1.25)
54
+ end
55
+
56
+ it "allows non-usd default currency" do
57
+ old_default_currency = StripeMock.default_currency
58
+ customer = begin
59
+ StripeMock.default_currency = "jpy"
60
+ Stripe::Customer.create({
61
+ email: 'johnny@appleseed.com',
62
+ source: stripe_helper.generate_card_token
63
+ })
64
+ ensure
65
+ StripeMock.default_currency = old_default_currency
66
+ end
67
+ expect(customer.currency).to eq("jpy")
68
+ end
69
+
70
+ context 'when creating sources with metadata' do
71
+ let(:customer) { Stripe::Customer.create(email: 'test@email.com') }
72
+ let(:metadata) { { test_key: 'test_value' } }
73
+
74
+ context 'for credit card' do
75
+ let(:credit_card) do
76
+ customer.sources.create(
77
+ source: stripe_helper.generate_card_token,
78
+ metadata: metadata
79
+ )
80
+ end
81
+
82
+ it('should save metadata') do
83
+ expect(credit_card.metadata.test_key).to eq metadata[:test_key]
84
+ end
85
+ end
86
+
87
+ context 'for bank account' do
88
+ let(:bank_account) do
89
+ customer.sources.create(
90
+ source: stripe_helper.generate_bank_token,
91
+ metadata: metadata
92
+ )
93
+ end
94
+
95
+ it('should save metadata') do
96
+ expect(bank_account.metadata.test_key).to eq metadata[:test_key]
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Charging with Tokens' do
4
+
5
+ describe "With OAuth", :oauth => true do
6
+
7
+ let(:cus) do
8
+ Stripe::Customer.create(
9
+ :source => stripe_helper.generate_card_token({ :number => '4242424242424242', :brand => 'Visa' })
10
+ )
11
+ end
12
+
13
+ let(:card_token) do
14
+ Stripe::Token.create({
15
+ :customer => cus.id,
16
+ :source => cus.sources.first.id
17
+ }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
18
+ end
19
+
20
+ it "creates with an oauth access token" do
21
+ charge = Stripe::Charge.create({
22
+ :amount => 1099,
23
+ :currency => 'usd',
24
+ :source => card_token.id
25
+ }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
26
+
27
+ expect(charge.source.id).to_not eq cus.sources.first.id
28
+ expect(charge.source.fingerprint).to eq cus.sources.first.fingerprint
29
+ expect(charge.source.last4).to eq '4242'
30
+ expect(charge.source.brand).to eq 'Visa'
31
+
32
+ retrieved_charge = Stripe::Charge.retrieve(charge.id)
33
+
34
+ expect(retrieved_charge.source.id).to_not eq cus.sources.first.id
35
+ expect(retrieved_charge.source.fingerprint).to eq cus.sources.first.fingerprint
36
+ expect(retrieved_charge.source.last4).to eq '4242'
37
+ expect(retrieved_charge.source.brand).to eq 'Visa'
38
+ end
39
+
40
+ it "throws an error when the card is not an id" do
41
+ expect {
42
+ charge = Stripe::Charge.create({
43
+ :amount => 1099,
44
+ :currency => 'usd',
45
+ :source => card_token
46
+ }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
47
+ }.to raise_error(Stripe::InvalidRequestError, /Invalid token id/)
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples "Multiple Customer Cards" do
4
+ it "handles multiple cards", :live => true do
5
+ tok1 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
6
+ tok2 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4012888888881881"
7
+
8
+ cus = Stripe::Customer.create(:email => 'alice@bob.com', :source => tok1.id)
9
+ default_card = cus.sources.first
10
+ cus.sources.create(:source => tok2.id)
11
+
12
+ cus = Stripe::Customer.retrieve(cus.id)
13
+ expect(cus.sources.count).to eq(2)
14
+ expect(cus.default_source).to eq default_card.id
15
+ end
16
+
17
+ it "gives the same two card numbers the same fingerprints", :live => true do
18
+ tok1 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
19
+ tok2 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
20
+
21
+ cus = Stripe::Customer.create(:email => 'alice@bob.com', :source => tok1.id)
22
+
23
+ cus = Stripe::Customer.retrieve(cus.id)
24
+ card = cus.sources.find do |existing_card|
25
+ existing_card.fingerprint == tok2.card.fingerprint
26
+ end
27
+ expect(card).to_not be_nil
28
+ end
29
+
30
+ it "gives different card numbers different fingerprints", :live => true do
31
+ tok1 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
32
+ tok2 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4012888888881881"
33
+
34
+ cus = Stripe::Customer.create(:email => 'alice@bob.com', :source => tok1.id)
35
+
36
+ cus = Stripe::Customer.retrieve(cus.id)
37
+ source = cus.sources.find do |existing_card|
38
+ existing_card.fingerprint == tok2.card.fingerprint
39
+ end
40
+ expect(source).to be_nil
41
+ end
42
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Card Error Prep' do
4
+
5
+ # it "prepares a card error" do
6
+ # StripeMock.prepare_card_error(:card_declined, :new_charge)
7
+ # cus = Stripe::Customer.create :email => 'alice@bob.com',
8
+ # :card => stripe_helper.generate_card_token({ :number => '4242424242424242', :brand => 'Visa' })
9
+
10
+ # expect {
11
+ # charge = Stripe::Charge.create({
12
+ # :amount => 999, :currency => 'usd',
13
+ # :customer => cus, :card => cus.cards.first,
14
+ # :description => 'hello'
15
+ # })
16
+ # }.to raise_error Stripe::CardError
17
+ # end
18
+
19
+ it 'is a valid card error', live: true do
20
+ stripe_helper.prepare_card_error
21
+
22
+ begin
23
+ Stripe::Customer.create(
24
+ email: 'alice@bob.com',
25
+ source: stripe_helper.generate_card_token(number: '123')
26
+ )
27
+ rescue Stripe::CardError => e
28
+ body = e.json_body
29
+ expect(body).to be_a(Hash)
30
+ error = body[:error]
31
+
32
+ expect(error[:type]).to eq 'card_error'
33
+ expect(error[:param]).to eq 'number'
34
+ expect(error[:code]).to eq 'invalid_number'
35
+ expect(error[:message]).to eq 'The card number is not a valid credit card number.'
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,140 @@
1
+ require "spec_helper"
2
+
3
+ describe StripeMock::Data::List do
4
+ let(:stripe_helper) { StripeMock.create_test_helper }
5
+
6
+ before :all do
7
+ StripeMock.start
8
+ end
9
+
10
+ after :all do
11
+ StripeMock.stop
12
+ end
13
+
14
+ it "contains data" do
15
+ obj = double
16
+ obj2 = double
17
+ obj3 = double
18
+ list = StripeMock::Data::List.new([obj, obj2, obj3])
19
+
20
+ expect(list.data).to eq([obj, obj2, obj3])
21
+ end
22
+
23
+ it "can accept a single object" do
24
+ list = StripeMock::Data::List.new(double)
25
+
26
+ expect(list.data).to be_kind_of(Array)
27
+ expect(list.data.size).to eq(1)
28
+ end
29
+
30
+ it "infers object type for url" do
31
+ customer = Stripe::Customer.create
32
+ list = StripeMock::Data::List.new([customer])
33
+
34
+ expect(list.url).to eq("/v1/customers")
35
+ end
36
+
37
+ it "returns in descending order if created available" do
38
+ charge_newer = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token, created: 5)
39
+ charge_older = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token, created: 4)
40
+ list = StripeMock::Data::List.new([charge_older, charge_newer])
41
+ hash = list.to_h
42
+
43
+ expect(hash).to eq(
44
+ object: "list",
45
+ data: [charge_newer, charge_older],
46
+ url: "/v1/charges",
47
+ has_more: false
48
+ )
49
+ end
50
+
51
+ it "eventually gets turned into a hash" do
52
+ charge1 = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
53
+ charge2 = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
54
+ charge3 = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
55
+ list = StripeMock::Data::List.new([charge1, charge2, charge3])
56
+ hash = list.to_h
57
+
58
+ expect(hash).to eq(
59
+ object: "list",
60
+ data: [charge3, charge2, charge1],
61
+ url: "/v1/charges",
62
+ has_more: false
63
+ )
64
+ end
65
+
66
+ it "delegates other methods to hash keys" do
67
+ list = StripeMock::Data::List.new([double, double, double])
68
+
69
+ expect(list).to respond_to(:data)
70
+ expect(list.data).to be_kind_of(Array)
71
+ expect(list.object).to eq("list")
72
+ expect(list.has_more).to eq(false)
73
+ expect(list.url).to eq("/v1/doubles")
74
+ expect { list.foobar }.to raise_error(NoMethodError)
75
+ end
76
+
77
+ context "with a limit" do
78
+ it "accepts a limit which is reflected in the data returned" do
79
+ list = StripeMock::Data::List.new([double] * 25)
80
+
81
+ expect(list.to_h[:data].size).to eq(10)
82
+
83
+ list = StripeMock::Data::List.new([double] * 25, limit: 15)
84
+
85
+ expect(list.limit).to eq(15)
86
+ expect(list.to_h[:data].size).to eq(15)
87
+ end
88
+
89
+ it "defaults to a limit of 10" do
90
+ list = StripeMock::Data::List.new([])
91
+
92
+ expect(list.limit).to eq(10)
93
+ end
94
+
95
+ it "won't accept a limit of > 100" do
96
+ list = StripeMock::Data::List.new([], limit: 105)
97
+
98
+ expect(list.limit).to eq(100)
99
+ end
100
+
101
+ it "won't accept a limit of < 1" do
102
+ list = StripeMock::Data::List.new([], limit: 0)
103
+
104
+ expect(list.limit).to eq(1)
105
+
106
+ list = StripeMock::Data::List.new([], limit: -4)
107
+
108
+ expect(list.limit).to eq(1)
109
+ end
110
+ end
111
+
112
+ context "pagination" do
113
+ it "has a has_more field when it has more" do
114
+ list = StripeMock::Data::List.new(
115
+ [Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)] * 256
116
+ )
117
+
118
+ expect(list).to have_more
119
+ end
120
+
121
+ it "accepts a starting_after parameter" do
122
+ data = []
123
+ 255.times { data << Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
124
+ new_charge = Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token)
125
+ data[89] = new_charge
126
+ list = StripeMock::Data::List.new(data, starting_after: new_charge.id)
127
+ hash = list.to_h
128
+ expect(hash[:data].size).to eq(10)
129
+ expect(hash[:data]).to eq(data[79, 10].reverse)
130
+ end
131
+
132
+ it "raises an error if starting_after cursor is not found" do
133
+ data = []
134
+ 255.times { data << Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
135
+ list = StripeMock::Data::List.new(data, starting_after: "test_ch_unknown")
136
+
137
+ expect { list.to_h }.to raise_error
138
+ end
139
+ end
140
+ end