Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use update in tests instead of save #1149

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 56 additions & 96 deletions test/stripe/api_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,34 +318,32 @@ class NestedTestAPIResource < APIResource
stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
.with(body: { "description" => "another_mn" })
.to_return(body: JSON.generate(customer_fixture))
c = Stripe::Customer.construct_from(customer_fixture)
c.description = "another_mn"
c.save
Stripe::Customer.construct_from(customer_fixture)
Stripe::Customer.update("cus_123", { description: "another_mn" })
end

should "updating should merge in returned properties" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure "merge in returned properties" describes what Update does. Maybe we should just delete these tests or keep them the way they are because even though it is deprecated it is good to have test coverage for .save until we entirely remove it

stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
.with(body: { "description" => "another_mn" })
.to_return(body: JSON.generate(customer_fixture))
c = Stripe::Customer.new("cus_123")
c.description = "another_mn"
c.save
Stripe::Customer.new("cus_123")
c = Stripe::Customer.update("cus_123", { description: "another_mn" })
assert_equal false, c.livemode
end

should "updating should fail if api_key is overwritten with nil" do
c = Stripe::Customer.new
Stripe::Customer.new("cus_123")
assert_raises TypeError do
c.save({}, api_key: nil)
Stripe::Customer.update("cus_123", {}, { api_key: nil })
end
end

should "updating should use the supplied api_key" do
stub_request(:post, "#{Stripe.api_base}/v1/customers")
.with(headers: { "Authorization" => "Bearer sk_test_local" })
.to_return(body: JSON.generate(customer_fixture))
c = Stripe::Customer.new
c.save({}, api_key: "sk_test_local")
Stripe::Customer.new("cus_123")
c = Stripe::Customer.update("cus_123", {}, api_key: "sk_test_local")
assert_equal false, c.livemode
end

Expand All @@ -372,65 +370,61 @@ class NestedTestAPIResource < APIResource
Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
end

should "passing in a stripe_account header should pass it through on save" do
should "passing in a stripe_account header should pass it through on update" do
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
.with(headers: { "Stripe-Account" => "acct_123" })
.to_return(body: JSON.generate(customer_fixture))
c = Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")

stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
.with(headers: { "Stripe-Account" => "acct_123" })
.to_return(body: JSON.generate(customer_fixture))
c.description = "FOO"
c.save
Stripe::Customer.update("cus_123", { description: "FOO" })
end

should "add key to nested objects" do
acct = Stripe::Account.construct_from(id: "myid",
legal_entity: {
size: "l",
score: 4,
height: 10,
})
Stripe::Account.construct_from(id: "myid",
business_profile: {
url: "example.com",
support_email: "[email protected]",
})

stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
.with(body: { legal_entity: { first_name: "Bob" } })
.with(body: { business_profile: { name: "Bob" } })
.to_return(body: JSON.generate("id" => "myid"))

acct.legal_entity.first_name = "Bob"
acct.save
Stripe::Account.update("myid", { business_profile: { name: "Bob" } })
end

should "save nothing if nothing changes" do
acct = Stripe::Account.construct_from(id: "acct_id",
metadata: {
key: "value",
})
should "update nothing if nothing changes" do
Stripe::Account.construct_from(id: "acct_id",
metadata: {
key: "value",
})

stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_id")
.with(body: {})
.to_return(body: JSON.generate("id" => "acct_id"))

acct.save
Stripe::Account.update("acct_id")
end

should "not save nested API resources" do
ch = Stripe::Charge.construct_from(id: "ch_id",
customer: {
object: "customer",
id: "customer_id",
})
Stripe::Charge.construct_from(id: "ch_id",
customer: {
object: "customer",
id: "customer_id",
})

stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_id")
.with(body: {})
.to_return(body: JSON.generate("id" => "ch_id"))

ch.customer.description = "Bob"
ch.save
Stripe::Charge.update("ch_id", { customer: { description: "Bob" } })
end

