annotate test/functional/users_controller_test.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 1d32c0a0efbf
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@0 99
Chris@0 100 def test_edit
Chris@0 101 ActionMailer::Base.deliveries.clear
Chris@0 102 post :edit, :id => 2, :user => {:firstname => 'Changed'}
Chris@0 103 assert_equal 'Changed', User.find(2).firstname
Chris@0 104 assert ActionMailer::Base.deliveries.empty?
Chris@0 105 end
Chris@0 106
Chris@0 107 def test_edit_with_activation_should_send_a_notification
Chris@0 108 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
Chris@0 109 u.login = 'foo'
Chris@0 110 u.status = User::STATUS_REGISTERED
Chris@0 111 u.save!
Chris@0 112 ActionMailer::Base.deliveries.clear
Chris@0 113 Setting.bcc_recipients = '1'
Chris@0 114
Chris@0 115 post :edit, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
Chris@0 116 assert u.reload.active?
Chris@0 117 mail = ActionMailer::Base.deliveries.last
Chris@0 118 assert_not_nil mail
Chris@0 119 assert_equal ['foo.bar@somenet.foo'], mail.bcc
Chris@0 120 assert mail.body.include?(ll('fr', :notice_account_activated))
Chris@0 121 end
Chris@0 122
Chris@0 123 def test_edit_with_password_change_should_send_a_notification
Chris@0 124 ActionMailer::Base.deliveries.clear
Chris@0 125 Setting.bcc_recipients = '1'
Chris@0 126
Chris@0 127 u = User.find(2)
Chris@0 128 post :edit, :id => u.id, :user => {}, :password => 'newpass', :password_confirmation => 'newpass', :send_information => '1'
Chris@0 129 assert_equal User.hash_password('newpass'), u.reload.hashed_password
Chris@0 130
Chris@0 131 mail = ActionMailer::Base.deliveries.last
Chris@0 132 assert_not_nil mail
Chris@0 133 assert_equal [u.mail], mail.bcc
Chris@0 134 assert mail.body.include?('newpass')
Chris@0 135 end
Chris@0 136
Chris@0 137 def test_edit_membership
Chris@0 138 post :edit_membership, :id => 2, :membership_id => 1,
Chris@0 139 :membership => { :role_ids => [2]}
Chris@0 140 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@0 141 assert_equal [2], Member.find(1).role_ids
Chris@0 142 end
Chris@0 143
Chris@0 144 def test_destroy_membership
Chris@0 145 post :destroy_membership, :id => 2, :membership_id => 1
Chris@0 146 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
Chris@0 147 assert_nil Member.find_by_id(1)
Chris@0 148 end
Chris@0 149 end