stripe 5.31.0 → 5.35.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/VERSION +1 -1
  4. data/lib/stripe/api_operations/request.rb +35 -2
  5. data/lib/stripe/api_resource.rb +8 -0
  6. data/lib/stripe/connection_manager.rb +17 -2
  7. data/lib/stripe/object_types.rb +3 -0
  8. data/lib/stripe/resources.rb +3 -0
  9. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  10. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  11. data/lib/stripe/resources/tax_code.rb +10 -0
  12. data/lib/stripe/stripe_client.rb +120 -57
  13. data/lib/stripe/stripe_response.rb +80 -52
  14. data/lib/stripe/util.rb +3 -1
  15. data/lib/stripe/version.rb +1 -1
  16. data/stripe.gemspec +9 -2
  17. metadata +9 -198
  18. data/.editorconfig +0 -10
  19. data/.gitattributes +0 -4
  20. data/.github/ISSUE_TEMPLATE.md +0 -5
  21. data/.github/workflows/ci.yml +0 -34
  22. data/.gitignore +0 -8
  23. data/.rubocop.yml +0 -81
  24. data/.rubocop_todo.yml +0 -33
  25. data/.travis.yml +0 -38
  26. data/.vscode/extensions.json +0 -7
  27. data/.vscode/settings.json +0 -8
  28. data/test/openapi/README.md +0 -9
  29. data/test/stripe/account_link_test.rb +0 -18
  30. data/test/stripe/account_test.rb +0 -412
  31. data/test/stripe/alipay_account_test.rb +0 -37
  32. data/test/stripe/api_operations_test.rb +0 -80
  33. data/test/stripe/api_resource_test.rb +0 -646
  34. data/test/stripe/apple_pay_domain_test.rb +0 -46
  35. data/test/stripe/application_fee_refund_test.rb +0 -37
  36. data/test/stripe/application_fee_test.rb +0 -58
  37. data/test/stripe/balance_test.rb +0 -13
  38. data/test/stripe/balance_transaction_test.rb +0 -20
  39. data/test/stripe/bank_account_test.rb +0 -36
  40. data/test/stripe/billing_portal/configuration_test.rb +0 -37
  41. data/test/stripe/billing_portal/session_test.rb +0 -18
  42. data/test/stripe/capability_test.rb +0 -45
  43. data/test/stripe/charge_test.rb +0 -64
  44. data/test/stripe/checkout/session_test.rb +0 -53
  45. data/test/stripe/connection_manager_test.rb +0 -210
  46. data/test/stripe/country_spec_test.rb +0 -20
  47. data/test/stripe/coupon_test.rb +0 -61
  48. data/test/stripe/credit_note_test.rb +0 -90
  49. data/test/stripe/customer_balance_transaction_test.rb +0 -37
  50. data/test/stripe/customer_card_test.rb +0 -48
  51. data/test/stripe/customer_test.rb +0 -226
  52. data/test/stripe/dispute_test.rb +0 -51
  53. data/test/stripe/ephemeral_key_test.rb +0 -93
  54. data/test/stripe/errors_test.rb +0 -53
  55. data/test/stripe/exchange_rate_test.rb +0 -20
  56. data/test/stripe/file_link_test.rb +0 -41
  57. data/test/stripe/file_test.rb +0 -87
  58. data/test/stripe/instrumentation_test.rb +0 -74
  59. data/test/stripe/invoice_item_test.rb +0 -66
  60. data/test/stripe/invoice_line_item_test.rb +0 -8
  61. data/test/stripe/invoice_test.rb +0 -229
  62. data/test/stripe/issuing/authorization_test.rb +0 -72
  63. data/test/stripe/issuing/card_test.rb +0 -74
  64. data/test/stripe/issuing/cardholder_test.rb +0 -53
  65. data/test/stripe/issuing/dispute_test.rb +0 -54
  66. data/test/stripe/issuing/transaction_test.rb +0 -48
  67. data/test/stripe/list_object_test.rb +0 -202
  68. data/test/stripe/login_link_test.rb +0 -37
  69. data/test/stripe/mandate_test.rb +0 -14
  70. data/test/stripe/multipart_encoder_test.rb +0 -130
  71. data/test/stripe/oauth_test.rb +0 -149
  72. data/test/stripe/order_return_test.rb +0 -21
  73. data/test/stripe/order_test.rb +0 -82
  74. data/test/stripe/payment_intent_test.rb +0 -107
  75. data/test/stripe/payment_method_test.rb +0 -84
  76. data/test/stripe/payout_test.rb +0 -72
  77. data/test/stripe/person_test.rb +0 -46
  78. data/test/stripe/plan_test.rb +0 -98
  79. data/test/stripe/price_test.rb +0 -48
  80. data/test/stripe/product_test.rb +0 -58
  81. data/test/stripe/promotion_code_test.rb +0 -42
  82. data/test/stripe/radar/early_fraud_warning_test.rb +0 -22
  83. data/test/stripe/radar/value_list_item_test.rb +0 -48
  84. data/test/stripe/radar/value_list_test.rb +0 -61
  85. data/test/stripe/recipient_test.rb +0 -62
  86. data/test/stripe/refund_test.rb +0 -39
  87. data/test/stripe/reporting/report_run_test.rb +0 -33
  88. data/test/stripe/reporting/report_type_test.rb +0 -22
  89. data/test/stripe/reversal_test.rb +0 -43
  90. data/test/stripe/review_test.rb +0 -27
  91. data/test/stripe/setup_attempt_test.rb +0 -16
  92. data/test/stripe/setup_intent_test.rb +0 -84
  93. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  94. data/test/stripe/sku_test.rb +0 -60
  95. data/test/stripe/source_test.rb +0 -119
  96. data/test/stripe/stripe_client_test.rb +0 -1456
  97. data/test/stripe/stripe_configuration_test.rb +0 -159
  98. data/test/stripe/stripe_object_test.rb +0 -510
  99. data/test/stripe/stripe_response_test.rb +0 -95
  100. data/test/stripe/subscription_item_test.rb +0 -84
  101. data/test/stripe/subscription_schedule_test.rb +0 -82
  102. data/test/stripe/subscription_test.rb +0 -80
  103. data/test/stripe/tax_id_test.rb +0 -31
  104. data/test/stripe/tax_rate_test.rb +0 -43
  105. data/test/stripe/terminal/connection_token_test.rb +0 -16
  106. data/test/stripe/terminal/location_test.rb +0 -68
  107. data/test/stripe/terminal/reader_test.rb +0 -62
  108. data/test/stripe/three_d_secure_test.rb +0 -23
  109. data/test/stripe/topup_test.rb +0 -62
  110. data/test/stripe/transfer_test.rb +0 -88
  111. data/test/stripe/usage_record_summary_test.rb +0 -29
  112. data/test/stripe/util_test.rb +0 -402
  113. data/test/stripe/webhook_endpoint_test.rb +0 -59
  114. data/test/stripe/webhook_test.rb +0 -135
  115. data/test/stripe_mock.rb +0 -78
  116. data/test/stripe_test.rb +0 -137
  117. data/test/test_data.rb +0 -61
  118. data/test/test_helper.rb +0 -74
