Skip to content

Commit

Permalink
Move file upload tests over to be handled by stripe-mock
Browse files Browse the repository at this point in the history
`stripe-mock` can now respond accurately for file API endpoints thanks
to a few improvements in how it handles `multipart/form-data` payloads
and the OpenAPI spec.

Here we upgrade `stripe-mock` to 0.15.0 and remove the manual stubbing
that we had previously.
  • Loading branch information
brandur committed May 4, 2018
1 parent 21b0514 commit b9a3971
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ sudo: false

env:
global:
- STRIPE_MOCK_VERSION=0.12.0
# If changing this number, please also change it in `test/test_helper.rb`.
- STRIPE_MOCK_VERSION=0.15.0

cache:
directories:
Expand Down
39 changes: 0 additions & 39 deletions test/stripe/file_upload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,18 @@

module Stripe
class FileUploadTest < Test::Unit::TestCase
# Note that these tests are written different from others because we
# don't have anything on the uploads service in our OpenAPI spec. This is
# something that should be looked into at some point. We may need to ship
# a separate spec for it though, so it's high effort with low reward for
# the time being.
FIXTURE = {
id: "file_123",
object: "file_upload",
}.freeze

should "be listable" do
stub_request(:get, "#{Stripe.uploads_base}/v1/files")
.to_return(body: JSON.generate(data: [FIXTURE],
object: "list",
resource_url: "/v1/files"))

files = Stripe::FileUpload.list
assert files.data.is_a?(Array)
assert files.data[0].is_a?(Stripe::FileUpload)
end

should "be retrievable" do
stub_request(:get, "#{Stripe.uploads_base}/v1/files/file_123")
.to_return(body: JSON.generate(FIXTURE))

file = Stripe::FileUpload.retrieve("file_123")
assert file.is_a?(Stripe::FileUpload)
end

should "be creatable with a File" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files")
.with(headers: {
"Content-Type" => /\A#{Faraday::Request::Multipart.mime_type}/,
}) do |request|
request.body =~ /FileUploadTest/
end.to_return(body: JSON.generate(FIXTURE))

file = Stripe::FileUpload.create(
purpose: "dispute_evidence",
file: File.new(__FILE__)
Expand All @@ -47,13 +22,6 @@ class FileUploadTest < Test::Unit::TestCase
end

should "be creatable with a Tempfile" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files")
.with(headers: {
"Content-Type" => /\A#{Faraday::Request::Multipart.mime_type}/,
}) do |request|
request.body =~ /Hello world/
end.to_return(body: JSON.generate(FIXTURE))

tempfile = Tempfile.new("foo")
tempfile.write("Hello world")
tempfile.rewind
Expand All @@ -66,13 +34,6 @@ class FileUploadTest < Test::Unit::TestCase
end

should "be creatable with Faraday::UploadIO" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files")
.with(headers: {
"Content-Type" => /\A#{Faraday::Request::Multipart.mime_type}/,
}) do |request|
request.body =~ /FileUploadTest/
end.to_return(body: JSON.generate(FIXTURE))

file = Stripe::FileUpload.create(
purpose: "dispute_evidence",
file: Faraday::UploadIO.new(File.new(__FILE__), nil)
Expand Down
8 changes: 7 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

require File.expand_path("../test_data", __FILE__)

MOCK_MINIMUM_VERSION = "0.12.0".freeze
# If changing this number, please also change it in `.travis.yml`.
MOCK_MINIMUM_VERSION = "0.15.0".freeze
MOCK_PORT = ENV["STRIPE_MOCK_PORT"] || 12_111

# Disable all real network connections except those that are outgoing to
Expand Down Expand Up @@ -46,6 +47,11 @@ class TestCase
setup do
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

Expand Down

0 comments on commit b9a3971

Please sign in to comment.