From f3dd3ab1bf416e5a7d4cff2a002504760592df23 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Mon, 17 Sep 2018 14:37:41 +0200 Subject: [PATCH 1/3] Handle file objects like file_upload --- lib/stripe.rb | 2 +- lib/stripe/{file_upload.rb => file.rb} | 12 +++++- lib/stripe/util.rb | 3 +- test/stripe/file_test.rb | 56 ++++++++++++++++++++++++++ test/stripe/file_upload_test.rb | 10 +++++ 5 files changed, 79 insertions(+), 4 deletions(-) rename lib/stripe/{file_upload.rb => file.rb} (66%) create mode 100644 test/stripe/file_test.rb diff --git a/lib/stripe.rb b/lib/stripe.rb index 7fe803fa4..c6d5a2993 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -55,8 +55,8 @@ require "stripe/ephemeral_key" require "stripe/event" require "stripe/exchange_rate" +require "stripe/file" require "stripe/file_link" -require "stripe/file_upload" require "stripe/invoice" require "stripe/invoice_item" require "stripe/invoice_line_item" diff --git a/lib/stripe/file_upload.rb b/lib/stripe/file.rb similarity index 66% rename from lib/stripe/file_upload.rb rename to lib/stripe/file.rb index f598ab012..b4cd50350 100644 --- a/lib/stripe/file_upload.rb +++ b/lib/stripe/file.rb @@ -1,11 +1,16 @@ # frozen_string_literal: true module Stripe - class FileUpload < APIResource + class File < APIResource extend Stripe::APIOperations::Create extend Stripe::APIOperations::List - OBJECT_NAME = "file_upload".freeze + # This resource can have two different object names. In latter API + # versions, only `file` is used, but since stripe-ruby may be used with + # any API version, we need to support deserializing the older + # `file_upload` object into the same class. + OBJECT_NAME = "file".freeze + OBJECT_NAME_ALT = "file_upload".freeze def self.resource_url "/v1/files" @@ -32,4 +37,7 @@ def self.create(params = {}, opts = {}) super end end + + # For backwards compatibility, the `File` class is aliased to `FileUpload`. + FileUpload = File end diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index a40716c88..ccda642f4 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -64,8 +64,9 @@ def self.object_classes # rubocop:disable Metrics/MethodLength EphemeralKey::OBJECT_NAME => EphemeralKey, Event::OBJECT_NAME => Event, ExchangeRate::OBJECT_NAME => ExchangeRate, + File::OBJECT_NAME => File, + File::OBJECT_NAME_ALT => File, FileLink::OBJECT_NAME => FileLink, - FileUpload::OBJECT_NAME => FileUpload, Invoice::OBJECT_NAME => Invoice, InvoiceItem::OBJECT_NAME => InvoiceItem, InvoiceLineItem::OBJECT_NAME => InvoiceLineItem, diff --git a/test/stripe/file_test.rb b/test/stripe/file_test.rb new file mode 100644 index 000000000..1a5393028 --- /dev/null +++ b/test/stripe/file_test.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class FileTest < Test::Unit::TestCase + should "be listable" do + files = Stripe::File.list + assert files.data.is_a?(Array) + assert files.data[0].is_a?(Stripe::File) + end + + should "be retrievable" do + file = Stripe::File.retrieve("file_123") + assert file.is_a?(Stripe::File) + end + + should "be creatable with a File" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: ::File.new(__FILE__) + ) + assert file.is_a?(Stripe::File) + end + + should "be creatable with a Tempfile" do + tempfile = Tempfile.new("foo") + tempfile.write("Hello world") + tempfile.rewind + + file = Stripe::File.create( + purpose: "dispute_evidence", + file: tempfile + ) + assert file.is_a?(Stripe::File) + end + + should "be creatable with Faraday::UploadIO" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: Faraday::UploadIO.new(::File.new(__FILE__), nil) + ) + assert file.is_a?(Stripe::File) + end + + should "be deserializable when `object=file`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}) + assert file.is_a?(Stripe::File) + end + + should "be deserializable when `object=file_upload`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}) + assert file.is_a?(Stripe::File) + end + end +end diff --git a/test/stripe/file_upload_test.rb b/test/stripe/file_upload_test.rb index 67de1055a..42556b18e 100644 --- a/test/stripe/file_upload_test.rb +++ b/test/stripe/file_upload_test.rb @@ -42,5 +42,15 @@ class FileUploadTest < Test::Unit::TestCase ) assert file.is_a?(Stripe::FileUpload) end + + should "be deserializable when `object=file`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}) + assert file.is_a?(Stripe::FileUpload) + end + + should "be deserializable when `object=file_upload`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}) + assert file.is_a?(Stripe::FileUpload) + end end end From ea09249e67dfd3319857cfbe819d2bcb418658d6 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 20 Sep 2018 22:45:24 +0200 Subject: [PATCH 2/3] Use files.stripe.com only for the file create endpoint --- lib/stripe.rb | 2 +- lib/stripe/file.rb | 8 +--- test/stripe/file_test.rb | 63 +++++++++++++++++++------------ test/stripe/file_upload_test.rb | 66 +++++++++++++++++++++------------ test/test_helper.rb | 4 -- 5 files changed, 85 insertions(+), 58 deletions(-) diff --git a/lib/stripe.rb b/lib/stripe.rb index c6d5a2993..73a186e79 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -103,7 +103,7 @@ module Stripe @api_base = "https://api.stripe.com" @connect_base = "https://connect.stripe.com" - @uploads_base = "https://uploads.stripe.com" + @uploads_base = "https://files.stripe.com" @log_level = nil @logger = nil diff --git a/lib/stripe/file.rb b/lib/stripe/file.rb index b4cd50350..eef999fac 100644 --- a/lib/stripe/file.rb +++ b/lib/stripe/file.rb @@ -16,13 +16,6 @@ def self.resource_url "/v1/files" end - def self.request(method, url, params = {}, opts = {}) - opts = { - api_base: Stripe.uploads_base, - }.merge(Util.normalize_opts(opts)) - super - end - def self.create(params = {}, opts = {}) # rest-client would accept a vanilla `File` for upload, but Faraday does # not. Support the old API by wrapping a `File`-like object with an @@ -32,6 +25,7 @@ def self.create(params = {}, opts = {}) end opts = { + api_base: Stripe.uploads_base, content_type: "multipart/form-data", }.merge(Util.normalize_opts(opts)) super diff --git a/test/stripe/file_test.rb b/test/stripe/file_test.rb index 1a5393028..858e1ae4d 100644 --- a/test/stripe/file_test.rb +++ b/test/stripe/file_test.rb @@ -6,41 +6,58 @@ module Stripe class FileTest < Test::Unit::TestCase should "be listable" do files = Stripe::File.list + assert_requested :get, "#{Stripe.api_base}/v1/files" assert files.data.is_a?(Array) assert files.data[0].is_a?(Stripe::File) end should "be retrievable" do file = Stripe::File.retrieve("file_123") + assert_requested :get, "#{Stripe.api_base}/v1/files/file_123" assert file.is_a?(Stripe::File) end - should "be creatable with a File" do - file = Stripe::File.create( - purpose: "dispute_evidence", - file: ::File.new(__FILE__) - ) - assert file.is_a?(Stripe::File) - end + context ".create" do + setup do + # We don't point to the same host for the API and uploads in + # production, but `stripe-mock` supports both APIs. + Stripe.uploads_base = Stripe.api_base - should "be creatable with a Tempfile" do - tempfile = Tempfile.new("foo") - tempfile.write("Hello world") - tempfile.rewind + # Set `api_base` to `nil` to ensure that these requests are _not_ sent + # to the default API hostname. + Stripe.api_base = nil + end - file = Stripe::File.create( - purpose: "dispute_evidence", - file: tempfile - ) - assert file.is_a?(Stripe::File) - end + should "be creatable with a File" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: ::File.new(__FILE__) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::File) + end - should "be creatable with Faraday::UploadIO" do - file = Stripe::File.create( - purpose: "dispute_evidence", - file: Faraday::UploadIO.new(::File.new(__FILE__), nil) - ) - assert file.is_a?(Stripe::File) + should "be creatable with a Tempfile" do + tempfile = Tempfile.new("foo") + tempfile.write("Hello world") + tempfile.rewind + + file = Stripe::File.create( + purpose: "dispute_evidence", + file: tempfile + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::File) + end + + should "be creatable with Faraday::UploadIO" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: Faraday::UploadIO.new(::File.new(__FILE__), nil) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::File) + end end should "be deserializable when `object=file`" do diff --git a/test/stripe/file_upload_test.rb b/test/stripe/file_upload_test.rb index 42556b18e..cb1a06765 100644 --- a/test/stripe/file_upload_test.rb +++ b/test/stripe/file_upload_test.rb @@ -3,44 +3,64 @@ require ::File.expand_path("../../test_helper", __FILE__) module Stripe + # This is a strict copy of `FileTest`, except that it uses + # `Stripe::FileUpload` instead of `Stripe::File`. class FileUploadTest < Test::Unit::TestCase should "be listable" do files = Stripe::FileUpload.list + assert_requested :get, "#{Stripe.api_base}/v1/files" assert files.data.is_a?(Array) assert files.data[0].is_a?(Stripe::FileUpload) end should "be retrievable" do file = Stripe::FileUpload.retrieve("file_123") + assert_requested :get, "#{Stripe.api_base}/v1/files/file_123" assert file.is_a?(Stripe::FileUpload) end - should "be creatable with a File" do - file = Stripe::FileUpload.create( - purpose: "dispute_evidence", - file: ::File.new(__FILE__) - ) - assert file.is_a?(Stripe::FileUpload) - end + context ".create" do + setup do + # We don't point to the same host for the API and uploads in + # production, but `stripe-mock` supports both APIs. + Stripe.uploads_base = Stripe.api_base - should "be creatable with a Tempfile" do - tempfile = Tempfile.new("foo") - tempfile.write("Hello world") - tempfile.rewind + # Set `api_base` to `nil` to ensure that these requests are _not_ sent + # to the default API hostname. `api_base` is reset when each test + # starts so this won't affect the global state. + Stripe.api_base = nil + end - file = Stripe::FileUpload.create( - purpose: "dispute_evidence", - file: tempfile - ) - assert file.is_a?(Stripe::FileUpload) - end + should "be creatable with a File" do + file = Stripe::FileUpload.create( + purpose: "dispute_evidence", + file: ::File.new(__FILE__) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::FileUpload) + end - should "be creatable with Faraday::UploadIO" do - file = Stripe::FileUpload.create( - purpose: "dispute_evidence", - file: Faraday::UploadIO.new(::File.new(__FILE__), nil) - ) - assert file.is_a?(Stripe::FileUpload) + should "be creatable with a Tempfile" do + tempfile = Tempfile.new("foo") + tempfile.write("Hello world") + tempfile.rewind + + file = Stripe::FileUpload.create( + purpose: "dispute_evidence", + file: tempfile + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::FileUpload) + end + + should "be creatable with Faraday::UploadIO" do + file = Stripe::FileUpload.create( + purpose: "dispute_evidence", + file: Faraday::UploadIO.new(::File.new(__FILE__), nil) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::FileUpload) + end end should "be deserializable when `object=file`" do diff --git a/test/test_helper.rb b/test/test_helper.rb index 71bbe09f3..086ad9bd5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -50,10 +50,6 @@ class TestCase Stripe.api_key = "sk_test_123" Stripe.api_base = "http://localhost:#{MOCK_PORT}" - # We don't point to the same host for the API and uploads in - # production, but `stripe-mock` supports both APIs. - Stripe.uploads_base = Stripe.api_base - stub_connect end From 9d64d31e2ac41a8fb59a08d359650f53120744d5 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Sat, 22 Sep 2018 16:46:12 +0200 Subject: [PATCH 3/3] Upgrade stripe-mock --- .travis.yml | 2 +- test/stripe/issuing/dispute_test.rb | 2 +- test/test_helper.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9b602e68c..d5b3aa921 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ sudo: false env: global: # If changing this number, please also change it in `test/test_helper.rb`. - - STRIPE_MOCK_VERSION=0.30.0 + - STRIPE_MOCK_VERSION=0.32.0 cache: directories: diff --git a/test/stripe/issuing/dispute_test.rb b/test/stripe/issuing/dispute_test.rb index faeb54cb2..f6d69be5e 100644 --- a/test/stripe/issuing/dispute_test.rb +++ b/test/stripe/issuing/dispute_test.rb @@ -8,7 +8,7 @@ class DisputeTest < Test::Unit::TestCase should "be creatable" do dispute = Stripe::Issuing::Dispute.create( reason: "fraudulent", - transaction: "ipi_123" + disputed_transaction: "ipi_123" ) assert_requested :post, "#{Stripe.api_base}/v1/issuing/disputes" assert dispute.is_a?(Stripe::Issuing::Dispute) diff --git a/test/test_helper.rb b/test/test_helper.rb index 086ad9bd5..e79e75a67 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,7 +16,7 @@ require ::File.expand_path("../test_data", __FILE__) # If changing this number, please also change it in `.travis.yml`. -MOCK_MINIMUM_VERSION = "0.30.0".freeze +MOCK_MINIMUM_VERSION = "0.32.0".freeze MOCK_PORT = ENV["STRIPE_MOCK_PORT"] || 12_111 # Disable all real network connections except those that are outgoing to