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@37: fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values 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@37: chris@37: assert_tag 'li', :content => /Phone number/ chris@37: end chris@37: chris@37: def test_show_should_not_display_hidden_custom_fields chris@37: @request.session[:user_id] = nil chris@37: UserCustomField.find_by_name('Phone number').update_attribute :visible, false chris@37: get :show, :id => 2 chris@37: assert_response :success chris@37: assert_template 'show' chris@37: assert_not_nil assigns(:user) chris@37: chris@37: assert_no_tag 'li', :content => /Phone number/ 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@37: context "GET :new" do chris@37: setup do chris@37: get :new chris@37: end chris@37: chris@37: should_assign_to :user chris@37: should_respond_with :success chris@37: should_render_template :new chris@37: end chris@37: chris@37: context "POST :create" do chris@37: context "when successful" do chris@37: setup do chris@37: post :create, :user => { chris@37: :firstname => 'John', chris@37: :lastname => 'Doe', chris@37: :login => 'jdoe', chris@37: :password => 'test', chris@37: :password_confirmation => 'test', chris@37: :mail => 'jdoe@gmail.com' chris@37: }, chris@37: :notification_option => 'none' chris@37: end chris@37: chris@37: should_assign_to :user chris@37: should_respond_with :redirect chris@37: should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}} chris@37: chris@37: should 'set the users mail notification' do chris@37: user = User.last chris@37: assert_equal 'none', user.mail_notification chris@37: end chris@37: end chris@37: chris@37: context "when unsuccessful" do chris@37: setup do chris@37: post :create, :user => {} chris@37: end chris@37: chris@37: should_assign_to :user chris@37: should_respond_with :success chris@37: should_render_template :new chris@37: end chris@37: chris@37: end chris@37: chris@37: def test_update Chris@0: ActionMailer::Base.deliveries.clear chris@37: put :update, :id => 2, :user => {:firstname => 'Changed'}, :notification_option => 'all', :pref => {:hide_mail => '1', :comments_sorting => 'desc'} chris@37: chris@37: user = User.find(2) chris@37: assert_equal 'Changed', user.firstname chris@37: assert_equal 'all', user.mail_notification chris@37: assert_equal true, user.pref[:hide_mail] chris@37: assert_equal 'desc', user.pref[:comments_sorting] Chris@0: assert ActionMailer::Base.deliveries.empty? Chris@0: end Chris@0: chris@37: def test_update_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@37: put :update, :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@37: def test_updat_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@37: put :update, :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@37: test "put :update 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@37: put :update, :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