stripe 3.8.2 → 5.45.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +424 -1
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +15 -20
  5. data/README.md +201 -58
  6. data/Rakefile +12 -9
  7. data/VERSION +1 -1
  8. data/bin/stripe-console +3 -1
  9. data/lib/stripe/api_operations/create.rb +3 -1
  10. data/lib/stripe/api_operations/delete.rb +30 -2
  11. data/lib/stripe/api_operations/list.rb +3 -12
  12. data/lib/stripe/api_operations/nested_resource.rb +40 -28
  13. data/lib/stripe/api_operations/request.rb +83 -9
  14. data/lib/stripe/api_operations/save.rb +19 -7
  15. data/lib/stripe/api_resource.rb +65 -4
  16. data/lib/stripe/connection_manager.rb +200 -0
  17. data/lib/stripe/error_object.rb +93 -0
  18. data/lib/stripe/errors.rb +47 -19
  19. data/lib/stripe/instrumentation.rb +84 -0
  20. data/lib/stripe/list_object.rb +43 -7
  21. data/lib/stripe/multipart_encoder.rb +131 -0
  22. data/lib/stripe/oauth.rb +14 -7
  23. data/lib/stripe/object_types.rb +110 -0
  24. data/lib/stripe/{account.rb → resources/account.rb} +60 -28
  25. data/lib/stripe/resources/account_link.rb +10 -0
  26. data/lib/stripe/resources/alipay_account.rb +34 -0
  27. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +4 -1
  28. data/lib/stripe/resources/application_fee.rb +14 -0
  29. data/lib/stripe/resources/application_fee_refund.rb +31 -0
  30. data/lib/stripe/resources/balance.rb +8 -0
  31. data/lib/stripe/resources/balance_transaction.rb +10 -0
  32. data/lib/stripe/resources/bank_account.rb +43 -0
  33. data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
  34. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  35. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +6 -6
  36. data/lib/stripe/resources/bitcoin_transaction.rb +16 -0
  37. data/lib/stripe/resources/capability.rb +34 -0
  38. data/lib/stripe/{card.rb → resources/card.rb} +15 -4
  39. data/lib/stripe/resources/charge.rb +23 -0
  40. data/lib/stripe/resources/checkout/session.rb +27 -0
  41. data/lib/stripe/resources/country_spec.rb +10 -0
  42. data/lib/stripe/{coupon.rb → resources/coupon.rb} +5 -2
  43. data/lib/stripe/resources/credit_note.rb +33 -0
  44. data/lib/stripe/resources/credit_note_line_item.rb +8 -0
  45. data/lib/stripe/resources/customer.rb +52 -0
  46. data/lib/stripe/resources/customer_balance_transaction.rb +31 -0
  47. data/lib/stripe/resources/discount.rb +8 -0
  48. data/lib/stripe/resources/dispute.rb +22 -0
  49. data/lib/stripe/resources/ephemeral_key.rb +20 -0
  50. data/lib/stripe/resources/event.rb +10 -0
  51. data/lib/stripe/resources/exchange_rate.rb +10 -0
  52. data/lib/stripe/resources/file.rb +36 -0
  53. data/lib/stripe/resources/file_link.rb +12 -0
  54. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  55. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  56. data/lib/stripe/resources/invoice.rb +74 -0
  57. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +5 -2
  58. data/lib/stripe/resources/invoice_line_item.rb +8 -0
  59. data/lib/stripe/resources/issuing/authorization.rb +34 -0
  60. data/lib/stripe/resources/issuing/card.rb +25 -0
  61. data/lib/stripe/resources/issuing/card_details.rb +10 -0
  62. data/lib/stripe/resources/issuing/cardholder.rb +14 -0
  63. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  64. data/lib/stripe/resources/issuing/transaction.rb +13 -0
  65. data/lib/stripe/resources/line_item.rb +8 -0
  66. data/lib/stripe/resources/login_link.rb +15 -0
  67. data/lib/stripe/resources/mandate.rb +8 -0
  68. data/lib/stripe/resources/order.rb +33 -0
  69. data/lib/stripe/resources/order_return.rb +10 -0
  70. data/lib/stripe/resources/payment_intent.rb +53 -0
  71. data/lib/stripe/resources/payment_link.rb +23 -0
  72. data/lib/stripe/resources/payment_method.rb +33 -0
  73. data/lib/stripe/resources/payout.rb +33 -0
  74. data/lib/stripe/resources/person.rb +32 -0
  75. data/lib/stripe/{plan.rb → resources/plan.rb} +4 -1
  76. data/lib/stripe/resources/price.rb +12 -0
  77. data/lib/stripe/{product.rb → resources/product.rb} +6 -3
  78. data/lib/stripe/resources/promotion_code.rb +12 -0
  79. data/lib/stripe/resources/quote.rb +105 -0
  80. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  81. data/lib/stripe/resources/radar/value_list.rb +15 -0
  82. data/lib/stripe/resources/radar/value_list_item.rb +14 -0
  83. data/lib/stripe/{recipient.rb → resources/recipient.rb} +5 -6
  84. data/lib/stripe/resources/recipient_transfer.rb +7 -0
  85. data/lib/stripe/{refund.rb → resources/refund.rb} +4 -1
  86. data/lib/stripe/resources/reporting/report_run.rb +13 -0
  87. data/lib/stripe/resources/reporting/report_type.rb +13 -0
  88. data/lib/stripe/resources/reversal.rb +30 -0
  89. data/lib/stripe/resources/review.rb +21 -0
  90. data/lib/stripe/resources/setup_attempt.rb +10 -0
  91. data/lib/stripe/resources/setup_intent.rb +43 -0
  92. data/lib/stripe/resources/shipping_rate.rb +12 -0
  93. data/lib/stripe/resources/sigma/scheduled_query_run.rb +16 -0
  94. data/lib/stripe/{sku.rb → resources/sku.rb} +6 -3
  95. data/lib/stripe/resources/source.rb +47 -0
  96. data/lib/stripe/resources/source_transaction.rb +8 -0
  97. data/lib/stripe/resources/subscription.rb +26 -0
  98. data/lib/stripe/resources/subscription_item.rb +26 -0
  99. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  100. data/lib/stripe/resources/tax_code.rb +10 -0
  101. data/lib/stripe/resources/tax_id.rb +27 -0
  102. data/lib/stripe/resources/tax_rate.rb +12 -0
  103. data/lib/stripe/resources/terminal/connection_token.rb +12 -0
  104. data/lib/stripe/resources/terminal/location.rb +15 -0
  105. data/lib/stripe/resources/terminal/reader.rb +15 -0
  106. data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
  107. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +4 -1
  108. data/lib/stripe/resources/token.rb +10 -0
  109. data/lib/stripe/resources/topup.rb +23 -0
  110. data/lib/stripe/resources/transfer.rb +27 -0
  111. data/lib/stripe/resources/usage_record.rb +8 -0
  112. data/lib/stripe/resources/usage_record_summary.rb +8 -0
  113. data/lib/stripe/resources/webhook_endpoint.rb +13 -0
  114. data/lib/stripe/resources.rb +93 -0
  115. data/lib/stripe/singleton_api_resource.rb +8 -2
  116. data/lib/stripe/stripe_client.rb +712 -233
  117. data/lib/stripe/stripe_configuration.rb +194 -0
  118. data/lib/stripe/stripe_object.rb +180 -67
  119. data/lib/stripe/stripe_response.rb +89 -27
  120. data/lib/stripe/util.rb +87 -168
  121. data/lib/stripe/version.rb +3 -1
  122. data/lib/stripe/webhook.rb +45 -10
  123. data/lib/stripe.rb +47 -160
  124. data/stripe.gemspec +27 -8
  125. metadata +112 -178
  126. data/.gitattributes +0 -4
  127. data/.github/ISSUE_TEMPLATE.md +0 -5
  128. data/.gitignore +0 -6
  129. data/.rubocop.yml +0 -20
  130. data/.rubocop_todo.yml +0 -60
  131. data/.travis.yml +0 -38
  132. data/lib/stripe/alipay_account.rb +0 -25
  133. data/lib/stripe/application_fee.rb +0 -25
  134. data/lib/stripe/application_fee_refund.rb +0 -20
  135. data/lib/stripe/balance.rb +0 -5
  136. data/lib/stripe/balance_transaction.rb +0 -11
  137. data/lib/stripe/bank_account.rb +0 -30
  138. data/lib/stripe/bitcoin_transaction.rb +0 -13
  139. data/lib/stripe/charge.rb +0 -82
  140. data/lib/stripe/country_spec.rb +0 -11
  141. data/lib/stripe/customer.rb +0 -88
  142. data/lib/stripe/dispute.rb +0 -17
  143. data/lib/stripe/ephemeral_key.rb +0 -18
  144. data/lib/stripe/event.rb +0 -7
  145. data/lib/stripe/exchange_rate.rb +0 -11
  146. data/lib/stripe/file_upload.rb +0 -35
  147. data/lib/stripe/invoice.rb +0 -30
  148. data/lib/stripe/invoice_line_item.rb +0 -5
  149. data/lib/stripe/login_link.rb +0 -9
  150. data/lib/stripe/order.rb +0 -35
  151. data/lib/stripe/order_return.rb +0 -11
  152. data/lib/stripe/payout.rb +0 -18
  153. data/lib/stripe/recipient_transfer.rb +0 -5
  154. data/lib/stripe/reversal.rb +0 -20
  155. data/lib/stripe/source.rb +0 -36
  156. data/lib/stripe/source_transaction.rb +0 -5
  157. data/lib/stripe/subscription.rb +0 -43
  158. data/lib/stripe/subscription_item.rb +0 -14
  159. data/lib/stripe/token.rb +0 -7
  160. data/lib/stripe/transfer.rb +0 -21
  161. data/test/api_stub_helpers.rb +0 -0
  162. data/test/stripe/account_external_accounts_operations_test.rb +0 -66
  163. data/test/stripe/account_login_links_operations_test.rb +0 -19
  164. data/test/stripe/account_test.rb +0 -190
  165. data/test/stripe/alipay_account_test.rb +0 -35
  166. data/test/stripe/api_operations_test.rb +0 -76
  167. data/test/stripe/api_resource_test.rb +0 -522
  168. data/test/stripe/apple_pay_domain_test.rb +0 -31
  169. data/test/stripe/application_fee_refund_test.rb +0 -35
  170. data/test/stripe/application_fee_refunds_operations_test.rb +0 -54
  171. data/test/stripe/application_fee_test.rb +0 -12
  172. data/test/stripe/balance_test.rb +0 -11
  173. data/test/stripe/bank_account_test.rb +0 -34
  174. data/test/stripe/charge_test.rb +0 -57
  175. data/test/stripe/country_spec_test.rb +0 -18
  176. data/test/stripe/coupon_test.rb +0 -42
  177. data/test/stripe/customer_card_test.rb +0 -42
  178. data/test/stripe/customer_sources_operations_test.rb +0 -66
  179. data/test/stripe/customer_test.rb +0 -113
  180. data/test/stripe/dispute_test.rb +0 -40
  181. data/test/stripe/ephemeral_key_test.rb +0 -84
  182. data/test/stripe/errors_test.rb +0 -18
  183. data/test/stripe/exchange_rate_test.rb +0 -18
  184. data/test/stripe/file_upload_test.rb +0 -83
  185. data/test/stripe/invoice_item_test.rb +0 -53
  186. data/test/stripe/invoice_line_item_test.rb +0 -6
  187. data/test/stripe/invoice_test.rb +0 -111
  188. data/test/stripe/list_object_test.rb +0 -154
  189. data/test/stripe/login_link_test.rb +0 -35
  190. data/test/stripe/oauth_test.rb +0 -77
  191. data/test/stripe/order_return_test.rb +0 -19
  192. data/test/stripe/order_test.rb +0 -57
  193. data/test/stripe/payout_test.rb +0 -48
  194. data/test/stripe/plan_test.rb +0 -50
  195. data/test/stripe/product_test.rb +0 -45
  196. data/test/stripe/recipient_test.rb +0 -47
  197. data/test/stripe/refund_test.rb +0 -37
  198. data/test/stripe/reversal_test.rb +0 -41
  199. data/test/stripe/sku_test.rb +0 -48
  200. data/test/stripe/source_test.rb +0 -84
  201. data/test/stripe/source_transaction_test.rb +0 -28
  202. data/test/stripe/stripe_client_test.rb +0 -728
  203. data/test/stripe/stripe_object_test.rb +0 -448
  204. data/test/stripe/stripe_response_test.rb +0 -47
  205. data/test/stripe/subscription_item_test.rb +0 -52
  206. data/test/stripe/subscription_test.rb +0 -104
  207. data/test/stripe/three_d_secure_test.rb +0 -21
  208. data/test/stripe/transfer_reversals_operations_test.rb +0 -55
  209. data/test/stripe/transfer_test.rb +0 -41
  210. data/test/stripe/util_test.rb +0 -432
  211. data/test/stripe/webhook_test.rb +0 -94
  212. data/test/stripe_test.rb +0 -59
  213. data/test/test_data.rb +0 -59
  214. data/test/test_helper.rb +0 -60
