annotate test/functional/users_controller_test.rb @ 878:30bfa805f6c8 feature_117

Close obsolete branch feature_117
author Chris Cannam
date Tue, 29 Mar 2011 17:41:07 +0100
parents 94944d00e43c
children af80e5618e9b
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@37 27 fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values
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@37 68
chris@37 69 assert_tag 'li', :content => /Phone number/
chris@37 70 end
chris@37 71
chris@37 72 def test_show_should_not_display_hidden_custom_fields
chris@37 73 @request.session[:user_id] = nil
chris@37 74 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
chris@37 75 get :show, :id => 2
chris@37 76 assert_response :success
chris@37 77 assert_template 'show'
chris@37 78 assert_not_nil assigns(:user)
chris@37 79
chris@37 80 assert_no_tag 'li', :content => /Phone number/
Chris@0 81 end
Chris@0 82
Chris@0 83 def test_show_should_not_fail_when_custom_values_are_nil
Chris@0 84 user = User.find(2)
Chris@0 85
Chris@0 86 # Create a custom field to illustrate the issue
Chris@0 87 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
Chris@0 88 custom_value = user.custom_values.build(:custom_field => custom_field).save!
Chris@0 89
Chris@0 90 get :show, :id => 2
Chris@0 91 assert_response :success
Chris@0 92 end
Chris@0 93
Chris@0 94 def test_show_inactive
Chris@0 95 @request.session[:user_id] = nil
Chris@0 96 get :show, :id => 5
Chris@0 97 assert_response 404
Chris@0 98 end
Chris@0 99
Chris@0 100 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
Chris@0 101 @request.session[:user_id] = nil
Chris@0 102 get :show, :id => 9
Chris@0 103 assert_response 404
Chris@0 104 end
Chris@0 105
Chris@0 106 def test_show_inactive_by_admin
Chris@0 107 @request.session[:user_id] = 1
Chris@0 108 get :show, :id => 5
Chris@0 109 assert_response 200
Chris@0 110 assert_not_nil assigns(:user)
Chris@0 111 end
Chris@14 112
Chris@14 113 def test_show_displays_memberships_based_on_project_visibility
Chris@14 114 @request.session[:user_id] = 1
Chris@14 115 get :show, :id => 2
Chris@14 116 assert_response :success
Chris@14 117 memberships = assigns(:memberships)
Chris@14 118 assert_not_nil memberships
Chris@14 119 project_ids = memberships.map(&:project_id)
Chris@14 120 assert project_ids.include?(2) #private project admin can see
Chris@14 121 end
Chris@0 122
chris@37 123 context "GET :new" do
chris@37 124 setup do
chris@37 125 get :new
chris@37 126 end
chris@37 127
chris@37 128 should_assign_to :user
chris@37 129 should_respond_with :success
chris@37 130 should_render_template :new
chris@37 131 end
chris@37 132
chris@37 133 context "POST :create" do
chris@37 134 context "when successful" do
chris@37 135 setup do
chris@37 136 post :create, :user => {
chris@37 137 :firstname => 'John',
chris@37 138 :lastname => 'Doe',
chris@37 139 :login => 'jdoe',
chris@37 140 :password => 'test',
chris@37 141 :password_confirmation => 'test',
chris@37 142 :mail => 'jdoe@gmail.com'
chris@37 143 },
chris@37 144 :notification_option => 'none'
chris@37 145 end
chris@37 146
chris@37 147 should_assign_to :user
chris@37 148 should_respond_with :redirect
chris@37 149 should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}}
chris@37 150
chris@37 151 should 'set the users mail notification' do
chris@37 152 user = User.last
chris@37 153 assert_equal 'none', user.mail_notification
chris@37 154 end
chris@37 155 end
chris@37 156
chris@37 157 context "when unsuccessful" do
chris@37 158 setup do
chris@37 159 post :create, :user => {}
chris@37 160 end
chris@37 161
chris@37 162 should_assign_to :user
chris@37 163 should_respond_with :success
chris@37 164 should_render_template :new
chris@37 165 end
chris@37 166
chris@37 167 end
chris@37 168
chris@37 169 def test_update
Chris@0 170 ActionMailer::Base.deliveries.clear
chris@37 171 put :update, :id => 2, :user => {:firstname => 'Changed'}, :notification_option => 'all', :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
chris@37 172
chris@37 173 user = User.find(2)
chris@37 174 assert_equal 'Changed', user.firstname
chris@37 175 assert_equal 'all', user.mail_notification
chris@37 176 assert_equal true, user.pref[:hide_mail]
chris@37 177 assert_equal 'desc', user.pref[:comments_sorting]
Chris@0 178 assert ActionMailer::Base.deliveries.empty?
Chris@0 179 end
Chris@0 180
chris@37 181 def test_update_with_activation_should_send_a_notification
Chris@0 182 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
Chris@0 183 u.login = 'foo'
Chris@0 184 u.status = User::STATUS_REGISTERED
Chris@0 185 u.save!
Chris@0 186 ActionMailer::Base.deliveries.clear
Chris@0 187 Setting.bcc_recipients = '1'
Chris@0 188
chris@37 189 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
Chris@0 190 assert u.reload.active?
Chris@0 191 mail = ActionMailer::Base.deliveries.last
Chris@0 192 assert_not_nil mail
Chris@0 193 assert_equal ['foo.bar@somenet.foo'], mail.bcc
Chris@0 194 assert mail.body.include?(ll('fr', :notice_account_activated))
Chris@0 195 end
Chris@0 196
chris@37 197 def test_updat_with_password_change_should_send_a_notification
Chris@0 198 ActionMailer::Base.deliveries.clear
Chris@0 199 Setting.bcc_recipients = '1'
Chris@0 200
Chris@0 201 u = User.find(2)
chris@37 202 put :update, :id => u.id, :user => {}, :password => 'newpass', :password_confirmation => 'newpass', :send_information => '1'
Chris@0 203 assert_equal User.hash_password('newpass'), u.reload.hashed_password
Chris@0 204
Chris@0 205 mail = ActionMailer::Base.deliveries.last
Chris@0 206 assert_not_nil mail
Chris@0 207 assert_equal [u.mail], mail.bcc
Chris@0 208 assert mail.body.include?('newpass')
Chris@0 209 end
chris@22 210
chris@37 211 test "put :update with a password change to an AuthSource user switching to Internal authentication" do
chris@22 212 # Configure as auth source
chris@22 213 u = User.find(2)
chris@22 214 u.auth_source = AuthSource.find(1)
chris@22 215 u.save!
chris@22 216
chris@37 217 put :update, :id => u.id, :user => {:auth_source_id => ''}, :password => 'newpass', :password_confirmation => 'newpass'
chris@22 218
chris@22 219 assert_equal nil, u.reload.auth_source
chris@22 220 assert_equal User.hash_password('newpass'), u.reload.hashed_password
chris@22 221 end
Chris@0 222
Chris@0 223 def test_edit_membership
Chris@0 224 post :edit_membership, :id => 2, :membership_id => 1,
Chris@0 225 :membership => { :role_ids => [2]}
Chris@0 226 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@0 227 assert_equal [2], Member.find(1).role_ids
Chris@0 228 end
Chris@0 229
Chris@0 230 def test_destroy_membership
Chris@0 231 post :destroy_membership, :id => 2, :membership_id => 1
Chris@0 232 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@0 233 assert_nil Member.find_by_id(1)
Chris@0 234 end
Chris@0 235 end