summaryrefslogtreecommitdiff
path: root/billing/test
diff options
context:
space:
mode:
Diffstat (limited to 'billing/test')
-rw-r--r--billing/test/factories.rb25
-rw-r--r--billing/test/functional/customer_controller_test.rb121
-rw-r--r--billing/test/functional/customers_controller_test.rb59
-rw-r--r--billing/test/functional/payments_controller_test.rb64
-rw-r--r--billing/test/integration/customer_creation_test.rb63
-rw-r--r--billing/test/test_helper.rb15
-rw-r--r--billing/test/unit/customer_test.rb36
-rw-r--r--billing/test/unit/customer_with_payment_info_test.rb37
8 files changed, 420 insertions, 0 deletions
diff --git a/billing/test/factories.rb b/billing/test/factories.rb
new file mode 100644
index 0000000..87543b2
--- /dev/null
+++ b/billing/test/factories.rb
@@ -0,0 +1,25 @@
+FactoryGirl.define do
+
+ TEST_CC_NUMBER = %w(4111 1111 1111 1111).join
+
+ factory :customer do
+ user
+
+ 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
+
+ factory :broken_customer do
+ credit_card number: '123456', expiration_date: '04/2016'
+ end
+ end
+
+end
diff --git a/billing/test/functional/customer_controller_test.rb b/billing/test/functional/customer_controller_test.rb
new file mode 100644
index 0000000..9bf2b5e
--- /dev/null
+++ b/billing/test/functional/customer_controller_test.rb
@@ -0,0 +1,121 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class CustomerControllerTest < ActionController::TestCase
+
+ test "new assigns redirect url" do
+ login
+ get :new
+
+ assert_response :success
+ assert assigns(:tr_data)
+ tr_data = Braintree::Util.parse_query_string(assigns(:tr_data))
+ assert_equal confirm_customer_url, tr_data[:redirect_url]
+ end
+
+ test "new requires login" do
+ get :new
+
+ assert_response :redirect
+ assert_redirected_to login_path
+ end
+
+ test "edit uses params[:id]" do
+ customer = FactoryGirl.create :customer_with_payment_info
+ login customer.user
+ get :edit, id: customer.id
+
+ assert_response :success
+ assert assigns(:tr_data)
+ tr_data = Braintree::Util.parse_query_string(assigns(:tr_data))
+ assert_equal customer.braintree_customer_id, tr_data[:customer_id]
+ assert_equal confirm_customer_url, tr_data[:redirect_url]
+ end
+
+ test "confirm user creation" do
+ login
+ 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, braintree: :query
+ end
+
+ assert_response :success
+ assert result = assigns(:result)
+ assert result.success?
+ assert result.customer.id
+ end
+
+ test "customer update" do
+ 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
+ end
+
+ assert_response :success
+ assert result = assigns(:result)
+ assert result.success?
+ assert_equal customer.braintree_customer, result.customer
+ end
+
+ test "failed user creation" do
+ skip "can't get user creation to fail"
+ login
+ FakeBraintree.decline_all_cards!
+ to_confirm = prepare_confirmation :create_customer_data,
+ customer: FactoryGirl.attributes_for(:broken_customer),
+ redirect_url: confirm_customer_url
+ post :confirm, to_confirm
+
+ FakeBraintree.clear!
+ assert_response :success
+ assert result = assigns(:result)
+ assert !result.success?
+ end
+
+ test "failed user creation with stubbing" do
+ login
+ Braintree::TransparentRedirect.expects(:confirm).returns(failure_response)
+ post :confirm, bla: :blub
+
+ assert_response :success
+ assert_template :new
+ end
+
+ test "failed user update with stubbing" do
+ customer = FactoryGirl.create :customer_with_payment_info
+ login customer.user
+ Braintree::TransparentRedirect.expects(:confirm).returns(failure_response)
+ post :confirm, bla: :blub
+
+ assert_response :success
+ assert_template :edit
+ end
+
+ def failure_response
+ stub success?: false,
+ errors: stub(for: nil, size: 0),
+ params: {}
+ end
+
+ def success_response(customer = nil)
+ stub success?: true,
+ customer: braintree_customer(customer)
+ end
+
+ def braintree_customer(customer)
+ if customer
+ customer.braintree_customer
+ else
+ FactoryGirl.build :braintree_customer
+ end
+ end
+
+end
diff --git a/billing/test/functional/customers_controller_test.rb b/billing/test/functional/customers_controller_test.rb
new file mode 100644
index 0000000..58b6155
--- /dev/null
+++ b/billing/test/functional/customers_controller_test.rb
@@ -0,0 +1,59 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class CustomersControllerTest < ActionController::TestCase
+ tests CustomerController
+
+ setup do
+ @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 "no access if not logged in" do
+ get :new
+ assert_access_denied(true, false)
+ get :show, :id => @customer.braintree_customer_id
+ assert_access_denied(true, false)
+ get :edit, :id => @customer.braintree_customer_id
+ assert_access_denied(true, false)
+ end
+
+
+ test "should get new if logged in and not customer" do
+ login @user
+ get :new
+ assert_not_nil assigns(:tr_data)
+ assert_response :success
+ end
+
+ test "new should direct edit if user is already a customer" do
+ login @other_user
+ get :new
+ assert_response :redirect
+ assert_equal edit_customer_url(@customer), response.header['Location']
+ end
+
+
+ test "show" do
+ login @other_user
+ # Below will fail, as when we go to fetch the customer data, Braintree::Customer.find(params[:id]) won't find the customer as it is a FakeBraintree customer.
+ #get :show, :id => @customer.braintree_customer_id
+
+ end
+
+end
diff --git a/billing/test/functional/payments_controller_test.rb b/billing/test/functional/payments_controller_test.rb
new file mode 100644
index 0000000..8f3bfe7
--- /dev/null
+++ b/billing/test/functional/payments_controller_test.rb
@@ -0,0 +1,64 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class PaymentsControllerTest < ActionController::TestCase
+
+ test "payment when unauthorized" do
+ get :new
+ assert_not_nil assigns(:tr_data)
+ assert_response :success
+ end
+
+ test "authenticated user must create account before making payment" do
+ 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
+ end
+
+ 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
diff --git a/billing/test/integration/customer_creation_test.rb b/billing/test/integration/customer_creation_test.rb
new file mode 100644
index 0000000..3ab2e4f
--- /dev/null
+++ b/billing/test/integration/customer_creation_test.rb
@@ -0,0 +1,63 @@
+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!
+ @user = FactoryGirl.create(:user)
+ login_as @user
+ end
+
+ teardown do
+ Warden.test_reset!
+ end
+
+ # Let's test both steps together with capybara
+ #
+ # This test is nice and clean but also a bit fragil:
+ # RackTest assumes all requests to be local. So we need
+ # BraintreeTestApp for the braintree transparent redirect to work.
+ test "create customer with braintree" do
+ visit '/customer/new'
+ assert_difference("Customer.count") do
+ click_button 'Save Payment Info'
+ end
+
+ assert customer = Customer.find_by_user_id(@user.id)
+ assert customer.braintree_customer
+ end
+
+ # We only test the confirmation here.
+ # The request to Braintree is triggered outside of rails
+ test "successfully confirms customer creation" do
+ 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
+
+ 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
diff --git a/billing/test/test_helper.rb b/billing/test/test_helper.rb
new file mode 100644
index 0000000..1e26a31
--- /dev/null
+++ b/billing/test/test_helper.rb
@@ -0,0 +1,15 @@
+# Configure Rails Environment
+ENV["RAILS_ENV"] = "test"
+
+require File.expand_path("../dummy/config/environment.rb", __FILE__)
+require "rails/test_help"
+
+Rails.backtrace_cleaner.remove_silencers!
+
+# Load support files
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
+
+# Load fixtures from the engine
+if ActiveSupport::TestCase.method_defined?(:fixture_path=)
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
+end
diff --git a/billing/test/unit/customer_test.rb b/billing/test/unit/customer_test.rb
new file mode 100644
index 0000000..2ea0b5e
--- /dev/null
+++ b/billing/test/unit/customer_test.rb
@@ -0,0 +1,36 @@
+require 'test_helper'
+
+class CustomerTest < ActiveSupport::TestCase
+
+ setup do
+ @customer = FactoryGirl.build(:customer)
+ end
+
+ test "test set of attributes should be valid" do
+ @customer.valid?
+ assert_equal Hash.new, @customer.errors.messages
+ end
+
+ test "customer belongs to user" do
+ assert_equal User, @customer.user.class
+ end
+
+ test "user validation" do
+ @customer.user = nil
+ assert !@customer.valid?
+ end
+
+ test "has no payment info" do
+ assert !@customer.braintree_customer_id
+ assert !@customer.has_payment_info?
+ end
+
+ test "with no braintree data" do
+ assert_equal @customer, @customer.with_braintree_data!
+ end
+
+ test "without default credit card" do
+ assert_nil @customer.default_credit_card
+ 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..ca89e65
--- /dev/null
+++ b/billing/test/unit/customer_with_payment_info_test.rb
@@ -0,0 +1,37 @@
+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 "can access braintree_customer after reload" do
+ @customer.save
+ @customer = Customer.find_by_user_id(@customer.user_id)
+ @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