From 797478786d795c571cddcfb8b0d3edbd253833cf Mon Sep 17 00:00:00 2001 From: Jieren Chen Date: Thu, 1 Oct 2015 13:39:11 -0700 Subject: [PATCH 1/3] Disallow directly overriding legal_entity --- lib/stripe/account.rb | 12 ++++++++++++ lib/stripe/stripe_object.rb | 8 ++++++++ test/stripe/account_test.rb | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/lib/stripe/account.rb b/lib/stripe/account.rb index 4898ca5a..e9e89be5 100644 --- a/lib/stripe/account.rb +++ b/lib/stripe/account.rb @@ -27,6 +27,18 @@ module Stripe super(id, opts) end + def overridden_fields + [:legal_entity] + end + + def legal_entity + self['legal_entity'] + end + + def legal_entity=(_) + raise NoMethodError.new('Overridding legal_entity can cause cause serious issues. Instead, set the individual fields of legal_entity like blah.legal_entity.first_name = \'Blah\'') + end + def deauthorize(client_id, opts={}) opts = {:api_base => Stripe.connect_base}.merge(Util.normalize_opts(opts)) response, opts = request(:post, '/oauth/deauthorize', { 'client_id' => client_id, 'stripe_user_id' => self.id }, opts) diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index 29d90e76..5dceeac6 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -205,9 +205,15 @@ module Stripe class << self; self; end end + def overridden_fields + [] + end + def remove_accessors(keys) + f = overridden_fields metaclass.instance_eval do keys.each do |k| + next if f.include?(k) next if @@permanent_attributes.include?(k) k_eq = :"#{k}=" remove_method(k) if method_defined?(k) @@ -217,8 +223,10 @@ module Stripe end def add_accessors(keys, values) + f = overridden_fields metaclass.instance_eval do keys.each do |k| + next if f.include?(k) next if @@permanent_attributes.include?(k) k_eq = :"#{k}=" define_method(k) { @values[k] } diff --git a/test/stripe/account_test.rb b/test/stripe/account_test.rb index f9632e74..9a0de71c 100644 --- a/test/stripe/account_test.rb +++ b/test/stripe/account_test.rb @@ -77,6 +77,24 @@ module Stripe a.save end + should 'disallow direct overrides of legal_entity' do + account = Stripe::Account.construct_from(make_account({ + :keys => { + :publishable => 'publishable-key', + :secret => 'secret-key', + }, + :legal_entity => { + :first_name => 'Bling' + } + })) + + assert_raise NoMethodError do + account.legal_entity = {:first_name => 'Blah'} + end + + account.legal_entity.first_name = 'Blah' + end + should "be able to deauthorize an account" do resp = {:id => 'acct_1234', :email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false} @mock.expects(:get).once.returns(make_response(resp)) From e451e3d3e2753f5b9ea0f4fa38b5f8921ba9dd81 Mon Sep 17 00:00:00 2001 From: Jieren Chen Date: Thu, 1 Oct 2015 13:51:55 -0700 Subject: [PATCH 2/3] Fix test --- test/stripe/api_resource_test.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index 293a4c97..4bd78724 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -545,6 +545,10 @@ module Stripe :id => 'myid', :legal_entity => { :last_name => 'Smith', + :address => { + :line1 => "test", + :city => "San Francisco" + } } }) @@ -552,12 +556,12 @@ module Stripe "#{Stripe.api_base}/v1/accounts/myid", nil, any_of( - 'legal_entity[first_name]=Bob&legal_entity[last_name]=', - 'legal_entity[last_name]=&legal_entity[first_name]=Bob' + 'legal_entity[address][line1]=Test2&legal_entity[address][city]=', + 'legal_entity[address][city]=&legal_entity[address][line1]=Test2' ) ).returns(make_response({"id" => "myid"})) - acct.legal_entity = {:first_name => 'Bob'} + acct.legal_entity.address = {:line1 => 'Test2'} acct.save end From 387edb5163df580fe9158a67f60f9a5c0454d577 Mon Sep 17 00:00:00 2001 From: Jieren Chen Date: Thu, 1 Oct 2015 16:26:30 -0700 Subject: [PATCH 3/3] PR Fix: rename to protected_fields --- lib/stripe/account.rb | 2 +- lib/stripe/stripe_object.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/stripe/account.rb b/lib/stripe/account.rb index e9e89be5..87bbb5ef 100644 --- a/lib/stripe/account.rb +++ b/lib/stripe/account.rb @@ -27,7 +27,7 @@ module Stripe super(id, opts) end - def overridden_fields + def protected_fields [:legal_entity] end diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index 5dceeac6..2b432fa6 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -205,12 +205,12 @@ module Stripe class << self; self; end end - def overridden_fields + def protected_fields [] end def remove_accessors(keys) - f = overridden_fields + f = protected_fields metaclass.instance_eval do keys.each do |k| next if f.include?(k) @@ -223,7 +223,7 @@ module Stripe end def add_accessors(keys, values) - f = overridden_fields + f = protected_fields metaclass.instance_eval do keys.each do |k| next if f.include?(k)