diff options
author | Azul <azul@leap.se> | 2013-07-02 17:14:32 +0200 |
---|---|---|
committer | Azul <azul@leap.se> | 2013-07-17 10:47:14 +0200 |
commit | 1e4245907ca17d1d6afbeca82b25c35c970ba499 (patch) | |
tree | 432d9012731d2f35b704541f1fd976aaef2ae92f | |
parent | 968448cea22364f1fd423cedc54cf6d8a0f2e09e (diff) |
billing: functional test for payments controller
-rw-r--r-- | billing/app/controllers/payments_controller.rb | 11 | ||||
-rw-r--r-- | billing/app/models/customer.rb | 2 | ||||
-rw-r--r-- | billing/app/views/customer/edit.html.haml | 4 | ||||
-rw-r--r-- | billing/app/views/payments/_customer_data.html.haml | 8 | ||||
-rw-r--r-- | billing/test/functional/payments_controller_test.rb | 65 |
5 files changed, 50 insertions, 40 deletions
diff --git a/billing/app/controllers/payments_controller.rb b/billing/app/controllers/payments_controller.rb index 97e0353..99b7af8 100644 --- a/billing/app/controllers/payments_controller.rb +++ b/billing/app/controllers/payments_controller.rb @@ -1,4 +1,4 @@ -class PaymentsController < ApplicationController +class PaymentsController < BillingBaseController before_filter :authorize, :only => [:index] def new @@ -28,17 +28,18 @@ class PaymentsController < ApplicationController def fetch_transparent_redirect if current_user if @customer = Customer.find_by_user_id(current_user.id) - @braintree_data = Braintree::Customer.find(@customer.braintree_customer_id) - @default_cc = @customer.default_credit_card(@braintree_data) + @customer.with_braintree_data! braintree_customer_id = @customer.braintree_customer_id + @default_cc = @customer.default_credit_card else # TODO: this requires user to add self to vault before making payment. Is that desired functionality? redirect_to new_customer_path, :notice => 'Before making payment, please add your customer data' end end - @tr_data = Braintree::TransparentRedirect.transaction_data(:redirect_url => confirm_payment_url, - :transaction => {:type => "sale", :customer_id => braintree_customer_id, :options => {:submit_for_settlement => true } }) + # TODO: What is this supposed to do if braintree_customer_id was not set yet? + @tr_data = Braintree::TransparentRedirect.transaction_data redirect_url: confirm_payment_url, + transaction: { type: "sale", customer_id: braintree_customer_id, options: {submit_for_settlement: true } } end end diff --git a/billing/app/models/customer.rb b/billing/app/models/customer.rb index 747f17f..515e204 100644 --- a/billing/app/models/customer.rb +++ b/billing/app/models/customer.rb @@ -33,7 +33,7 @@ class Customer < CouchRest::Model::Base self end - def default_credit_card(braintree_data = nil) + def default_credit_card return unless has_payment_info? credit_cards.find { |cc| cc.default? } diff --git a/billing/app/views/customer/edit.html.haml b/billing/app/views/customer/edit.html.haml index dfc932e..f11e948 100644 --- a/billing/app/views/customer/edit.html.haml +++ b/billing/app/views/customer/edit.html.haml @@ -2,7 +2,7 @@ #total-errors{:style => "color:red;"} = h(@result.errors.size) error(s) -= form_for :customer, :url => Braintree::TransparentRedirect.url, :params => @result && @result.params[:customer],:existing => @customer, :builder => BraintreeHelper::BraintreeFormBuilder, :errors => @result && @result.errors.for(:customer) do |f| | += form_for :customer, url: Braintree::TransparentRedirect.url, params: @result && @result.params[:customer], existing: @customer, builder: BraintreeFormHelper::BraintreeFormBuilder, errors: @result && @result.errors.for(:customer) do |f| | = field_set_tag "Customer" do %dl %dt= f.label :first_name, 'First Name' @@ -19,4 +19,4 @@ = link_to 'Change credit card', edit_credit_card_info_path(:id => @default_cc.token), :class => :btn = hidden_field_tag :tr_data, @tr_data = f.submit 'Save Customer Info', :class => :btn -= link_to 'Show Customer Information', show_customer_path(@braintree_data.id), :class=> :btn
\ No newline at end of file += link_to 'Show Customer Information', show_customer_path(@customer), :class=> :btn diff --git a/billing/app/views/payments/_customer_data.html.haml b/billing/app/views/payments/_customer_data.html.haml index d46065c..71a5eaf 100644 --- a/billing/app/views/payments/_customer_data.html.haml +++ b/billing/app/views/payments/_customer_data.html.haml @@ -1,15 +1,15 @@ %h2 Customer Information %dl %dt First Name - %dd= @braintree_data.first_name + %dd= @customer.first_name %dt Last Name - %dd= @braintree_data.last_name + %dd= @customer.last_name %dt Phone - %dd= @braintree_data.phone + %dd= @customer.phone %h2 Credit Card Information %dl %dt Number %dd= @default_cc.masked_number %dt Expiration Date %dd= @default_cc.expiration_date -= link_to 'Edit Saved Data', edit_customer_path(@customer.braintree_customer_id), :class => :btn += link_to 'Edit Saved Data', edit_customer_path(@customer), :class => :btn diff --git a/billing/test/functional/payments_controller_test.rb b/billing/test/functional/payments_controller_test.rb index c8ef205..8f3bfe7 100644 --- a/billing/test/functional/payments_controller_test.rb +++ b/billing/test/functional/payments_controller_test.rb @@ -1,27 +1,8 @@ require 'test_helper' +require 'fake_braintree' class PaymentsControllerTest < ActionController::TestCase - setup do - FakeBraintree.clear! - @user = FactoryGirl.create :user - @other_user = FactoryGirl.create :user - FakeBraintree.clear! - FakeBraintree.verify_all_cards! - testid = 'testid' - FakeBraintree::Customer.new({:credit_cards => [{:number=>"5105105105105100", :expiration_date=>"05/2013"}]}, {:id => testid, :merchant_id => Braintree::Configuration.merchant_id}) - # any reason to call the create instance method on the FakeBraintree::Customer ? - @customer = Customer.new(:user_id => @other_user.id) - @customer.braintree_customer_id = testid - @customer.save - end - - teardown do - @user.destroy - @other_user.destroy - @customer.destroy - end - test "payment when unauthorized" do get :new assert_not_nil assigns(:tr_data) @@ -29,27 +10,55 @@ class PaymentsControllerTest < ActionController::TestCase end test "authenticated user must create account before making payment" do - login @user + login get :new assert_response :redirect assert_equal new_customer_url, response.header['Location'] end test "payment when authenticated as customer" do + customer = FactoryGirl.create :customer_with_payment_info + login customer.user get :new assert_not_nil assigns(:tr_data) assert_response :success - #TODO check more here end - # what would we test with something like this? - test "fake transaction" do - transaction = FakeBraintree.generate_transaction(:amount => '20.00', - #:status => Braintree::Transaction::Status::Settled, - #:subscription_id => 'foobar', - ) + test "successful confirmation renders confirm" do + Braintree::TransparentRedirect.expects(:confirm).returns(success_response) + get :confirm + assert_response :success + assert_template :confirm end + test "failed confirmation renders new" do + Braintree::TransparentRedirect.expects(:confirm).returns(failure_response) + get :confirm + + assert_response :success + assert_not_nil assigns(:tr_data) + assert_template :new + end + + def failure_response + stub success?: false, + errors: stub(for: nil, size: 0), + params: {}, + transaction: stub(status: nil) + end + + def success_response + stub success?: true, + transaction: stub_transaction + end + + # that's what you get when not following the law of demeter... + def stub_transaction + stub amount: "100.00", + id: "ASDF", + customer_details: FactoryGirl.build(:braintree_customer), + credit_card_details: FactoryGirl.build(:braintree_customer).credit_cards.first + end end |