From b8cf64edc4f7e77075b1931a13ce4c4cd7c21621 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 9 Sep 2013 14:52:31 -0700 Subject: First run at having admins cancel user subscriptions. --- billing/app/controllers/billing_base_controller.rb | 2 +- billing/app/controllers/subscriptions_controller.rb | 2 +- billing/app/views/subscriptions/show.html.haml | 3 +-- billing/config/routes.rb | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) (limited to 'billing') diff --git a/billing/app/controllers/billing_base_controller.rb b/billing/app/controllers/billing_base_controller.rb index c250831..0453677 100644 --- a/billing/app/controllers/billing_base_controller.rb +++ b/billing/app/controllers/billing_base_controller.rb @@ -7,7 +7,7 @@ class BillingBaseController < ApplicationController def assign_user if params[:user_id] @user = User.find(params[:user_id]) - elsif params[:action] == "confirm" or params[:action] == "destroy" # confirms and subscription deletes will come back with different ID set, so check for this first + elsif params[:action] == "confirm"# confirms will come back with different ID set, so check for this first # This is only for cases where an admin cannot apply action for customer, but should be all confirms @user = current_user elsif params[:id] diff --git a/billing/app/controllers/subscriptions_controller.rb b/billing/app/controllers/subscriptions_controller.rb index 4047847..4b1851a 100644 --- a/billing/app/controllers/subscriptions_controller.rb +++ b/billing/app/controllers/subscriptions_controller.rb @@ -3,7 +3,7 @@ class SubscriptionsController < BillingBaseController before_filter :fetch_subscription, :only => [:show, :destroy] before_filter :confirm_no_active_subscription, :only => [:new, :create] # for now, admins cannot create or destroy subscriptions for others: - before_filter :confirm_self, :only => [:destroy, :new, :create] + before_filter :confirm_self, :only => [:new, :create] def new # don't show link to subscribe if they are already subscribed? diff --git a/billing/app/views/subscriptions/show.html.haml b/billing/app/views/subscriptions/show.html.haml index ebb7e0d..39f4d1a 100644 --- a/billing/app/views/subscriptions/show.html.haml +++ b/billing/app/views/subscriptions/show.html.haml @@ -3,5 +3,4 @@ Current Subscription = render :partial => "subscription_details", :locals => {:subscription => @subscription} -- if @user == current_user - = link_to t(:cancel_subscription), subscription_path(@subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if @subscription.status == 'Active' # permission check or should that just be on show? += link_to t(:cancel_subscription), user_subscription_path(@user, @subscription.id), :confirm => t(:are_you_sure), :method => :delete, :class => 'btn btn-danger' if @subscription.status == 'Active' # permission check or should that just be on show? diff --git a/billing/config/routes.rb b/billing/config/routes.rb index 8b7b5bf..e024f43 100644 --- a/billing/config/routes.rb +++ b/billing/config/routes.rb @@ -4,7 +4,7 @@ Rails.application.routes.draw do match 'payments/confirm' => 'payments#confirm', :as => :confirm_payment resources :users do resources :payments, :only => [:index] - resources :subscriptions, :only => [:index, :show] + resources :subscriptions, :only => [:index, :show, :destroy] end resources :customer, :only => [:new, :edit] @@ -14,7 +14,7 @@ Rails.application.routes.draw do match 'customer/show/:id' => 'customer#show', :as => :show_customer match 'credit_card_info/confirm' => 'credit_card_info#confirm', :as => :confirm_credit_card_info - resources :subscriptions, :only => [:new, :create, :update, :destroy] # index and show are within users path + resources :subscriptions, :only => [:new, :create, :update] # index, show & destroy are within users path #match 'transactions/:product_id/new' => 'transactions#new', :as => :new_transaction #match 'transactions/confirm/:product_id' => 'transactions#confirm', :as => :confirm_transaction -- cgit v1.2.3 From e9ddb2a47678eb3d4298e0f1de044a6846f4c696 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 17 Sep 2013 10:57:03 -0700 Subject: This doesn't actually run any tests, but at least includes what we might want to test regarding subscriptions. --- billing/test/integration/subscription_test.rb | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 billing/test/integration/subscription_test.rb (limited to 'billing') diff --git a/billing/test/integration/subscription_test.rb b/billing/test/integration/subscription_test.rb new file mode 100644 index 0000000..7410118 --- /dev/null +++ b/billing/test/integration/subscription_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' +require 'fake_braintree' +require 'capybara/rails' + +class SubscriptionTest < ActionDispatch::IntegrationTest + include Warden::Test::Helpers + include Capybara::DSL + + 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 + 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" + login_as @admin + #visit user_subscriptions_path(@customer.user_id) + #delete :destroy, :id => @subscription.id + 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 -- cgit v1.2.3 From d9c088fb288783f6afca441d870003c0f288a532 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 18 Sep 2013 10:30:01 +0200 Subject: failing tests for subscriptions functional: test canceling a subscription integration: investigate the issue with creating a subscription --- billing/test/functional/subsciptions_controller_test.rb | 17 +++++++++++++++++ billing/test/integration/subscription_test.rb | 7 +++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 billing/test/functional/subsciptions_controller_test.rb (limited to 'billing') diff --git a/billing/test/functional/subsciptions_controller_test.rb b/billing/test/functional/subsciptions_controller_test.rb new file mode 100644 index 0000000..bf8cf85 --- /dev/null +++ b/billing/test/functional/subsciptions_controller_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' +require 'fake_braintree' + +class SubscriptionsControllerTest < ActionController::TestCase + + test "destroy cancels subscription" 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) + 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..c53131e 100644 --- a/billing/test/integration/subscription_test.rb +++ b/billing/test/integration/subscription_test.rb @@ -13,8 +13,6 @@ class SubscriptionTest < ActionDispatch::IntegrationTest @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 end teardown do @@ -23,6 +21,11 @@ class SubscriptionTest < ActionDispatch::IntegrationTest @customer.destroy end + test "can create subscription" do + @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}) + assert @braintree_customer.credit_cards.first.subscriptions.present? + end + test "admin can cancel subscription for another" do skip "not sure about testing admin cancelling subscription with fake_braintree" login_as @admin -- cgit v1.2.3 From 18a00ade1a88887b9a99c72ed2054fa2012ae2dc Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 18 Sep 2013 12:03:41 +0200 Subject: test subscription owner via credit card not transactions There might not have been any transactions. Fixes the functional test --- billing/app/controllers/subscriptions_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'billing') 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) -- cgit v1.2.3 From 0c687f4ad5193bbf5b8429a47140e45db408fc80 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 18 Sep 2013 12:13:48 +0200 Subject: refactor: stub_customer test helper --- .../test/functional/customer_controller_test.rb | 23 +++++++++------------- .../test/functional/payments_controller_test.rb | 4 +--- .../functional/subsciptions_controller_test.rb | 5 ++--- billing/test/support/customer_test_helper.rb | 9 +++++++++ 4 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 billing/test/support/customer_test_helper.rb (limited to 'billing') 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..dd742ba 100644 --- a/billing/test/functional/payments_controller_test.rb +++ b/billing/test/functional/payments_controller_test.rb @@ -17,9 +17,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 index bf8cf85..a6a1057 100644 --- a/billing/test/functional/subsciptions_controller_test.rb +++ b/billing/test/functional/subsciptions_controller_test.rb @@ -2,11 +2,10 @@ require 'test_helper' require 'fake_braintree' class SubscriptionsControllerTest < ActionController::TestCase + include CustomerTestHelper test "destroy cancels subscription" 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 result = Braintree::Subscription.create plan_id: 'my_plan', payment_method_token: customer.braintree_customer.credit_cards.first.token diff --git a/billing/test/support/customer_test_helper.rb b/billing/test/support/customer_test_helper.rb new file mode 100644 index 0000000..ad72edd --- /dev/null +++ b/billing/test/support/customer_test_helper.rb @@ -0,0 +1,9 @@ +module CustomerTestHelper + + def stub_customer(user = nil) + user ||= find_record :user + customer = stub_record :customer_with_payment_info, user: user + Customer.stubs(:find_by_user_id).with(user.id).returns(customer) + return customer + end +end -- cgit v1.2.3 From c934787aa13977c3bb13155745f51ea71728d175 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 18 Sep 2013 12:47:42 +0200 Subject: working integration test --- billing/test/integration/subscription_test.rb | 31 +++++++++++++-------------- billing/test/support/customer_test_helper.rb | 4 +++- 2 files changed, 18 insertions(+), 17 deletions(-) (limited to 'billing') diff --git a/billing/test/integration/subscription_test.rb b/billing/test/integration/subscription_test.rb index c53131e..b893896 100644 --- a/billing/test/integration/subscription_test.rb +++ b/billing/test/integration/subscription_test.rb @@ -5,32 +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 + @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 "can create subscription" do - @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}) - assert @braintree_customer.credit_cards.first.subscriptions.present? - 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 index ad72edd..adac00a 100644 --- a/billing/test/support/customer_test_helper.rb +++ b/billing/test/support/customer_test_helper.rb @@ -2,7 +2,9 @@ module CustomerTestHelper def stub_customer(user = nil) user ||= find_record :user - customer = stub_record :customer_with_payment_info, user: 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 -- cgit v1.2.3 From 7398048b0fa261cd3178cb5309858eb845d01c77 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 18 Sep 2013 13:58:01 +0200 Subject: minor: include missing CustomerTestHelper --- billing/test/functional/payments_controller_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'billing') diff --git a/billing/test/functional/payments_controller_test.rb b/billing/test/functional/payments_controller_test.rb index dd742ba..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 -- cgit v1.2.3