stripe 5.1.0 → 5.55.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +241 -17
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +9 -5
  5. data/Makefile +7 -0
  6. data/README.md +105 -37
  7. data/VERSION +1 -1
  8. data/lib/stripe/api_operations/create.rb +1 -1
  9. data/lib/stripe/api_operations/delete.rb +7 -3
  10. data/lib/stripe/api_operations/list.rb +1 -6
  11. data/lib/stripe/api_operations/nested_resource.rb +29 -26
  12. data/lib/stripe/api_operations/request.rb +82 -6
  13. data/lib/stripe/api_operations/save.rb +7 -4
  14. data/lib/stripe/api_operations/search.rb +19 -0
  15. data/lib/stripe/api_resource.rb +13 -19
  16. data/lib/stripe/api_resource_test_helpers.rb +47 -0
  17. data/lib/stripe/connection_manager.rb +85 -26
  18. data/lib/stripe/error_object.rb +2 -3
  19. data/lib/stripe/errors.rb +9 -1
  20. data/lib/stripe/instrumentation.rb +84 -0
  21. data/lib/stripe/list_object.rb +32 -4
  22. data/lib/stripe/oauth.rb +8 -6
  23. data/lib/stripe/object_types.rb +26 -1
  24. data/lib/stripe/resources/account.rb +5 -9
  25. data/lib/stripe/resources/account_link.rb +1 -0
  26. data/lib/stripe/resources/apple_pay_domain.rb +1 -0
  27. data/lib/stripe/resources/application_fee.rb +1 -0
  28. data/lib/stripe/resources/application_fee_refund.rb +2 -1
  29. data/lib/stripe/resources/balance.rb +1 -0
  30. data/lib/stripe/resources/balance_transaction.rb +1 -0
  31. data/lib/stripe/resources/bank_account.rb +2 -1
  32. data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
  33. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  34. data/lib/stripe/resources/bitcoin_receiver.rb +1 -0
  35. data/lib/stripe/resources/bitcoin_transaction.rb +3 -2
  36. data/lib/stripe/resources/capability.rb +2 -1
  37. data/lib/stripe/resources/card.rb +1 -0
  38. data/lib/stripe/resources/cash_balance.rb +22 -0
  39. data/lib/stripe/resources/charge.rb +10 -0
  40. data/lib/stripe/resources/checkout/session.rb +16 -0
  41. data/lib/stripe/resources/country_spec.rb +1 -0
  42. data/lib/stripe/resources/coupon.rb +1 -0
  43. data/lib/stripe/resources/credit_note.rb +11 -0
  44. data/lib/stripe/resources/credit_note_line_item.rb +8 -0
  45. data/lib/stripe/resources/customer.rb +41 -2
  46. data/lib/stripe/resources/customer_balance_transaction.rb +3 -2
  47. data/lib/stripe/resources/discount.rb +1 -0
  48. data/lib/stripe/resources/dispute.rb +1 -0
  49. data/lib/stripe/resources/ephemeral_key.rb +1 -0
  50. data/lib/stripe/resources/event.rb +1 -0
  51. data/lib/stripe/resources/exchange_rate.rb +1 -0
  52. data/lib/stripe/resources/file.rb +3 -1
  53. data/lib/stripe/resources/file_link.rb +1 -0
  54. data/lib/stripe/resources/financial_connections/account.rb +31 -0
  55. data/lib/stripe/resources/financial_connections/account_owner.rb +10 -0
  56. data/lib/stripe/resources/financial_connections/account_ownership.rb +10 -0
  57. data/lib/stripe/resources/financial_connections/session.rb +12 -0
  58. data/lib/stripe/resources/funding_instructions.rb +16 -0
  59. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  60. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  61. data/lib/stripe/resources/invoice.rb +12 -2
  62. data/lib/stripe/resources/invoice_item.rb +1 -0
  63. data/lib/stripe/resources/invoice_line_item.rb +1 -0
  64. data/lib/stripe/resources/issuing/authorization.rb +1 -0
  65. data/lib/stripe/resources/issuing/card.rb +1 -0
  66. data/lib/stripe/resources/issuing/card_details.rb +2 -1
  67. data/lib/stripe/resources/issuing/cardholder.rb +1 -0
  68. data/lib/stripe/resources/issuing/dispute.rb +12 -0
  69. data/lib/stripe/resources/issuing/transaction.rb +1 -0
  70. data/lib/stripe/resources/line_item.rb +8 -0
  71. data/lib/stripe/resources/login_link.rb +1 -0
  72. data/lib/stripe/resources/mandate.rb +8 -0
  73. data/lib/stripe/resources/order.rb +1 -0
  74. data/lib/stripe/resources/order_return.rb +1 -0
  75. data/lib/stripe/resources/payment_intent.rb +40 -0
  76. data/lib/stripe/resources/payment_link.rb +23 -0
  77. data/lib/stripe/resources/payment_method.rb +1 -0
  78. data/lib/stripe/resources/payout.rb +11 -0
  79. data/lib/stripe/resources/person.rb +1 -0
  80. data/lib/stripe/resources/plan.rb +1 -0
  81. data/lib/stripe/resources/price.rb +21 -0
  82. data/lib/stripe/resources/product.rb +10 -0
  83. data/lib/stripe/resources/promotion_code.rb +12 -0
  84. data/lib/stripe/resources/quote.rb +105 -0
  85. data/lib/stripe/resources/radar/early_fraud_warning.rb +1 -0
  86. data/lib/stripe/resources/radar/value_list.rb +1 -0
  87. data/lib/stripe/resources/radar/value_list_item.rb +1 -0
  88. data/lib/stripe/resources/recipient.rb +1 -0
  89. data/lib/stripe/resources/refund.rb +31 -0
  90. data/lib/stripe/resources/reporting/report_run.rb +1 -0
  91. data/lib/stripe/resources/reporting/report_type.rb +1 -0
  92. data/lib/stripe/resources/reversal.rb +3 -2
  93. data/lib/stripe/resources/review.rb +1 -0
  94. data/lib/stripe/resources/setup_attempt.rb +10 -0
  95. data/lib/stripe/resources/setup_intent.rb +11 -0
  96. data/lib/stripe/resources/shipping_rate.rb +12 -0
  97. data/lib/stripe/resources/sigma/scheduled_query_run.rb +1 -0
  98. data/lib/stripe/resources/sku.rb +1 -0
  99. data/lib/stripe/resources/source.rb +10 -3
  100. data/lib/stripe/resources/source_transaction.rb +1 -0
  101. data/lib/stripe/resources/subscription.rb +10 -0
  102. data/lib/stripe/resources/subscription_item.rb +7 -1
  103. data/lib/stripe/resources/subscription_schedule.rb +1 -0
  104. data/lib/stripe/resources/tax_code.rb +10 -0
  105. data/lib/stripe/resources/tax_id.rb +1 -0
  106. data/lib/stripe/resources/tax_rate.rb +1 -0
  107. data/lib/stripe/resources/terminal/configuration.rb +15 -0
  108. data/lib/stripe/resources/terminal/connection_token.rb +1 -0
  109. data/lib/stripe/resources/terminal/location.rb +1 -0
  110. data/lib/stripe/resources/terminal/reader.rb +61 -0
  111. data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
  112. data/lib/stripe/resources/three_d_secure.rb +1 -0
  113. data/lib/stripe/resources/token.rb +1 -0
  114. data/lib/stripe/resources/topup.rb +1 -0
  115. data/lib/stripe/resources/transfer.rb +1 -0
  116. data/lib/stripe/resources/usage_record.rb +1 -0
  117. data/lib/stripe/resources/usage_record_summary.rb +1 -0
  118. data/lib/stripe/resources/webhook_endpoint.rb +1 -0
  119. data/lib/stripe/resources.rb +23 -0
  120. data/lib/stripe/search_result_object.rb +86 -0
  121. data/lib/stripe/stripe_client.rb +412 -146
  122. data/lib/stripe/stripe_configuration.rb +194 -0
  123. data/lib/stripe/stripe_object.rb +26 -2
  124. data/lib/stripe/stripe_response.rb +80 -52
  125. data/lib/stripe/util.rb +74 -7
  126. data/lib/stripe/version.rb +1 -1
  127. data/lib/stripe/webhook.rb +38 -7
  128. data/lib/stripe.rb +39 -168
  129. data/stripe.gemspec +12 -5
  130. metadata +36 -186
  131. data/.editorconfig +0 -10
  132. data/.gitattributes +0 -4
  133. data/.github/ISSUE_TEMPLATE.md +0 -5
  134. data/.gitignore +0 -8
  135. data/.rubocop.yml +0 -56
  136. data/.rubocop_todo.yml +0 -39
  137. data/.travis.yml +0 -39
  138. data/.vscode/extensions.json +0 -7
  139. data/.vscode/settings.json +0 -8
  140. data/test/api_stub_helpers.rb +0 -1
  141. data/test/openapi/README.md +0 -9
  142. data/test/stripe/account_link_test.rb +0 -18
  143. data/test/stripe/account_test.rb +0 -412
  144. data/test/stripe/alipay_account_test.rb +0 -37
  145. data/test/stripe/api_operations_test.rb +0 -80
  146. data/test/stripe/api_resource_test.rb +0 -613
  147. data/test/stripe/apple_pay_domain_test.rb +0 -46
  148. data/test/stripe/application_fee_refund_test.rb +0 -37
  149. data/test/stripe/application_fee_test.rb +0 -58
  150. data/test/stripe/balance_test.rb +0 -13
  151. data/test/stripe/balance_transaction_test.rb +0 -20
  152. data/test/stripe/bank_account_test.rb +0 -36
  153. data/test/stripe/capability_test.rb +0 -45
  154. data/test/stripe/charge_test.rb +0 -64
  155. data/test/stripe/checkout/session_test.rb +0 -41
  156. data/test/stripe/connection_manager_test.rb +0 -138
  157. data/test/stripe/country_spec_test.rb +0 -20
  158. data/test/stripe/coupon_test.rb +0 -61
  159. data/test/stripe/credit_note_test.rb +0 -61
  160. data/test/stripe/customer_balance_transaction_test.rb +0 -37
  161. data/test/stripe/customer_card_test.rb +0 -42
  162. data/test/stripe/customer_test.rb +0 -226
  163. data/test/stripe/dispute_test.rb +0 -51
  164. data/test/stripe/ephemeral_key_test.rb +0 -93
  165. data/test/stripe/errors_test.rb +0 -41
  166. data/test/stripe/exchange_rate_test.rb +0 -20
  167. data/test/stripe/file_link_test.rb +0 -41
  168. data/test/stripe/file_test.rb +0 -87
  169. data/test/stripe/invoice_item_test.rb +0 -66
  170. data/test/stripe/invoice_line_item_test.rb +0 -8
  171. data/test/stripe/invoice_test.rb +0 -229
  172. data/test/stripe/issuing/authorization_test.rb +0 -72
  173. data/test/stripe/issuing/card_test.rb +0 -62
  174. data/test/stripe/issuing/cardholder_test.rb +0 -53
  175. data/test/stripe/issuing/dispute_test.rb +0 -45
  176. data/test/stripe/issuing/transaction_test.rb +0 -48
  177. data/test/stripe/list_object_test.rb +0 -140
  178. data/test/stripe/login_link_test.rb +0 -37
  179. data/test/stripe/multipart_encoder_test.rb +0 -130
  180. data/test/stripe/oauth_test.rb +0 -88
  181. data/test/stripe/order_return_test.rb +0 -21
  182. data/test/stripe/order_test.rb +0 -82
  183. data/test/stripe/payment_intent_test.rb +0 -107
  184. data/test/stripe/payment_method_test.rb +0 -84
  185. data/test/stripe/payout_test.rb +0 -57
  186. data/test/stripe/person_test.rb +0 -46
  187. data/test/stripe/plan_test.rb +0 -98
  188. data/test/stripe/product_test.rb +0 -59
  189. data/test/stripe/radar/early_fraud_warning_test.rb +0 -22
  190. data/test/stripe/radar/value_list_item_test.rb +0 -48
  191. data/test/stripe/radar/value_list_test.rb +0 -61
  192. data/test/stripe/recipient_test.rb +0 -62
  193. data/test/stripe/refund_test.rb +0 -39
  194. data/test/stripe/reporting/report_run_test.rb +0 -33
  195. data/test/stripe/reporting/report_type_test.rb +0 -22
  196. data/test/stripe/reversal_test.rb +0 -43
  197. data/test/stripe/review_test.rb +0 -27
  198. data/test/stripe/setup_intent_test.rb +0 -84
  199. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  200. data/test/stripe/sku_test.rb +0 -60
  201. data/test/stripe/source_test.rb +0 -81
  202. data/test/stripe/source_transaction_test.rb +0 -19
  203. data/test/stripe/stripe_client_test.rb +0 -1039
  204. data/test/stripe/stripe_object_test.rb +0 -497
  205. data/test/stripe/stripe_response_test.rb +0 -95
  206. data/test/stripe/subscription_item_test.rb +0 -75
  207. data/test/stripe/subscription_schedule_test.rb +0 -82
  208. data/test/stripe/subscription_test.rb +0 -80
  209. data/test/stripe/tax_id_test.rb +0 -31
  210. data/test/stripe/tax_rate_test.rb +0 -43
  211. data/test/stripe/terminal/connection_token_test.rb +0 -16
  212. data/test/stripe/terminal/location_test.rb +0 -68
  213. data/test/stripe/terminal/reader_test.rb +0 -62
  214. data/test/stripe/three_d_secure_test.rb +0 -23
  215. data/test/stripe/topup_test.rb +0 -62
  216. data/test/stripe/transfer_test.rb +0 -88
  217. data/test/stripe/usage_record_summary_test.rb +0 -19
  218. data/test/stripe/util_test.rb +0 -402
  219. data/test/stripe/webhook_endpoint_test.rb +0 -59
  220. data/test/stripe/webhook_test.rb +0 -96
  221. data/test/stripe_mock.rb +0 -78
  222. data/test/stripe_test.rb +0 -50
  223. data/test/test_data.rb +0 -61
  224. data/test/test_helper.rb +0 -76
