diff --git a/.gitignore b/.gitignore index 63f5dc4..b7a6679 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,7 @@ doc # jeweler generated pkg -# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: +# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: # # * Create a file at ~/.gitignore # * Include files you want ignored @@ -50,6 +50,7 @@ pkg # For Project test/rails_app/log/ test/rails_app/db/*.sqlite3 +test/rails_app/tmp/ Gemfile.lock .ruby-version .ruby-gemset diff --git a/app/controllers/devise/checkga_controller.rb b/app/controllers/devise/checkga_controller.rb index 56f1165..7d115b0 100644 --- a/app/controllers/devise/checkga_controller.rb +++ b/app/controllers/devise/checkga_controller.rb @@ -24,7 +24,7 @@ def update warden.manager._run_callbacks(:after_set_user, resource, warden, {:event => :authentication}) respond_with resource, :location => after_sign_in_path_for(resource) - if not resource.class.ga_remembertime.nil? + if not resource.class.ga_remembertime.nil? cookies.signed[:gauth] = { :value => resource.email << "," << Time.now.to_i.to_s, :secure => !(Rails.env.test? || Rails.env.development?), @@ -33,18 +33,22 @@ def update end else set_flash_message(:error, :error) - redirect_to :root + redirect_to redirect_on_error_url end else set_flash_message(:error, :error) - redirect_to :root + redirect_to redirect_on_error_url end end + def redirect_on_error_url + :root + end + private def devise_resource self.resource = resource_class.new end -end \ No newline at end of file +end diff --git a/test/integration/gauth_test.rb b/test/integration/gauth_test.rb index 92bc477..5f6a3a2 100644 --- a/test/integration/gauth_test.rb +++ b/test/integration/gauth_test.rb @@ -83,6 +83,22 @@ def teardown end + test 'if resource is nil redirects back to custom url' do + User.stubs(:find_by_gauth_tmp).returns(nil) + Devise::CheckgaController.any_instance.stubs(:redirect_on_error_url).returns('/foo') + testuser = create_full_user + + visit new_user_session_path + fill_in 'user_email', :with => 'fulluser@test.com' + fill_in 'user_password', :with => '123456' + click_button 'Log in' + + fill_in 'user_gauth_token', :with => ROTP::TOTP.new(testuser.get_qr).at(Time.now) + click_button 'Check Token' + assert_equal foo_path, current_path + Capybara.reset_sessions! + end + test 'fail token authentication' do create_and_signin_gauth_user fill_in 'user_gauth_token', :with => '1' @@ -92,6 +108,20 @@ def teardown Capybara.reset_sessions! end + test 'fail token authentication redirects back to custom url' do + Devise::CheckgaController.any_instance.stubs(:redirect_on_error_url).returns('/foo') + create_full_user + visit new_user_session_path + fill_in 'user_email', :with => 'fulluser@test.com' + fill_in 'user_password', :with => '123456' + click_button 'Log in' + + fill_in 'user_gauth_token', :with => "wrong token" + click_button 'Check Token' + assert_equal foo_path, current_path + Capybara.reset_sessions! + end + test 'successfull token authentication' do testuser = User.find_by_email("fulluser@test.com") visit new_user_session_path @@ -179,4 +209,4 @@ def teardown Timecop.return end -end \ No newline at end of file +end diff --git a/test/integration_tests_helper.rb b/test/integration_tests_helper.rb index d13671d..d9f8aa9 100644 --- a/test/integration_tests_helper.rb +++ b/test/integration_tests_helper.rb @@ -3,7 +3,7 @@ class ActionController::IntegrationTest def warden request.env['warden'] end - + def create_full_user @@user ||= begin user = User.create!( diff --git a/test/rails_app/app/controllers/foo_controller.rb b/test/rails_app/app/controllers/foo_controller.rb new file mode 100644 index 0000000..f71673f --- /dev/null +++ b/test/rails_app/app/controllers/foo_controller.rb @@ -0,0 +1,7 @@ +class FooController < ApplicationController + skip_before_filter :authenticate_user! + + def index + render :nothing => true + end +end diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index 907831d..0c3475a 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -2,5 +2,6 @@ devise_for :users resources :posts + get 'foo' => 'foo#index' root :to => 'posts#index' end diff --git a/test/test_helper.rb b/test/test_helper.rb index 435dc32..f1a82c9 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,6 +10,9 @@ require 'capybara/rails' require 'timecop' +require "mocha/integration/test_unit" +Mocha::Integration::TestUnit.activate + I18n.load_path << File.expand_path("../support/locale/en.yml", __FILE__) if DEVISE_ORM == :mongoid ActiveSupport::Deprecation.silenced = true