diff options
author | jessib <jessib@riseup.net> | 2013-09-19 12:02:36 -0700 |
---|---|---|
committer | jessib <jessib@riseup.net> | 2013-09-19 12:02:36 -0700 |
commit | 890c9e170fc038eccb46eca3c1ddcf6f05eaa53f (patch) | |
tree | 2b1ab7c9e89f2aaae379938430f40e5b3babfbbf | |
parent | 16b8966a0df0104390ce95e35f1bb17f173f8d4d (diff) | |
parent | 7398048b0fa261cd3178cb5309858eb845d01c77 (diff) |
Merge pull request #84 from azul/test/billing-sessions0.2.3
Test/billing subscriptions
-rw-r--r-- | billing/app/controllers/subscriptions_controller.rb | 3 | ||||
-rw-r--r-- | billing/test/functional/customer_controller_test.rb | 23 | ||||
-rw-r--r-- | billing/test/functional/payments_controller_test.rb | 5 | ||||
-rw-r--r-- | billing/test/functional/subsciptions_controller_test.rb | 16 | ||||
-rw-r--r-- | billing/test/integration/subscription_test.rb | 28 | ||||
-rw-r--r-- | billing/test/support/customer_test_helper.rb | 11 |
6 files changed, 55 insertions, 31 deletions
diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 4b1851a..7689f35 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -31,7 +31,8 @@ class SubscriptionsController < BillingBaseController def fetch_subscription @subscription = Braintree::Subscription.find params[:id] - @subscription_customer_id = @subscription.transactions.first.customer_details.id #all of subscriptions transactions should have same customer + @credit_card = Braintree::CreditCard.find @subscription.payment_method_token + @subscription_customer_id = @credit_card.customer_id current_user_customer = Customer.find_by_user_id(current_user.id) access_denied unless admin? or (current_user_customer and current_user_customer.braintree_customer_id == @subscription_customer_id) diff --git a/billing/test/functional/customer_controller_test.rb b/billing/test/functional/customer_controller_test.rb index 878ed48..d943e23 100644 --- a/billing/test/functional/customer_controller_test.rb +++ b/billing/test/functional/customer_controller_test.rb @@ -2,6 +2,7 @@ require 'test_helper' require 'fake_braintree' class CustomerControllerTest < ActionController::TestCase + include CustomerTestHelper test "new assigns redirect url" do login @@ -21,9 +22,7 @@ class CustomerControllerTest < ActionController::TestCase end test "edit uses params[:id]" do - user = find_record :user - customer = stub_record :customer_with_payment_info, user: user - Customer.stubs(:find_by_user_id).with(user.id).returns(customer) + customer = stub_customer login customer.user get :edit, id: customer.user.id @@ -34,7 +33,7 @@ class CustomerControllerTest < ActionController::TestCase assert_equal confirm_customer_url, tr_data[:redirect_url] end - test "confirm user creation" do + test "confirm customer creation" do login Braintree::TransparentRedirect.expects(:confirm).returns(success_response) # to_confirm = prepare_confirmation :create_customer_data, @@ -52,10 +51,8 @@ class CustomerControllerTest < ActionController::TestCase end test "customer update" do - user = find_record :user - customer = stub_record :customer_with_payment_info, user: user + customer = stub_customer customer.expects(:save) - Customer.stubs(:find_by_user_id).with(user.id).returns(customer) login customer.user Braintree::TransparentRedirect.expects(:confirm). returns(success_response(customer)) @@ -70,8 +67,8 @@ class CustomerControllerTest < ActionController::TestCase assert_equal customer.braintree_customer, result.customer end - test "failed user creation" do - skip "can't get user creation to fail" + 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 @@ -86,7 +83,7 @@ class CustomerControllerTest < ActionController::TestCase assert !result.success? end - test "failed user creation with stubbing" do + test "failed customer creation with stubbing" do login Braintree::TransparentRedirect.expects(:confirm).returns(failure_response) post :confirm, bla: :blub @@ -95,10 +92,8 @@ class CustomerControllerTest < ActionController::TestCase assert_template :new end - test "failed user update with stubbing" do - user = find_record :user - customer = stub_record :customer_with_payment_info, user: user - Customer.stubs(:find_by_user_id).with(user.id).returns(customer) + test "failed customer update with stubbing" do + customer = stub_customer login customer.user Braintree::TransparentRedirect.expects(:confirm).returns(failure_response) post :confirm, bla: :blub diff --git a/billing/test/functional/payments_controller_test.rb b/billing/test/functional/payments_controller_test.rb index 055a990..655aa16 100644 --- a/billing/test/functional/payments_controller_test.rb +++ b/billing/test/functional/payments_controller_test.rb @@ -2,6 +2,7 @@ require 'test_helper' require 'fake_braintree' class PaymentsControllerTest < ActionController::TestCase + include CustomerTestHelper test "payment when unauthorized" do get :new @@ -17,9 +18,7 @@ class PaymentsControllerTest < ActionController::TestCase end test "payment when authenticated as customer" do - user = find_record :user - customer = stub_record :customer_with_payment_info, user: user - Customer.stubs(:find_by_user_id).with(user.id).returns(customer) + customer = stub_customer login customer.user get :new assert_not_nil assigns(:tr_data) diff --git a/billing/test/functional/subsciptions_controller_test.rb b/billing/test/functional/subsciptions_controller_test.rb new file mode 100644 index 0000000..a6a1057 --- /dev/null +++ b/billing/test/functional/subsciptions_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/billing/test/integration/subscription_test.rb b/billing/test/integration/subscription_test.rb index 7410118..b893896 100644 --- a/billing/test/integration/subscription_test.rb +++ b/billing/test/integration/subscription_test.rb @@ -5,29 +5,31 @@ require 'capybara/rails' class SubscriptionTest < ActionDispatch::IntegrationTest include Warden::Test::Helpers include Capybara::DSL + include CustomerTestHelper + include StubRecordHelper setup do Warden.test_mode! - @admin = User.find_by_login('admin') || FactoryGirl.create(:user, login: 'admin') - @customer = FactoryGirl.create(:customer) - @braintree_customer = FactoryGirl.create(:braintree_customer) - @customer.braintree_customer_id = @braintree_customer.id - @customer.save - @subscription = FakeBraintree::Subscription.new({:payment_method_token => @braintree_customer.credit_cards.first, :plan_id => '5'}, {:id => @braintree_customer.id, :merchant_id => Braintree::Configuration.merchant_id}) - # unfortunately @braintree_customer.credit_cards.first.subscriptions still returns empty array + @admin = stub_record :user, :admin => true + @customer = stub_customer + @braintree_customer = @customer.braintree_customer + response = Braintree::Subscription.create plan_id: '5', + payment_method_token: @braintree_customer.credit_cards.first.token + @subscription = response.subscription + Capybara.current_driver = Capybara.javascript_driver end teardown do Warden.test_reset! - @admin.destroy - @customer.destroy end - test "admin can cancel subscription for another" do - skip "not sure about testing admin cancelling subscription with fake_braintree" + test "admin can see subscription for another" do login_as @admin - #visit user_subscriptions_path(@customer.user_id) - #delete :destroy, :id => @subscription.id + @customer.stubs(:subscriptions).returns([@subscription]) + visit user_subscriptions_path(@customer.user_id) + assert page.has_content?("Subscriptions") + assert page.has_content?("Status: Active") + page.save_screenshot('/tmp/subscriptions.png') end #test "admin cannot add subscription for another" do diff --git a/billing/test/support/customer_test_helper.rb b/billing/test/support/customer_test_helper.rb new file mode 100644 index 0000000..adac00a --- /dev/null +++ b/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 |