@@ -1,159 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../test_helper", __dir__)
4
-
5
- module Stripe
6
- class StripeConfigurationTest < Test::Unit::TestCase
7
- context ".setup" do
8
- should "initialize a new configuration with defaults" do
9
- config = Stripe::StripeConfiguration.setup
10
-
11
- assert_equal Stripe::DEFAULT_CA_BUNDLE_PATH, config.ca_bundle_path
12
- assert_equal true, config.enable_telemetry
13
- assert_equal true, config.verify_ssl_certs
14
- assert_equal 2, config.max_network_retry_delay
15
- assert_equal 0.5, config.initial_network_retry_delay
16
- assert_equal 0, config.max_network_retries
17
- assert_equal 30, config.open_timeout
18
- assert_equal 80, config.read_timeout
19
- assert_equal 30, config.write_timeout
20
- assert_equal "https://api.stripe.com", config.api_base
21
- assert_equal "https://connect.stripe.com", config.connect_base
22
- assert_equal "https://files.stripe.com", config.uploads_base
23
- assert_equal nil, config.api_version
24
- end
25
-
26
- should "allow for overrides when a block is passed" do
27
- config = Stripe::StripeConfiguration.setup do |c|
28
- c.open_timeout = 100
29
- c.read_timeout = 100
30
- c.write_timeout = 100 if WRITE_TIMEOUT_SUPPORTED
31
- end
32
-
33
- assert_equal 100, config.open_timeout
34
- assert_equal 100, config.read_timeout
35
- assert_equal 100, config.write_timeout if WRITE_TIMEOUT_SUPPORTED
36
- end
37
- end
38
-
39
- context "#reverse_duplicate_merge" do
40
- should "return a duplicate object with overrides" do
41
- config = Stripe::StripeConfiguration.setup do |c|
42
- c.open_timeout = 100
43
- end
44
-
45
- duped_config = config.reverse_duplicate_merge(read_timeout: 500, api_version: "2018-08-02")
46
-
47
- assert_equal config.open_timeout, duped_config.open_timeout
48
- assert_equal 500, duped_config.read_timeout
49
- end
50
- end
51
-
52
- context "#max_network_retries=" do
53
- should "coerce the option into an integer" do
54
- config = Stripe::StripeConfiguration.setup
55
-
56
- config.max_network_retries = "10"
57
- assert_equal 10, config.max_network_retries
58
- end
59
- end
60
-
61
- context "#max_network_retry_delay=" do
62
- should "coerce the option into an integer" do
63
- config = Stripe::StripeConfiguration.setup
64
-
65
- config.max_network_retry_delay = "10"
66
- assert_equal 10, config.max_network_retry_delay
67
- end
68
- end
69
-
70
- context "#initial_network_retry_delay=" do
71
- should "coerce the option into an integer" do
72
- config = Stripe::StripeConfiguration.setup
73
-
74
- config.initial_network_retry_delay = "10"
75
- assert_equal 10, config.initial_network_retry_delay
76
- end
77
- end
78
-
79
- context "#log_level=" do
80
- should "be backwards compatible with old values" do
81
- config = Stripe::StripeConfiguration.setup
82
-
83
- config.log_level = "debug"
84
- assert_equal Stripe::LEVEL_DEBUG, config.log_level
85
-
86
- config.log_level = "info"
87
- assert_equal Stripe::LEVEL_INFO, config.log_level
88
- end
89
-
90
- should "raise an error if the value isn't valid" do
91
- config = Stripe::StripeConfiguration.setup
92
-
93
- assert_raises ArgumentError do
94
- config.log_level = "Foo"
95
- end
96
- end
97
- end
98
-
99
- context "options that require all connection managers to be cleared" do
100
- should "clear when setting allow ca_bundle_path" do
101
- config = Stripe::StripeConfiguration.setup
102
-
103
- StripeClient.expects(:clear_all_connection_managers).with(config: config)
104
- config.ca_bundle_path = "/path/to/ca/bundle"
105
- end
106
-
107
- should "clear when setting open timeout" do
108
- config = Stripe::StripeConfiguration.setup
109
-
110
- StripeClient.expects(:clear_all_connection_managers).with(config: config)
111
- config.open_timeout = 10
112
- end
113
-
114
- should "clear when setting read timeout" do
115
- config = Stripe::StripeConfiguration.setup
116
-
117
- StripeClient.expects(:clear_all_connection_managers).with(config: config)
118
- config.read_timeout = 10
119
- end
120
-
121
- should "clear when setting uploads_base" do
122
- config = Stripe::StripeConfiguration.setup
123
-
124
- StripeClient.expects(:clear_all_connection_managers).with(config: config)
125
- config.uploads_base = "https://other.stripe.com"
126
- end
127
-
128
- should "clear when setting api_base to be configured" do
129
- config = Stripe::StripeConfiguration.setup
130
-
131
- StripeClient.expects(:clear_all_connection_managers).with(config: config)
132
- config.api_base = "https://other.stripe.com"
133
- end
134
-
135
- should "clear when setting connect_base" do
136
- config = Stripe::StripeConfiguration.setup
137
-
138
- StripeClient.expects(:clear_all_connection_managers).with(config: config)
139
- config.connect_base = "https://other.stripe.com"
140
- end
141
-
142
- should "clear when setting verify_ssl_certs" do
143
- config = Stripe::StripeConfiguration.setup
144
-
145
- StripeClient.expects(:clear_all_connection_managers).with(config: config)
146
- config.verify_ssl_certs = false
147
- end
148
- end
149
-
150
- context "#key" do
151
- should "generate the same key when values are identicial" do
152
- assert_equal StripeConfiguration.setup.key, StripeConfiguration.setup.key
153
-
154
- custom_config = StripeConfiguration.setup { |c| c.open_timeout = 1000 }
155
- refute_equal StripeConfiguration.setup.key, custom_config.key
156
- end
157
- end
158
- end
159
- end
@@ -1,510 +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
-
216
- obj.update_attributes({ name: "STRIPE" })
217
-
218
- assert_equal "STRIPE", obj.name
219
-
220
- # unfortunately, we even assign unknown properties to duplicate the
221
- # behavior that we currently have via magic accessors with
222
- # method_missing
223
- obj.update_attributes({ unknown: "foo" })
224
-
225
- assert_equal "foo", obj.unknown
226
- end
227
-
228
- should "#update_attributes with a hash" do
229
- obj = Stripe::StripeObject.construct_from({})
230
- obj.update_attributes({ metadata: { foo: "bar" } })
231
- assert_equal Stripe::StripeObject, obj.metadata.class
232
- end
233
-
234
- should "create accessors when #update_attributes is called" do
235
- obj = Stripe::StripeObject.construct_from({})
236
- assert_equal false, obj.send(:metaclass).method_defined?(:foo)
237
- obj.update_attributes({ foo: "bar" })
238
- assert_equal true, obj.send(:metaclass).method_defined?(:foo)
239
- end
240
-
241
- should "pass opts down to children when initializing" do
242
- opts = { custom: "opts" }
243
-
244
- # customer comes with a `sources` list that makes a convenient object to
245
- # perform tests on
246
- obj = Stripe::StripeObject.construct_from({
247
- sources: [
248
- {},
249
- ],
250
- }, opts)
251
-
252
- source = obj.sources.first
253
- # Pulling `@opts` as an instance variable here is not ideal, but it's
254
- # important enough argument that the test here is worth it. we should
255
- # consider exposing it publicly on a future pull (and possibly renaming
256
- # it to something more useful).
257
- assert_equal opts, source.instance_variable_get(:@opts)
258
- end
259
-
260
- should "#serialize_params on an empty object" do
261
- obj = Stripe::StripeObject.construct_from({})
262
- assert_equal({}, obj.serialize_params)
263
- end
264
-
265
- should "#serialize_params on a new object with a subobject" do
266
- obj = Stripe::StripeObject.new
267
- obj.metadata = { foo: "bar" }
268
- assert_equal({ metadata: { foo: "bar" } },
269
- obj.serialize_params)
270
- end
271
-
272
- should "#serialize_params on a basic object" do
273
- obj = Stripe::StripeObject.construct_from(foo: nil)
274
- obj.update_attributes({ foo: "bar" })
275
- assert_equal({ foo: "bar" }, obj.serialize_params)
276
- end
277
-
278
- should "#serialize_params on a more complex object" do
279
- obj = Stripe::StripeObject.construct_from(foo: Stripe::StripeObject.construct_from(bar: nil,
280
- baz: nil))
281
- obj.foo.bar = "newbar"
282
- assert_equal({ foo: { bar: "newbar" } },
283
- obj.serialize_params)
284
- end
285
-
286
- should "#serialize_params on an array" do
287
- obj = Stripe::StripeObject.construct_from(foo: nil)
288
- obj.foo = ["new-value"]
289
- assert_equal({ foo: ["new-value"] },
290
- obj.serialize_params)
291
- end
292
-
293
- should "#serialize_params on an array that shortens" do
294
- obj = Stripe::StripeObject.construct_from(foo: %w[0-index 1-index 2-index])
295
- obj.foo = ["new-value"]
296
- assert_equal({ foo: ["new-value"] },
297
- obj.serialize_params)
298
- end
299
-
300
- should "#serialize_params on an array that lengthens" do
301
- obj = Stripe::StripeObject.construct_from(foo: %w[0-index 1-index 2-index])
302
- obj.foo = ["new-value"] * 4
303
- assert_equal({ foo: ["new-value"] * 4 },
304
- obj.serialize_params)
305
- end
306
-
307
- should "#serialize_params on an array of hashes" do
308
- obj = Stripe::StripeObject.construct_from(foo: nil)
309
- obj.foo = [
310
- Stripe::StripeObject.construct_from(bar: nil),
311
- ]
312
- obj.foo[0].bar = "baz"
313
- assert_equal({ foo: [{ bar: "baz" }] },
314
- obj.serialize_params)
315
- end
316
-
317
- should "#serialize_params doesn't include unchanged values" do
318
- obj = Stripe::StripeObject.construct_from(foo: nil)
319
- assert_equal({}, obj.serialize_params)
320
- end
321
-
322
- should "#serialize_params on an array that is unchanged" do
323
- obj = Stripe::StripeObject.construct_from(foo: %w[0-index 1-index 2-index])
324
- obj.foo = %w[0-index 1-index 2-index]
325
- assert_equal({}, obj.serialize_params)
326
- end
327
-
328
- should "#serialize_params with a StripeObject" do
329
- obj = Stripe::StripeObject.construct_from({})
330
-
331
- # using an #update_attributes will end up converting a Hash into a
332
- # StripeObject
333
- obj.metadata =
334
- Stripe::StripeObject.construct_from(foo: "bar")
335
-
336
- serialized = obj.serialize_params
337
- assert_equal({ foo: "bar" }, serialized[:metadata])
338
- end
339
-
340
- should "#serialize_params with StripeObject that's been replaced" do
341
- obj = Stripe::StripeObject.construct_from(source: Stripe::StripeObject.construct_from(bar: "foo"))
342
-
343
- # Here we replace the object wholesale.
344
- obj.source =
345
- Stripe::StripeObject.construct_from(baz: "foo")
346
-
347
- serialized = obj.serialize_params
348
- assert_equal({ baz: "foo" }, serialized[:source])
349
- end
350
-
351
- should "#serialize_params with StripeObject that's been replaced which is `metadata`" do
352
- class WithAdditiveObjectParam < Stripe::StripeObject
353
- additive_object_param :metadata
354
- end
355
-
356
- obj = WithAdditiveObjectParam.construct_from(metadata: Stripe::StripeObject.construct_from(bar: "foo"))
357
-
358
- # Here we replace the object wholesale. Because it's `metadata`, the
359
- # client must be able to blank out the values that were in the old
360
- # object, but which are no longer present in the new one.
361
- obj.metadata =
362
- Stripe::StripeObject.construct_from(baz: "foo")
363
-
364
- serialized = obj.serialize_params
365
- assert_equal({ bar: "", baz: "foo" }, serialized[:metadata])
366
- end
367
-
368
- should "#serialize_params with an array of StripeObjects" do
369
- obj = Stripe::StripeObject.construct_from({})
370
- obj.metadata = [
371
- Stripe::StripeObject.construct_from(foo: "bar"),
372
- ]
373
-
374
- serialized = obj.serialize_params
375
- assert_equal([{ foo: "bar" }], serialized[:metadata])
376
- end
377
-
378
- should "#serialize_params and embed an API resource that's been set and has an ID" do
379
- customer = Customer.construct_from(id: "cus_123")
380
- obj = Stripe::StripeObject.construct_from({})
381
-
382
- # the key here is that the property is set explicitly (and therefore
383
- # marked as unsaved), which is why it gets included below
384
- obj.customer = customer
385
-
386
- serialized = obj.serialize_params
387
- assert_equal({ customer: customer }, serialized)
388
- end
389
-
390
- should "#serialize_params and not include API resources that have not been set" do
391
- customer = Customer.construct_from(id: "cus_123")
392
- obj = Stripe::StripeObject.construct_from(customer: customer)
393
-
394
- serialized = obj.serialize_params
395
- assert_equal({}, serialized)
396
- end
397
-
398
- should "#serialize_params serializes API resources flagged with save_with_parent" do
399
- c = Customer.construct_from({})
400
- c.save_with_parent = true
401
-
402
- obj = Stripe::StripeObject.construct_from(customer: c)
403
-
404
- serialized = obj.serialize_params
405
- assert_equal({ customer: {} }, serialized)
406
- end
407
-
408
- should "#serialize_params should raise an error on other embedded API resources" do
409
- # This customer doesn't have an ID and therefore the library doesn't know
410
- # what to do with it and throws an ArgumentError because it's probably
411
- # not what the user expected to happen.
412
- customer = Customer.construct_from({})
413
-
414
- obj = Stripe::StripeObject.construct_from({})
415
- obj.customer = customer
416
-
417
- e = assert_raises(ArgumentError) do
418
- obj.serialize_params
419
- end
420
- assert_equal "Cannot save property `customer` containing " \
421
- "an API resource. It doesn't appear to be persisted and is " \
422
- "not marked as `save_with_parent`.", e.message
423
- end
424
-
425
- should "#serialize_params takes a force option" do
426
- obj = Stripe::StripeObject.construct_from(id: "id",
427
- metadata: Stripe::StripeObject.construct_from(foo: "bar"))
428
-
429
- serialized = obj.serialize_params(force: true)
430
- assert_equal({ id: "id", metadata: { foo: "bar" } }, serialized)
431
- end
432
-
433
- should "#dirty! forces an object and its subobjects to be saved" do
434
- obj = Stripe::StripeObject.construct_from(id: "id",
435
- metadata: Stripe::StripeObject.construct_from(foo: "bar"))
436
-
437
- # note that `force` and `dirty!` are for different things, but are
438
- # functionally equivalent
439
- obj.dirty!
440
-
441
- serialized = obj.serialize_params
442
- assert_equal({ id: "id", metadata: { foo: "bar" } }, serialized)
443
- end
444
-
445
- should "#to_s will call to_s for all embedded stripe objects" do
446
- obj = Stripe::StripeObject.construct_from(id: "id",
447
- # embedded list object
448
- refunds: Stripe::ListObject.construct_from(data: [
449
- # embedded object in list
450
- Stripe::StripeObject.construct_from(id: "id",
451
- # embedded object in an object in a list object
452
- metadata: Stripe::StripeObject.construct_from(foo: "bar")),
453
- ]),
454
- # embedded stripe object
455
- metadata: Stripe::StripeObject.construct_from(foo: "bar"))
456
- expected = JSON.pretty_generate(id: "id",
457
- refunds: {
458
- data: [
459
- { id: "id", metadata: { foo: "bar" } },
460
- ],
461
- },
462
- metadata: { foo: "bar" })
463
-
464
- assert_equal(expected, obj.to_s)
465
- end
466
-
467
- should "error on setting a property to an empty string" do
468
- obj = Stripe::StripeObject.construct_from(foo: "bar")
469
- e = assert_raises ArgumentError do
470
- obj.foo = ""
471
- end
472
- assert_match(/\(object\).foo = nil/, e.message)
473
- end
474
-
475
- should "marshal and unmarshal using custom encoder and decoder" do
476
- obj = Stripe::StripeObject.construct_from(
477
- { id: 1, name: "Stripe" },
478
- api_key: "apikey",
479
- client: StripeClient.active_client
480
- )
481
- m = Marshal.load(Marshal.dump(obj))
482
- assert_equal 1, m.id
483
- assert_equal "Stripe", m.name
484
- expected_hash = { api_key: "apikey" }
485
- assert_equal expected_hash, m.instance_variable_get("@opts")
486
- end
487
-
488
- context "#method" do
489
- should "act as a getter if no arguments are provided" do
490
- obj = Stripe::StripeObject.construct_from(id: 1, method: "foo")
491
- assert_equal "foo", obj.method
492
- end
493
-
494
- should "call Object#method if an argument is provided" do
495
- obj = Stripe::StripeObject.construct_from(id: 1, method: "foo")
496
- assert obj.method(:id).is_a?(Method)
497
- end
498
- end
499
-
500
- should "ignore properties that are reserved names" do
501
- obj = Stripe::StripeObject.construct_from(metadata: { class: "something" })
502
-
503
- # See comment on `StripeObject::RESERVED_FIELD_NAMES`
504
- assert_equal Stripe::StripeObject, obj.metadata.class
505
-
506
- # Value still accessible with hash syntax
507
- assert_equal "something", obj.metadata[:class]
508
- end
509
- end
510
- end