From ddac87a924cf6f7975ba2e649e9dda2e91af1ce1 Mon Sep 17 00:00:00 2001 From: Annie Li Date: Wed, 9 Nov 2022 11:19:45 -0800 Subject: [PATCH 1/4] [wip] --- test/stripe/api_resource_test.rb | 37 +++++++++++++++----------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index 706801eee..5357c80a4 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -318,9 +318,8 @@ 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 @@ -334,9 +333,9 @@ class NestedTestAPIResource < APIResource 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 @@ -386,32 +385,30 @@ class NestedTestAPIResource < APIResource 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 From ecc33d6a0e3c2fecbcb6bf84314c5a5794e46830 Mon Sep 17 00:00:00 2001 From: Annie Li Date: Thu, 10 Nov 2022 10:59:31 -0800 Subject: [PATCH 2/4] [wip] --- test/stripe/api_resource_test.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index 5357c80a4..2ff1b39f6 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -326,9 +326,8 @@ 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.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 @@ -343,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 From 1e885017f46592ce9518c16895fc9fa23006fe5d Mon Sep 17 00:00:00 2001 From: Annie Li Date: Fri, 18 Nov 2022 09:38:41 -0800 Subject: [PATCH 3/4] Update more tests --- test/stripe/api_resource_test.rb | 70 ++++++++++++++++---------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index 2ff1b39f6..049c4de10 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -370,17 +370,16 @@ 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 @@ -411,22 +410,21 @@ class NestedTestAPIResource < APIResource 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", @@ -441,8 +439,7 @@ 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 @@ -506,40 +503,41 @@ class NestedTestAPIResource < APIResource 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 + Stripe::Account.update("myid", {}) 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) + # should "should create a new resource when an object without an id is saved" do + # 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")) + # 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 + # # account.display_name = "stripe" + # # account.save + # Stripe::Account.update(nil, { display_name: "stripe" }) + # end - should "set attributes as part of save" do - account = Stripe::Account.construct_from(id: nil, - display_name: nil) + # 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")) + # 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" }) - end + # account.save(display_name: "stripe", metadata: { key: "value" }) + # end end context "#request_stripe_object" do From 5b6ab4315cf887fd51fe9535e49aa44c6276e5d7 Mon Sep 17 00:00:00 2001 From: Annie Li Date: Fri, 18 Nov 2022 13:35:07 -0800 Subject: [PATCH 4/4] Fix array tests and remove tests involving nil id --- test/stripe/api_resource_test.rb | 72 +++++++++----------------------- 1 file changed, 19 insertions(+), 53 deletions(-) diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index 049c4de10..eef4c7c5a 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -443,63 +443,53 @@ class NestedTestAPIResource < APIResource 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 @@ -514,30 +504,6 @@ class NestedTestAPIResource < APIResource Stripe::Account.update("myid", {}) end - - # should "should create a new resource when an object without an id is saved" do - # 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 - # Stripe::Account.update(nil, { display_name: "stripe" }) - # 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" }) - # end end context "#request_stripe_object" do