diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index 706801eee..eef4c7c5a 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -318,25 +318,23 @@ 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 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 @@ -344,8 +342,8 @@ class NestedTestAPIResource < APIResource 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 @@ -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: "test@example.com", + }) 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", @@ -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