annotate test/functional/users_controller_test.rb @ 36:de76cd3e8c8e cc-branches

* Probably abortive experiments in extracting the branch from Hg
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 20 Oct 2010 10:07:29 +0100
parents 40f7cfd4df19
children 94944d00e43c
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require File.dirname(__FILE__) + '/../test_helper'
Chris@0 19 require 'users_controller'
Chris@0 20
Chris@0 21 # Re-raise errors caught by the controller.
Chris@0 22 class UsersController; def rescue_action(e) raise e end; end
Chris@0 23
Chris@0 24 class UsersControllerTest < ActionController::TestCase
Chris@0 25 include Redmine::I18n
Chris@0 26
Chris@0 27 fixtures :users, :projects, :members, :member_roles, :roles
Chris@0 28
Chris@0 29 def setup
Chris@0 30 @controller = UsersController.new
Chris@0 31 @request = ActionController::TestRequest.new
Chris@0 32 @response = ActionController::TestResponse.new
Chris@0 33 User.current = nil
Chris@0 34 @request.session[:user_id] = 1 # admin
Chris@0 35 end
Chris@0 36
Chris@0 37 def test_index
Chris@0 38 get :index
Chris@0 39 assert_response :success
Chris@0 40 assert_template 'index'
Chris@0 41 end
Chris@0 42
Chris@0 43 def test_index
Chris@0 44 get :index
Chris@0 45 assert_response :success
Chris@0 46 assert_template 'index'
Chris@0 47 assert_not_nil assigns(:users)
Chris@0 48 # active users only
Chris@0 49 assert_nil assigns(:users).detect {|u| !u.active?}
Chris@0 50 end
Chris@0 51
Chris@0 52 def test_index_with_name_filter
Chris@0 53 get :index, :name => 'john'
Chris@0 54 assert_response :success
Chris@0 55 assert_template 'index'
Chris@0 56 users = assigns(:users)
Chris@0 57 assert_not_nil users
Chris@0 58 assert_equal 1, users.size
Chris@0 59 assert_equal 'John', users.first.firstname
Chris@0 60 end
Chris@0 61
Chris@0 62 def test_show
Chris@0 63 @request.session[:user_id] = nil
Chris@0 64 get :show, :id => 2
Chris@0 65 assert_response :success
Chris@0 66 assert_template 'show'
Chris@0 67 assert_not_nil assigns(:user)
Chris@0 68 end
Chris@0 69
Chris@0 70 def test_show_should_not_fail_when_custom_values_are_nil
Chris@0 71 user = User.find(2)
Chris@0 72
Chris@0 73 # Create a custom field to illustrate the issue
Chris@0 74 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
Chris@0 75 custom_value = user.custom_values.build(:custom_field => custom_field).save!
Chris@0 76
Chris@0 77 get :show, :id => 2
Chris@0 78 assert_response :success
Chris@0 79 end
Chris@0 80
Chris@0 81 def test_show_inactive
Chris@0 82 @request.session[:user_id] = nil
Chris@0 83 get :show, :id => 5
Chris@0 84 assert_response 404
Chris@0 85 end
Chris@0 86
Chris@0 87 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
Chris@0 88 @request.session[:user_id] = nil
Chris@0 89 get :show, :id => 9
Chris@0 90 assert_response 404
Chris@0 91 end
Chris@0 92
Chris@0 93 def test_show_inactive_by_admin
Chris@0 94 @request.session[:user_id] = 1
Chris@0 95 get :show, :id => 5
Chris@0 96 assert_response 200
Chris@0 97 assert_not_nil assigns(:user)
Chris@0 98 end
Chris@14 99
Chris@14 100 def test_show_displays_memberships_based_on_project_visibility
Chris@14 101 @request.session[:user_id] = 1
Chris@14 102 get :show, :id => 2
Chris@14 103 assert_response :success
Chris@14 104 memberships = assigns(:memberships)
Chris@14 105 assert_not_nil memberships
Chris@14 106 project_ids = memberships.map(&:project_id)
Chris@14 107 assert project_ids.include?(2) #private project admin can see
Chris@14 108 end
Chris@0 109
Chris@0 110 def test_edit
Chris@0 111 ActionMailer::Base.deliveries.clear
Chris@0 112 post :edit, :id => 2, :user => {:firstname => 'Changed'}
Chris@0 113 assert_equal 'Changed', User.find(2).firstname
Chris@0 114 assert ActionMailer::Base.deliveries.empty?
Chris@0 115 end
Chris@0 116
Chris@0 117 def test_edit_with_activation_should_send_a_notification
Chris@0 118 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
Chris@0 119 u.login = 'foo'
Chris@0 120 u.status = User::STATUS_REGISTERED
Chris@0 121 u.save!
Chris@0 122 ActionMailer::Base.deliveries.clear
Chris@0 123 Setting.bcc_recipients = '1'
Chris@0 124
Chris@0 125 post :edit, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
Chris@0 126 assert u.reload.active?
Chris@0 127 mail = ActionMailer::Base.deliveries.last
Chris@0 128 assert_not_nil mail
Chris@0 129 assert_equal ['foo.bar@somenet.foo'], mail.bcc
Chris@0 130 assert mail.body.include?(ll('fr', :notice_account_activated))
Chris@0 131 end
Chris@0 132
Chris@0 133 def test_edit_with_password_change_should_send_a_notification
Chris@0 134 ActionMailer::Base.deliveries.clear
Chris@0 135 Setting.bcc_recipients = '1'
Chris@0 136
Chris@0 137 u = User.find(2)
Chris@0 138 post :edit, :id => u.id, :user => {}, :password => 'newpass', :password_confirmation => 'newpass', :send_information => '1'
Chris@0 139 assert_equal User.hash_password('newpass'), u.reload.hashed_password
Chris@0 140
Chris@0 141 mail = ActionMailer::Base.deliveries.last
Chris@0 142 assert_not_nil mail
Chris@0 143 assert_equal [u.mail], mail.bcc
Chris@0 144 assert mail.body.include?('newpass')
Chris@0 145 end
chris@22 146
chris@22 147 test "POST :edit with a password change to an AuthSource user switching to Internal authentication" do
chris@22 148 # Configure as auth source
chris@22 149 u = User.find(2)
chris@22 150 u.auth_source = AuthSource.find(1)
chris@22 151 u.save!
chris@22 152
chris@22 153 post :edit, :id => u.id, :user => {:auth_source_id => ''}, :password => 'newpass', :password_confirmation => 'newpass'
chris@22 154
chris@22 155 assert_equal nil, u.reload.auth_source
chris@22 156 assert_equal User.hash_password('newpass'), u.reload.hashed_password
chris@22 157 end
Chris@0 158
Chris@0 159 def test_edit_membership
Chris@0 160 post :edit_membership, :id => 2, :membership_id => 1,
Chris@0 161 :membership => { :role_ids => [2]}
Chris@0 162 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@0 163 assert_equal [2], Member.find(1).role_ids
Chris@0 164 end
Chris@0 165
Chris@0 166 def test_destroy_membership
Chris@0 167 post :destroy_membership, :id => 2, :membership_id => 1
Chris@0 168 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@0 169 assert_nil Member.find_by_id(1)
Chris@0 170 end
Chris@0 171 end