Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: require File.dirname(__FILE__) + '/../test_helper' Chris@0: require 'users_controller' Chris@0: Chris@0: # Re-raise errors caught by the controller. Chris@0: class UsersController; def rescue_action(e) raise e end; end Chris@0: Chris@0: class UsersControllerTest < ActionController::TestCase Chris@0: include Redmine::I18n Chris@0: Chris@0: fixtures :users, :projects, :members, :member_roles, :roles Chris@0: Chris@0: def setup Chris@0: @controller = UsersController.new Chris@0: @request = ActionController::TestRequest.new Chris@0: @response = ActionController::TestResponse.new Chris@0: User.current = nil Chris@0: @request.session[:user_id] = 1 # admin Chris@0: end Chris@0: Chris@0: def test_index Chris@0: get :index Chris@0: assert_response :success Chris@0: assert_template 'index' Chris@0: end Chris@0: Chris@0: def test_index Chris@0: get :index Chris@0: assert_response :success Chris@0: assert_template 'index' Chris@0: assert_not_nil assigns(:users) Chris@0: # active users only Chris@0: assert_nil assigns(:users).detect {|u| !u.active?} Chris@0: end Chris@0: Chris@0: def test_index_with_name_filter Chris@0: get :index, :name => 'john' Chris@0: assert_response :success Chris@0: assert_template 'index' Chris@0: users = assigns(:users) Chris@0: assert_not_nil users Chris@0: assert_equal 1, users.size Chris@0: assert_equal 'John', users.first.firstname Chris@0: end Chris@0: Chris@0: def test_show Chris@0: @request.session[:user_id] = nil Chris@0: get :show, :id => 2 Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: assert_not_nil assigns(:user) Chris@0: end Chris@0: Chris@0: def test_show_should_not_fail_when_custom_values_are_nil Chris@0: user = User.find(2) Chris@0: Chris@0: # Create a custom field to illustrate the issue Chris@0: custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text') Chris@0: custom_value = user.custom_values.build(:custom_field => custom_field).save! Chris@0: Chris@0: get :show, :id => 2 Chris@0: assert_response :success Chris@0: end Chris@0: Chris@0: def test_show_inactive Chris@0: @request.session[:user_id] = nil Chris@0: get :show, :id => 5 Chris@0: assert_response 404 Chris@0: end Chris@0: Chris@0: def test_show_should_not_reveal_users_with_no_visible_activity_or_project Chris@0: @request.session[:user_id] = nil Chris@0: get :show, :id => 9 Chris@0: assert_response 404 Chris@0: end Chris@0: Chris@0: def test_show_inactive_by_admin Chris@0: @request.session[:user_id] = 1 Chris@0: get :show, :id => 5 Chris@0: assert_response 200 Chris@0: assert_not_nil assigns(:user) Chris@0: end Chris@14: Chris@14: def test_show_displays_memberships_based_on_project_visibility Chris@14: @request.session[:user_id] = 1 Chris@14: get :show, :id => 2 Chris@14: assert_response :success Chris@14: memberships = assigns(:memberships) Chris@14: assert_not_nil memberships Chris@14: project_ids = memberships.map(&:project_id) Chris@14: assert project_ids.include?(2) #private project admin can see Chris@14: end Chris@0: Chris@0: def test_edit Chris@0: ActionMailer::Base.deliveries.clear Chris@0: post :edit, :id => 2, :user => {:firstname => 'Changed'} Chris@0: assert_equal 'Changed', User.find(2).firstname Chris@0: assert ActionMailer::Base.deliveries.empty? Chris@0: end Chris@0: Chris@0: def test_edit_with_activation_should_send_a_notification Chris@0: u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr') Chris@0: u.login = 'foo' Chris@0: u.status = User::STATUS_REGISTERED Chris@0: u.save! Chris@0: ActionMailer::Base.deliveries.clear Chris@0: Setting.bcc_recipients = '1' Chris@0: Chris@0: post :edit, :id => u.id, :user => {:status => User::STATUS_ACTIVE} Chris@0: assert u.reload.active? Chris@0: mail = ActionMailer::Base.deliveries.last Chris@0: assert_not_nil mail Chris@0: assert_equal ['foo.bar@somenet.foo'], mail.bcc Chris@0: assert mail.body.include?(ll('fr', :notice_account_activated)) Chris@0: end Chris@0: Chris@0: def test_edit_with_password_change_should_send_a_notification Chris@0: ActionMailer::Base.deliveries.clear Chris@0: Setting.bcc_recipients = '1' Chris@0: Chris@0: u = User.find(2) Chris@0: post :edit, :id => u.id, :user => {}, :password => 'newpass', :password_confirmation => 'newpass', :send_information => '1' Chris@0: assert_equal User.hash_password('newpass'), u.reload.hashed_password Chris@0: Chris@0: mail = ActionMailer::Base.deliveries.last Chris@0: assert_not_nil mail Chris@0: assert_equal [u.mail], mail.bcc Chris@0: assert mail.body.include?('newpass') Chris@0: end chris@22: chris@22: test "POST :edit with a password change to an AuthSource user switching to Internal authentication" do chris@22: # Configure as auth source chris@22: u = User.find(2) chris@22: u.auth_source = AuthSource.find(1) chris@22: u.save! chris@22: chris@22: post :edit, :id => u.id, :user => {:auth_source_id => ''}, :password => 'newpass', :password_confirmation => 'newpass' chris@22: chris@22: assert_equal nil, u.reload.auth_source chris@22: assert_equal User.hash_password('newpass'), u.reload.hashed_password chris@22: end Chris@0: Chris@0: def test_edit_membership Chris@0: post :edit_membership, :id => 2, :membership_id => 1, Chris@0: :membership => { :role_ids => [2]} Chris@0: assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' Chris@0: assert_equal [2], Member.find(1).role_ids Chris@0: end Chris@0: Chris@0: def test_destroy_membership Chris@0: post :destroy_membership, :id => 2, :membership_id => 1 Chris@0: assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' Chris@0: assert_nil Member.find_by_id(1) Chris@0: end Chris@0: end