1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 require File.dirname(__FILE__) + '/../test_helper'
20 class UserTest < ActiveSupport::TestCase
21 fixtures :users, :members, :projects, :roles, :member_roles
25 @jsmith = User.find(2)
26 @dlopper = User.find(3)
29 test 'object_daddy creation' do
30 User.generate_with_protected!(:firstname => 'Testing connection')
31 User.generate_with_protected!(:firstname => 'Testing connection')
32 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
36 assert_kind_of User, @jsmith
40 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
43 user.password, user.password_confirmation = "password", "password"
46 assert_equal 1, user.errors.count
48 user.login = "newuser"
49 user.password, user.password_confirmation = "passwd", "password"
50 # password confirmation
52 assert_equal 1, user.errors.count
54 user.password, user.password_confirmation = "password", "password"
58 def test_mail_uniqueness_should_not_be_case_sensitive
59 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
61 u.password, u.password_confirmation = "password", "password"
64 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
66 u.password, u.password_confirmation = "password", "password"
68 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail)
72 assert_equal "admin", @admin.login
74 assert @admin.save, @admin.errors.full_messages.join("; ")
76 assert_equal "john", @admin.login
81 assert_nil User.find_by_id(2)
82 assert Member.find_all_by_user_id(2).empty?
88 assert_equal 1, @admin.errors.count
92 user = User.try_to_login("admin", "admin")
93 assert_kind_of User, user
94 assert_equal "admin", user.login
95 user.password = "hello"
98 user = User.try_to_login("admin", "hello")
99 assert_kind_of User, user
100 assert_equal "admin", user.login
101 assert_equal User.hash_password("hello"), user.hashed_password
105 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
106 Setting.user_format = :firstname_lastname
107 assert_equal 'John Smith', @jsmith.reload.name
108 Setting.user_format = :username
109 assert_equal 'jsmith', @jsmith.reload.name
113 user = User.try_to_login("jsmith", "jsmith")
114 assert_equal @jsmith, user
116 @jsmith.status = User::STATUS_LOCKED
119 user = User.try_to_login("jsmith", "jsmith")
120 assert_equal nil, user
123 def test_create_anonymous
124 AnonymousUser.delete_all
125 anon = User.anonymous
126 assert !anon.new_record?
127 assert_kind_of AnonymousUser, anon
131 assert_nil @jsmith.rss_token
132 key = @jsmith.rss_key
133 assert_equal 40, key.length
136 assert_equal key, @jsmith.rss_key
139 def test_roles_for_project
141 roles = @jsmith.roles_for_project(Project.find(1))
142 assert_kind_of Role, roles.first
143 assert_equal "Manager", roles.first.name
146 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
149 def test_mail_notification_all
150 @jsmith.mail_notification = true
151 @jsmith.notified_project_ids = []
154 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
157 def test_mail_notification_selected
158 @jsmith.mail_notification = false
159 @jsmith.notified_project_ids = [1]
162 assert Project.find(1).recipients.include?(@jsmith.mail)
165 def test_mail_notification_none
166 @jsmith.mail_notification = false
167 @jsmith.notified_project_ids = []
170 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
173 def test_comments_sorting_preference
174 assert !@jsmith.wants_comments_in_reverse_order?
175 @jsmith.pref.comments_sorting = 'asc'
176 assert !@jsmith.wants_comments_in_reverse_order?
177 @jsmith.pref.comments_sorting = 'desc'
178 assert @jsmith.wants_comments_in_reverse_order?
181 def test_find_by_mail_should_be_case_insensitive
182 u = User.find_by_mail('JSmith@somenet.foo')
184 assert_equal 'jsmith@somenet.foo', u.mail
187 def test_random_password
190 assert !u.password.blank?
191 assert !u.password_confirmation.blank?
194 if Object.const_defined?(:OpenID)
196 def test_setting_identity_url
197 normalized_open_id_url = 'http://example.com/'
198 u = User.new( :identity_url => 'http://example.com/' )
199 assert_equal normalized_open_id_url, u.identity_url
202 def test_setting_identity_url_without_trailing_slash
203 normalized_open_id_url = 'http://example.com/'
204 u = User.new( :identity_url => 'http://example.com' )
205 assert_equal normalized_open_id_url, u.identity_url
208 def test_setting_identity_url_without_protocol
209 normalized_open_id_url = 'http://example.com/'
210 u = User.new( :identity_url => 'example.com' )
211 assert_equal normalized_open_id_url, u.identity_url
214 def test_setting_blank_identity_url
215 u = User.new( :identity_url => 'example.com' )
217 assert u.identity_url.blank?
220 def test_setting_invalid_identity_url
221 u = User.new( :identity_url => 'this is not an openid url' )
222 assert u.identity_url.blank?
226 puts "Skipping openid tests."