##// END OF EJS Templates
Disabled on the fly registration for auth_source fixture....
Jean-Philippe Lang -
r8793:dedfdefcf300
parent child
Show More
@@ -1,13 +1,13
1 ---
1 ---
2 auth_sources_001:
2 auth_sources_001:
3 id: 1
3 id: 1
4 type: AuthSourceLdap
4 type: AuthSourceLdap
5 name: 'LDAP test server'
5 name: 'LDAP test server'
6 host: '127.0.0.1'
6 host: '127.0.0.1'
7 port: 389
7 port: 389
8 base_dn: 'OU=Person,DC=redmine,DC=org'
8 base_dn: 'OU=Person,DC=redmine,DC=org'
9 attr_login: uid
9 attr_login: uid
10 attr_firstname: givenName
10 attr_firstname: givenName
11 attr_lastname: sn
11 attr_lastname: sn
12 attr_mail: mail
12 attr_mail: mail
13 onthefly_register: true
13 onthefly_register: false
@@ -1,89 +1,90
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class AuthSourceLdapTest < ActiveSupport::TestCase
20 class AuthSourceLdapTest < ActiveSupport::TestCase
21 fixtures :auth_sources
21 fixtures :auth_sources
22
22
23 def setup
23 def setup
24 end
24 end
25
25
26 def test_create
26 def test_create
27 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
27 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
28 assert a.save
28 assert a.save
29 end
29 end
30
30
31 def test_should_strip_ldap_attributes
31 def test_should_strip_ldap_attributes
32 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
32 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
33 :attr_firstname => 'givenName ')
33 :attr_firstname => 'givenName ')
34 assert a.save
34 assert a.save
35 assert_equal 'givenName', a.reload.attr_firstname
35 assert_equal 'givenName', a.reload.attr_firstname
36 end
36 end
37
37
38 def test_replace_port_zero_to_389
38 def test_replace_port_zero_to_389
39 a = AuthSourceLdap.new(
39 a = AuthSourceLdap.new(
40 :name => 'My LDAP', :host => 'ldap.example.net', :port => 0,
40 :name => 'My LDAP', :host => 'ldap.example.net', :port => 0,
41 :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
41 :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
42 :attr_firstname => 'givenName ')
42 :attr_firstname => 'givenName ')
43 assert a.save
43 assert a.save
44 assert_equal 389, a.port
44 assert_equal 389, a.port
45 end
45 end
46
46
47 if ldap_configured?
47 if ldap_configured?
48 context '#authenticate' do
48 context '#authenticate' do
49 setup do
49 setup do
50 @auth = AuthSourceLdap.find(1)
50 @auth = AuthSourceLdap.find(1)
51 @auth.update_attribute :onthefly_register, true
51 end
52 end
52
53
53 context 'with a valid LDAP user' do
54 context 'with a valid LDAP user' do
54 should 'return the user attributes' do
55 should 'return the user attributes' do
55 attributes = @auth.authenticate('example1','123456')
56 attributes = @auth.authenticate('example1','123456')
56 assert attributes.is_a?(Hash), "An hash was not returned"
57 assert attributes.is_a?(Hash), "An hash was not returned"
57 assert_equal 'Example', attributes[:firstname]
58 assert_equal 'Example', attributes[:firstname]
58 assert_equal 'One', attributes[:lastname]
59 assert_equal 'One', attributes[:lastname]
59 assert_equal 'example1@redmine.org', attributes[:mail]
60 assert_equal 'example1@redmine.org', attributes[:mail]
60 assert_equal @auth.id, attributes[:auth_source_id]
61 assert_equal @auth.id, attributes[:auth_source_id]
61 attributes.keys.each do |attribute|
62 attributes.keys.each do |attribute|
62 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
63 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
63 end
64 end
64 end
65 end
65 end
66 end
66
67
67 context 'with an invalid LDAP user' do
68 context 'with an invalid LDAP user' do
68 should 'return nil' do
69 should 'return nil' do
69 assert_equal nil, @auth.authenticate('nouser','123456')
70 assert_equal nil, @auth.authenticate('nouser','123456')
70 end
71 end
71 end
72 end
72
73
73 context 'without a login' do
74 context 'without a login' do
74 should 'return nil' do
75 should 'return nil' do
75 assert_equal nil, @auth.authenticate('','123456')
76 assert_equal nil, @auth.authenticate('','123456')
76 end
77 end
77 end
78 end
78
79
79 context 'without a password' do
80 context 'without a password' do
80 should 'return nil' do
81 should 'return nil' do
81 assert_equal nil, @auth.authenticate('edavis','')
82 assert_equal nil, @auth.authenticate('edavis','')
82 end
83 end
83 end
84 end
84
85
85 end
86 end
86 else
87 else
87 puts '(Test LDAP server not configured)'
88 puts '(Test LDAP server not configured)'
88 end
89 end
89 end
90 end
@@ -1,891 +1,892
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class UserTest < ActiveSupport::TestCase
20 class UserTest < ActiveSupport::TestCase
21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
22 :trackers, :issue_statuses,
22 :trackers, :issue_statuses,
23 :projects_trackers,
23 :projects_trackers,
24 :watchers,
24 :watchers,
25 :issue_categories, :enumerations, :issues,
25 :issue_categories, :enumerations, :issues,
26 :journals, :journal_details,
26 :journals, :journal_details,
27 :groups_users,
27 :groups_users,
28 :enabled_modules,
28 :enabled_modules,
29 :workflows
29 :workflows
30
30
31 def setup
31 def setup
32 @admin = User.find(1)
32 @admin = User.find(1)
33 @jsmith = User.find(2)
33 @jsmith = User.find(2)
34 @dlopper = User.find(3)
34 @dlopper = User.find(3)
35 end
35 end
36
36
37 test 'object_daddy creation' do
37 test 'object_daddy creation' do
38 User.generate_with_protected!(:firstname => 'Testing connection')
38 User.generate_with_protected!(:firstname => 'Testing connection')
39 User.generate_with_protected!(:firstname => 'Testing connection')
39 User.generate_with_protected!(:firstname => 'Testing connection')
40 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
40 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
41 end
41 end
42
42
43 def test_truth
43 def test_truth
44 assert_kind_of User, @jsmith
44 assert_kind_of User, @jsmith
45 end
45 end
46
46
47 def test_mail_should_be_stripped
47 def test_mail_should_be_stripped
48 u = User.new
48 u = User.new
49 u.mail = " foo@bar.com "
49 u.mail = " foo@bar.com "
50 assert_equal "foo@bar.com", u.mail
50 assert_equal "foo@bar.com", u.mail
51 end
51 end
52
52
53 def test_mail_validation
53 def test_mail_validation
54 u = User.new
54 u = User.new
55 u.mail = ''
55 u.mail = ''
56 assert !u.valid?
56 assert !u.valid?
57 assert_equal I18n.translate('activerecord.errors.messages.blank'),
57 assert_equal I18n.translate('activerecord.errors.messages.blank'),
58 u.errors[:mail].to_s
58 u.errors[:mail].to_s
59 end
59 end
60
60
61 def test_login_length_validation
61 def test_login_length_validation
62 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
62 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
63 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
63 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
64 assert !user.valid?
64 assert !user.valid?
65
65
66 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
66 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
67 assert user.valid?
67 assert user.valid?
68 assert user.save
68 assert user.save
69 end
69 end
70
70
71 def test_create
71 def test_create
72 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
72 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
73
73
74 user.login = "jsmith"
74 user.login = "jsmith"
75 user.password, user.password_confirmation = "password", "password"
75 user.password, user.password_confirmation = "password", "password"
76 # login uniqueness
76 # login uniqueness
77 assert !user.save
77 assert !user.save
78 assert_equal 1, user.errors.count
78 assert_equal 1, user.errors.count
79
79
80 user.login = "newuser"
80 user.login = "newuser"
81 user.password, user.password_confirmation = "passwd", "password"
81 user.password, user.password_confirmation = "passwd", "password"
82 # password confirmation
82 # password confirmation
83 assert !user.save
83 assert !user.save
84 assert_equal 1, user.errors.count
84 assert_equal 1, user.errors.count
85
85
86 user.password, user.password_confirmation = "password", "password"
86 user.password, user.password_confirmation = "password", "password"
87 assert user.save
87 assert user.save
88 end
88 end
89
89
90 context "User#before_create" do
90 context "User#before_create" do
91 should "set the mail_notification to the default Setting" do
91 should "set the mail_notification to the default Setting" do
92 @user1 = User.generate_with_protected!
92 @user1 = User.generate_with_protected!
93 assert_equal 'only_my_events', @user1.mail_notification
93 assert_equal 'only_my_events', @user1.mail_notification
94
94
95 with_settings :default_notification_option => 'all' do
95 with_settings :default_notification_option => 'all' do
96 @user2 = User.generate_with_protected!
96 @user2 = User.generate_with_protected!
97 assert_equal 'all', @user2.mail_notification
97 assert_equal 'all', @user2.mail_notification
98 end
98 end
99 end
99 end
100 end
100 end
101
101
102 context "User.login" do
102 context "User.login" do
103 should "be case-insensitive." do
103 should "be case-insensitive." do
104 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
104 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
105 u.login = 'newuser'
105 u.login = 'newuser'
106 u.password, u.password_confirmation = "password", "password"
106 u.password, u.password_confirmation = "password", "password"
107 assert u.save
107 assert u.save
108
108
109 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
109 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
110 u.login = 'NewUser'
110 u.login = 'NewUser'
111 u.password, u.password_confirmation = "password", "password"
111 u.password, u.password_confirmation = "password", "password"
112 assert !u.save
112 assert !u.save
113 assert_equal I18n.translate('activerecord.errors.messages.taken'),
113 assert_equal I18n.translate('activerecord.errors.messages.taken'),
114 u.errors[:login].to_s
114 u.errors[:login].to_s
115 end
115 end
116 end
116 end
117
117
118 def test_mail_uniqueness_should_not_be_case_sensitive
118 def test_mail_uniqueness_should_not_be_case_sensitive
119 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
119 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
120 u.login = 'newuser1'
120 u.login = 'newuser1'
121 u.password, u.password_confirmation = "password", "password"
121 u.password, u.password_confirmation = "password", "password"
122 assert u.save
122 assert u.save
123
123
124 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
124 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
125 u.login = 'newuser2'
125 u.login = 'newuser2'
126 u.password, u.password_confirmation = "password", "password"
126 u.password, u.password_confirmation = "password", "password"
127 assert !u.save
127 assert !u.save
128 assert_equal I18n.translate('activerecord.errors.messages.taken'),
128 assert_equal I18n.translate('activerecord.errors.messages.taken'),
129 u.errors[:mail].to_s
129 u.errors[:mail].to_s
130 end
130 end
131
131
132 def test_update
132 def test_update
133 assert_equal "admin", @admin.login
133 assert_equal "admin", @admin.login
134 @admin.login = "john"
134 @admin.login = "john"
135 assert @admin.save, @admin.errors.full_messages.join("; ")
135 assert @admin.save, @admin.errors.full_messages.join("; ")
136 @admin.reload
136 @admin.reload
137 assert_equal "john", @admin.login
137 assert_equal "john", @admin.login
138 end
138 end
139
139
140 def test_destroy_should_delete_members_and_roles
140 def test_destroy_should_delete_members_and_roles
141 members = Member.find_all_by_user_id(2)
141 members = Member.find_all_by_user_id(2)
142 ms = members.size
142 ms = members.size
143 rs = members.collect(&:roles).flatten.size
143 rs = members.collect(&:roles).flatten.size
144
144
145 assert_difference 'Member.count', - ms do
145 assert_difference 'Member.count', - ms do
146 assert_difference 'MemberRole.count', - rs do
146 assert_difference 'MemberRole.count', - rs do
147 User.find(2).destroy
147 User.find(2).destroy
148 end
148 end
149 end
149 end
150
150
151 assert_nil User.find_by_id(2)
151 assert_nil User.find_by_id(2)
152 assert Member.find_all_by_user_id(2).empty?
152 assert Member.find_all_by_user_id(2).empty?
153 end
153 end
154
154
155 def test_destroy_should_update_attachments
155 def test_destroy_should_update_attachments
156 attachment = Attachment.create!(:container => Project.find(1),
156 attachment = Attachment.create!(:container => Project.find(1),
157 :file => uploaded_test_file("testfile.txt", "text/plain"),
157 :file => uploaded_test_file("testfile.txt", "text/plain"),
158 :author_id => 2)
158 :author_id => 2)
159
159
160 User.find(2).destroy
160 User.find(2).destroy
161 assert_nil User.find_by_id(2)
161 assert_nil User.find_by_id(2)
162 assert_equal User.anonymous, attachment.reload.author
162 assert_equal User.anonymous, attachment.reload.author
163 end
163 end
164
164
165 def test_destroy_should_update_comments
165 def test_destroy_should_update_comments
166 comment = Comment.create!(
166 comment = Comment.create!(
167 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
167 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
168 :author => User.find(2),
168 :author => User.find(2),
169 :comments => 'foo'
169 :comments => 'foo'
170 )
170 )
171
171
172 User.find(2).destroy
172 User.find(2).destroy
173 assert_nil User.find_by_id(2)
173 assert_nil User.find_by_id(2)
174 assert_equal User.anonymous, comment.reload.author
174 assert_equal User.anonymous, comment.reload.author
175 end
175 end
176
176
177 def test_destroy_should_update_issues
177 def test_destroy_should_update_issues
178 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
178 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
179
179
180 User.find(2).destroy
180 User.find(2).destroy
181 assert_nil User.find_by_id(2)
181 assert_nil User.find_by_id(2)
182 assert_equal User.anonymous, issue.reload.author
182 assert_equal User.anonymous, issue.reload.author
183 end
183 end
184
184
185 def test_destroy_should_unassign_issues
185 def test_destroy_should_unassign_issues
186 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
186 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
187
187
188 User.find(2).destroy
188 User.find(2).destroy
189 assert_nil User.find_by_id(2)
189 assert_nil User.find_by_id(2)
190 assert_nil issue.reload.assigned_to
190 assert_nil issue.reload.assigned_to
191 end
191 end
192
192
193 def test_destroy_should_update_journals
193 def test_destroy_should_update_journals
194 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
194 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
195 issue.init_journal(User.find(2), "update")
195 issue.init_journal(User.find(2), "update")
196 issue.save!
196 issue.save!
197
197
198 User.find(2).destroy
198 User.find(2).destroy
199 assert_nil User.find_by_id(2)
199 assert_nil User.find_by_id(2)
200 assert_equal User.anonymous, issue.journals.first.reload.user
200 assert_equal User.anonymous, issue.journals.first.reload.user
201 end
201 end
202
202
203 def test_destroy_should_update_journal_details_old_value
203 def test_destroy_should_update_journal_details_old_value
204 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
204 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
205 issue.init_journal(User.find(1), "update")
205 issue.init_journal(User.find(1), "update")
206 issue.assigned_to_id = nil
206 issue.assigned_to_id = nil
207 assert_difference 'JournalDetail.count' do
207 assert_difference 'JournalDetail.count' do
208 issue.save!
208 issue.save!
209 end
209 end
210 journal_detail = JournalDetail.first(:order => 'id DESC')
210 journal_detail = JournalDetail.first(:order => 'id DESC')
211 assert_equal '2', journal_detail.old_value
211 assert_equal '2', journal_detail.old_value
212
212
213 User.find(2).destroy
213 User.find(2).destroy
214 assert_nil User.find_by_id(2)
214 assert_nil User.find_by_id(2)
215 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
215 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
216 end
216 end
217
217
218 def test_destroy_should_update_journal_details_value
218 def test_destroy_should_update_journal_details_value
219 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
219 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
220 issue.init_journal(User.find(1), "update")
220 issue.init_journal(User.find(1), "update")
221 issue.assigned_to_id = 2
221 issue.assigned_to_id = 2
222 assert_difference 'JournalDetail.count' do
222 assert_difference 'JournalDetail.count' do
223 issue.save!
223 issue.save!
224 end
224 end
225 journal_detail = JournalDetail.first(:order => 'id DESC')
225 journal_detail = JournalDetail.first(:order => 'id DESC')
226 assert_equal '2', journal_detail.value
226 assert_equal '2', journal_detail.value
227
227
228 User.find(2).destroy
228 User.find(2).destroy
229 assert_nil User.find_by_id(2)
229 assert_nil User.find_by_id(2)
230 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
230 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
231 end
231 end
232
232
233 def test_destroy_should_update_messages
233 def test_destroy_should_update_messages
234 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
234 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
235 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
235 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
236
236
237 User.find(2).destroy
237 User.find(2).destroy
238 assert_nil User.find_by_id(2)
238 assert_nil User.find_by_id(2)
239 assert_equal User.anonymous, message.reload.author
239 assert_equal User.anonymous, message.reload.author
240 end
240 end
241
241
242 def test_destroy_should_update_news
242 def test_destroy_should_update_news
243 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
243 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
244
244
245 User.find(2).destroy
245 User.find(2).destroy
246 assert_nil User.find_by_id(2)
246 assert_nil User.find_by_id(2)
247 assert_equal User.anonymous, news.reload.author
247 assert_equal User.anonymous, news.reload.author
248 end
248 end
249
249
250 def test_destroy_should_delete_private_queries
250 def test_destroy_should_delete_private_queries
251 query = Query.new(:name => 'foo', :is_public => false)
251 query = Query.new(:name => 'foo', :is_public => false)
252 query.project_id = 1
252 query.project_id = 1
253 query.user_id = 2
253 query.user_id = 2
254 query.save!
254 query.save!
255
255
256 User.find(2).destroy
256 User.find(2).destroy
257 assert_nil User.find_by_id(2)
257 assert_nil User.find_by_id(2)
258 assert_nil Query.find_by_id(query.id)
258 assert_nil Query.find_by_id(query.id)
259 end
259 end
260
260
261 def test_destroy_should_update_public_queries
261 def test_destroy_should_update_public_queries
262 query = Query.new(:name => 'foo', :is_public => true)
262 query = Query.new(:name => 'foo', :is_public => true)
263 query.project_id = 1
263 query.project_id = 1
264 query.user_id = 2
264 query.user_id = 2
265 query.save!
265 query.save!
266
266
267 User.find(2).destroy
267 User.find(2).destroy
268 assert_nil User.find_by_id(2)
268 assert_nil User.find_by_id(2)
269 assert_equal User.anonymous, query.reload.user
269 assert_equal User.anonymous, query.reload.user
270 end
270 end
271
271
272 def test_destroy_should_update_time_entries
272 def test_destroy_should_update_time_entries
273 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
273 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
274 entry.project_id = 1
274 entry.project_id = 1
275 entry.user_id = 2
275 entry.user_id = 2
276 entry.save!
276 entry.save!
277
277
278 User.find(2).destroy
278 User.find(2).destroy
279 assert_nil User.find_by_id(2)
279 assert_nil User.find_by_id(2)
280 assert_equal User.anonymous, entry.reload.user
280 assert_equal User.anonymous, entry.reload.user
281 end
281 end
282
282
283 def test_destroy_should_delete_tokens
283 def test_destroy_should_delete_tokens
284 token = Token.create!(:user_id => 2, :value => 'foo')
284 token = Token.create!(:user_id => 2, :value => 'foo')
285
285
286 User.find(2).destroy
286 User.find(2).destroy
287 assert_nil User.find_by_id(2)
287 assert_nil User.find_by_id(2)
288 assert_nil Token.find_by_id(token.id)
288 assert_nil Token.find_by_id(token.id)
289 end
289 end
290
290
291 def test_destroy_should_delete_watchers
291 def test_destroy_should_delete_watchers
292 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
292 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
293 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
293 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
294
294
295 User.find(2).destroy
295 User.find(2).destroy
296 assert_nil User.find_by_id(2)
296 assert_nil User.find_by_id(2)
297 assert_nil Watcher.find_by_id(watcher.id)
297 assert_nil Watcher.find_by_id(watcher.id)
298 end
298 end
299
299
300 def test_destroy_should_update_wiki_contents
300 def test_destroy_should_update_wiki_contents
301 wiki_content = WikiContent.create!(
301 wiki_content = WikiContent.create!(
302 :text => 'foo',
302 :text => 'foo',
303 :author_id => 2,
303 :author_id => 2,
304 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
304 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
305 )
305 )
306 wiki_content.text = 'bar'
306 wiki_content.text = 'bar'
307 assert_difference 'WikiContent::Version.count' do
307 assert_difference 'WikiContent::Version.count' do
308 wiki_content.save!
308 wiki_content.save!
309 end
309 end
310
310
311 User.find(2).destroy
311 User.find(2).destroy
312 assert_nil User.find_by_id(2)
312 assert_nil User.find_by_id(2)
313 assert_equal User.anonymous, wiki_content.reload.author
313 assert_equal User.anonymous, wiki_content.reload.author
314 wiki_content.versions.each do |version|
314 wiki_content.versions.each do |version|
315 assert_equal User.anonymous, version.reload.author
315 assert_equal User.anonymous, version.reload.author
316 end
316 end
317 end
317 end
318
318
319 def test_destroy_should_nullify_issue_categories
319 def test_destroy_should_nullify_issue_categories
320 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
320 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
321
321
322 User.find(2).destroy
322 User.find(2).destroy
323 assert_nil User.find_by_id(2)
323 assert_nil User.find_by_id(2)
324 assert_nil category.reload.assigned_to_id
324 assert_nil category.reload.assigned_to_id
325 end
325 end
326
326
327 def test_destroy_should_nullify_changesets
327 def test_destroy_should_nullify_changesets
328 changeset = Changeset.create!(
328 changeset = Changeset.create!(
329 :repository => Repository::Subversion.generate!(
329 :repository => Repository::Subversion.generate!(
330 :project_id => 1
330 :project_id => 1
331 ),
331 ),
332 :revision => '12',
332 :revision => '12',
333 :committed_on => Time.now,
333 :committed_on => Time.now,
334 :committer => 'jsmith'
334 :committer => 'jsmith'
335 )
335 )
336 assert_equal 2, changeset.user_id
336 assert_equal 2, changeset.user_id
337
337
338 User.find(2).destroy
338 User.find(2).destroy
339 assert_nil User.find_by_id(2)
339 assert_nil User.find_by_id(2)
340 assert_nil changeset.reload.user_id
340 assert_nil changeset.reload.user_id
341 end
341 end
342
342
343 def test_anonymous_user_should_not_be_destroyable
343 def test_anonymous_user_should_not_be_destroyable
344 assert_no_difference 'User.count' do
344 assert_no_difference 'User.count' do
345 assert_equal false, User.anonymous.destroy
345 assert_equal false, User.anonymous.destroy
346 end
346 end
347 end
347 end
348
348
349 def test_validate_login_presence
349 def test_validate_login_presence
350 @admin.login = ""
350 @admin.login = ""
351 assert !@admin.save
351 assert !@admin.save
352 assert_equal 1, @admin.errors.count
352 assert_equal 1, @admin.errors.count
353 end
353 end
354
354
355 def test_validate_mail_notification_inclusion
355 def test_validate_mail_notification_inclusion
356 u = User.new
356 u = User.new
357 u.mail_notification = 'foo'
357 u.mail_notification = 'foo'
358 u.save
358 u.save
359 assert_not_nil u.errors[:mail_notification]
359 assert_not_nil u.errors[:mail_notification]
360 end
360 end
361
361
362 context "User#try_to_login" do
362 context "User#try_to_login" do
363 should "fall-back to case-insensitive if user login is not found as-typed." do
363 should "fall-back to case-insensitive if user login is not found as-typed." do
364 user = User.try_to_login("AdMin", "admin")
364 user = User.try_to_login("AdMin", "admin")
365 assert_kind_of User, user
365 assert_kind_of User, user
366 assert_equal "admin", user.login
366 assert_equal "admin", user.login
367 end
367 end
368
368
369 should "select the exact matching user first" do
369 should "select the exact matching user first" do
370 case_sensitive_user = User.generate_with_protected!(
370 case_sensitive_user = User.generate_with_protected!(
371 :login => 'changed', :password => 'admin',
371 :login => 'changed', :password => 'admin',
372 :password_confirmation => 'admin')
372 :password_confirmation => 'admin')
373 # bypass validations to make it appear like existing data
373 # bypass validations to make it appear like existing data
374 case_sensitive_user.update_attribute(:login, 'ADMIN')
374 case_sensitive_user.update_attribute(:login, 'ADMIN')
375
375
376 user = User.try_to_login("ADMIN", "admin")
376 user = User.try_to_login("ADMIN", "admin")
377 assert_kind_of User, user
377 assert_kind_of User, user
378 assert_equal "ADMIN", user.login
378 assert_equal "ADMIN", user.login
379
379
380 end
380 end
381 end
381 end
382
382
383 def test_password
383 def test_password
384 user = User.try_to_login("admin", "admin")
384 user = User.try_to_login("admin", "admin")
385 assert_kind_of User, user
385 assert_kind_of User, user
386 assert_equal "admin", user.login
386 assert_equal "admin", user.login
387 user.password = "hello"
387 user.password = "hello"
388 assert user.save
388 assert user.save
389
389
390 user = User.try_to_login("admin", "hello")
390 user = User.try_to_login("admin", "hello")
391 assert_kind_of User, user
391 assert_kind_of User, user
392 assert_equal "admin", user.login
392 assert_equal "admin", user.login
393 end
393 end
394
394
395 def test_validate_password_length
395 def test_validate_password_length
396 with_settings :password_min_length => '100' do
396 with_settings :password_min_length => '100' do
397 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
397 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
398 user.login = "newuser100"
398 user.login = "newuser100"
399 user.password, user.password_confirmation = "password100", "password100"
399 user.password, user.password_confirmation = "password100", "password100"
400 assert !user.save
400 assert !user.save
401 assert_equal 1, user.errors.count
401 assert_equal 1, user.errors.count
402 end
402 end
403 end
403 end
404
404
405 def test_name_format
405 def test_name_format
406 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
406 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
407 with_settings :user_format => :firstname_lastname do
407 with_settings :user_format => :firstname_lastname do
408 assert_equal 'John Smith', @jsmith.reload.name
408 assert_equal 'John Smith', @jsmith.reload.name
409 end
409 end
410 with_settings :user_format => :username do
410 with_settings :user_format => :username do
411 assert_equal 'jsmith', @jsmith.reload.name
411 assert_equal 'jsmith', @jsmith.reload.name
412 end
412 end
413 end
413 end
414
414
415 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
415 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
416 with_settings :user_format => 'lastname_coma_firstname' do
416 with_settings :user_format => 'lastname_coma_firstname' do
417 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
417 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
418 end
418 end
419 end
419 end
420
420
421 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
421 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
422 with_settings :user_format => 'lastname_firstname' do
422 with_settings :user_format => 'lastname_firstname' do
423 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
423 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
424 end
424 end
425 end
425 end
426
426
427 def test_fields_for_order_statement_with_blank_format_should_return_default
427 def test_fields_for_order_statement_with_blank_format_should_return_default
428 with_settings :user_format => '' do
428 with_settings :user_format => '' do
429 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
429 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
430 end
430 end
431 end
431 end
432
432
433 def test_fields_for_order_statement_with_invalid_format_should_return_default
433 def test_fields_for_order_statement_with_invalid_format_should_return_default
434 with_settings :user_format => 'foo' do
434 with_settings :user_format => 'foo' do
435 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
435 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
436 end
436 end
437 end
437 end
438
438
439 def test_lock
439 def test_lock
440 user = User.try_to_login("jsmith", "jsmith")
440 user = User.try_to_login("jsmith", "jsmith")
441 assert_equal @jsmith, user
441 assert_equal @jsmith, user
442
442
443 @jsmith.status = User::STATUS_LOCKED
443 @jsmith.status = User::STATUS_LOCKED
444 assert @jsmith.save
444 assert @jsmith.save
445
445
446 user = User.try_to_login("jsmith", "jsmith")
446 user = User.try_to_login("jsmith", "jsmith")
447 assert_equal nil, user
447 assert_equal nil, user
448 end
448 end
449
449
450 context ".try_to_login" do
450 context ".try_to_login" do
451 context "with good credentials" do
451 context "with good credentials" do
452 should "return the user" do
452 should "return the user" do
453 user = User.try_to_login("admin", "admin")
453 user = User.try_to_login("admin", "admin")
454 assert_kind_of User, user
454 assert_kind_of User, user
455 assert_equal "admin", user.login
455 assert_equal "admin", user.login
456 end
456 end
457 end
457 end
458
458
459 context "with wrong credentials" do
459 context "with wrong credentials" do
460 should "return nil" do
460 should "return nil" do
461 assert_nil User.try_to_login("admin", "foo")
461 assert_nil User.try_to_login("admin", "foo")
462 end
462 end
463 end
463 end
464 end
464 end
465
465
466 if ldap_configured?
466 if ldap_configured?
467 context "#try_to_login using LDAP" do
467 context "#try_to_login using LDAP" do
468 context "with failed connection to the LDAP server" do
468 context "with failed connection to the LDAP server" do
469 should "return nil" do
469 should "return nil" do
470 @auth_source = AuthSourceLdap.find(1)
470 @auth_source = AuthSourceLdap.find(1)
471 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
471 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
472
472
473 assert_equal nil, User.try_to_login('edavis', 'wrong')
473 assert_equal nil, User.try_to_login('edavis', 'wrong')
474 end
474 end
475 end
475 end
476
476
477 context "with an unsuccessful authentication" do
477 context "with an unsuccessful authentication" do
478 should "return nil" do
478 should "return nil" do
479 assert_equal nil, User.try_to_login('edavis', 'wrong')
479 assert_equal nil, User.try_to_login('edavis', 'wrong')
480 end
480 end
481 end
481 end
482
482
483 context "on the fly registration" do
483 context "on the fly registration" do
484 setup do
484 setup do
485 @auth_source = AuthSourceLdap.find(1)
485 @auth_source = AuthSourceLdap.find(1)
486 @auth.update_attribute :onthefly_register, true
486 end
487 end
487
488
488 context "with a successful authentication" do
489 context "with a successful authentication" do
489 should "create a new user account if it doesn't exist" do
490 should "create a new user account if it doesn't exist" do
490 assert_difference('User.count') do
491 assert_difference('User.count') do
491 user = User.try_to_login('edavis', '123456')
492 user = User.try_to_login('edavis', '123456')
492 assert !user.admin?
493 assert !user.admin?
493 end
494 end
494 end
495 end
495
496
496 should "retrieve existing user" do
497 should "retrieve existing user" do
497 user = User.try_to_login('edavis', '123456')
498 user = User.try_to_login('edavis', '123456')
498 user.admin = true
499 user.admin = true
499 user.save!
500 user.save!
500
501
501 assert_no_difference('User.count') do
502 assert_no_difference('User.count') do
502 user = User.try_to_login('edavis', '123456')
503 user = User.try_to_login('edavis', '123456')
503 assert user.admin?
504 assert user.admin?
504 end
505 end
505 end
506 end
506 end
507 end
507 end
508 end
508 end
509 end
509
510
510 else
511 else
511 puts "Skipping LDAP tests."
512 puts "Skipping LDAP tests."
512 end
513 end
513
514
514 def test_create_anonymous
515 def test_create_anonymous
515 AnonymousUser.delete_all
516 AnonymousUser.delete_all
516 anon = User.anonymous
517 anon = User.anonymous
517 assert !anon.new_record?
518 assert !anon.new_record?
518 assert_kind_of AnonymousUser, anon
519 assert_kind_of AnonymousUser, anon
519 end
520 end
520
521
521 def test_ensure_single_anonymous_user
522 def test_ensure_single_anonymous_user
522 AnonymousUser.delete_all
523 AnonymousUser.delete_all
523 anon1 = User.anonymous
524 anon1 = User.anonymous
524 assert !anon1.new_record?
525 assert !anon1.new_record?
525 assert_kind_of AnonymousUser, anon1
526 assert_kind_of AnonymousUser, anon1
526 anon2 = AnonymousUser.create(
527 anon2 = AnonymousUser.create(
527 :lastname => 'Anonymous', :firstname => '',
528 :lastname => 'Anonymous', :firstname => '',
528 :mail => '', :login => '', :status => 0)
529 :mail => '', :login => '', :status => 0)
529 assert_equal 1, anon2.errors.count
530 assert_equal 1, anon2.errors.count
530 end
531 end
531
532
532 should_have_one :rss_token
533 should_have_one :rss_token
533
534
534 def test_rss_key
535 def test_rss_key
535 assert_nil @jsmith.rss_token
536 assert_nil @jsmith.rss_token
536 key = @jsmith.rss_key
537 key = @jsmith.rss_key
537 assert_equal 40, key.length
538 assert_equal 40, key.length
538
539
539 @jsmith.reload
540 @jsmith.reload
540 assert_equal key, @jsmith.rss_key
541 assert_equal key, @jsmith.rss_key
541 end
542 end
542
543
543
544
544 should_have_one :api_token
545 should_have_one :api_token
545
546
546 context "User#api_key" do
547 context "User#api_key" do
547 should "generate a new one if the user doesn't have one" do
548 should "generate a new one if the user doesn't have one" do
548 user = User.generate_with_protected!(:api_token => nil)
549 user = User.generate_with_protected!(:api_token => nil)
549 assert_nil user.api_token
550 assert_nil user.api_token
550
551
551 key = user.api_key
552 key = user.api_key
552 assert_equal 40, key.length
553 assert_equal 40, key.length
553 user.reload
554 user.reload
554 assert_equal key, user.api_key
555 assert_equal key, user.api_key
555 end
556 end
556
557
557 should "return the existing api token value" do
558 should "return the existing api token value" do
558 user = User.generate_with_protected!
559 user = User.generate_with_protected!
559 token = Token.generate!(:action => 'api')
560 token = Token.generate!(:action => 'api')
560 user.api_token = token
561 user.api_token = token
561 assert user.save
562 assert user.save
562
563
563 assert_equal token.value, user.api_key
564 assert_equal token.value, user.api_key
564 end
565 end
565 end
566 end
566
567
567 context "User#find_by_api_key" do
568 context "User#find_by_api_key" do
568 should "return nil if no matching key is found" do
569 should "return nil if no matching key is found" do
569 assert_nil User.find_by_api_key('zzzzzzzzz')
570 assert_nil User.find_by_api_key('zzzzzzzzz')
570 end
571 end
571
572
572 should "return nil if the key is found for an inactive user" do
573 should "return nil if the key is found for an inactive user" do
573 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
574 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
574 token = Token.generate!(:action => 'api')
575 token = Token.generate!(:action => 'api')
575 user.api_token = token
576 user.api_token = token
576 user.save
577 user.save
577
578
578 assert_nil User.find_by_api_key(token.value)
579 assert_nil User.find_by_api_key(token.value)
579 end
580 end
580
581
581 should "return the user if the key is found for an active user" do
582 should "return the user if the key is found for an active user" do
582 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
583 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
583 token = Token.generate!(:action => 'api')
584 token = Token.generate!(:action => 'api')
584 user.api_token = token
585 user.api_token = token
585 user.save
586 user.save
586
587
587 assert_equal user, User.find_by_api_key(token.value)
588 assert_equal user, User.find_by_api_key(token.value)
588 end
589 end
589 end
590 end
590
591
591 def test_roles_for_project
592 def test_roles_for_project
592 # user with a role
593 # user with a role
593 roles = @jsmith.roles_for_project(Project.find(1))
594 roles = @jsmith.roles_for_project(Project.find(1))
594 assert_kind_of Role, roles.first
595 assert_kind_of Role, roles.first
595 assert_equal "Manager", roles.first.name
596 assert_equal "Manager", roles.first.name
596
597
597 # user with no role
598 # user with no role
598 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
599 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
599 end
600 end
600
601
601 def test_projects_by_role_for_user_with_role
602 def test_projects_by_role_for_user_with_role
602 user = User.find(2)
603 user = User.find(2)
603 assert_kind_of Hash, user.projects_by_role
604 assert_kind_of Hash, user.projects_by_role
604 assert_equal 2, user.projects_by_role.size
605 assert_equal 2, user.projects_by_role.size
605 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
606 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
606 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
607 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
607 end
608 end
608
609
609 def test_projects_by_role_for_user_with_no_role
610 def test_projects_by_role_for_user_with_no_role
610 user = User.generate!
611 user = User.generate!
611 assert_equal({}, user.projects_by_role)
612 assert_equal({}, user.projects_by_role)
612 end
613 end
613
614
614 def test_projects_by_role_for_anonymous
615 def test_projects_by_role_for_anonymous
615 assert_equal({}, User.anonymous.projects_by_role)
616 assert_equal({}, User.anonymous.projects_by_role)
616 end
617 end
617
618
618 def test_valid_notification_options
619 def test_valid_notification_options
619 # without memberships
620 # without memberships
620 assert_equal 5, User.find(7).valid_notification_options.size
621 assert_equal 5, User.find(7).valid_notification_options.size
621 # with memberships
622 # with memberships
622 assert_equal 6, User.find(2).valid_notification_options.size
623 assert_equal 6, User.find(2).valid_notification_options.size
623 end
624 end
624
625
625 def test_valid_notification_options_class_method
626 def test_valid_notification_options_class_method
626 assert_equal 5, User.valid_notification_options.size
627 assert_equal 5, User.valid_notification_options.size
627 assert_equal 5, User.valid_notification_options(User.find(7)).size
628 assert_equal 5, User.valid_notification_options(User.find(7)).size
628 assert_equal 6, User.valid_notification_options(User.find(2)).size
629 assert_equal 6, User.valid_notification_options(User.find(2)).size
629 end
630 end
630
631
631 def test_mail_notification_all
632 def test_mail_notification_all
632 @jsmith.mail_notification = 'all'
633 @jsmith.mail_notification = 'all'
633 @jsmith.notified_project_ids = []
634 @jsmith.notified_project_ids = []
634 @jsmith.save
635 @jsmith.save
635 @jsmith.reload
636 @jsmith.reload
636 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
637 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
637 end
638 end
638
639
639 def test_mail_notification_selected
640 def test_mail_notification_selected
640 @jsmith.mail_notification = 'selected'
641 @jsmith.mail_notification = 'selected'
641 @jsmith.notified_project_ids = [1]
642 @jsmith.notified_project_ids = [1]
642 @jsmith.save
643 @jsmith.save
643 @jsmith.reload
644 @jsmith.reload
644 assert Project.find(1).recipients.include?(@jsmith.mail)
645 assert Project.find(1).recipients.include?(@jsmith.mail)
645 end
646 end
646
647
647 def test_mail_notification_only_my_events
648 def test_mail_notification_only_my_events
648 @jsmith.mail_notification = 'only_my_events'
649 @jsmith.mail_notification = 'only_my_events'
649 @jsmith.notified_project_ids = []
650 @jsmith.notified_project_ids = []
650 @jsmith.save
651 @jsmith.save
651 @jsmith.reload
652 @jsmith.reload
652 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
653 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
653 end
654 end
654
655
655 def test_comments_sorting_preference
656 def test_comments_sorting_preference
656 assert !@jsmith.wants_comments_in_reverse_order?
657 assert !@jsmith.wants_comments_in_reverse_order?
657 @jsmith.pref.comments_sorting = 'asc'
658 @jsmith.pref.comments_sorting = 'asc'
658 assert !@jsmith.wants_comments_in_reverse_order?
659 assert !@jsmith.wants_comments_in_reverse_order?
659 @jsmith.pref.comments_sorting = 'desc'
660 @jsmith.pref.comments_sorting = 'desc'
660 assert @jsmith.wants_comments_in_reverse_order?
661 assert @jsmith.wants_comments_in_reverse_order?
661 end
662 end
662
663
663 def test_find_by_mail_should_be_case_insensitive
664 def test_find_by_mail_should_be_case_insensitive
664 u = User.find_by_mail('JSmith@somenet.foo')
665 u = User.find_by_mail('JSmith@somenet.foo')
665 assert_not_nil u
666 assert_not_nil u
666 assert_equal 'jsmith@somenet.foo', u.mail
667 assert_equal 'jsmith@somenet.foo', u.mail
667 end
668 end
668
669
669 def test_random_password
670 def test_random_password
670 u = User.new
671 u = User.new
671 u.random_password
672 u.random_password
672 assert !u.password.blank?
673 assert !u.password.blank?
673 assert !u.password_confirmation.blank?
674 assert !u.password_confirmation.blank?
674 end
675 end
675
676
676 context "#change_password_allowed?" do
677 context "#change_password_allowed?" do
677 should "be allowed if no auth source is set" do
678 should "be allowed if no auth source is set" do
678 user = User.generate_with_protected!
679 user = User.generate_with_protected!
679 assert user.change_password_allowed?
680 assert user.change_password_allowed?
680 end
681 end
681
682
682 should "delegate to the auth source" do
683 should "delegate to the auth source" do
683 user = User.generate_with_protected!
684 user = User.generate_with_protected!
684
685
685 allowed_auth_source = AuthSource.generate!
686 allowed_auth_source = AuthSource.generate!
686 def allowed_auth_source.allow_password_changes?; true; end
687 def allowed_auth_source.allow_password_changes?; true; end
687
688
688 denied_auth_source = AuthSource.generate!
689 denied_auth_source = AuthSource.generate!
689 def denied_auth_source.allow_password_changes?; false; end
690 def denied_auth_source.allow_password_changes?; false; end
690
691
691 assert user.change_password_allowed?
692 assert user.change_password_allowed?
692
693
693 user.auth_source = allowed_auth_source
694 user.auth_source = allowed_auth_source
694 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
695 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
695
696
696 user.auth_source = denied_auth_source
697 user.auth_source = denied_auth_source
697 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
698 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
698 end
699 end
699
700
700 end
701 end
701
702
702 context "#allowed_to?" do
703 context "#allowed_to?" do
703 context "with a unique project" do
704 context "with a unique project" do
704 should "return false if project is archived" do
705 should "return false if project is archived" do
705 project = Project.find(1)
706 project = Project.find(1)
706 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
707 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
707 assert ! @admin.allowed_to?(:view_issues, Project.find(1))
708 assert ! @admin.allowed_to?(:view_issues, Project.find(1))
708 end
709 end
709
710
710 should "return false if related module is disabled" do
711 should "return false if related module is disabled" do
711 project = Project.find(1)
712 project = Project.find(1)
712 project.enabled_module_names = ["issue_tracking"]
713 project.enabled_module_names = ["issue_tracking"]
713 assert @admin.allowed_to?(:add_issues, project)
714 assert @admin.allowed_to?(:add_issues, project)
714 assert ! @admin.allowed_to?(:view_wiki_pages, project)
715 assert ! @admin.allowed_to?(:view_wiki_pages, project)
715 end
716 end
716
717
717 should "authorize nearly everything for admin users" do
718 should "authorize nearly everything for admin users" do
718 project = Project.find(1)
719 project = Project.find(1)
719 assert ! @admin.member_of?(project)
720 assert ! @admin.member_of?(project)
720 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
721 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
721 assert @admin.allowed_to?(p.to_sym, project)
722 assert @admin.allowed_to?(p.to_sym, project)
722 end
723 end
723 end
724 end
724
725
725 should "authorize normal users depending on their roles" do
726 should "authorize normal users depending on their roles" do
726 project = Project.find(1)
727 project = Project.find(1)
727 assert @jsmith.allowed_to?(:delete_messages, project) #Manager
728 assert @jsmith.allowed_to?(:delete_messages, project) #Manager
728 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
729 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
729 end
730 end
730 end
731 end
731
732
732 context "with multiple projects" do
733 context "with multiple projects" do
733 should "return false if array is empty" do
734 should "return false if array is empty" do
734 assert ! @admin.allowed_to?(:view_project, [])
735 assert ! @admin.allowed_to?(:view_project, [])
735 end
736 end
736
737
737 should "return true only if user has permission on all these projects" do
738 should "return true only if user has permission on all these projects" do
738 assert @admin.allowed_to?(:view_project, Project.all)
739 assert @admin.allowed_to?(:view_project, Project.all)
739 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
740 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
740 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
741 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
741 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
742 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
742 end
743 end
743
744
744 should "behave correctly with arrays of 1 project" do
745 should "behave correctly with arrays of 1 project" do
745 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
746 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
746 end
747 end
747 end
748 end
748
749
749 context "with options[:global]" do
750 context "with options[:global]" do
750 should "authorize if user has at least one role that has this permission" do
751 should "authorize if user has at least one role that has this permission" do
751 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
752 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
752 @anonymous = User.find(6)
753 @anonymous = User.find(6)
753 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
754 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
754 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
755 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
755 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
756 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
756 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
757 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
757 assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
758 assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
758 end
759 end
759 end
760 end
760 end
761 end
761
762
762 context "User#notify_about?" do
763 context "User#notify_about?" do
763 context "Issues" do
764 context "Issues" do
764 setup do
765 setup do
765 @project = Project.find(1)
766 @project = Project.find(1)
766 @author = User.generate_with_protected!
767 @author = User.generate_with_protected!
767 @assignee = User.generate_with_protected!
768 @assignee = User.generate_with_protected!
768 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
769 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
769 end
770 end
770
771
771 should "be true for a user with :all" do
772 should "be true for a user with :all" do
772 @author.update_attribute(:mail_notification, 'all')
773 @author.update_attribute(:mail_notification, 'all')
773 assert @author.notify_about?(@issue)
774 assert @author.notify_about?(@issue)
774 end
775 end
775
776
776 should "be false for a user with :none" do
777 should "be false for a user with :none" do
777 @author.update_attribute(:mail_notification, 'none')
778 @author.update_attribute(:mail_notification, 'none')
778 assert ! @author.notify_about?(@issue)
779 assert ! @author.notify_about?(@issue)
779 end
780 end
780
781
781 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
782 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
782 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
783 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
783 Member.create!(:user => @user, :project => @project, :role_ids => [1])
784 Member.create!(:user => @user, :project => @project, :role_ids => [1])
784 assert ! @user.notify_about?(@issue)
785 assert ! @user.notify_about?(@issue)
785 end
786 end
786
787
787 should "be true for a user with :only_my_events and is the author" do
788 should "be true for a user with :only_my_events and is the author" do
788 @author.update_attribute(:mail_notification, 'only_my_events')
789 @author.update_attribute(:mail_notification, 'only_my_events')
789 assert @author.notify_about?(@issue)
790 assert @author.notify_about?(@issue)
790 end
791 end
791
792
792 should "be true for a user with :only_my_events and is the assignee" do
793 should "be true for a user with :only_my_events and is the assignee" do
793 @assignee.update_attribute(:mail_notification, 'only_my_events')
794 @assignee.update_attribute(:mail_notification, 'only_my_events')
794 assert @assignee.notify_about?(@issue)
795 assert @assignee.notify_about?(@issue)
795 end
796 end
796
797
797 should "be true for a user with :only_assigned and is the assignee" do
798 should "be true for a user with :only_assigned and is the assignee" do
798 @assignee.update_attribute(:mail_notification, 'only_assigned')
799 @assignee.update_attribute(:mail_notification, 'only_assigned')
799 assert @assignee.notify_about?(@issue)
800 assert @assignee.notify_about?(@issue)
800 end
801 end
801
802
802 should "be false for a user with :only_assigned and is not the assignee" do
803 should "be false for a user with :only_assigned and is not the assignee" do
803 @author.update_attribute(:mail_notification, 'only_assigned')
804 @author.update_attribute(:mail_notification, 'only_assigned')
804 assert ! @author.notify_about?(@issue)
805 assert ! @author.notify_about?(@issue)
805 end
806 end
806
807
807 should "be true for a user with :only_owner and is the author" do
808 should "be true for a user with :only_owner and is the author" do
808 @author.update_attribute(:mail_notification, 'only_owner')
809 @author.update_attribute(:mail_notification, 'only_owner')
809 assert @author.notify_about?(@issue)
810 assert @author.notify_about?(@issue)
810 end
811 end
811
812
812 should "be false for a user with :only_owner and is not the author" do
813 should "be false for a user with :only_owner and is not the author" do
813 @assignee.update_attribute(:mail_notification, 'only_owner')
814 @assignee.update_attribute(:mail_notification, 'only_owner')
814 assert ! @assignee.notify_about?(@issue)
815 assert ! @assignee.notify_about?(@issue)
815 end
816 end
816
817
817 should "be true for a user with :selected and is the author" do
818 should "be true for a user with :selected and is the author" do
818 @author.update_attribute(:mail_notification, 'selected')
819 @author.update_attribute(:mail_notification, 'selected')
819 assert @author.notify_about?(@issue)
820 assert @author.notify_about?(@issue)
820 end
821 end
821
822
822 should "be true for a user with :selected and is the assignee" do
823 should "be true for a user with :selected and is the assignee" do
823 @assignee.update_attribute(:mail_notification, 'selected')
824 @assignee.update_attribute(:mail_notification, 'selected')
824 assert @assignee.notify_about?(@issue)
825 assert @assignee.notify_about?(@issue)
825 end
826 end
826
827
827 should "be false for a user with :selected and is not the author or assignee" do
828 should "be false for a user with :selected and is not the author or assignee" do
828 @user = User.generate_with_protected!(:mail_notification => 'selected')
829 @user = User.generate_with_protected!(:mail_notification => 'selected')
829 Member.create!(:user => @user, :project => @project, :role_ids => [1])
830 Member.create!(:user => @user, :project => @project, :role_ids => [1])
830 assert ! @user.notify_about?(@issue)
831 assert ! @user.notify_about?(@issue)
831 end
832 end
832 end
833 end
833
834
834 context "other events" do
835 context "other events" do
835 should 'be added and tested'
836 should 'be added and tested'
836 end
837 end
837 end
838 end
838
839
839 def test_salt_unsalted_passwords
840 def test_salt_unsalted_passwords
840 # Restore a user with an unsalted password
841 # Restore a user with an unsalted password
841 user = User.find(1)
842 user = User.find(1)
842 user.salt = nil
843 user.salt = nil
843 user.hashed_password = User.hash_password("unsalted")
844 user.hashed_password = User.hash_password("unsalted")
844 user.save!
845 user.save!
845
846
846 User.salt_unsalted_passwords!
847 User.salt_unsalted_passwords!
847
848
848 user.reload
849 user.reload
849 # Salt added
850 # Salt added
850 assert !user.salt.blank?
851 assert !user.salt.blank?
851 # Password still valid
852 # Password still valid
852 assert user.check_password?("unsalted")
853 assert user.check_password?("unsalted")
853 assert_equal user, User.try_to_login(user.login, "unsalted")
854 assert_equal user, User.try_to_login(user.login, "unsalted")
854 end
855 end
855
856
856 if Object.const_defined?(:OpenID)
857 if Object.const_defined?(:OpenID)
857
858
858 def test_setting_identity_url
859 def test_setting_identity_url
859 normalized_open_id_url = 'http://example.com/'
860 normalized_open_id_url = 'http://example.com/'
860 u = User.new( :identity_url => 'http://example.com/' )
861 u = User.new( :identity_url => 'http://example.com/' )
861 assert_equal normalized_open_id_url, u.identity_url
862 assert_equal normalized_open_id_url, u.identity_url
862 end
863 end
863
864
864 def test_setting_identity_url_without_trailing_slash
865 def test_setting_identity_url_without_trailing_slash
865 normalized_open_id_url = 'http://example.com/'
866 normalized_open_id_url = 'http://example.com/'
866 u = User.new( :identity_url => 'http://example.com' )
867 u = User.new( :identity_url => 'http://example.com' )
867 assert_equal normalized_open_id_url, u.identity_url
868 assert_equal normalized_open_id_url, u.identity_url
868 end
869 end
869
870
870 def test_setting_identity_url_without_protocol
871 def test_setting_identity_url_without_protocol
871 normalized_open_id_url = 'http://example.com/'
872 normalized_open_id_url = 'http://example.com/'
872 u = User.new( :identity_url => 'example.com' )
873 u = User.new( :identity_url => 'example.com' )
873 assert_equal normalized_open_id_url, u.identity_url
874 assert_equal normalized_open_id_url, u.identity_url
874 end
875 end
875
876
876 def test_setting_blank_identity_url
877 def test_setting_blank_identity_url
877 u = User.new( :identity_url => 'example.com' )
878 u = User.new( :identity_url => 'example.com' )
878 u.identity_url = ''
879 u.identity_url = ''
879 assert u.identity_url.blank?
880 assert u.identity_url.blank?
880 end
881 end
881
882
882 def test_setting_invalid_identity_url
883 def test_setting_invalid_identity_url
883 u = User.new( :identity_url => 'this is not an openid url' )
884 u = User.new( :identity_url => 'this is not an openid url' )
884 assert u.identity_url.blank?
885 assert u.identity_url.blank?
885 end
886 end
886
887
887 else
888 else
888 puts "Skipping openid tests."
889 puts "Skipping openid tests."
889 end
890 end
890
891
891 end
892 end
General Comments 0
You need to be logged in to leave comments. Login now