annotate .svn/pristine/85/85cb4b5b2aa4587d3f8e85f0dc40fcad929d49ad.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1296 19 require 'users_controller'
Chris@1296 20
Chris@1296 21 # Re-raise errors caught by the controller.
Chris@1296 22 class UsersController; def rescue_action(e) raise e end; end
Chris@1296 23
Chris@1296 24 class UsersControllerTest < ActionController::TestCase
Chris@1296 25 include Redmine::I18n
Chris@1296 26
Chris@1296 27 fixtures :users, :projects, :members, :member_roles, :roles,
Chris@1296 28 :custom_fields, :custom_values, :groups_users,
Chris@1296 29 :auth_sources
Chris@1296 30
Chris@1296 31 def setup
Chris@1296 32 @controller = UsersController.new
Chris@1296 33 @request = ActionController::TestRequest.new
Chris@1296 34 @response = ActionController::TestResponse.new
Chris@1296 35 User.current = nil
Chris@1296 36 @request.session[:user_id] = 1 # admin
Chris@1296 37 end
Chris@1296 38
Chris@1296 39 def test_index
Chris@1296 40 get :index
Chris@1296 41 assert_response :success
Chris@1296 42 assert_template 'index'
Chris@1296 43 end
Chris@1296 44
Chris@1296 45 def test_index
Chris@1296 46 get :index
Chris@1296 47 assert_response :success
Chris@1296 48 assert_template 'index'
Chris@1296 49 assert_not_nil assigns(:users)
Chris@1296 50 # active users only
Chris@1296 51 assert_nil assigns(:users).detect {|u| !u.active?}
Chris@1296 52 end
Chris@1296 53
Chris@1296 54 def test_index_with_status_filter
Chris@1296 55 get :index, :status => 3
Chris@1296 56 assert_response :success
Chris@1296 57 assert_template 'index'
Chris@1296 58 assert_not_nil assigns(:users)
Chris@1296 59 assert_equal [3], assigns(:users).map(&:status).uniq
Chris@1296 60 end
Chris@1296 61
Chris@1296 62 def test_index_with_name_filter
Chris@1296 63 get :index, :name => 'john'
Chris@1296 64 assert_response :success
Chris@1296 65 assert_template 'index'
Chris@1296 66 users = assigns(:users)
Chris@1296 67 assert_not_nil users
Chris@1296 68 assert_equal 1, users.size
Chris@1296 69 assert_equal 'John', users.first.firstname
Chris@1296 70 end
Chris@1296 71
Chris@1296 72 def test_index_with_group_filter
Chris@1296 73 get :index, :group_id => '10'
Chris@1296 74 assert_response :success
Chris@1296 75 assert_template 'index'
Chris@1296 76 users = assigns(:users)
Chris@1296 77 assert users.any?
Chris@1296 78 assert_equal([], (users - Group.find(10).users))
Chris@1296 79 assert_select 'select[name=group_id]' do
Chris@1296 80 assert_select 'option[value=10][selected=selected]'
Chris@1296 81 end
Chris@1296 82 end
Chris@1296 83
Chris@1296 84 def test_show
Chris@1296 85 @request.session[:user_id] = nil
Chris@1296 86 get :show, :id => 2
Chris@1296 87 assert_response :success
Chris@1296 88 assert_template 'show'
Chris@1296 89 assert_not_nil assigns(:user)
Chris@1296 90
Chris@1296 91 assert_tag 'li', :content => /Phone number/
Chris@1296 92 end
Chris@1296 93
Chris@1296 94 def test_show_should_not_display_hidden_custom_fields
Chris@1296 95 @request.session[:user_id] = nil
Chris@1296 96 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
Chris@1296 97 get :show, :id => 2
Chris@1296 98 assert_response :success
Chris@1296 99 assert_template 'show'
Chris@1296 100 assert_not_nil assigns(:user)
Chris@1296 101
Chris@1296 102 assert_no_tag 'li', :content => /Phone number/
Chris@1296 103 end
Chris@1296 104
Chris@1296 105 def test_show_should_not_fail_when_custom_values_are_nil
Chris@1296 106 user = User.find(2)
Chris@1296 107
Chris@1296 108 # Create a custom field to illustrate the issue
Chris@1296 109 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
Chris@1296 110 custom_value = user.custom_values.build(:custom_field => custom_field).save!
Chris@1296 111
Chris@1296 112 get :show, :id => 2
Chris@1296 113 assert_response :success
Chris@1296 114 end
Chris@1296 115
Chris@1296 116 def test_show_inactive
Chris@1296 117 @request.session[:user_id] = nil
Chris@1296 118 get :show, :id => 5
Chris@1296 119 assert_response 404
Chris@1296 120 end
Chris@1296 121
Chris@1296 122 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
Chris@1296 123 @request.session[:user_id] = nil
Chris@1296 124 get :show, :id => 9
Chris@1296 125 assert_response 404
Chris@1296 126 end
Chris@1296 127
Chris@1296 128 def test_show_inactive_by_admin
Chris@1296 129 @request.session[:user_id] = 1
Chris@1296 130 get :show, :id => 5
Chris@1296 131 assert_response 200
Chris@1296 132 assert_not_nil assigns(:user)
Chris@1296 133 end
Chris@1296 134
Chris@1296 135 def test_show_displays_memberships_based_on_project_visibility
Chris@1296 136 @request.session[:user_id] = 1
Chris@1296 137 get :show, :id => 2
Chris@1296 138 assert_response :success
Chris@1296 139 memberships = assigns(:memberships)
Chris@1296 140 assert_not_nil memberships
Chris@1296 141 project_ids = memberships.map(&:project_id)
Chris@1296 142 assert project_ids.include?(2) #private project admin can see
Chris@1296 143 end
Chris@1296 144
Chris@1296 145 def test_show_current_should_require_authentication
Chris@1296 146 @request.session[:user_id] = nil
Chris@1296 147 get :show, :id => 'current'
Chris@1296 148 assert_response 302
Chris@1296 149 end
Chris@1296 150
Chris@1296 151 def test_show_current
Chris@1296 152 @request.session[:user_id] = 2
Chris@1296 153 get :show, :id => 'current'
Chris@1296 154 assert_response :success
Chris@1296 155 assert_template 'show'
Chris@1296 156 assert_equal User.find(2), assigns(:user)
Chris@1296 157 end
Chris@1296 158
Chris@1296 159 def test_new
Chris@1296 160 get :new
Chris@1296 161 assert_response :success
Chris@1296 162 assert_template :new
Chris@1296 163 assert assigns(:user)
Chris@1296 164 end
Chris@1296 165
Chris@1296 166 def test_create
Chris@1296 167 Setting.bcc_recipients = '1'
Chris@1296 168
Chris@1296 169 assert_difference 'User.count' do
Chris@1296 170 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1296 171 post :create,
Chris@1296 172 :user => {
Chris@1296 173 :firstname => 'John',
Chris@1296 174 :lastname => 'Doe',
Chris@1296 175 :login => 'jdoe',
Chris@1296 176 :password => 'secret123',
Chris@1296 177 :password_confirmation => 'secret123',
Chris@1296 178 :mail => 'jdoe@gmail.com',
Chris@1296 179 :mail_notification => 'none'
Chris@1296 180 },
Chris@1296 181 :send_information => '1'
Chris@1296 182 end
Chris@1296 183 end
Chris@1296 184
Chris@1296 185 user = User.first(:order => 'id DESC')
Chris@1296 186 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
Chris@1296 187
Chris@1296 188 assert_equal 'John', user.firstname
Chris@1296 189 assert_equal 'Doe', user.lastname
Chris@1296 190 assert_equal 'jdoe', user.login
Chris@1296 191 assert_equal 'jdoe@gmail.com', user.mail
Chris@1296 192 assert_equal 'none', user.mail_notification
Chris@1296 193 assert user.check_password?('secret123')
Chris@1296 194
Chris@1296 195 mail = ActionMailer::Base.deliveries.last
Chris@1296 196 assert_not_nil mail
Chris@1296 197 assert_equal [user.mail], mail.bcc
Chris@1296 198 assert_mail_body_match 'secret', mail
Chris@1296 199 end
Chris@1296 200
Chris@1296 201 def test_create_with_preferences
Chris@1296 202 assert_difference 'User.count' do
Chris@1296 203 post :create,
Chris@1296 204 :user => {
Chris@1296 205 :firstname => 'John',
Chris@1296 206 :lastname => 'Doe',
Chris@1296 207 :login => 'jdoe',
Chris@1296 208 :password => 'secret123',
Chris@1296 209 :password_confirmation => 'secret123',
Chris@1296 210 :mail => 'jdoe@gmail.com',
Chris@1296 211 :mail_notification => 'none'
Chris@1296 212 },
Chris@1296 213 :pref => {
Chris@1296 214 'hide_mail' => '1',
Chris@1296 215 'time_zone' => 'Paris',
Chris@1296 216 'comments_sorting' => 'desc',
Chris@1296 217 'warn_on_leaving_unsaved' => '0'
Chris@1296 218 }
Chris@1296 219 end
Chris@1296 220 user = User.first(:order => 'id DESC')
Chris@1296 221 assert_equal 'jdoe', user.login
Chris@1296 222 assert_equal true, user.pref.hide_mail
Chris@1296 223 assert_equal 'Paris', user.pref.time_zone
Chris@1296 224 assert_equal 'desc', user.pref[:comments_sorting]
Chris@1296 225 assert_equal '0', user.pref[:warn_on_leaving_unsaved]
Chris@1296 226 end
Chris@1296 227
Chris@1296 228 def test_create_with_failure
Chris@1296 229 assert_no_difference 'User.count' do
Chris@1296 230 post :create, :user => {}
Chris@1296 231 end
Chris@1296 232 assert_response :success
Chris@1296 233 assert_template 'new'
Chris@1296 234 end
Chris@1296 235
Chris@1296 236 def test_edit
Chris@1296 237 get :edit, :id => 2
Chris@1296 238 assert_response :success
Chris@1296 239 assert_template 'edit'
Chris@1296 240 assert_equal User.find(2), assigns(:user)
Chris@1296 241 end
Chris@1296 242
Chris@1296 243 def test_update
Chris@1296 244 ActionMailer::Base.deliveries.clear
Chris@1296 245 put :update, :id => 2,
Chris@1296 246 :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'},
Chris@1296 247 :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
Chris@1296 248 user = User.find(2)
Chris@1296 249 assert_equal 'Changed', user.firstname
Chris@1296 250 assert_equal 'only_assigned', user.mail_notification
Chris@1296 251 assert_equal true, user.pref[:hide_mail]
Chris@1296 252 assert_equal 'desc', user.pref[:comments_sorting]
Chris@1296 253 assert ActionMailer::Base.deliveries.empty?
Chris@1296 254 end
Chris@1296 255
Chris@1296 256 def test_update_with_failure
Chris@1296 257 assert_no_difference 'User.count' do
Chris@1296 258 put :update, :id => 2, :user => {:firstname => ''}
Chris@1296 259 end
Chris@1296 260 assert_response :success
Chris@1296 261 assert_template 'edit'
Chris@1296 262 end
Chris@1296 263
Chris@1296 264 def test_update_with_group_ids_should_assign_groups
Chris@1296 265 put :update, :id => 2, :user => {:group_ids => ['10']}
Chris@1296 266 user = User.find(2)
Chris@1296 267 assert_equal [10], user.group_ids
Chris@1296 268 end
Chris@1296 269
Chris@1296 270 def test_update_with_activation_should_send_a_notification
Chris@1296 271 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
Chris@1296 272 u.login = 'foo'
Chris@1296 273 u.status = User::STATUS_REGISTERED
Chris@1296 274 u.save!
Chris@1296 275 ActionMailer::Base.deliveries.clear
Chris@1296 276 Setting.bcc_recipients = '1'
Chris@1296 277
Chris@1296 278 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
Chris@1296 279 assert u.reload.active?
Chris@1296 280 mail = ActionMailer::Base.deliveries.last
Chris@1296 281 assert_not_nil mail
Chris@1296 282 assert_equal ['foo.bar@somenet.foo'], mail.bcc
Chris@1296 283 assert_mail_body_match ll('fr', :notice_account_activated), mail
Chris@1296 284 end
Chris@1296 285
Chris@1296 286 def test_update_with_password_change_should_send_a_notification
Chris@1296 287 ActionMailer::Base.deliveries.clear
Chris@1296 288 Setting.bcc_recipients = '1'
Chris@1296 289
Chris@1296 290 put :update, :id => 2, :user => {:password => 'newpass123', :password_confirmation => 'newpass123'}, :send_information => '1'
Chris@1296 291 u = User.find(2)
Chris@1296 292 assert u.check_password?('newpass123')
Chris@1296 293
Chris@1296 294 mail = ActionMailer::Base.deliveries.last
Chris@1296 295 assert_not_nil mail
Chris@1296 296 assert_equal [u.mail], mail.bcc
Chris@1296 297 assert_mail_body_match 'newpass123', mail
Chris@1296 298 end
Chris@1296 299
Chris@1296 300 def test_update_user_switchin_from_auth_source_to_password_authentication
Chris@1296 301 # Configure as auth source
Chris@1296 302 u = User.find(2)
Chris@1296 303 u.auth_source = AuthSource.find(1)
Chris@1296 304 u.save!
Chris@1296 305
Chris@1296 306 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'}
Chris@1296 307
Chris@1296 308 assert_equal nil, u.reload.auth_source
Chris@1296 309 assert u.check_password?('newpass123')
Chris@1296 310 end
Chris@1296 311
Chris@1296 312 def test_update_notified_project
Chris@1296 313 get :edit, :id => 2
Chris@1296 314 assert_response :success
Chris@1296 315 assert_template 'edit'
Chris@1296 316 u = User.find(2)
Chris@1296 317 assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort
Chris@1296 318 assert_equal [1, 2, 5], u.notified_projects_ids.sort
Chris@1296 319 assert_tag :tag => 'input',
Chris@1296 320 :attributes => {
Chris@1296 321 :id => 'notified_project_ids_',
Chris@1296 322 :value => 1,
Chris@1296 323 }
Chris@1296 324 assert_equal 'all', u.mail_notification
Chris@1296 325 put :update, :id => 2,
Chris@1296 326 :user => {
Chris@1296 327 :mail_notification => 'selected',
Chris@1296 328 },
Chris@1296 329 :notified_project_ids => [1, 2]
Chris@1296 330 u = User.find(2)
Chris@1296 331 assert_equal 'selected', u.mail_notification
Chris@1296 332 assert_equal [1, 2], u.notified_projects_ids.sort
Chris@1296 333 end
Chris@1296 334
Chris@1296 335 def test_destroy
Chris@1296 336 assert_difference 'User.count', -1 do
Chris@1296 337 delete :destroy, :id => 2
Chris@1296 338 end
Chris@1296 339 assert_redirected_to '/users'
Chris@1296 340 assert_nil User.find_by_id(2)
Chris@1296 341 end
Chris@1296 342
Chris@1296 343 def test_destroy_should_be_denied_for_non_admin_users
Chris@1296 344 @request.session[:user_id] = 3
Chris@1296 345
Chris@1296 346 assert_no_difference 'User.count' do
Chris@1296 347 get :destroy, :id => 2
Chris@1296 348 end
Chris@1296 349 assert_response 403
Chris@1296 350 end
Chris@1296 351
Chris@1296 352 def test_destroy_should_redirect_to_back_url_param
Chris@1296 353 assert_difference 'User.count', -1 do
Chris@1296 354 delete :destroy, :id => 2, :back_url => '/users?name=foo'
Chris@1296 355 end
Chris@1296 356 assert_redirected_to '/users?name=foo'
Chris@1296 357 end
Chris@1296 358
Chris@1296 359 def test_create_membership
Chris@1296 360 assert_difference 'Member.count' do
Chris@1296 361 post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]}
Chris@1296 362 end
Chris@1296 363 assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships'
Chris@1296 364 member = Member.first(:order => 'id DESC')
Chris@1296 365 assert_equal User.find(7), member.principal
Chris@1296 366 assert_equal [2], member.role_ids
Chris@1296 367 assert_equal 3, member.project_id
Chris@1296 368 end
Chris@1296 369
Chris@1296 370 def test_create_membership_js_format
Chris@1296 371 assert_difference 'Member.count' do
Chris@1296 372 post :edit_membership, :id => 7, :membership => {:project_id => 3, :role_ids => [2]}, :format => 'js'
Chris@1296 373 assert_response :success
Chris@1296 374 assert_template 'edit_membership'
Chris@1296 375 assert_equal 'text/javascript', response.content_type
Chris@1296 376 end
Chris@1296 377 member = Member.first(:order => 'id DESC')
Chris@1296 378 assert_equal User.find(7), member.principal
Chris@1296 379 assert_equal [2], member.role_ids
Chris@1296 380 assert_equal 3, member.project_id
Chris@1296 381 assert_include 'tab-content-memberships', response.body
Chris@1296 382 end
Chris@1296 383
Chris@1296 384 def test_create_membership_js_format_with_failure
Chris@1296 385 assert_no_difference 'Member.count' do
Chris@1296 386 post :edit_membership, :id => 7, :membership => {:project_id => 3}, :format => 'js'
Chris@1296 387 assert_response :success
Chris@1296 388 assert_template 'edit_membership'
Chris@1296 389 assert_equal 'text/javascript', response.content_type
Chris@1296 390 end
Chris@1296 391 assert_include 'alert', response.body, "Alert message not sent"
Chris@1296 392 assert_include 'Role can\\\'t be empty', response.body, "Error message not sent"
Chris@1296 393 end
Chris@1296 394
Chris@1296 395 def test_update_membership
Chris@1296 396 assert_no_difference 'Member.count' do
Chris@1296 397 put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]}
Chris@1296 398 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@1296 399 end
Chris@1296 400 assert_equal [2], Member.find(1).role_ids
Chris@1296 401 end
Chris@1296 402
Chris@1296 403 def test_update_membership_js_format
Chris@1296 404 assert_no_difference 'Member.count' do
Chris@1296 405 put :edit_membership, :id => 2, :membership_id => 1, :membership => {:role_ids => [2]}, :format => 'js'
Chris@1296 406 assert_response :success
Chris@1296 407 assert_template 'edit_membership'
Chris@1296 408 assert_equal 'text/javascript', response.content_type
Chris@1296 409 end
Chris@1296 410 assert_equal [2], Member.find(1).role_ids
Chris@1296 411 assert_include 'tab-content-memberships', response.body
Chris@1296 412 end
Chris@1296 413
Chris@1296 414 def test_destroy_membership
Chris@1296 415 assert_difference 'Member.count', -1 do
Chris@1296 416 delete :destroy_membership, :id => 2, :membership_id => 1
Chris@1296 417 end
Chris@1296 418 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@1296 419 assert_nil Member.find_by_id(1)
Chris@1296 420 end
Chris@1296 421
Chris@1296 422 def test_destroy_membership_js_format
Chris@1296 423 assert_difference 'Member.count', -1 do
Chris@1296 424 delete :destroy_membership, :id => 2, :membership_id => 1, :format => 'js'
Chris@1296 425 assert_response :success
Chris@1296 426 assert_template 'destroy_membership'
Chris@1296 427 assert_equal 'text/javascript', response.content_type
Chris@1296 428 end
Chris@1296 429 assert_nil Member.find_by_id(1)
Chris@1296 430 assert_include 'tab-content-memberships', response.body
Chris@1296 431 end
Chris@1296 432 end