;]
[askyou.git] / test / unit / user_test.rb
blobc263eafc098af5c0dfdd7ffa7b7334db2ef69452
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.
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.
18 require File.dirname(__FILE__) + '/../test_helper'
20 class UserTest < ActiveSupport::TestCase
21   fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources
23   def setup
24     @admin = User.find(1)
25     @jsmith = User.find(2)
26     @dlopper = User.find(3)
27   end
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_mail_should_be_stripped
40     u = User.new
41     u.mail = " foo@bar.com  "
42     assert_equal "foo@bar.com", u.mail
43   end
45   def test_create
46     user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
47     
48     user.login = "jsmith"
49     user.password, user.password_confirmation = "password", "password"
50     # login uniqueness
51     assert !user.save
52     assert_equal 1, user.errors.count
53   
54     user.login = "newuser"
55     user.password, user.password_confirmation = "passwd", "password"
56     # password confirmation
57     assert !user.save
58     assert_equal 1, user.errors.count
60     user.password, user.password_confirmation = "password", "password"
61     assert user.save
62   end
63   
64   context "User.login" do
65     should "be case-insensitive." do
66       u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
67       u.login = 'newuser'
68       u.password, u.password_confirmation = "password", "password"
69       assert u.save
70       
71       u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
72       u.login = 'NewUser'
73       u.password, u.password_confirmation = "password", "password"
74       assert !u.save
75       assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:login)
76     end
77   end
79   def test_mail_uniqueness_should_not_be_case_sensitive
80     u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
81     u.login = 'newuser1'
82     u.password, u.password_confirmation = "password", "password"
83     assert u.save
84     
85     u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
86     u.login = 'newuser2'
87     u.password, u.password_confirmation = "password", "password"
88     assert !u.save
89     assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail)
90   end
92   def test_update
93     assert_equal "admin", @admin.login
94     @admin.login = "john"
95     assert @admin.save, @admin.errors.full_messages.join("; ")
96     @admin.reload
97     assert_equal "john", @admin.login
98   end
99   
100   def test_destroy
101     User.find(2).destroy
102     assert_nil User.find_by_id(2)
103     assert Member.find_all_by_user_id(2).empty?
104   end
105   
106   def test_validate
107     @admin.login = ""
108     assert !@admin.save
109     assert_equal 1, @admin.errors.count
110   end
111   
112   context "User#try_to_login" do
113     should "fall-back to case-insensitive if user login is not found as-typed." do
114       user = User.try_to_login("AdMin", "admin")
115       assert_kind_of User, user
116       assert_equal "admin", user.login
117     end
119     should "select the exact matching user first" do
120       case_sensitive_user = User.generate_with_protected!(:login => 'changed', :password => 'admin', :password_confirmation => 'admin')
121       # bypass validations to make it appear like existing data
122       case_sensitive_user.update_attribute(:login, 'ADMIN')
124       user = User.try_to_login("ADMIN", "admin")
125       assert_kind_of User, user
126       assert_equal "ADMIN", user.login
128     end
129   end
131   def test_password
132     user = User.try_to_login("admin", "admin")
133     assert_kind_of User, user
134     assert_equal "admin", user.login
135     user.password = "hello"
136     assert user.save
137     
138     user = User.try_to_login("admin", "hello")
139     assert_kind_of User, user
140     assert_equal "admin", user.login
141     assert_equal User.hash_password("hello"), user.hashed_password    
142   end
143   
144   def test_name_format
145     assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
146     Setting.user_format = :firstname_lastname
147     assert_equal 'John Smith', @jsmith.reload.name
148     Setting.user_format = :username
149     assert_equal 'jsmith', @jsmith.reload.name
150   end
151   
152   def test_lock
153     user = User.try_to_login("jsmith", "jsmith")
154     assert_equal @jsmith, user
155     
156     @jsmith.status = User::STATUS_LOCKED
157     assert @jsmith.save
158     
159     user = User.try_to_login("jsmith", "jsmith")
160     assert_equal nil, user  
161   end
162   
163   if ldap_configured?
164     context "#try_to_login using LDAP" do
165       context "with failed connection to the LDAP server" do
166         should "return nil" do
167           @auth_source = AuthSourceLdap.find(1)
168           AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
169           
170           assert_equal nil, User.try_to_login('edavis', 'wrong')
171         end
172       end
174       context "with an unsuccessful authentication" do
175         should "return nil" do
176           assert_equal nil, User.try_to_login('edavis', 'wrong')
177         end
178       end
179       
180       context "on the fly registration" do
181         setup do
182           @auth_source = AuthSourceLdap.find(1)
183         end
185         context "with a successful authentication" do
186           should "create a new user account if it doesn't exist" do
187             assert_difference('User.count') do
188               user = User.try_to_login('edavis', '123456')
189               assert !user.admin?
190             end
191           end
192           
193           should "retrieve existing user" do
194             user = User.try_to_login('edavis', '123456')
195             user.admin = true
196             user.save!
197             
198             assert_no_difference('User.count') do
199               user = User.try_to_login('edavis', '123456')
200               assert user.admin?
201             end
202           end
203         end
204       end
205     end
207   else
208     puts "Skipping LDAP tests."
209   end
210   
211   def test_create_anonymous
212     AnonymousUser.delete_all
213     anon = User.anonymous
214     assert !anon.new_record?
215     assert_kind_of AnonymousUser, anon
216   end
218   should_have_one :rss_token
220   def test_rss_key
221     assert_nil @jsmith.rss_token
222     key = @jsmith.rss_key
223     assert_equal 40, key.length
224     
225     @jsmith.reload
226     assert_equal key, @jsmith.rss_key
227   end
229   
230   should_have_one :api_token
232   context "User#api_key" do
233     should "generate a new one if the user doesn't have one" do
234       user = User.generate_with_protected!(:api_token => nil)
235       assert_nil user.api_token
237       key = user.api_key
238       assert_equal 40, key.length
239       user.reload
240       assert_equal key, user.api_key
241     end
243     should "return the existing api token value" do
244       user = User.generate_with_protected!
245       token = Token.generate!(:action => 'api')
246       user.api_token = token
247       assert user.save
248       
249       assert_equal token.value, user.api_key
250     end
251   end
253   context "User#find_by_api_key" do
254     should "return nil if no matching key is found" do
255       assert_nil User.find_by_api_key('zzzzzzzzz')
256     end
258     should "return nil if the key is found for an inactive user" do
259       user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
260       token = Token.generate!(:action => 'api')
261       user.api_token = token
262       user.save
264       assert_nil User.find_by_api_key(token.value)
265     end
267     should "return the user if the key is found for an active user" do
268       user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
269       token = Token.generate!(:action => 'api')
270       user.api_token = token
271       user.save
272       
273       assert_equal user, User.find_by_api_key(token.value)
274     end
275   end
277   def test_roles_for_project
278     # user with a role
279     roles = @jsmith.roles_for_project(Project.find(1))
280     assert_kind_of Role, roles.first
281     assert_equal "Manager", roles.first.name
282     
283     # user with no role
284     assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
285   end
286   
287   def test_mail_notification_all
288     @jsmith.mail_notification = true
289     @jsmith.notified_project_ids = []
290     @jsmith.save
291     @jsmith.reload
292     assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
293   end
294   
295   def test_mail_notification_selected
296     @jsmith.mail_notification = false
297     @jsmith.notified_project_ids = [1]
298     @jsmith.save
299     @jsmith.reload
300     assert Project.find(1).recipients.include?(@jsmith.mail)
301   end
302   
303   def test_mail_notification_none
304     @jsmith.mail_notification = false
305     @jsmith.notified_project_ids = []
306     @jsmith.save
307     @jsmith.reload
308     assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
309   end
310   
311   def test_comments_sorting_preference
312     assert !@jsmith.wants_comments_in_reverse_order?
313     @jsmith.pref.comments_sorting = 'asc'
314     assert !@jsmith.wants_comments_in_reverse_order?
315     @jsmith.pref.comments_sorting = 'desc'
316     assert @jsmith.wants_comments_in_reverse_order?
317   end
318   
319   def test_find_by_mail_should_be_case_insensitive
320     u = User.find_by_mail('JSmith@somenet.foo')
321     assert_not_nil u
322     assert_equal 'jsmith@somenet.foo', u.mail
323   end
324   
325   def test_random_password
326     u = User.new
327     u.random_password
328     assert !u.password.blank?
329     assert !u.password_confirmation.blank?
330   end
332   context "#change_password_allowed?" do
333     should "be allowed if no auth source is set" do
334       user = User.generate_with_protected!
335       assert user.change_password_allowed?
336     end
338     should "delegate to the auth source" do
339       user = User.generate_with_protected!
340       
341       allowed_auth_source = AuthSource.generate!
342       def allowed_auth_source.allow_password_changes?; true; end
344       denied_auth_source = AuthSource.generate!
345       def denied_auth_source.allow_password_changes?; false; end
347       assert user.change_password_allowed?
349       user.auth_source = allowed_auth_source
350       assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
352       user.auth_source = denied_auth_source
353       assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
354     end
356   end
357   
358   context "#allowed_to?" do
359     context "with a unique project" do
360       should "return false if project is archived" do
361         project = Project.find(1)
362         Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
363         assert ! @admin.allowed_to?(:view_issues, Project.find(1))
364       end
365       
366       should "return false if related module is disabled" do
367         project = Project.find(1)
368         project.enabled_module_names = ["issue_tracking"]
369         assert @admin.allowed_to?(:add_issues, project)
370         assert ! @admin.allowed_to?(:view_wiki_pages, project)
371       end
372       
373       should "authorize nearly everything for admin users" do
374         project = Project.find(1)
375         assert ! @admin.member_of?(project)
376         %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
377           assert @admin.allowed_to?(p.to_sym, project)
378         end
379       end
380       
381       should "authorize normal users depending on their roles" do
382         project = Project.find(1)
383         assert @jsmith.allowed_to?(:delete_messages, project)    #Manager
384         assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
385       end
386     end
387     
388     context "with options[:global]" do
389       should "authorize if user has at least one role that has this permission" do
390         @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
391         @anonymous = User.find(6)
392         assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
393         assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
394         assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
395         assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
396         assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
397       end
398     end
399   end
400   
401   if Object.const_defined?(:OpenID)
402     
403   def test_setting_identity_url
404     normalized_open_id_url = 'http://example.com/'
405     u = User.new( :identity_url => 'http://example.com/' )
406     assert_equal normalized_open_id_url, u.identity_url
407   end
409   def test_setting_identity_url_without_trailing_slash
410     normalized_open_id_url = 'http://example.com/'
411     u = User.new( :identity_url => 'http://example.com' )
412     assert_equal normalized_open_id_url, u.identity_url
413   end
415   def test_setting_identity_url_without_protocol
416     normalized_open_id_url = 'http://example.com/'
417     u = User.new( :identity_url => 'example.com' )
418     assert_equal normalized_open_id_url, u.identity_url
419   end
420     
421   def test_setting_blank_identity_url
422     u = User.new( :identity_url => 'example.com' )
423     u.identity_url = ''
424     assert u.identity_url.blank?
425   end
426     
427   def test_setting_invalid_identity_url
428     u = User.new( :identity_url => 'this is not an openid url' )
429     assert u.identity_url.blank?
430   end
431   
432   else
433     puts "Skipping openid tests."
434   end