stripe-ruby/test/stripe/source_test.rb
Brandur 863da48398 Add frozen_string_literal to every file and enforce Rubocop rule
Adds the magic `frozen_string_literal: true` comment to every file and
enables a Rubocop rule to make sure that it's always going to be there
going forward as well.

See here for more background [1], but the basic idea is that unlike many
other languages, static strings in code are mutable by default. This has
since been acknowledged as not a particularly good idea, and the
intention is to rectify the mistake when Ruby 3 comes out, where all
string literals will be frozen. The `frozen_string_literal` magic
comment was introduced in Ruby 2.3 as a way of easing the transition,
and allows libraries and projects to freeze their literals in advance.

I don't think this is breaking in any way: it's possible that users
might've been pulling out one of are literals somehow and mutating it,
but that would probably not have been useful for anything and would
certainly not be recommended, so I'm quite comfortable pushing this
change through as a minor version.

As discussed in #641.

[1] https://stackoverflow.com/a/37799399
2018-05-10 14:56:14 -07:00

87 lines
2.6 KiB
Ruby

# frozen_string_literal: true
require File.expand_path("../../test_helper", __FILE__)
module Stripe
class SourceTest < Test::Unit::TestCase
should "be retrievable" do
source = Stripe::Source.retrieve("src_123")
assert_requested :get, "#{Stripe.api_base}/v1/sources/src_123"
assert source.is_a?(Stripe::Source)
end
should "be creatable" do
source = Stripe::Source.create(
type: "card",
token: "tok_123"
)
assert_requested :post, "#{Stripe.api_base}/v1/sources"
assert source.is_a?(Stripe::Source)
end
should "be saveable" do
source = Stripe::Source.retrieve("src_123")
source.metadata["key"] = "value"
source.save
assert_requested :post, "#{Stripe.api_base}/v1/sources/#{source.id}"
end
should "be updateable" do
source = Stripe::Source.update("src_123", metadata: { foo: "bar" })
assert_requested :post, "#{Stripe.api_base}/v1/sources/src_123"
assert source.is_a?(Stripe::Source)
end
context "#detach" do
should "not be deletable when unattached" do
source = Stripe::Source.retrieve("src_123")
assert_raises NotImplementedError do
source.detach
end
end
should "be deletable when attached to a customer" do
source = Stripe::Source.construct_from(customer: "cus_123",
id: "src_123",
object: "source")
source = source.detach
assert_requested :delete, "#{Stripe.api_base}/v1/customers/cus_123/sources/src_123"
assert source.is_a?(Stripe::Source)
end
end
context "#delete" do
should "warn that #delete is deprecated" do
old_stderr = $stderr
$stderr = StringIO.new
begin
source = Stripe::Source.construct_from(customer: "cus_123",
id: "src_123",
object: "source")
source.delete
message = "NOTE: Stripe::Source#delete is " \
"deprecated; use #detach instead"
assert_match Regexp.new(message), $stderr.string
ensure
$stderr = old_stderr
end
end
end
should "not be listable" do
assert_raises NoMethodError do
Stripe::Source.list
end
end
context "#verify" do
should "verify the source" do
source = Stripe::Source.retrieve("src_123")
source = source.verify(values: [1, 2])
assert source.is_a?(Stripe::Source)
end
end
end
end