OSDN Git Service

add Redmine trunk rev 3089
[redminele/redminele.git] / redmine / test / unit / user_test.rb
1 # redMine - project management software
2 # Copyright (C) 2006  Jean-Philippe Lang
3 #
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.
8
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.
13
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.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19
20 class UserTest < ActiveSupport::TestCase
21   fixtures :users, :members, :projects, :roles, :member_roles
22
23   def setup
24     @admin = User.find(1)
25     @jsmith = User.find(2)
26     @dlopper = User.find(3)
27   end
28
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'})
33   end
34   
35   def test_truth
36     assert_kind_of User, @jsmith
37   end
38
39   def test_create
40     user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
41     
42     user.login = "jsmith"
43     user.password, user.password_confirmation = "password", "password"
44     # login uniqueness
45     assert !user.save
46     assert_equal 1, user.errors.count
47   
48     user.login = "newuser"
49     user.password, user.password_confirmation = "passwd", "password"
50     # password confirmation
51     assert !user.save
52     assert_equal 1, user.errors.count
53
54     user.password, user.password_confirmation = "password", "password"
55     assert user.save
56   end
57   
58   def test_mail_uniqueness_should_not_be_case_sensitive
59     u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
60     u.login = 'newuser1'
61     u.password, u.password_confirmation = "password", "password"
62     assert u.save
63     
64     u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
65     u.login = 'newuser2'
66     u.password, u.password_confirmation = "password", "password"
67     assert !u.save
68     assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail)
69   end
70
71   def test_update
72     assert_equal "admin", @admin.login
73     @admin.login = "john"
74     assert @admin.save, @admin.errors.full_messages.join("; ")
75     @admin.reload
76     assert_equal "john", @admin.login
77   end
78   
79   def test_destroy
80     User.find(2).destroy
81     assert_nil User.find_by_id(2)
82     assert Member.find_all_by_user_id(2).empty?
83   end
84   
85   def test_validate
86     @admin.login = ""
87     assert !@admin.save
88     assert_equal 1, @admin.errors.count
89   end
90   
91   def test_password
92     user = User.try_to_login("admin", "admin")
93     assert_kind_of User, user
94     assert_equal "admin", user.login
95     user.password = "hello"
96     assert user.save
97     
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    
102   end
103   
104   def test_name_format
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
110   end
111   
112   def test_lock
113     user = User.try_to_login("jsmith", "jsmith")
114     assert_equal @jsmith, user
115     
116     @jsmith.status = User::STATUS_LOCKED
117     assert @jsmith.save
118     
119     user = User.try_to_login("jsmith", "jsmith")
120     assert_equal nil, user  
121   end
122   
123   def test_create_anonymous
124     AnonymousUser.delete_all
125     anon = User.anonymous
126     assert !anon.new_record?
127     assert_kind_of AnonymousUser, anon
128   end
129   
130   def test_rss_key
131     assert_nil @jsmith.rss_token
132     key = @jsmith.rss_key
133     assert_equal 40, key.length
134     
135     @jsmith.reload
136     assert_equal key, @jsmith.rss_key
137   end
138   
139   def test_roles_for_project
140     # user with a role
141     roles = @jsmith.roles_for_project(Project.find(1))
142     assert_kind_of Role, roles.first
143     assert_equal "Manager", roles.first.name
144     
145     # user with no role
146     assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
147   end
148   
149   def test_mail_notification_all
150     @jsmith.mail_notification = true
151     @jsmith.notified_project_ids = []
152     @jsmith.save
153     @jsmith.reload
154     assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
155   end
156   
157   def test_mail_notification_selected
158     @jsmith.mail_notification = false
159     @jsmith.notified_project_ids = [1]
160     @jsmith.save
161     @jsmith.reload
162     assert Project.find(1).recipients.include?(@jsmith.mail)
163   end
164   
165   def test_mail_notification_none
166     @jsmith.mail_notification = false
167     @jsmith.notified_project_ids = []
168     @jsmith.save
169     @jsmith.reload
170     assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
171   end
172   
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?
179   end
180   
181   def test_find_by_mail_should_be_case_insensitive
182     u = User.find_by_mail('JSmith@somenet.foo')
183     assert_not_nil u
184     assert_equal 'jsmith@somenet.foo', u.mail
185   end
186   
187   def test_random_password
188     u = User.new
189     u.random_password
190     assert !u.password.blank?
191     assert !u.password_confirmation.blank?
192   end
193   
194   if Object.const_defined?(:OpenID)
195     
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
200   end
201
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
206   end
207
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
212   end
213     
214   def test_setting_blank_identity_url
215     u = User.new( :identity_url => 'example.com' )
216     u.identity_url = ''
217     assert u.identity_url.blank?
218   end
219     
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?
223   end
224   
225   else
226     puts "Skipping openid tests."
227   end
228
229 end