Skip to content

Commit

Permalink
Pass params to transfer reversal (#1399)
Browse files Browse the repository at this point in the history
* Pass params to transfer reversal

* Add tests

* Add test for header check
  • Loading branch information
prathmesh-stripe authored May 8, 2024
1 parent f538d42 commit 7f2bdfa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/stripe/api_operations/nested_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
end
end

# rubocop:disable Metrics/MethodLength
private def define_operation(
resource,
operation,
Expand All @@ -53,12 +54,30 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
)
end
when :retrieve
# TODO: (Major) Split params_or_opts to params and opts and get rid of the complicated way to add params
define_singleton_method(:"retrieve_#{resource}") \
do |id, nested_id, opts = {}|
do |id, nested_id, params_or_opts = {}, definitely_opts = nil|
opts = nil
params = nil
if definitely_opts.nil?
unrecognized_key = params_or_opts.keys.find { |k| !Util::OPTS_USER_SPECIFIED.include?(k) }
if unrecognized_key
raise ArgumentError,
"Unrecognized request option: #{unrecognized_key}. Did you mean to specify this as " \
"retrieve params? " \
"If so, you must explicitly pass an opts hash as a fourth argument. " \
"For example: .retrieve(#{id}, #{nested_id}, {#{unrecognized_key}: 'foo'}, {})"
end

opts = params_or_opts
else
opts = definitely_opts
params = params_or_opts
end
request_stripe_object(
method: :get,
path: send(resource_url_method, id, nested_id),
params: {},
params: params,
opts: opts
)
end
Expand Down Expand Up @@ -96,6 +115,7 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
raise ArgumentError, "Unknown operation: #{operation.inspect}"
end
end
# rubocop:enable Metrics/MethodLength
end
end
end
30 changes: 30 additions & 0 deletions test/stripe/api_operations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ def self.object_name
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with only opts" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
.with(headers: { "Stripe-Account" => "acct_123" })
.to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
nested_resource = MainResource.retrieve_nested("id", "nested_id", { stripe_account: "acct_123" })
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with both opts and params" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id?expand[]=reverse")
.with(headers: { "Stripe-Account" => "acct_123" })
.to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
nested_resource = MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] }, { stripe_account: "acct_123" })
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with params and explicitly empty opts" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id?expand[]=reverse")
.to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
nested_resource = MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] }, {})
assert_equal "bar", nested_resource.foo
end

should "warns when attempting to retrieve and pass only params" do
exception = assert_raises(ArgumentError) do
MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] })
end
assert_match(/Unrecognized request option/, exception.message)
end

should "define an update method" do
stub_request(:post, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
.with(body: { foo: "baz" })
Expand Down
54 changes: 54 additions & 0 deletions test/stripe/transfer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,59 @@ class TransferTest < Test::Unit::TestCase
assert reversals.data.is_a?(Array)
end
end

should "retrieve a reversal with expand" do
reversal = Stripe::Transfer.retrieve_reversal(
"tr_123",
"trr_123",
{ expand: %w[transfer] },
{}
)
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand%5B%5D=transfer"
assert reversal.is_a?(Stripe::Reversal)
end

should "be retrievable with opts only" do
transfer_reversal = Stripe::Transfer.retrieve_reversal(
"tr_123",
"trr_123",
{ stripe_account: "acct_123" }
)
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123" do |req|
assert_equal("acct_123", req.headers["Stripe-Account"])
true
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "be retrievable with opts and params" do
transfer_reversal = Stripe::Transfer.retrieve_reversal("tr_123",
"trr_123",
{ expand: ["available"] },
{ stripe_account: "acct_123" })
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand[]=available" do |req|
assert_equal("acct_123", req.headers["Stripe-Account"])
true
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "be retrievable with params and an explicitly empty opts" do
transfer_reversal = Stripe::Transfer.retrieve_reversal(
"tr_123",
"trr_123",
{ expand: ["available"] },
{}
)
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand[]=available" do |req|
assert_nil(req.headers["Stripe-Account"])
true
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "warn you if you are attempting to pass only params" do
exception = assert_raises(ArgumentError) do
Stripe::Transfer.retrieve_reversal("tr_123", "trr_123", { expand: ["available"] })
end
assert_match(/Unrecognized request option/, exception.message)
end
end
end

0 comments on commit 7f2bdfa

Please sign in to comment.