should "correctly handle replaced nested objects" do
acct = Stripe::Account.construct_from(
Stripe::Account.construct_from(
id: "acct_123",
company: {
name: "company_name",
Expand All @@ -445,104 +439,70 @@ class NestedTestAPIResource < APIResource
.with(body: { company: { address: { line1: "Test2", city: "" } } })
.to_return(body: JSON.generate("id" => "my_id"))

acct.company.address = { line1: "Test2" }
acct.save
Stripe::Account.update("acct_123", { company: { address: { line1: "Test2" } } })
end

should "correctly handle array setting" do
acct = Stripe::Account.construct_from(id: "myid",
legal_entity: {})
Stripe::Subscription.construct_from(id: "myid")

stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
.with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
stub_request(:post, "#{Stripe.api_base}/v1/subscriptions/myid")
.with(body: { items: [{ plan: "foo", quantity: 2 }] })
.to_return(body: JSON.generate("id" => "myid"))

acct.legal_entity.additional_owners = [{ first_name: "Bob" }]
acct.save
Stripe::Subscription.update("myid", { items: [{ plan: "foo", quantity: 2 }] })
end

should "correctly handle array insertion" do
acct = Stripe::Account.construct_from(id: "myid",
legal_entity: {
additional_owners: [],
})
Stripe::Subscription.construct_from(id: "myid", items: [])

# Note that this isn't a perfect check because we're using webmock's
# data decoding, which isn't aware of the Stripe array encoding that we
# use here.
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
.with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
stub_request(:post, "#{Stripe.api_base}/v1/subscriptions/myid")
.with(body: { items: [{ plan: "foo", quantity: 2 }] })
.to_return(body: JSON.generate("id" => "myid"))

acct.legal_entity.additional_owners << { first_name: "Bob" }
acct.save
Stripe::Subscription.update("myid", { items: [{ plan: "foo", quantity: 2 }] })
end

should "correctly handle array updates" do
acct = Stripe::Account.construct_from(id: "myid",
legal_entity: {
additional_owners: [{ first_name: "Bob" }, { first_name: "Jane" }],
})
Stripe::Subscription.construct_from(id: "myid",
items: [
{ plan: "foo", quantity: 2 },
{ plan: "bar", quantity: 2 },
])

# Note that this isn't a perfect check because we're using webmock's
# data decoding, which isn't aware of the Stripe array encoding that we
# use here.
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
.with(body: { legal_entity: { additional_owners: [{ first_name: "Janet" }] } })
stub_request(:post, "#{Stripe.api_base}/v1/subscriptions/myid")
.with(body: { items: [{ plan: "foos", quantity: 3 }, { plan: "bar", quantity: 3 }] })
.to_return(body: JSON.generate("id" => "myid"))

acct.legal_entity.additional_owners[1].first_name = "Janet"
acct.save
Stripe::Subscription.update("myid", { items: [{ plan: "foos", quantity: 3 }, { plan: "bar", quantity: 3 }] })
end

should "correctly handle array noops" do
acct = Stripe::Account.construct_from(id: "myid",
legal_entity: {
additional_owners: [{ first_name: "Bob" }],
},
currencies_supported: %w[usd cad])
Stripe::Subscription.construct_from(id: "myid", items: [{ plan: "foo", quantity: 2 }])

stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
stub_request(:post, "#{Stripe.api_base}/v1/subscriptions/myid")
.with(body: {})
.to_return(body: JSON.generate("id" => "myid"))

acct.save
Stripe::Subscription.update("myid", {})
end

should "correctly handle hash noops" do
acct = Stripe::Account.construct_from(id: "myid",
legal_entity: {
address: { line1: "1 Two Three" },
})
Stripe::Account.construct_from(id: "myid",
legal_entity: {
address: { line1: "1 Two Three" },
})

stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
.with(body: {})
.to_return(body: JSON.generate("id" => "myid"))

acct.save
end

should "should create a new resource when an object without an id is saved" do
account = Stripe::Account.construct_from(id: nil,
display_name: nil)

stub_request(:post, "#{Stripe.api_base}/v1/accounts")
.with(body: { display_name: "stripe" })
.to_return(body: JSON.generate("id" => "acct_123"))

account.display_name = "stripe"
account.save
end

should "set attributes as part of save" do
account = Stripe::Account.construct_from(id: nil,
display_name: nil)

stub_request(:post, "#{Stripe.api_base}/v1/accounts")
.with(body: { display_name: "stripe", metadata: { key: "value" } })
.to_return(body: JSON.generate("id" => "acct_123"))

account.save(display_name: "stripe", metadata: { key: "value" })
Stripe::Account.update("myid", {})
end
end

Expand Down