diff --git a/test/stripe/metadata_test.rb b/test/stripe/metadata_test.rb new file mode 100644 index 00000000..8e99db9c --- /dev/null +++ b/test/stripe/metadata_test.rb @@ -0,0 +1,91 @@ +require File.expand_path('../../test_helper', __FILE__) + +module Stripe + class MetadataTest < Test::Unit::TestCase + setup do + @metadata_supported = { + :charge => {:new => Stripe::Charge.method(:new), + :test => method(:test_charge), + :url => "/v1/charges/#{test_charge()[:id]}"}, + :customer => {:new => Stripe::Customer.method(:new), + :test => method(:test_customer), + :url => "/v1/customers/#{test_customer()[:id]}"}, + :recipient => {:new => Stripe::Recipient.method(:new), + :test => method(:test_recipient), + :url => "/v1/recipients/#{test_recipient()[:id]}"}, + :transfer => {:new => Stripe::Transfer.method(:new), + :test => method(:test_transfer), + :url => "/v1/transfers/#{test_transfer()[:id]}"} + } + + @base_url = 'https://api.stripe.com' + end + + should "update metadata as a whole" do + update_actions = lambda {|obj| obj.metadata = {'uuid' => '6735'}} + + metadata_updates = {} + metadata_updates.update({'uuid' => '6735'}) + metadata_updates.update({'initial' => ''}) + curl_args = Stripe.uri_encode({:metadata => metadata_updates}) + + check_metadata({:metadata => {'initial' => 'true'}}, + 'metadata[uuid]=6735&metadata[initial]=', + update_actions) + end + + should "update metadata keys individually" do + update_actions = lambda {|obj| obj.metadata['txn_id'] = '134a13'} + check_metadata({:metadata => {'initial' => 'true'}}, + 'metadata[txn_id]=134a13', + update_actions) + end + + should "clear metadata as a whole" do + update_actions = lambda {|obj| obj.metadata = nil} + check_metadata({:metadata => {'initial' => 'true'}}, + 'metadata=', + update_actions) + end + + should "clear metadata keys individually" do + update_actions = lambda {|obj| obj.metadata['initial'] = nil} + check_metadata({:metadata => {'initial' => 'true'}}, + 'metadata[initial]=', + update_actions) + end + + should "handle combinations of whole and partial metadata updates" do + update_actions = lambda do |obj| + obj.metadata = {'type' => 'summer'} + obj.metadata['uuid'] = '6735' + end + params = {:metadata => {'type' => 'summer', 'uuid' => '6735'}} + curl_args = Stripe.uri_encode(params) + check_metadata({:metadata => {'type' => 'christmas'}}, + curl_args, + update_actions) + end + + def check_metadata (initial_params, curl_args, metadata_update) + @metadata_supported.each do |name, methods| + neu = methods[:new] + test = methods[:test] + url = @base_url + methods[:url] + + initial_test_obj = test.call(initial_params) + @mock.expects(:get).once.returns(test_response(initial_test_obj)) + + final_test_obj = test.call() + @mock.expects(:post).once. + returns(test_response(final_test_obj)). + with(url, nil, curl_args) + + obj = neu.call("test") + obj.refresh() + metadata_update.call(obj) + obj.save + end + end + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index b90b7a7e..45e0d4e3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -80,7 +80,8 @@ def test_customer(params={}) :id => "c_test_customer", :default_card => "cc_test_card", :created => 1304114758, - :cards => test_card_array('c_test_customer') + :cards => test_card_array('c_test_customer'), + :metadata => {} }.merge(params) end @@ -111,7 +112,8 @@ def test_charge(params={}) :livemode => false, :currency => "usd", :object => "charge", - :created => 1304114826 + :created => 1304114826, + :metadata => {} }.merge(params) end @@ -249,7 +251,8 @@ def test_recipient(params={}) :object => "bank_account" }, :created => 1304114758, - :verified => true + :verified => true, + :metadata => {} }.merge(params) end @@ -278,7 +281,8 @@ def test_transfer(params={}) :livemode => false, :currency => "usd", :object => "transfer", - :date => 1304114826 + :date => 1304114826, + :metadata => {} }.merge(params) end