stripe-ruby/test/stripe/account_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

193 lines
6.1 KiB
Ruby

# frozen_string_literal: true
require File.expand_path("../../test_helper", __FILE__)
module Stripe
class AccountTest < Test::Unit::TestCase
should "be listable" do
accounts = Stripe::Account.list
assert_requested :get, "#{Stripe.api_base}/v1/accounts"
assert accounts.data.is_a?(Array)
assert accounts.data[0].is_a?(Stripe::Account)
end
should "be retrievable using singular endpoint" do
account = Stripe::Account.retrieve
assert_requested :get, "#{Stripe.api_base}/v1/account"
assert account.is_a?(Stripe::Account)
end
should "be retrievable using plural endpoint" do
account = Stripe::Account.retrieve("acct_123")
assert_requested :get, "#{Stripe.api_base}/v1/accounts/acct_123"
assert account.is_a?(Stripe::Account)
end
should "be rejectable" do
account_data = { id: "acct_foo" }
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acct_foo")
.to_return(body: JSON.generate(account_data))
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_foo/reject")
.to_return(body: JSON.generate(account_data))
account = Stripe::Account.retrieve("acct_foo")
account.reject(reason: "fraud")
end
should "be creatable" do
account = Stripe::Account.create(metadata: {}, type: "standard")
assert_requested :post, "#{Stripe.api_base}/v1/accounts"
assert account.is_a?(Stripe::Account)
end
should "be saveable" do
account = Stripe::Account.retrieve("acct_123")
account.metadata["key"] = "value"
account.save
assert_requested :post, "#{Stripe.api_base}/v1/accounts/#{account.id}"
end
should "be updateable" do
account = Stripe::Account.update("acct_123", metadata: { foo: "bar" })
assert_requested :post, "#{Stripe.api_base}/v1/accounts/acct_123"
assert account.is_a?(Stripe::Account)
end
should "be deletable" do
account = Stripe::Account.retrieve("acct_123")
account = account.delete
assert_requested :delete, "#{Stripe.api_base}/v1/accounts/#{account.id}"
assert account.is_a?(Stripe::Account)
end
context "#bank_account=" do
should "warn that #bank_account= is deprecated" do
old_stderr = $stderr
$stderr = StringIO.new
begin
account = Stripe::Account.retrieve("acct_123")
account.bank_account = "tok_123"
message = "NOTE: Stripe::Account#bank_account= is " \
"deprecated; use #external_account= instead"
assert_match Regexp.new(message), $stderr.string
ensure
$stderr = old_stderr
end
end
end
context "#deauthorize" do
should "deauthorize an account" do
account = Stripe::Account.retrieve("acct_123")
# Unfortunately, the OpenAPI spec doesn't yet cover anything under the
# Connect endpoints, so for just stub this out with Webmock.
stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize")
.with(body: { "client_id" => "ca_1234", "stripe_user_id" => account.id })
.to_return(body: JSON.generate("stripe_user_id" => account.id))
account.deauthorize("ca_1234", "sk_test_1234")
end
end
context "#legal_entity=" do
should "disallow direct overrides" do
account = Stripe::Account.retrieve("acct_123")
assert_raise NoMethodError do
account.legal_entity = { first_name: "Blah" }
end
account.legal_entity.first_name = "Blah"
end
end
context "#serialize_params" do
should "serialize an a new additional_owners" do
obj = Stripe::Util.convert_to_stripe_object({
object: "account",
legal_entity: Stripe::StripeObject.construct_from({
}),
}, {})
obj.legal_entity.additional_owners = [
{ first_name: "Joe" },
{ first_name: "Jane" },
]
expected = {
legal_entity: {
additional_owners: {
"0" => { first_name: "Joe" },
"1" => { first_name: "Jane" },
},
},
}
assert_equal(expected, obj.serialize_params)
end
should "serialize on an partially changed additional_owners" do
obj = Stripe::Util.convert_to_stripe_object({
object: "account",
legal_entity: {
additional_owners: [
Stripe::StripeObject.construct_from(first_name: "Joe"),
Stripe::StripeObject.construct_from(first_name: "Jane"),
],
},
}, {})
obj.legal_entity.additional_owners[1].first_name = "Stripe"
expected = {
legal_entity: {
additional_owners: {
"1" => { first_name: "Stripe" },
},
},
}
assert_equal(expected, obj.serialize_params)
end
should "serialize on an unchanged additional_owners" do
obj = Stripe::Util.convert_to_stripe_object({
object: "account",
legal_entity: {
additional_owners: [
Stripe::StripeObject.construct_from(first_name: "Joe"),
Stripe::StripeObject.construct_from(first_name: "Jane"),
],
},
}, {})
expected = {
legal_entity: {
additional_owners: {},
},
}
assert_equal(expected, obj.serialize_params)
end
# Note that the empty string that we send for this one has a special
# meaning for the server, which interprets it as an array unset.
should "serialize on an unset additional_owners" do
obj = Stripe::Util.convert_to_stripe_object({
object: "account",
legal_entity: {
additional_owners: [
Stripe::StripeObject.construct_from(first_name: "Joe"),
Stripe::StripeObject.construct_from(first_name: "Jane"),
],
},
}, {})
obj.legal_entity.additional_owners = nil
expected = {
legal_entity: {
additional_owners: "",
},
}
assert_equal(expected, obj.serialize_params)
end
end
end
end