Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require File.expand_path('../../test_helper', __FILE__) Chris@909: require 'account_controller' Chris@909: Chris@909: # Re-raise errors caught by the controller. Chris@909: class AccountController; def rescue_action(e) raise e end; end Chris@909: Chris@909: class AccountControllerTest < ActionController::TestCase Chris@909: fixtures :users, :roles Chris@909: Chris@909: def setup Chris@909: @controller = AccountController.new Chris@909: @request = ActionController::TestRequest.new Chris@909: @response = ActionController::TestResponse.new Chris@909: User.current = nil Chris@909: end Chris@909: Chris@909: def test_login_should_redirect_to_back_url_param Chris@909: # request.uri is "test.host" in test environment Chris@909: post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1' Chris@909: assert_redirected_to '/issues/show/1' Chris@909: end Chris@909: Chris@909: def test_login_should_not_redirect_to_another_host Chris@909: post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' Chris@909: assert_redirected_to '/my/page' Chris@909: end Chris@909: Chris@909: def test_login_with_wrong_password Chris@909: post :login, :username => 'admin', :password => 'bad' Chris@909: assert_response :success Chris@909: assert_template 'login' Chris@909: assert_tag 'div', Chris@909: :attributes => { :class => "flash error" }, Chris@909: :content => /Invalid user or password/ Chris@909: end Chris@909: Chris@909: if Object.const_defined?(:OpenID) Chris@909: Chris@909: def test_login_with_openid_for_existing_user Chris@909: Setting.self_registration = '3' Chris@909: Setting.openid = '1' Chris@909: existing_user = User.new(:firstname => 'Cool', Chris@909: :lastname => 'User', Chris@909: :mail => 'user@somedomain.com', Chris@909: :identity_url => 'http://openid.example.com/good_user') Chris@909: existing_user.login = 'cool_user' Chris@909: assert existing_user.save! Chris@909: Chris@909: post :login, :openid_url => existing_user.identity_url Chris@909: assert_redirected_to '/my/page' Chris@909: end Chris@909: Chris@909: def test_login_with_invalid_openid_provider Chris@909: Setting.self_registration = '0' Chris@909: Setting.openid = '1' Chris@909: post :login, :openid_url => 'http;//openid.example.com/good_user' Chris@909: assert_redirected_to home_url Chris@909: end Chris@909: Chris@909: def test_login_with_openid_for_existing_non_active_user Chris@909: Setting.self_registration = '2' Chris@909: Setting.openid = '1' Chris@909: existing_user = User.new(:firstname => 'Cool', Chris@909: :lastname => 'User', Chris@909: :mail => 'user@somedomain.com', Chris@909: :identity_url => 'http://openid.example.com/good_user', Chris@909: :status => User::STATUS_REGISTERED) Chris@909: existing_user.login = 'cool_user' Chris@909: assert existing_user.save! Chris@909: Chris@909: post :login, :openid_url => existing_user.identity_url Chris@909: assert_redirected_to '/login' Chris@909: end Chris@909: Chris@909: def test_login_with_openid_with_new_user_created Chris@909: Setting.self_registration = '3' Chris@909: Setting.openid = '1' Chris@909: post :login, :openid_url => 'http://openid.example.com/good_user' Chris@909: assert_redirected_to '/my/account' Chris@909: user = User.find_by_login('cool_user') Chris@909: assert user Chris@909: assert_equal 'Cool', user.firstname Chris@909: assert_equal 'User', user.lastname Chris@909: end Chris@909: Chris@909: def test_login_with_openid_with_new_user_and_self_registration_off Chris@909: Setting.self_registration = '0' Chris@909: Setting.openid = '1' Chris@909: post :login, :openid_url => 'http://openid.example.com/good_user' Chris@909: assert_redirected_to home_url Chris@909: user = User.find_by_login('cool_user') Chris@909: assert ! user Chris@909: end Chris@909: Chris@909: def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token Chris@909: Setting.self_registration = '1' Chris@909: Setting.openid = '1' Chris@909: post :login, :openid_url => 'http://openid.example.com/good_user' Chris@909: assert_redirected_to '/login' Chris@909: user = User.find_by_login('cool_user') Chris@909: assert user Chris@909: Chris@909: token = Token.find_by_user_id_and_action(user.id, 'register') Chris@909: assert token Chris@909: end Chris@909: Chris@909: def test_login_with_openid_with_new_user_created_with_manual_activation Chris@909: Setting.self_registration = '2' Chris@909: Setting.openid = '1' Chris@909: post :login, :openid_url => 'http://openid.example.com/good_user' Chris@909: assert_redirected_to '/login' Chris@909: user = User.find_by_login('cool_user') Chris@909: assert user Chris@909: assert_equal User::STATUS_REGISTERED, user.status Chris@909: end Chris@909: Chris@909: def test_login_with_openid_with_new_user_with_conflict_should_register Chris@909: Setting.self_registration = '3' Chris@909: Setting.openid = '1' Chris@909: existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com') Chris@909: existing_user.login = 'cool_user' Chris@909: assert existing_user.save! Chris@909: Chris@909: post :login, :openid_url => 'http://openid.example.com/good_user' Chris@909: assert_response :success Chris@909: assert_template 'register' Chris@909: assert assigns(:user) Chris@909: assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url] Chris@909: end Chris@909: Chris@909: def test_setting_openid_should_return_true_when_set_to_true Chris@909: Setting.openid = '1' Chris@909: assert_equal true, Setting.openid? Chris@909: end Chris@909: Chris@909: else Chris@909: puts "Skipping openid tests." Chris@909: end Chris@909: Chris@909: def test_logout Chris@909: @request.session[:user_id] = 2 Chris@909: get :logout Chris@909: assert_redirected_to '/' Chris@909: assert_nil @request.session[:user_id] Chris@909: end Chris@909: Chris@909: context "GET #register" do Chris@909: context "with self registration on" do Chris@909: setup do Chris@909: Setting.self_registration = '3' Chris@909: get :register Chris@909: end Chris@909: Chris@909: should_respond_with :success Chris@909: should_render_template :register Chris@909: should_assign_to :user Chris@909: end Chris@909: Chris@909: context "with self registration off" do Chris@909: setup do Chris@909: Setting.self_registration = '0' Chris@909: get :register Chris@909: end Chris@909: Chris@909: should_redirect_to('/') { home_url } Chris@909: end Chris@909: end Chris@909: Chris@909: # See integration/account_test.rb for the full test Chris@909: context "POST #register" do Chris@909: context "with self registration on automatic" do Chris@909: setup do Chris@909: Setting.self_registration = '3' Chris@909: post :register, :user => { Chris@909: :login => 'register', Chris@909: :password => 'test', Chris@909: :password_confirmation => 'test', Chris@909: :firstname => 'John', Chris@909: :lastname => 'Doe', Chris@909: :mail => 'register@example.com' Chris@909: } Chris@909: end Chris@909: Chris@909: should_respond_with :redirect Chris@909: should_assign_to :user Chris@909: should_redirect_to('my page') { {:controller => 'my', :action => 'account'} } Chris@909: Chris@909: should_create_a_new_user { User.last(:conditions => {:login => 'register'}) } Chris@909: Chris@909: should 'set the user status to active' do Chris@909: user = User.last(:conditions => {:login => 'register'}) Chris@909: assert user Chris@909: assert_equal User::STATUS_ACTIVE, user.status Chris@909: end Chris@909: end Chris@909: Chris@909: context "with self registration off" do Chris@909: setup do Chris@909: Setting.self_registration = '0' Chris@909: post :register Chris@909: end Chris@909: Chris@909: should_redirect_to('/') { home_url } Chris@909: end Chris@909: end Chris@909: end