@@ -1,55 +0,0 @@
1
- require File.expand_path("../../test_helper", __FILE__)
2
-
3
- module Stripe
4
- class TransferReversalsOperationsTest < Test::Unit::TestCase
5
- setup do
6
- @transfer_id = "tr_123"
7
- @reversal_id = "trr_123"
8
- end
9
-
10
- context "#create_reversal" do
11
- should "create a reversal" do
12
- reversal = Stripe::Transfer.create_reversal(
13
- @transfer_id,
14
- amount: 100
15
- )
16
- assert_requested :post, "#{Stripe.api_base}/v1/transfers/#{@transfer_id}/reversals"
17
- assert reversal.is_a?(Stripe::Reversal)
18
- end
19
- end
20
-
21
- context "#retrieve_reversal" do
22
- should "retrieve a reversal" do
23
- reversal = Stripe::Transfer.retrieve_reversal(
24
- @transfer_id,
25
- @reversal_id
26
- )
27
- assert_requested :get, "#{Stripe.api_base}/v1/transfers/#{@transfer_id}/reversals/#{@reversal_id}"
28
- assert reversal.is_a?(Stripe::Reversal)
29
- end
30
- end
31
-
32
- context "#update_reversal" do
33
- should "update a reversal" do
34
- reversal = Stripe::Transfer.update_reversal(
35
- @transfer_id,
36
- @reversal_id,
37
- metadata: { foo: "bar" }
38
- )
39
- assert_requested :post, "#{Stripe.api_base}/v1/transfers/#{@transfer_id}/reversals/#{@reversal_id}"
40
- assert reversal.is_a?(Stripe::Reversal)
41
- end
42
- end
43
-
44
- context "#list_reversals" do
45
- should "list the transfer's reversals" do
46
- reversals = Stripe::Transfer.list_reversals(
47
- @transfer_id
48
- )
49
- assert_requested :get, "#{Stripe.api_base}/v1/transfers/#{@transfer_id}/reversals"
50
- assert reversals.is_a?(Stripe::ListObject)
51
- assert reversals.data.is_a?(Array)
52
- end
53
- end
54
- end
55
- end
@@ -1,41 +0,0 @@
1
- require File.expand_path("../../test_helper", __FILE__)
2
-
3
- module Stripe
4
- class TransferTest < Test::Unit::TestCase
5
- should "be listable" do
6
- transfers = Stripe::Transfer.list
7
- assert_requested :get, "#{Stripe.api_base}/v1/transfers"
8
- assert transfers.data.is_a?(Array)
9
- assert transfers.data[0].is_a?(Stripe::Transfer)
10
- end
11
-
12
- should "be retrievable" do
13
- transfer = Stripe::Transfer.retrieve("tr_123")
14
- assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123"
15
- assert transfer.is_a?(Stripe::Transfer)
16
- end
17
-
18
- should "be creatable" do
19
- transfer = Stripe::Transfer.create(
20
- amount: 100,
21
- currency: "USD",
22
- destination: "acct_123"
23
- )
24
- assert_requested :post, "#{Stripe.api_base}/v1/transfers"
25
- assert transfer.is_a?(Stripe::Transfer)
26
- end
27
-
28
- should "be saveable" do
29
- transfer = Stripe::Transfer.retrieve("tr_123")
30
- transfer.metadata["key"] = "value"
31
- transfer.save
32
- assert_requested :post, "#{Stripe.api_base}/v1/transfers/#{transfer.id}"
33
- end
34
-
35
- should "be updateable" do
36
- transfer = Stripe::Transfer.update("tr_123", metadata: { foo: "bar" })
37
- assert_requested :post, "#{Stripe.api_base}/v1/transfers/tr_123"
38
- assert transfer.is_a?(Stripe::Transfer)
39
- end
40
- end
41
- end
@@ -1,432 +0,0 @@
1
- require File.expand_path("../../test_helper", __FILE__)
2
-
3
- module Stripe
4
- class UtilTest < Test::Unit::TestCase
5
- context "OPTS_COPYABLE" do
6
- should "include :apibase" do
7
- assert_include Stripe::Util::OPTS_COPYABLE, :api_base
8
- end
9
- end
10
-
11
- context "OPTS_PERSISTABLE" do
12
- should "include :client" do
13
- assert_include Stripe::Util::OPTS_PERSISTABLE, :client
14
- end
15
-
16
- should "not include :idempotency_key" do
17
- refute_includes Stripe::Util::OPTS_PERSISTABLE, :idempotency_key
18
- end
19
- end
20
-
21
- should "#encode_parameters should prepare parameters for an HTTP request" do
22
- params = {
23
- a: 3,
24
- b: "+foo?",
25
- c: "bar&baz",
26
- d: { a: "a", b: "b" },
27
- e: [0, 1],
28
- f: "",
29
-
30
- # note the empty hash won't even show up in the request
31
- g: [],
32
- }
33
- assert_equal(
34
- "a=3&b=%2Bfoo%3F&c=bar%26baz&d[a]=a&d[b]=b&e[]=0&e[]=1&f=",
35
- Stripe::Util.encode_parameters(params)
36
- )
37
- end
38
-
39
- should "#encode_params should throw an error on an array of maps that cannot be encoded" do
40
- params = {
41
- a: [
42
- { a: 1, b: 2 },
43
- { c: 3, a: 4 },
44
- ],
45
- }
46
- e = assert_raises(ArgumentError) do
47
- Stripe::Util.encode_parameters(params)
48
- end
49
- expected = "All maps nested in an array should start with the same key " \
50
- "(expected starting key 'a', got 'c')"
51
- assert_equal expected, e.message
52
-
53
- # Make sure the check is recursive by taking our original params and
54
- # nesting it into yet another map and array. Should throw exactly the
55
- # same error because it's still the in inner array of maps that's wrong.
56
- params = {
57
- x: [
58
- params,
59
- ],
60
- }
61
- e = assert_raises(ArgumentError) do
62
- Stripe::Util.encode_parameters(params)
63
- end
64
- assert_equal expected, e.message
65
- end
66
-
67
- should "#url_encode should prepare strings for HTTP" do
68
- assert_equal "foo", Stripe::Util.url_encode("foo")
69
- assert_equal "foo", Stripe::Util.url_encode(:foo)
70
- assert_equal "foo%2B", Stripe::Util.url_encode("foo+")
71
- assert_equal "foo%26", Stripe::Util.url_encode("foo&")
72
- assert_equal "foo[bar]", Stripe::Util.url_encode("foo[bar]")
73
- end
74
-
75
- should "#flatten_params should encode parameters according to Rails convention" do
76
- params = [
77
- [:a, 3],
78
- [:b, "foo?"],
79
- [:c, "bar&baz"],
80
- [:d, { a: "a", b: "b" }],
81
- [:e, [0, 1]],
82
- [:f, [
83
- { foo: "1", ghi: "2" },
84
- { foo: "3", bar: "4" },
85
- ],],
86
- ]
87
- assert_equal([
88
- ["a", 3],
89
- ["b", "foo?"],
90
- ["c", "bar&baz"],
91
- ["d[a]", "a"],
92
- ["d[b]", "b"],
93
- ["e[]", 0],
94
- ["e[]", 1],
95
-
96
- # *The key here is the order*. In order to be properly interpreted as
97
- # an array of hashes on the server, everything from a single hash must
98
- # come in at once. A duplicate key in an array triggers a new element.
99
- ["f[][foo]", "1"],
100
- ["f[][ghi]", "2"],
101
- ["f[][foo]", "3"],
102
- ["f[][bar]", "4"],
103
- ], Stripe::Util.flatten_params(params))
104
- end
105
-
106
- should "#symbolize_names should convert names to symbols" do
107
- start = {
108
- "foo" => "bar",
109
- "array" => [{ "foo" => "bar" }],
110
- "nested" => {
111
- 1 => 2,
112
- :symbol => 9,
113
- "string" => nil,
114
- },
115
- }
116
- finish = {
117
- foo: "bar",
118
- array: [{ foo: "bar" }],
119
- nested: {
120
- 1 => 2,
121
- :symbol => 9,
122
- :string => nil,
123
- },
124
- }
125
-
126
- symbolized = Stripe::Util.symbolize_names(start)
127
- assert_equal(finish, symbolized)
128
- end
129
-
130
- should "#normalize_opts should reject nil keys" do
131
- assert_raise { Stripe::Util.normalize_opts(nil) }
132
- assert_raise { Stripe::Util.normalize_opts(api_key: nil) }
133
- end
134
-
135
- should "#convert_to_stripe_object should pass through unknown types" do
136
- obj = Util.convert_to_stripe_object(7, {})
137
- assert_equal 7, obj
138
- end
139
-
140
- should "#convert_to_stripe_object should turn hashes into StripeObjects" do
141
- obj = Util.convert_to_stripe_object({ foo: "bar" }, {})
142
- assert obj.is_a?(StripeObject)
143
- assert_equal "bar", obj.foo
144
- end
145
-
146
- should "#convert_to_stripe_object should turn lists into ListObjects" do
147
- obj = Util.convert_to_stripe_object({ object: "list" }, {})
148
- assert obj.is_a?(ListObject)
149
- end
150
-
151
- should "#convert_to_stripe_object should marshal other classes" do
152
- obj = Util.convert_to_stripe_object({ object: "account" }, {})
153
- assert obj.is_a?(Account)
154
- end
155
-
156
- should "#convert_to_stripe_object should marshal arrays" do
157
- obj = Util.convert_to_stripe_object([1, 2, 3], {})
158
- assert_equal [1, 2, 3], obj
159
- end
160
-
161
- should "#array_to_hash should convert an array into a hash with integer keys" do
162
- assert_equal({ "0" => 1, "1" => 2, "2" => 3 }, Util.array_to_hash([1, 2, 3]))
163
- end
164
-
165
- context ".request_id_dashboard_url" do
166
- should "generate a livemode URL" do
167
- assert_equal "https://dashboard.stripe.com/live/logs/request-id",
168
- Util.request_id_dashboard_url("request-id", "sk_live_123")
169
- end
170
-
171
- should "generate a testmode URL" do
172
- assert_equal "https://dashboard.stripe.com/test/logs/request-id",
173
- Util.request_id_dashboard_url("request-id", "sk_test_123")
174
- end
175
-
176
- should "default to a testmode URL" do
177
- assert_equal "https://dashboard.stripe.com/test/logs/request-id",
178
- Util.request_id_dashboard_url("request-id", nil)
179
- end
180
- end
181
-
182
- context ".log_*" do
183
- setup do
184
- @old_log_level = Stripe.log_level
185
- Stripe.log_level = nil
186
-
187
- @old_stderr = $stderr
188
- $stderr = StringIO.new
189
-
190
- @old_stdout = $stdout
191
- $stdout = StringIO.new
192
- end
193
-
194
- teardown do
195
- Stripe.log_level = @old_log_level
196
- $stderr = @old_stderr
197
- $stdout = @old_stdout
198
- end
199
-
200
- context ".log_debug" do
201
- should "not log if logging is disabled" do
202
- Util.log_debug("foo")
203
- assert_equal "", $stdout.string
204
- end
205
-
206
- should "log if level set to debug" do
207
- Stripe.log_level = Stripe::LEVEL_DEBUG
208
- Util.log_debug("foo")
209
- assert_equal "message=foo level=debug \n", $stdout.string
210
- end
211
-
212
- should "not log if level set to error" do
213
- Stripe.log_level = Stripe::LEVEL_ERROR
214
- Util.log_debug("foo")
215
- assert_equal "", $stdout.string
216
- end
217
-
218
- should "not log if level set to info" do
219
- Stripe.log_level = Stripe::LEVEL_INFO
220
- Util.log_debug("foo")
221
- assert_equal "", $stdout.string
222
- end
223
- end
224
-
225
- context ".log_error" do
226
- should "not log if logging is disabled" do
227
- Util.log_error("foo")
228
- assert_equal "", $stdout.string
229
- end
230
-
231
- should "log if level set to debug" do
232
- Stripe.log_level = Stripe::LEVEL_DEBUG
233
- Util.log_error("foo")
234
- assert_equal "message=foo level=error \n", $stderr.string
235
- end
236
-
237
- should "log if level set to error" do
238
- Stripe.log_level = Stripe::LEVEL_ERROR
239
- Util.log_error("foo")
240
- assert_equal "message=foo level=error \n", $stderr.string
241
- end
242
-
243
- should "log if level set to info" do
244
- Stripe.log_level = Stripe::LEVEL_INFO
245
- Util.log_error("foo")
246
- assert_equal "message=foo level=error \n", $stderr.string
247
- end
248
- end
249
-
250
- context ".log_info" do
251
- should "not log if logging is disabled" do
252
- Util.log_info("foo")
253
- assert_equal "", $stdout.string
254
- end
255
-
256
- should "log if level set to debug" do
257
- Stripe.log_level = Stripe::LEVEL_DEBUG
258
- Util.log_info("foo")
259
- assert_equal "message=foo level=info \n", $stdout.string
260
- end
261
-
262
- should "not log if level set to error" do
263
- Stripe.log_level = Stripe::LEVEL_ERROR
264
- Util.log_debug("foo")
265
- assert_equal "", $stdout.string
266
- end
267
-
268
- should "log if level set to info" do
269
- Stripe.log_level = Stripe::LEVEL_INFO
270
- Util.log_info("foo")
271
- assert_equal "message=foo level=info \n", $stdout.string
272
- end
273
- end
274
- end
275
-
276
- context ".log_* with a logger" do
277
- setup do
278
- @out = StringIO.new
279
- logger = ::Logger.new(@out)
280
-
281
- # Set a really simple formatter to make matching output as easy as
282
- # possible.
283
- logger.formatter = proc { |_severity, _datetime, _progname, message|
284
- message
285
- }
286
-
287
- Stripe.logger = logger
288
- end
289
-
290
- context ".log_debug" do
291
- should "log to the logger" do
292
- Util.log_debug("foo")
293
- assert_equal "message=foo ", @out.string
294
- end
295
- end
296
-
297
- context ".log_error" do
298
- should "log to the logger" do
299
- Util.log_error("foo")
300
- assert_equal "message=foo ", @out.string
301
- end
302
- end
303
-
304
- context ".log_info" do
305
- should "log to the logger" do
306
- Util.log_info("foo")
307
- assert_equal "message=foo ", @out.string
308
- end
309
- end
310
- end
311
-
312
- context ".normalize_headers" do
313
- should "normalize the format of a header key" do
314
- assert_equal({ "Request-Id" => nil },
315
- Util.normalize_headers("Request-Id" => nil))
316
- assert_equal({ "Request-Id" => nil },
317
- Util.normalize_headers("request-id" => nil))
318
- assert_equal({ "Request-Id" => nil },
319
- Util.normalize_headers("Request-ID" => nil))
320
- assert_equal({ "Request-Id" => nil },
321
- Util.normalize_headers(request_id: nil))
322
- end
323
-
324
- should "tolerate bad formatting" do
325
- assert_equal({ "Request-Id" => nil },
326
- Util.normalize_headers("-Request--Id-" => nil))
327
- assert_equal({ "Request-Id" => nil },
328
- Util.normalize_headers(request__id: nil))
329
- end
330
- end
331
-
332
- #
333
- # private
334
- #
335
- # I don't feel particularly good about using #send to invoke these, but I
336
- # want them hidden from the public interface, and each method is far easier
337
- # to test in isolation (as opposed to going through a public method).
338
- #
339
-
340
- context ".colorize" do
341
- should "colorize for a TTY" do
342
- assert_equal "\033[0;32;49mfoo\033[0m",
343
- Util.send(:colorize, "foo", :green, true)
344
- end
345
-
346
- should "not colorize otherwise" do
347
- assert_equal "foo", Util.send(:colorize, "foo", :green, false)
348
- end
349
- end
350
-
351
- context ".level_name" do
352
- should "convert levels to names" do
353
- assert_equal "debug", Util.send(:level_name, LEVEL_DEBUG)
354
- assert_equal "error", Util.send(:level_name, LEVEL_ERROR)
355
- assert_equal "info", Util.send(:level_name, LEVEL_INFO)
356
- end
357
- end
358
-
359
- context ".log_internal" do
360
- should "log in a terminal friendly way" do
361
- out = StringIO.new
362
-
363
- # Sketchy as anything, but saves us from pulling in a mocking library.
364
- # Open this instance of StringIO, and add a method override so that it
365
- # looks like a TTY.
366
- out.instance_eval do
367
- def isatty
368
- true
369
- end
370
- end
371
-
372
- Util.send(:log_internal, "message", { foo: "bar" },
373
- color: :green, level: Stripe::LEVEL_DEBUG, logger: nil, out: out)
374
- assert_equal "\e[0;32;49mDEBU\e[0m message \e[0;32;49mfoo\e[0m=bar\n",
375
- out.string
376
- end
377
-
378
- should "log in a data friendly way" do
379
- out = StringIO.new
380
- Util.send(:log_internal, "message", { foo: "bar" },
381
- color: :green, level: Stripe::LEVEL_DEBUG, logger: nil, out: out)
382
- assert_equal "message=message level=debug foo=bar\n",
383
- out.string
384
- end
385
-
386
- should "log to a logger if set" do
387
- out = StringIO.new
388
- logger = ::Logger.new(out)
389
-
390
- # Set a really simple formatter to make matching output as easy as
391
- # possible.
392
- logger.formatter = proc { |_severity, _datetime, _progname, message|
393
- message
394
- }
395
-
396
- Util.send(:log_internal, "message", { foo: "bar" },
397
- color: :green, level: Stripe::LEVEL_DEBUG, logger: logger, out: nil)
398
- assert_equal "message=message foo=bar",
399
- out.string
400
- end
401
- end
402
-
403
- context ".wrap_logfmt_value" do
404
- should "pass through simple values" do
405
- assert_equal "abc", Util.send(:wrap_logfmt_value, "abc")
406
- assert_equal "123", Util.send(:wrap_logfmt_value, "123")
407
- assert_equal "a-b_c/d", Util.send(:wrap_logfmt_value, "a-b_c/d")
408
- end
409
-
410
- should "pass through numerics" do
411
- assert_equal 123, Util.send(:wrap_logfmt_value, 123)
412
- assert_equal 1.23, Util.send(:wrap_logfmt_value, 1.23)
413
- end
414
-
415
- should "wrap more complex values in double quotes" do
416
- assert_equal %("abc=123"), Util.send(:wrap_logfmt_value, %(abc=123))
417
- end
418
-
419
- should "escape double quotes already in the value" do
420
- assert_equal %("abc=\\"123\\""), Util.send(:wrap_logfmt_value, %(abc="123"))
421
- end
422
-
423
- should "remove newlines" do
424
- assert_equal %("abc"), Util.send(:wrap_logfmt_value, "a\nb\nc")
425
- end
426
-
427
- should "not error if given a non-string" do
428
- assert_equal "true", Util.send(:wrap_logfmt_value, true)
429
- end
430
- end
431
- end
432
- end
@@ -1,94 +0,0 @@
1
- require File.expand_path("../../test_helper", __FILE__)
2
-
3
- module Stripe
4
- class WebhookTest < Test::Unit::TestCase
5
- EVENT_PAYLOAD = <<-PAYLOAD.freeze
6
- {
7
- "id": "evt_test_webhook",
8
- "object": "event"
9
- }
10
- PAYLOAD
11
- SECRET = "whsec_test_secret".freeze
12
-
13
- def generate_header(opts = {})
14
- opts[:timestamp] ||= Time.now.to_i
15
- opts[:payload] ||= EVENT_PAYLOAD
16
- opts[:secret] ||= SECRET
17
- opts[:scheme] ||= Stripe::Webhook::Signature::EXPECTED_SCHEME
18
- opts[:signature] ||= Stripe::Webhook::Signature.send(:compute_signature, "#{opts[:timestamp]}.#{opts[:payload]}", opts[:secret])
19
- "t=#{opts[:timestamp]},#{opts[:scheme]}=#{opts[:signature]}"
20
- end
21
-
22
- context ".construct_event" do
23
- should "return an Event instance from a valid JSON payload and valid signature header" do
24
- header = generate_header
25
- event = Stripe::Webhook.construct_event(EVENT_PAYLOAD, header, SECRET)
26
- assert event.is_a?(Stripe::Event)
27
- end
28
-
29
- should "raise a JSON::ParserError from an invalid JSON payload" do
30
- assert_raises JSON::ParserError do
31
- payload = "this is not valid JSON"
32
- header = generate_header(payload: payload)
33
- Stripe::Webhook.construct_event(payload, header, SECRET)
34
- end
35
- end
36
-
37
- should "raise a SignatureVerificationError from a valid JSON payload and an invalid signature header" do
38
- header = "bad_header"
39
- assert_raises Stripe::SignatureVerificationError do
40
- Stripe::Webhook.construct_event(EVENT_PAYLOAD, header, SECRET)
41
- end
42
- end
43
- end
44
-
45
- context ".verify_signature_header" do
46
- should "raise a SignatureVerificationError when the header does not have the expected format" do
47
- header = 'i\'m not even a real signature header'
48
- e = assert_raises(Stripe::SignatureVerificationError) do
49
- Stripe::Webhook::Signature.verify_header(EVENT_PAYLOAD, header, "secret")
50
- end
51
- assert_match("Unable to extract timestamp and signatures from header", e.message)
52
- end
53
-
54
- should "raise a SignatureVerificationError when there are no signatures with the expected scheme" do
55
- header = generate_header(scheme: "v0")
56
- e = assert_raises(Stripe::SignatureVerificationError) do
57
- Stripe::Webhook::Signature.verify_header(EVENT_PAYLOAD, header, "secret")
58
- end
59
- assert_match("No signatures found with expected scheme", e.message)
60
- end
61
-
62
- should "raise a SignatureVerificationError when there are no valid signatures for the payload" do
63
- header = generate_header(signature: "bad_signature")
64
- e = assert_raises(Stripe::SignatureVerificationError) do
65
- Stripe::Webhook::Signature.verify_header(EVENT_PAYLOAD, header, "secret")
66
- end
67
- assert_match("No signatures found matching the expected signature for payload", e.message)
68
- end
69
-
70
- should "raise a SignatureVerificationError when the timestamp is not within the tolerance" do
71
- header = generate_header(timestamp: Time.now.to_i - 15)
72
- e = assert_raises(Stripe::SignatureVerificationError) do
73
- Stripe::Webhook::Signature.verify_header(EVENT_PAYLOAD, header, SECRET, tolerance: 10)
74
- end
75
- assert_match("Timestamp outside the tolerance zone", e.message)
76
- end
77
-
78
- should "return true when the header contains a valid signature and the timestamp is within the tolerance" do
79
- header = generate_header
80
- assert(Stripe::Webhook::Signature.verify_header(EVENT_PAYLOAD, header, SECRET, tolerance: 10))
81
- end
82
-
83
- should "return true when the header contains at least one valid signature" do
84
- header = generate_header + ",v1=bad_signature"
85
- assert(Stripe::Webhook::Signature.verify_header(EVENT_PAYLOAD, header, SECRET, tolerance: 10))
86
- end
87
-
88
- should "return true when the header contains a valid signature and the timestamp is off but no tolerance is provided" do
89
- header = generate_header(timestamp: 12_345)
90
- assert(Stripe::Webhook::Signature.verify_header(EVENT_PAYLOAD, header, SECRET))
91
- end
92
- end
93
- end
94
- end
data/test/stripe_test.rb DELETED
@@ -1,59 +0,0 @@
1
- require File.expand_path("../test_helper", __FILE__)
2
-
3
- class StripeTest < Test::Unit::TestCase
4
- should "warn that #refresh_from is deprecated" do
5
- old_stderr = $stderr
6
- $stderr = StringIO.new
7
- begin
8
- Stripe.uri_encode({})
9
- message = "NOTE: Stripe.uri_encode is deprecated; use " \
10
- "Stripe::Util#encode_parameters instead"
11
- assert_match Regexp.new(message), $stderr.string
12
- ensure
13
- $stderr = old_stderr
14
- end
15
- end
16
-
17
- should "allow app_info to be configured" do
18
- begin
19
- old = Stripe.app_info
20
- Stripe.set_app_info(
21
- "MyAwesomePlugin",
22
- url: "https://myawesomeplugin.info",
23
- version: "1.2.34"
24
- )
25
- assert_equal({
26
- name: "MyAwesomePlugin",
27
- url: "https://myawesomeplugin.info",
28
- version: "1.2.34",
29
- }, Stripe.app_info)
30
- ensure
31
- Stripe.app_info = old
32
- end
33
- end
34
-
35
- should "allow ca_bundle_path to be configured" do
36
- begin
37
- old = Stripe.ca_bundle_path
38
- Stripe.ca_bundle_path = "path/to/ca/bundle"
39
- assert_equal "path/to/ca/bundle", Stripe.ca_bundle_path
40
- ensure
41
- Stripe.ca_bundle_path = old
42
- end
43
- end
44
-
45
- should "allow max_network_retries to be configured" do
46
- begin
47
- old = Stripe.max_network_retries
48
- Stripe.max_network_retries = 99
49
- assert_equal 99, Stripe.max_network_retries
50
- ensure
51
- Stripe.max_network_retries = old
52
- end
53
- end
54
-
55
- should "have default open and read timeouts" do
56
- assert_equal Stripe.open_timeout, 30
57
- assert_equal Stripe.read_timeout, 80
58
- end
59
- end