summaryrefslogtreecommitdiff
path: root/engines/billing/test
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-04-11 10:03:19 +0200
committerAzul <azul@leap.se>2014-04-11 10:07:23 +0200
commit636692f9921bd695d726695d2d46c91f5a6e56f3 (patch)
treea7cc0b89007bd273ae7719f31c16e052a141fec7 /engines/billing/test
parent32136605ddd405a0bf47f3b795b22fd4b49465b5 (diff)
move engines into engines directory
Also renamed help to support so it's harder to confuse it with documentation
Diffstat (limited to 'engines/billing/test')
-rw-r--r--engines/billing/test/broken/admin_customer_test.rb31
-rw-r--r--engines/billing/test/broken/customer_creation_test.rb84
-rw-r--r--engines/billing/test/broken/subscription_test.rb49
-rw-r--r--engines/billing/test/factories.rb25
-rw-r--r--engines/billing/test/functional/customer_controller_test.rb124
-rw-r--r--engines/billing/test/functional/customers_controller_test.rb61
-rw-r--r--engines/billing/test/functional/payments_controller_test.rb50
-rw-r--r--engines/billing/test/functional/subscriptions_controller_test.rb16
-rw-r--r--engines/billing/test/support/braintree_integration_test.rb18
-rw-r--r--engines/billing/test/support/customer_test_helper.rb11
-rw-r--r--engines/billing/test/test_helper.rb15
-rw-r--r--engines/billing/test/unit/customer_test.rb38
-rw-r--r--engines/billing/test/unit/customer_with_payment_info_test.rb40
13 files changed, 562 insertions, 0 deletions
diff --git a/engines/billing/test/broken/admin_customer_test.rb b/engines/billing/test/broken/admin_customer_test.rb
new file mode 100644
index 0000000..df92a0d
--- /dev/null
+++ b/engines/billing/test/broken/admin_customer_test.rb
@@ -0,0 +1,31 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class AdminCustomerTest < BraintreeIntegrationTest
+
+ setup do
+ @admin = User.find_by_login('admin') || FactoryGirl.create(:user, login: 'admin')
+ @user = FactoryGirl.create(:user)
+ end
+
+ teardown do
+ @user.destroy if @user
+ @admin.destroy if @admin
+ end
+
+ test "check non customer as admin" do
+ login_as @admin
+ visit '/'
+ click_link 'Users'
+ click_link @user.login
+ click_link 'Billing Settings'
+ assert page.has_content? @user.email_address
+ assert page.has_content? 'No Saved Customer'
+ end
+
+ test "check customer as admin" do
+ skip "cannot check customer as admin"
+ # it would be good to have a test where an admin tries to view the 'Billing Settings' for another user.
+ # However, partially due to limitations of FakeBraintree, this doesn't seem pursuing at this time.
+ end
+end
diff --git a/engines/billing/test/broken/customer_creation_test.rb b/engines/billing/test/broken/customer_creation_test.rb
new file mode 100644
index 0000000..90319a9
--- /dev/null
+++ b/engines/billing/test/broken/customer_creation_test.rb
@@ -0,0 +1,84 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class CustomerCreationTest < BraintreeIntegrationTest
+
+ setup do
+ @user = FactoryGirl.create(:user)
+ login_as @user
+ end
+
+ teardown do
+ @user.destroy
+ end
+
+ # Let's test both steps together with capybara
+ #
+ # This test is nice and clean but also a bit fragile:
+ # RackTest assumes all requests to be local. So we need
+ # BraintreeTestApp for the braintree transparent redirect to work.
+ #
+ # this mystifies me why this works. when i type the click_button line (and the
+ # customer.braintree_customer line) in the debugger, it gives a timeout,
+ # but it works fine embedded in the test.
+ test "create customer with braintree" do
+ visit '/'
+ click_link 'Billing Settings'
+ # i am a bit unclear why this works, as it seems there will be validation errors
+ assert_difference("Customer.count") do
+ click_button 'Save Payment Info' # this gives me a timeout
+ 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
+ # In skippped test below, we see this works even if the attributes are
+ # for a broken customer
+ 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
+
+
+ test "failed customer creation" do
+ skip "cannot get customer creation to fail"
+
+ FakeBraintree.decline_all_cards!
+
+ response = post_transparent_redirect :create_customer_data,
+ customer: FactoryGirl.attributes_for(:broken_customer),
+ redirect_url: confirm_customer_url
+
+ assert FakeBraintree.decline_all_cards?
+ assert_no_difference("Customer.count") do
+ post response['Location'] #this gives me a timeout when run alone
+ end
+ assert_nil Customer.find_by_user_id(@user.id)
+
+ 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/engines/billing/test/broken/subscription_test.rb b/engines/billing/test/broken/subscription_test.rb
new file mode 100644
index 0000000..cd010bd
--- /dev/null
+++ b/engines/billing/test/broken/subscription_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class SubscriptionTest < BraintreeIntegrationTest
+ include CustomerTestHelper
+ include StubRecordHelper
+
+ setup do
+ @admin = User.find_by_login('admin') || FactoryGirl.create(:user, login: 'admin')
+ @customer = stub_customer
+ @braintree_customer = @customer.braintree_customer
+ response = Braintree::Subscription.create plan_id: '5',
+ payment_method_token: @braintree_customer.credit_cards.first.token,
+ price: '10'
+ @subscription = response.subscription
+ end
+
+ teardown do
+ @admin.destroy
+ end
+
+ test "admin can see all subscriptions for another" do
+ login_as @admin
+ @customer.stubs(:subscriptions).returns([@subscription])
+ @subscription.stubs(:balance).returns 0
+ visit user_subscriptions_path(@customer.user_id, :locale => nil)
+ assert page.has_content?("Subscriptions")
+ assert page.has_content?("Status: Active")
+ end
+
+ # test "user cannot see all subscriptions for other user" do
+ #end
+
+ #test "admin cannot add subscription for another" do
+ #end
+
+ #test "authenticated user can cancel own subscription" do
+ #end
+
+ #test "user cannot add subscription if they have active one" do
+ #end
+
+ #test "user can view own subscriptions"
+ #end
+
+ #test "admin can view another user's subscriptions" do
+ #end
+
+end
diff --git a/engines/billing/test/factories.rb b/engines/billing/test/factories.rb
new file mode 100644
index 0000000..87543b2
--- /dev/null
+++ b/engines/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/engines/billing/test/functional/customer_controller_test.rb b/engines/billing/test/functional/customer_controller_test.rb
new file mode 100644
index 0000000..d943e23
--- /dev/null
+++ b/engines/billing/test/functional/customer_controller_test.rb
@@ -0,0 +1,124 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class CustomerControllerTest < ActionController::TestCase
+ include CustomerTestHelper
+
+ 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 = stub_customer
+ login customer.user
+ get :edit, id: customer.user.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 customer 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 = stub_customer
+ customer.expects(:save)
+ 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 customer creation" do
+ skip "can't get customer creation to fail"
+ login
+ FakeBraintree.decline_all_cards!
+ # what is prepare_confirmation ?? this method isn't found
+ 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 customer 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 customer update with stubbing" do
+ customer = stub_customer
+ 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/engines/billing/test/functional/customers_controller_test.rb b/engines/billing/test/functional/customers_controller_test.rb
new file mode 100644
index 0000000..46c33c9
--- /dev/null
+++ b/engines/billing/test/functional/customers_controller_test.rb
@@ -0,0 +1,61 @@
+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'
+ #this wasn't actually being used
+ #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.user), response.header['Location']
+ end
+
+
+ test "show" do
+ skip "show customer"
+ 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/engines/billing/test/functional/payments_controller_test.rb b/engines/billing/test/functional/payments_controller_test.rb
new file mode 100644
index 0000000..90b7582
--- /dev/null
+++ b/engines/billing/test/functional/payments_controller_test.rb
@@ -0,0 +1,50 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class PaymentsControllerTest < ActionController::TestCase
+ include CustomerTestHelper
+
+ test "payment when unauthorized" do
+ 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/engines/billing/test/functional/subscriptions_controller_test.rb b/engines/billing/test/functional/subscriptions_controller_test.rb
new file mode 100644
index 0000000..a6a1057
--- /dev/null
+++ b/engines/billing/test/functional/subscriptions_controller_test.rb
@@ -0,0 +1,16 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class SubscriptionsControllerTest < ActionController::TestCase
+ include CustomerTestHelper
+
+ test "destroy cancels subscription" do
+ customer = stub_customer
+ login customer.user
+ result = Braintree::Subscription.create plan_id: 'my_plan',
+ payment_method_token: customer.braintree_customer.credit_cards.first.token
+ subscription = result.subscription
+ delete :destroy, id: subscription.id, user_id: customer.user.id
+ assert_equal "Canceled", Braintree::Subscription.find(subscription.id).status
+ end
+end
diff --git a/engines/billing/test/support/braintree_integration_test.rb b/engines/billing/test/support/braintree_integration_test.rb
new file mode 100644
index 0000000..976c5a2
--- /dev/null
+++ b/engines/billing/test/support/braintree_integration_test.rb
@@ -0,0 +1,18 @@
+require 'capybara/rails'
+# require 'fake_braintree' - messes up other integration tests
+require 'braintree_test_app'
+
+class BraintreeIntegrationTest < BrowserIntegrationTest
+ include Warden::Test::Helpers
+
+ setup do
+ Warden.test_mode!
+ Rails.application.config.middleware.use BraintreeTestApp
+ end
+
+ teardown do
+ Warden.test_reset!
+ Rails.application.config.middleware.delete "BraintreeTestApp"
+ end
+
+end
diff --git a/engines/billing/test/support/customer_test_helper.rb b/engines/billing/test/support/customer_test_helper.rb
new file mode 100644
index 0000000..adac00a
--- /dev/null
+++ b/engines/billing/test/support/customer_test_helper.rb
@@ -0,0 +1,11 @@
+module CustomerTestHelper
+
+ def stub_customer(user = nil)
+ user ||= find_record :user
+ customer = stub_record :customer_with_payment_info,
+ user: user,
+ user_id: user.id
+ Customer.stubs(:find_by_user_id).with(user.id).returns(customer)
+ return customer
+ end
+end
diff --git a/engines/billing/test/test_helper.rb b/engines/billing/test/test_helper.rb
new file mode 100644
index 0000000..7ad3869
--- /dev/null
+++ b/engines/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/engines/billing/test/unit/customer_test.rb b/engines/billing/test/unit/customer_test.rb
new file mode 100644
index 0000000..6156f87
--- /dev/null
+++ b/engines/billing/test/unit/customer_test.rb
@@ -0,0 +1,38 @@
+require 'test_helper'
+
+class CustomerTest < ActiveSupport::TestCase
+ include StubRecordHelper
+
+ setup do
+ @user = find_record :user
+ @customer = FactoryGirl.build(:customer, user: @user)
+ 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/engines/billing/test/unit/customer_with_payment_info_test.rb b/engines/billing/test/unit/customer_with_payment_info_test.rb
new file mode 100644
index 0000000..0589a59
--- /dev/null
+++ b/engines/billing/test/unit/customer_with_payment_info_test.rb
@@ -0,0 +1,40 @@
+require 'test_helper'
+require 'fake_braintree'
+
+class CustomerWithPaymentInfoTest < ActiveSupport::TestCase
+ include StubRecordHelper
+
+ setup do
+ @user = find_record :user
+ @customer = FactoryGirl.build(:customer_with_payment_info, user: @user)
+ 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
+ @customer.destroy
+ end
+
+ test "sets default_credit_card" do
+ @customer.with_braintree_data!
+ assert_equal @customer.credit_cards.first, @customer.default_credit_card
+ end
+end