From b51f300fe374e9eecf7e98266c96631fa3c8a278 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 3 Jul 2013 09:28:44 +0200 Subject: billing: integration test creating customer --- Gemfile | 1 + .../test/functional/customer_controller_test.rb | 46 ++++++---------- billing/test/integration/customer_creation_test.rb | 61 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 billing/test/integration/customer_creation_test.rb diff --git a/Gemfile b/Gemfile index c9a361b..a562f73 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,7 @@ gem 'debugger', :platforms => :mri_19 group :test do gem 'fake_braintree', require: false + gem 'capybara', require: false end # unreleased so far ... but leap_web_certs need it diff --git a/billing/test/functional/customer_controller_test.rb b/billing/test/functional/customer_controller_test.rb index af09a41..9bf2b5e 100644 --- a/billing/test/functional/customer_controller_test.rb +++ b/billing/test/functional/customer_controller_test.rb @@ -34,12 +34,13 @@ class CustomerControllerTest < ActionController::TestCase test "confirm user creation" do login - to_confirm = prepare_confirmation :create_customer_data, - customer: FactoryGirl.attributes_for(:braintree_customer), - redirect_url: confirm_customer_url + Braintree::TransparentRedirect.expects(:confirm).returns(success_response) + # to_confirm = prepare_confirmation :create_customer_data, + # customer: FactoryGirl.attributes_for(:braintree_customer), + # redirect_url: confirm_customer_url assert_difference("Customer.count") do - post :confirm, to_confirm + post :confirm, braintree: :query end assert_response :success @@ -49,9 +50,10 @@ class CustomerControllerTest < ActionController::TestCase end test "customer update" do - @customer = FactoryGirl.create :customer_with_payment_info - login @customer.user - Braintree::TransparentRedirect.expects(:confirm).returns(success_response) + customer = FactoryGirl.create :customer_with_payment_info + login customer.user + Braintree::TransparentRedirect.expects(:confirm). + returns(success_response(customer)) assert_no_difference("Customer.count") do post :confirm, query: :from_braintree @@ -60,7 +62,7 @@ class CustomerControllerTest < ActionController::TestCase assert_response :success assert result = assigns(:result) assert result.success? - assert_equal @customer, result.customer + assert_equal customer.braintree_customer, result.customer end test "failed user creation" do @@ -97,37 +99,23 @@ class CustomerControllerTest < ActionController::TestCase assert_template :edit end - def prepare_confirmation(type, data) - parse_redirect post_transparent_redirect(type, data) - end - def failure_response stub success?: false, errors: stub(for: nil, size: 0), params: {} end - def success_response + def success_response(customer = nil) stub success?: true, - customer: @customer.with_braintree_data! - end - - def post_transparent_redirect(type, data) - params = data.dup - params[:tr_data] = Braintree::TransparentRedirect.send(type, params) - post_transparent_redirect_params(params) + customer: braintree_customer(customer) end - def post_transparent_redirect_params(params) - uri = URI.parse(Braintree::TransparentRedirect.url) - Net::HTTP.start(uri.host, uri.port) do |http| - http.post(uri.path, Rack::Utils.build_nested_query(params)) + def braintree_customer(customer) + if customer + customer.braintree_customer + else + FactoryGirl.build :braintree_customer end end - def parse_redirect(response) - uri = URI.parse(response['Location']) - Braintree::Util.parse_query_string uri.query - end - end diff --git a/billing/test/integration/customer_creation_test.rb b/billing/test/integration/customer_creation_test.rb new file mode 100644 index 0000000..50116db --- /dev/null +++ b/billing/test/integration/customer_creation_test.rb @@ -0,0 +1,61 @@ +require 'test_helper' +require 'fake_braintree' +require 'capybara/rails' + +class CustomerCreationTest < ActionDispatch::IntegrationTest + include Warden::Test::Helpers + include Capybara::DSL + + setup do + Warden.test_mode! + end + + teardown do + Warden.test_reset! + end + + # We only test the confirmation here. + # The first request to Braintree is triggered outside of rails + test "successfully confirms customer creation" do + user = FactoryGirl.create(:user) + login_as user + response = post_transparent_redirect :create_customer_data, + customer: FactoryGirl.attributes_for(:braintree_customer), + redirect_url: confirm_customer_url + + assert_difference("Customer.count") do + post response['Location'] + end + + assert_equal 200, status + assert customer = Customer.find_by_user_id(user.id) + assert customer.braintree_customer + end + + # let's test both steps together with capybara + test "create customer with braintree" do + user = FactoryGirl.create(:user) + login_as user + visit '/customer/new' + assert_difference("Customer.count") do + click_button 'Save Payment Info' + end + assert_equal 200, status + assert customer = Customer.find_by_user_id(user.id) + assert customer.braintree_customer + end + + def post_transparent_redirect(type, data) + params = data.dup + params[:tr_data] = Braintree::TransparentRedirect.send(type, params) + post_transparent_redirect_params(params) + end + + def post_transparent_redirect_params(params) + uri = URI.parse(Braintree::TransparentRedirect.url) + Net::HTTP.start(uri.host, uri.port) do |http| + http.post(uri.path, Rack::Utils.build_nested_query(params)) + end + end + +end -- cgit v1.2.3