diff options
Diffstat (limited to 'engines/billing/test')
8 files changed, 115 insertions, 303 deletions
diff --git a/engines/billing/test/factories.rb b/engines/billing/test/factories.rb index 87543b2..6352211 100644 --- a/engines/billing/test/factories.rb +++ b/engines/billing/test/factories.rb @@ -14,7 +14,8 @@ FactoryGirl.define do first_name 'Big' last_name 'Spender' credit_card number: TEST_CC_NUMBER, expiration_date: '04/2016' - initialize_with { Braintree::Customer.create(attributes).customer } + initialize_with { Braintree::Configuration.environment = :sandbox + Braintree::Customer.create(attributes).customer } skip_create factory :broken_customer do diff --git a/engines/billing/test/functional/customer_controller_test.rb b/engines/billing/test/functional/customer_controller_test.rb deleted file mode 100644 index d943e23..0000000 --- a/engines/billing/test/functional/customer_controller_test.rb +++ /dev/null @@ -1,124 +0,0 @@ -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 deleted file mode 100644 index 4d84fb0..0000000 --- a/engines/billing/test/functional/customers_controller_test.rb +++ /dev/null @@ -1,62 +0,0 @@ -require 'test_helper' -require 'fake_braintree' - -class CustomersControllerTest < ActionController::TestCase - tests CustomerController - - setup do - InviteCodeValidator.any_instance.stubs(:validate) - @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_login_required - get :show, :id => @customer.braintree_customer_id - assert_login_required - get :edit, :id => @customer.braintree_customer_id - assert_login_required - 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 index 90b7582..e015a07 100644 --- a/engines/billing/test/functional/payments_controller_test.rb +++ b/engines/billing/test/functional/payments_controller_test.rb @@ -4,47 +4,56 @@ 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 + def setup + FakeBraintree.activate! end - test "successful confirmation renders confirm" do - Braintree::TransparentRedirect.expects(:confirm).returns(success_response) - get :confirm - - assert_response :success - assert_template :confirm + def teardown + FakeBraintree.clear! end - test "failed confirmation renders new" do - Braintree::TransparentRedirect.expects(:confirm).returns(failure_response) - get :confirm + test "payment new" do + get :new + assert_not_nil assigns(:client_token) 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) + test "sucess confirmation" do + #already included with FakeBraintree + #Braintree::Transaction.sale.expects(:confirm).returns(success_response) + post :confirm, { + amount: "100", + payment_method_nonce: "fake-valid-nonce", + customer: { + first_name: "Test", + last_name: "Testing", + company: "RGSoC", + email: "any@email.com", + phone: "555-888-1234" } + } + + assert assigns(:result).success? + assert_not_nil flash[:success] end - def success_response - stub success?: true, - transaction: stub_transaction + test "failed confirmation renders new" do + FakeBraintree.decline_all_cards! + post :confirm, { + amount: "100", + payment_method_nonce: "fake-valid-nonce", + customer: { + first_name: "Test", + last_name: "Testing", + company: "RGSoC", + email: "any@email.com", + phone: "555-888-1234" } + } + + assert !assigns(:result).success? + assert_not_nil flash[:error] + FakeBraintree.clear! 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 index a6a1057..1e98eff 100644 --- a/engines/billing/test/functional/subscriptions_controller_test.rb +++ b/engines/billing/test/functional/subscriptions_controller_test.rb @@ -4,13 +4,78 @@ 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 + def setup + FakeBraintree.activate! end + + def teardown + FakeBraintree.clear! + end + + test "get all subscriptions when the user doesn't have an active subscription" do + user = find_record :user + login user + plans = [stub(:id => 1, :name => "First Plan", :price => 10), stub(:id => 2, :name => "Other Plan", :price => 30)] + Braintree::Plan.expects(:all).returns(plans) + + get :index + + assert assigns(:subscriptions) + assert_response :success + end + + test "get subscriptions when user has an active subscription" do + user = find_record :user + login user + plans = [stub(:id => 1, :name => "First Plan", :price => 10), stub(:id => 2, :name => "Other Plan", :price => 30)] + Braintree::Plan.expects(:all).returns(plans) + result = Braintree::Subscription.create(payment_method_token: 'user_token', plan_id: 1) + user.subscription_id = result.subscription.id + + get :index + + assert assigns(:subscription) + assert assigns(:plan) + assert_response :success + end + + test "subscriptions show" do + user = find_record :user + login user + plans = [stub(:id => "1", :name => "First Plan", :price => 10), stub(:id => "2", :name => "Other Plan", :price => 30)] + Braintree::Plan.expects(:all).returns(plans) + + get :show, :id => "1" + + assert assigns(:plan) + assert_response :success + end + + test "subscribe creates subscription" do + user = find_record :user + user.expects(:save).returns(true) + login user + payment_methods = [stub(:token => 'user_token')] + Braintree::Customer.any_instance.stubs(:payment_methods).returns(payment_methods) + user.expects(:save).returns(true) + + post :subscribe, :id => "1", :first_name => "Test", :last_name => "Testing", :company => "RGSoC", :email => "any@email.com", :phone => "555-888-1234" + + assert assigns(:result).success? + assert_not_nil flash[:success] + end + + test "unsubscribe cancels subscription" do + user = find_record :user + user.expects(:save).returns(true) + result = Braintree::Subscription.create(payment_method_token: 'user_token', plan_id: '1') + user.subscription_id = result.subscription.id + login user + + delete :unsubscribe, :id => "1" + + assert assigns(:result).success? + assert_not_nil flash[:success] + end + end diff --git a/engines/billing/test/test_helper.rb b/engines/billing/test/test_helper.rb index 57cdd63..14e453e 100644 --- a/engines/billing/test/test_helper.rb +++ b/engines/billing/test/test_helper.rb @@ -2,6 +2,7 @@ ENV["RAILS_ENV"] = "test" require "rails/test_help" +require 'mocha/setup' Rails.backtrace_cleaner.remove_silencers! diff --git a/engines/billing/test/unit/customer_test.rb b/engines/billing/test/unit/customer_test.rb deleted file mode 100644 index 6156f87..0000000 --- a/engines/billing/test/unit/customer_test.rb +++ /dev/null @@ -1,38 +0,0 @@ -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 deleted file mode 100644 index 0589a59..0000000 --- a/engines/billing/test/unit/customer_with_payment_info_test.rb +++ /dev/null @@ -1,40 +0,0 @@ -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 |