diff options
author | Azul <azul@leap.se> | 2013-07-01 13:13:02 +0200 |
---|---|---|
committer | Azul <azul@leap.se> | 2013-07-17 10:47:14 +0200 |
commit | 1c40963194aa6f1dc985b949fb3c05e70f7530c0 (patch) | |
tree | d03cb2b420f0b8d5dfec2f0b2c875d157782c275 | |
parent | a49c63eb117abf47ca1804493c25ae34804f8ee1 (diff) |
use fake_braintree, fix and test customer with braintree info
-rw-r--r-- | Gemfile | 5 | ||||
-rw-r--r-- | billing/app/models/customer.rb | 25 | ||||
-rw-r--r-- | billing/test/factories.rb | 14 | ||||
-rw-r--r-- | billing/test/unit/customer_test.rb | 6 | ||||
-rw-r--r-- | billing/test/unit/customer_with_payment_info_test.rb | 27 |
5 files changed, 53 insertions, 24 deletions
@@ -17,6 +17,11 @@ gem 'debugger', :platforms => :mri_19 # ruby 1.8 is not supported anymore # gem 'ruby-debug', :platforms => :mri_18 + +group :test do + gem 'fake_braintree', require: false +end + # unreleased so far ... but leap_web_certs need it gem 'certificate_authority', :git => 'git://github.com/cchandler/certificate_authority.git' diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb index 7d35756..4788118 100644 --- a/billing/app/models/customer.rb +++ b/billing/app/models/customer.rb @@ -1,10 +1,11 @@ class Customer < CouchRest::Model::Base - #FIELDS = [:first_name, :last_name, :phone, :website, :company, :fax, :addresses, :credit_cards, :custom_fields] + FIELDS = [:first_name, :last_name, :phone, :website, :company, :fax, :addresses, :credit_cards, :custom_fields] + attr_accessor *FIELDS use_database "customers" belongs_to :user - property :braintree_customer_id + belongs_to :braintree_customer, class: Braintree::Customer validates :user, presence: true @@ -18,35 +19,27 @@ class Customer < CouchRest::Model::Base end # from braintree_ruby_examples/rails3_tr_devise and should be tweaked -=begin def with_braintree_data! return self unless has_payment_info? - braintree_data = Braintree::Customer.find(braintree_customer_id) - #FIELDS.each do |field| - # send(:"#{field}=", braintree_data.send(field)) - #end + FIELDS.each do |field| + send(:"#{field}=", braintree_customer.send(field)) + end self end -=end - #slow to get Braintree Customer data, so pass it if have already retrieved it - # won't really have multiple credit cards on file - # instead of having method, should just be able to call braintree_data.credit_cards.first if just one is allowed def default_credit_card(braintree_data = nil) return unless has_payment_info? - braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id) - braintree_data.credit_cards.find { |cc| cc.default? } - end + credit_cards.find { |cc| cc.default? } + end # based on 2nd parameter, either returns the single active subscription (or nil if there isn't one), or an array of all subsciptions def subscriptions(braintree_data=nil, only_active=true) return unless has_payment_info? - braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id) subscriptions = [] - braintree_data.credit_cards.first.subscriptions.each do |sub| + credit_cards.first.subscriptions.each do |sub| if only_active and sub.status == 'Active' return sub else diff --git a/billing/test/factories.rb b/billing/test/factories.rb index 8648847..8314542 100644 --- a/billing/test/factories.rb +++ b/billing/test/factories.rb @@ -1,11 +1,21 @@ FactoryGirl.define do + TEST_CC_NUMBER = %w(4111 1111 1111 1111).join + factory :customer do user - factory :braintree_customer do - braintree_customer_id { 1 } + factory :customer_with_payment_info do + braintree_customer end end + factory :braintree_customer, class: Braintree::Customer do + first_name 'Big' + last_name 'Spender' + credit_card number: TEST_CC_NUMBER, expiration_date: '04/2016' + initialize_with { Braintree::Customer.create(attributes).customer } + skip_create + end + end diff --git a/billing/test/unit/customer_test.rb b/billing/test/unit/customer_test.rb index abcf96a..0358f38 100644 --- a/billing/test/unit/customer_test.rb +++ b/billing/test/unit/customer_test.rb @@ -34,10 +34,4 @@ class CustomerTest < ActiveSupport::TestCase assert_nil @customer.default_credit_card end - test "user with braintree id" do - @customer = FactoryGirl.build(:braintree_customer) - assert @customer.braintree_customer_id - assert @customer.has_payment_info? - end - end diff --git a/billing/test/unit/customer_with_payment_info_test.rb b/billing/test/unit/customer_with_payment_info_test.rb new file mode 100644 index 0000000..f887674 --- /dev/null +++ b/billing/test/unit/customer_with_payment_info_test.rb @@ -0,0 +1,27 @@ +require 'test_helper' +require 'fake_braintree' + +class CustomerWithPaymentInfoTest < ActiveSupport::TestCase + + setup do + @customer = FactoryGirl.build(:customer_with_payment_info) + end + + test "has payment_info" do + assert @customer.braintree_customer_id + assert @customer.has_payment_info? + end + + test "constructs customer with braintree data" do + @customer.with_braintree_data! + assert_equal 'Big', @customer.first_name + assert_equal 'Spender', @customer.last_name + assert_equal 1, @customer.credit_cards.size + assert_equal Hash.new, @customer.custom_fields + end + + test "sets default_credit_card" do + @customer.with_braintree_data! + assert_equal @customer.credit_cards.first, @customer.default_credit_card + end +end |