@@ -1,497 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../test_helper", __dir__)
4
-
5
- module Stripe
6
- class StripeObjectTest < Test::Unit::TestCase
7
- should "implement #==" do
8
- obj1 = Stripe::StripeObject.construct_from(id: 1, foo: "bar")
9
- obj2 = Stripe::StripeObject.construct_from(id: 1, foo: "bar")
10
- obj3 = Stripe::StripeObject.construct_from(id: 1, foo: "rab")
11
-
12
- assert obj1 == obj2
13
- refute obj1 == obj3
14
- end
15
-
16
- should "implement #deleted?" do
17
- obj = Stripe::StripeObject.construct_from({})
18
- refute obj.deleted?
19
-
20
- obj = Stripe::StripeObject.construct_from(deleted: false)
21
- refute obj.deleted?
22
-
23
- obj = Stripe::StripeObject.construct_from(deleted: true)
24
- assert obj.deleted?
25
- end
26
-
27
- should "implement #respond_to" do
28
- obj = Stripe::StripeObject.construct_from(id: 1, foo: "bar")
29
- assert obj.respond_to?(:id)
30
- assert obj.respond_to?(:foo)
31
- assert !obj.respond_to?(:baz)
32
- end
33
-
34
- should "marshal be insensitive to strings vs. symbols when constructin" do
35
- obj = Stripe::StripeObject.construct_from(:id => 1, "name" => "Stripe")
36
- assert_equal 1, obj[:id]
37
- assert_equal "Stripe", obj[:name]
38
- end
39
-
40
- context "#deep_copy" do
41
- should "produce a deep copy" do
42
- opts = {
43
- api_base: Stripe.api_base,
44
- api_key: "apikey",
45
- }
46
- values = {
47
- id: 1,
48
- name: "Stripe",
49
- arr: [
50
- StripeObject.construct_from({ id: "index0" }, opts),
51
- "index1",
52
- 2,
53
- ],
54
- map: {
55
- "0": StripeObject.construct_from({ id: "index0" }, opts),
56
- "1": "index1",
57
- "2": 2,
58
- },
59
- }
60
-
61
- # it's not good to test methods with `#send` like this, but I've done
62
- # it in the interest of trying to keep `.deep_copy` as internal as
63
- # possible
64
- copy_values = Stripe::StripeObject.send(:deep_copy, values)
65
-
66
- # we can't compare the hashes directly because they have embedded
67
- # objects which are different from each other
68
- assert_equal values[:id], copy_values[:id]
69
- assert_equal values[:name], copy_values[:name]
70
-
71
- assert_equal values[:arr].length, copy_values[:arr].length
72
-
73
- # internal values of the copied StripeObject should be the same
74
- # (including opts), but the object itself should be new (hence the
75
- # refutation of equality on #object_id)
76
- assert_equal values[:arr][0][:id], copy_values[:arr][0][:id]
77
- refute_equal values[:arr][0].object_id, copy_values[:arr][0].object_id
78
- assert_equal values[:arr][0].instance_variable_get(:@opts),
79
- copy_values[:arr][0].instance_variable_get(:@opts)
80
-
81
- # scalars however, can be compared
82
- assert_equal values[:arr][1], copy_values[:arr][1]
83
- assert_equal values[:arr][2], copy_values[:arr][2]
84
-
85
- # and a similar story with the hash
86
- assert_equal values[:map].keys, copy_values[:map].keys
87
- assert_equal values[:map][:"0"][:id], copy_values[:map][:"0"][:id]
88
- refute_equal values[:map][:"0"].object_id, copy_values[:map][:"0"].object_id
89
- assert_equal values[:map][:"0"].instance_variable_get(:@opts),
90
- copy_values[:map][:"0"].instance_variable_get(:@opts)
91
- assert_equal values[:map][:"1"], copy_values[:map][:"1"]
92
- assert_equal values[:map][:"2"], copy_values[:map][:"2"]
93
- end
94
-
95
- should "not copy a client" do
96
- opts = {
97
- api_key: "apikey",
98
- client: StripeClient.active_client,
99
- }
100
- values = { id: 1, name: "Stripe" }
101
-
102
- obj = Stripe::StripeObject.construct_from(values, opts)
103
- copy_obj = Stripe::StripeObject.send(:deep_copy, obj)
104
-
105
- assert_equal values, copy_obj.instance_variable_get(:@values)
106
- assert_equal opts.reject { |k, _v| k == :client },
107
- copy_obj.instance_variable_get(:@opts)
108
- end
109
-
110
- should "return an instance of the same class" do
111
- class TestObject < Stripe::StripeObject; end
112
-
113
- obj = TestObject.construct_from(id: 1)
114
- copy_obj = obj.class.send(:deep_copy, obj)
115
-
116
- assert_equal obj.class, copy_obj.class
117
- end
118
- end
119
-
120
- context "#eql?" do
121
- should "produce true for two equivalent Stripe objects" do
122
- obj1 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
123
- obj2 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
124
- assert obj1.eql?(obj2)
125
- end
126
-
127
- should "produce false for non-equivalent Stripe objects" do
128
- obj1 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
129
- obj2 = Stripe::StripeObject.construct_from(id: 2, name: "Stripe")
130
- refute obj1.eql?(obj2)
131
- end
132
-
133
- should "produce false for different types" do
134
- obj1 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
135
- obj2 = 7
136
- refute obj1.eql?(obj2)
137
- end
138
- end
139
-
140
- context "#hash" do
141
- should "produce the same hash for two equivalent Stripe objects" do
142
- obj1 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
143
- obj2 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
144
- assert_equal obj1.hash, obj2.hash
145
- end
146
-
147
- should "produce different hashes for non-equivalent Stripe objects" do
148
- obj1 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
149
- obj2 = Stripe::StripeObject.construct_from(id: 2, name: "Stripe")
150
- refute_equal obj1.hash, obj2.hash
151
- end
152
-
153
- should "produce different hashes for different types" do
154
- obj1 = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
155
- obj2 = 7
156
- refute_equal obj1.hash, obj2.hash
157
- end
158
- end
159
-
160
- context "#to_hash" do
161
- should "skip calling to_hash on nil" do
162
- begin
163
- module NilWithToHash
164
- def to_hash
165
- raise "Can't call to_hash on nil"
166
- end
167
- end
168
- ::NilClass.include NilWithToHash
169
-
170
- hash_with_nil = { id: 3, foo: nil }
171
- obj = StripeObject.construct_from(hash_with_nil)
172
- expected_hash = { id: 3, foo: nil }
173
- assert_equal expected_hash, obj.to_hash
174
- ensure
175
- ::NilClass.send(:undef_method, :to_hash)
176
- end
177
- end
178
-
179
- should "recursively call to_hash on its values" do
180
- # deep nested hash (when contained in an array) or StripeObject
181
- nested_hash = { id: 7, foo: "bar" }
182
- nested = Stripe::StripeObject.construct_from(nested_hash)
183
-
184
- obj = Stripe::StripeObject.construct_from(id: 1,
185
- # simple hash that contains a StripeObject to help us test deep
186
- # recursion
187
- nested: { object: "list", data: [nested] },
188
- list: [nested])
189
-
190
- expected_hash = {
191
- id: 1,
192
- nested: { object: "list", data: [nested_hash] },
193
- list: [nested_hash],
194
- }
195
- assert_equal expected_hash, obj.to_hash
196
- end
197
- end
198
-
199
- should "assign question mark accessors for booleans" do
200
- obj = Stripe::StripeObject.construct_from(id: 1, bool: true, not_bool: "bar")
201
- assert obj.respond_to?(:bool?)
202
- assert obj.bool?
203
- refute obj.respond_to?(:not_bool?)
204
- end
205
-
206
- should "assign question mark accessors for booleans added after initialization" do
207
- obj = Stripe::StripeObject.new
208
- obj.bool = true
209
- assert obj.respond_to?(:bool?)
210
- assert obj.bool?
211
- end
212
-
213
- should "mass assign values with #update_attributes" do
214
- obj = Stripe::StripeObject.construct_from(id: 1, name: "Stripe")
215
- obj.update_attributes(name: "STRIPE")
216
- assert_equal "STRIPE", obj.name
217
-
218
- # unfortunately, we even assign unknown properties to duplicate the
219
- # behavior that we currently have via magic accessors with
220
- # method_missing
221
- obj.update_attributes(unknown: "foo")
222
- assert_equal "foo", obj.unknown
223
- end
224
-
225
- should "#update_attributes with a hash" do
226
- obj = Stripe::StripeObject.construct_from({})
227
- obj.update_attributes(metadata: { foo: "bar" })
228
- assert_equal Stripe::StripeObject, obj.metadata.class
229
- end
230
-
231
- should "create accessors when #update_attributes is called" do
232
- obj = Stripe::StripeObject.construct_from({})
233
- assert_equal false, obj.send(:metaclass).method_defined?(:foo)
234
- obj.update_attributes(foo: "bar")
235
- assert_equal true, obj.send(:metaclass).method_defined?(:foo)
236
- end
237
-
238
- should "pass opts down to children when initializing" do
239
- opts = { custom: "opts" }
240
-
241
- # customer comes with a `sources` list that makes a convenient object to
242
- # perform tests on
243
- obj = Stripe::StripeObject.construct_from({
244
- sources: [
245
- {},
246
- ],
247
- }, opts)
248
-
249
- source = obj.sources.first
250
- # Pulling `@opts` as an instance variable here is not ideal, but it's
251
- # important enough argument that the test here is worth it. we should
252
- # consider exposing it publicly on a future pull (and possibly renaming
253
- # it to something more useful).
254
- assert_equal opts, source.instance_variable_get(:@opts)
255
- end
256
-
257
- should "#serialize_params on an empty object" do
258
- obj = Stripe::StripeObject.construct_from({})
259
- assert_equal({}, obj.serialize_params)
260
- end
261
-
262
- should "#serialize_params on a new object with a subobject" do
263
- obj = Stripe::StripeObject.new
264
- obj.metadata = { foo: "bar" }
265
- assert_equal({ metadata: { foo: "bar" } },
266
- obj.serialize_params)
267
- end
268
-
269
- should "#serialize_params on a basic object" do
270
- obj = Stripe::StripeObject.construct_from(foo: nil)
271
- obj.update_attributes(foo: "bar")
272
- assert_equal({ foo: "bar" }, obj.serialize_params)
273
- end
274
-
275
- should "#serialize_params on a more complex object" do
276
- obj = Stripe::StripeObject.construct_from(foo: Stripe::StripeObject.construct_from(bar: nil,
277
- baz: nil))
278
- obj.foo.bar = "newbar"
279
- assert_equal({ foo: { bar: "newbar" } },
280
- obj.serialize_params)
281
- end
282
-
283
- should "#serialize_params on an array" do
284
- obj = Stripe::StripeObject.construct_from(foo: nil)
285
- obj.foo = ["new-value"]
286
- assert_equal({ foo: ["new-value"] },
287
- obj.serialize_params)
288
- end
289
-
290
- should "#serialize_params on an array that shortens" do
291
- obj = Stripe::StripeObject.construct_from(foo: %w[0-index 1-index 2-index])
292
- obj.foo = ["new-value"]
293
- assert_equal({ foo: ["new-value"] },
294
- obj.serialize_params)
295
- end
296
-
297
- should "#serialize_params on an array that lengthens" do
298
- obj = Stripe::StripeObject.construct_from(foo: %w[0-index 1-index 2-index])
299
- obj.foo = ["new-value"] * 4
300
- assert_equal({ foo: ["new-value"] * 4 },
301
- obj.serialize_params)
302
- end
303
-
304
- should "#serialize_params on an array of hashes" do
305
- obj = Stripe::StripeObject.construct_from(foo: nil)
306
- obj.foo = [
307
- Stripe::StripeObject.construct_from(bar: nil),
308
- ]
309
- obj.foo[0].bar = "baz"
310
- assert_equal({ foo: [{ bar: "baz" }] },
311
- obj.serialize_params)
312
- end
313
-
314
- should "#serialize_params doesn't include unchanged values" do
315
- obj = Stripe::StripeObject.construct_from(foo: nil)
316
- assert_equal({}, obj.serialize_params)
317
- end
318
-
319
- should "#serialize_params on an array that is unchanged" do
320
- obj = Stripe::StripeObject.construct_from(foo: %w[0-index 1-index 2-index])
321
- obj.foo = %w[0-index 1-index 2-index]
322
- assert_equal({}, obj.serialize_params)
323
- end
324
-
325
- should "#serialize_params with a StripeObject" do
326
- obj = Stripe::StripeObject.construct_from({})
327
-
328
- # using an #update_attributes will end up converting a Hash into a
329
- # StripeObject
330
- obj.metadata =
331
- Stripe::StripeObject.construct_from(foo: "bar")
332
-
333
- serialized = obj.serialize_params
334
- assert_equal({ foo: "bar" }, serialized[:metadata])
335
- end
336
-
337
- should "#serialize_params with StripeObject that's been replaced" do
338
- obj = Stripe::StripeObject.construct_from(source: Stripe::StripeObject.construct_from(bar: "foo"))
339
-
340
- # Here we replace the object wholesale.
341
- obj.source =
342
- Stripe::StripeObject.construct_from(baz: "foo")
343
-
344
- serialized = obj.serialize_params
345
- assert_equal({ baz: "foo" }, serialized[:source])
346
- end
347
-
348
- should "#serialize_params with StripeObject that's been replaced which is `metadata`" do
349
- class WithAdditiveObjectParam < Stripe::StripeObject
350
- additive_object_param :metadata
351
- end
352
-
353
- obj = WithAdditiveObjectParam.construct_from(metadata: Stripe::StripeObject.construct_from(bar: "foo"))
354
-
355
- # Here we replace the object wholesale. Because it's `metadata`, the
356
- # client must be able to blank out the values that were in the old
357
- # object, but which are no longer present in the new one.
358
- obj.metadata =
359
- Stripe::StripeObject.construct_from(baz: "foo")
360
-
361
- serialized = obj.serialize_params
362
- assert_equal({ bar: "", baz: "foo" }, serialized[:metadata])
363
- end
364
-
365
- should "#serialize_params with an array of StripeObjects" do
366
- obj = Stripe::StripeObject.construct_from({})
367
- obj.metadata = [
368
- Stripe::StripeObject.construct_from(foo: "bar"),
369
- ]
370
-
371
- serialized = obj.serialize_params
372
- assert_equal([{ foo: "bar" }], serialized[:metadata])
373
- end
374
-
375
- should "#serialize_params and embed an API resource that's been set and has an ID" do
376
- customer = Customer.construct_from(id: "cus_123")
377
- obj = Stripe::StripeObject.construct_from({})
378
-
379
- # the key here is that the property is set explicitly (and therefore
380
- # marked as unsaved), which is why it gets included below
381
- obj.customer = customer
382
-
383
- serialized = obj.serialize_params
384
- assert_equal({ customer: customer }, serialized)
385
- end
386
-
387
- should "#serialize_params and not include API resources that have not been set" do
388
- customer = Customer.construct_from(id: "cus_123")
389
- obj = Stripe::StripeObject.construct_from(customer: customer)
390
-
391
- serialized = obj.serialize_params
392
- assert_equal({}, serialized)
393
- end
394
-
395
- should "#serialize_params serializes API resources flagged with save_with_parent" do
396
- c = Customer.construct_from({})
397
- c.save_with_parent = true
398
-
399
- obj = Stripe::StripeObject.construct_from(customer: c)
400
-
401
- serialized = obj.serialize_params
402
- assert_equal({ customer: {} }, serialized)
403
- end
404
-
405
- should "#serialize_params should raise an error on other embedded API resources" do
406
- # This customer doesn't have an ID and therefore the library doesn't know
407
- # what to do with it and throws an ArgumentError because it's probably
408
- # not what the user expected to happen.
409
- customer = Customer.construct_from({})
410
-
411
- obj = Stripe::StripeObject.construct_from({})
412
- obj.customer = customer
413
-
414
- e = assert_raises(ArgumentError) do
415
- obj.serialize_params
416
- end
417
- assert_equal "Cannot save property `customer` containing " \
418
- "an API resource. It doesn't appear to be persisted and is " \
419
- "not marked as `save_with_parent`.", e.message
420
- end
421
-
422
- should "#serialize_params takes a force option" do
423
- obj = Stripe::StripeObject.construct_from(id: "id",
424
- metadata: Stripe::StripeObject.construct_from(foo: "bar"))
425
-
426
- serialized = obj.serialize_params(force: true)
427
- assert_equal({ id: "id", metadata: { foo: "bar" } }, serialized)
428
- end
429
-
430
- should "#dirty! forces an object and its subobjects to be saved" do
431
- obj = Stripe::StripeObject.construct_from(id: "id",
432
- metadata: Stripe::StripeObject.construct_from(foo: "bar"))
433
-
434
- # note that `force` and `dirty!` are for different things, but are
435
- # functionally equivalent
436
- obj.dirty!
437
-
438
- serialized = obj.serialize_params
439
- assert_equal({ id: "id", metadata: { foo: "bar" } }, serialized)
440
- end
441
-
442
- should "#to_s will call to_s for all embedded stripe objects" do
443
- obj = Stripe::StripeObject.construct_from(id: "id",
444
- # embedded list object
445
- refunds: Stripe::ListObject.construct_from(data: [
446
- # embedded object in list
447
- Stripe::StripeObject.construct_from(id: "id",
448
- # embedded object in an object in a list object
449
- metadata: Stripe::StripeObject.construct_from(foo: "bar")),
450
- ]),
451
- # embedded stripe object
452
- metadata: Stripe::StripeObject.construct_from(foo: "bar"))
453
- expected = JSON.pretty_generate(id: "id",
454
- refunds: {
455
- data: [
456
- { id: "id", metadata: { foo: "bar" } },
457
- ],
458
- },
459
- metadata: { foo: "bar" })
460
-
461
- assert_equal(expected, obj.to_s)
462
- end
463
-
464
- should "error on setting a property to an empty string" do
465
- obj = Stripe::StripeObject.construct_from(foo: "bar")
466
- e = assert_raises ArgumentError do
467
- obj.foo = ""
468
- end
469
- assert_match(/\(object\).foo = nil/, e.message)
470
- end
471
-
472
- should "marshal and unmarshal using custom encoder and decoder" do
473
- obj = Stripe::StripeObject.construct_from(
474
- { id: 1, name: "Stripe" },
475
- api_key: "apikey",
476
- client: StripeClient.active_client
477
- )
478
- m = Marshal.load(Marshal.dump(obj))
479
- assert_equal 1, m.id
480
- assert_equal "Stripe", m.name
481
- expected_hash = { api_key: "apikey" }
482
- assert_equal expected_hash, m.instance_variable_get("@opts")
483
- end
484
-
485
- context "#method" do
486
- should "act as a getter if no arguments are provided" do
487
- obj = Stripe::StripeObject.construct_from(id: 1, method: "foo")
488
- assert_equal "foo", obj.method
489
- end
490
-
491
- should "call Object#method if an argument is provided" do
492
- obj = Stripe::StripeObject.construct_from(id: 1, method: "foo")
493
- assert obj.method(:id).is_a?(Method)
494
- end
495
- end
496
- end
497
- end
@@ -1,95 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../test_helper", __dir__)
4
-
5
- module Stripe
6
- class StripeResponseTest < Test::Unit::TestCase
7
- context "Headers" do
8
- should "allow case-insensitive header access" do
9
- headers = { "Request-Id" => "request-id" }
10
- http_resp = create_net_http_resp(200, "", headers)
11
-
12
- headers = StripeResponse::Headers.from_net_http(http_resp)
13
-
14
- assert_equal "request-id", headers["request-id"]
15
- assert_equal "request-id", headers["Request-Id"]
16
- assert_equal "request-id", headers["Request-ID"]
17
- end
18
-
19
- should "initialize without error" do
20
- StripeResponse::Headers.new({})
21
- StripeResponse::Headers.new("Request-Id" => [])
22
- StripeResponse::Headers.new("Request-Id" => ["request-id"])
23
- end
24
-
25
- should "initialize with error on a malformed hash" do
26
- assert_raises(ArgumentError) do
27
- StripeResponse::Headers.new(nil)
28
- end
29
-
30
- assert_raises(ArgumentError) do
31
- StripeResponse::Headers.new(1 => [])
32
- end
33
-
34
- assert_raises(ArgumentError) do
35
- StripeResponse::Headers.new("Request-Id" => 1)
36
- end
37
-
38
- assert_raises(ArgumentError) do
39
- StripeResponse::Headers.new("Request-Id" => [1])
40
- end
41
- end
42
-
43
- should "warn on duplicate header values" do
44
- old_stderr = $stderr
45
- $stderr = StringIO.new
46
- begin
47
- headers = StripeResponse::Headers.new("Duplicated" => %w[a b])
48
- assert_equal "a", headers["Duplicated"]
49
- assert_equal "Duplicate header values for `Duplicated`; returning only first",
50
- $stderr.string.rstrip
51
- ensure
52
- $stderr = old_stderr
53
- end
54
- end
55
- end
56
-
57
- context ".from_net_http" do
58
- should "converts to StripeResponse" do
59
- code = 200
60
- body = '{"foo": "bar"}'
61
- headers = { "Request-Id" => "request-id" }
62
- http_resp = create_net_http_resp(code, body, headers)
63
-
64
- resp = StripeResponse.from_net_http(http_resp)
65
-
66
- assert_equal JSON.parse(body, symbolize_names: true), resp.data
67
- assert_equal body, resp.http_body
68
- assert_equal "request-id", resp.http_headers["Request-ID"]
69
- assert_equal code, resp.http_status
70
- assert_equal "request-id", resp.request_id
71
- end
72
- end
73
-
74
- # Synthesizes a `Net::HTTPResponse` object for testing purposes.
75
- private def create_net_http_resp(code, body, headers)
76
- # The "1.1" is HTTP version.
77
- http_resp = Net::HTTPResponse.new("1.1", code.to_s, nil)
78
- http_resp.body = body
79
-
80
- # This is obviously super sketchy, but the Ruby team has done everything
81
- # in their power to make these objects as difficult to test with as
82
- # possible. Even if you specify a body, accessing `#body` the first time
83
- # will attempt to read from a non-existent socket which will subsequently
84
- # blow up. Setting this internal variable skips that read and allows the
85
- # object to use the body that we specified above.
86
- http_resp.instance_variable_set(:@read, true)
87
-
88
- headers.each do |name, value|
89
- http_resp[name] = value
90
- end
91
-
92
- http_resp
93
- end
94
- end
95
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../test_helper", __dir__)
4
-
5
- module Stripe
6
- class SubscriptionItemTest < Test::Unit::TestCase
7
- should "be listable" do
8
- items = Stripe::SubscriptionItem.list(
9
- subscription: "sub_123"
10
- )
11
- assert_requested :get, "#{Stripe.api_base}/v1/subscription_items",
12
- query: { subscription: "sub_123" }
13
- assert items.data.is_a?(Array)
14
- assert items.data[0].is_a?(Stripe::SubscriptionItem)
15
- end
16
-
17
- should "be retrievable" do
18
- item = Stripe::SubscriptionItem.retrieve("si_123")
19
- assert_requested :get, "#{Stripe.api_base}/v1/subscription_items/si_123"
20
- assert item.is_a?(Stripe::SubscriptionItem)
21
- end
22
-
23
- should "be creatable" do
24
- item = Stripe::SubscriptionItem.create(
25
- plan: "sapphire-elite",
26
- quantity: 3,
27
- subscription: "sub_123"
28
- )
29
- assert_requested :post, "#{Stripe.api_base}/v1/subscription_items"
30
- assert item.is_a?(Stripe::SubscriptionItem)
31
- end
32
-
33
- should "be saveable" do
34
- item = Stripe::SubscriptionItem.retrieve("si_123")
35
- item.quantity = 4
36
- item.save
37
- assert_requested :post, "#{Stripe.api_base}/v1/subscription_items/#{item.id}"
38
- end
39
-
40
- should "be updateable" do
41
- item = Stripe::SubscriptionItem.update("si_123", metadata: { foo: "bar" })
42
- assert_requested :post, "#{Stripe.api_base}/v1/subscription_items/si_123"
43
- assert item.is_a?(Stripe::SubscriptionItem)
44
- end
45
-
46
- context "#delete" do
47
- should "be deletable" do
48
- item = Stripe::SubscriptionItem.retrieve("si_123")
49
- item = item.delete
50
- assert_requested :delete, "#{Stripe.api_base}/v1/subscription_items/#{item.id}"
51
- assert item.is_a?(Stripe::SubscriptionItem)
52
- end
53
- end
54
-
55
- context ".delete" do
56
- should "be deletable" do
57
- item = Stripe::SubscriptionItem.delete("si_123")
58
- assert_requested :delete, "#{Stripe.api_base}/v1/subscription_items/si_123"
59
- assert item.is_a?(Stripe::SubscriptionItem)
60
- end
61
- end
62
-
63
- context "#create_usage_record" do
64
- should "create a usage record" do
65
- Stripe::SubscriptionItem.create_usage_record(
66
- "si_123",
67
- quantity: 5000,
68
- timestamp: Time.now.to_i,
69
- action: "increment"
70
- )
71
- assert_requested :post, "#{Stripe.api_base}/v1/subscription_items/si_123/usage_records"
72
- end
73
- end
74
- end
75
- end