##// END OF EJS Templates
Adds a test for User.try_to_login with active_only set to false....
Jean-Philippe Lang -
r11718:86cfa025edc8
parent child
Show More
@@ -1,1130 +1,1135
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 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
29
30 def setup
30 def setup
31 @admin = User.find(1)
31 @admin = User.find(1)
32 @jsmith = User.find(2)
32 @jsmith = User.find(2)
33 @dlopper = User.find(3)
33 @dlopper = User.find(3)
34 end
34 end
35
35
36 def test_sorted_scope_should_sort_user_by_display_name
36 def test_sorted_scope_should_sort_user_by_display_name
37 assert_equal User.all.map(&:name).map(&:downcase).sort, User.sorted.all.map(&:name).map(&:downcase)
37 assert_equal User.all.map(&:name).map(&:downcase).sort, User.sorted.all.map(&:name).map(&:downcase)
38 end
38 end
39
39
40 def test_generate
40 def test_generate
41 User.generate!(:firstname => 'Testing connection')
41 User.generate!(:firstname => 'Testing connection')
42 User.generate!(:firstname => 'Testing connection')
42 User.generate!(:firstname => 'Testing connection')
43 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
43 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
44 end
44 end
45
45
46 def test_truth
46 def test_truth
47 assert_kind_of User, @jsmith
47 assert_kind_of User, @jsmith
48 end
48 end
49
49
50 def test_mail_should_be_stripped
50 def test_mail_should_be_stripped
51 u = User.new
51 u = User.new
52 u.mail = " foo@bar.com "
52 u.mail = " foo@bar.com "
53 assert_equal "foo@bar.com", u.mail
53 assert_equal "foo@bar.com", u.mail
54 end
54 end
55
55
56 def test_mail_validation
56 def test_mail_validation
57 u = User.new
57 u = User.new
58 u.mail = ''
58 u.mail = ''
59 assert !u.valid?
59 assert !u.valid?
60 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
60 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
61 end
61 end
62
62
63 def test_login_length_validation
63 def test_login_length_validation
64 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
64 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
65 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
65 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
66 assert !user.valid?
66 assert !user.valid?
67
67
68 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
68 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
69 assert user.valid?
69 assert user.valid?
70 assert user.save
70 assert user.save
71 end
71 end
72
72
73 def test_generate_password_should_respect_minimum_password_length
73 def test_generate_password_should_respect_minimum_password_length
74 with_settings :password_min_length => 15 do
74 with_settings :password_min_length => 15 do
75 user = User.generate!(:generate_password => true)
75 user = User.generate!(:generate_password => true)
76 assert user.password.length >= 15
76 assert user.password.length >= 15
77 end
77 end
78 end
78 end
79
79
80 def test_generate_password_should_not_generate_password_with_less_than_10_characters
80 def test_generate_password_should_not_generate_password_with_less_than_10_characters
81 with_settings :password_min_length => 4 do
81 with_settings :password_min_length => 4 do
82 user = User.generate!(:generate_password => true)
82 user = User.generate!(:generate_password => true)
83 assert user.password.length >= 10
83 assert user.password.length >= 10
84 end
84 end
85 end
85 end
86
86
87 def test_generate_password_on_create_should_set_password
87 def test_generate_password_on_create_should_set_password
88 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
88 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
89 user.login = "newuser"
89 user.login = "newuser"
90 user.generate_password = true
90 user.generate_password = true
91 assert user.save
91 assert user.save
92
92
93 password = user.password
93 password = user.password
94 assert user.check_password?(password)
94 assert user.check_password?(password)
95 end
95 end
96
96
97 def test_generate_password_on_update_should_update_password
97 def test_generate_password_on_update_should_update_password
98 user = User.find(2)
98 user = User.find(2)
99 hash = user.hashed_password
99 hash = user.hashed_password
100 user.generate_password = true
100 user.generate_password = true
101 assert user.save
101 assert user.save
102
102
103 password = user.password
103 password = user.password
104 assert user.check_password?(password)
104 assert user.check_password?(password)
105 assert_not_equal hash, user.reload.hashed_password
105 assert_not_equal hash, user.reload.hashed_password
106 end
106 end
107
107
108 def test_create
108 def test_create
109 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
109 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
110
110
111 user.login = "jsmith"
111 user.login = "jsmith"
112 user.password, user.password_confirmation = "password", "password"
112 user.password, user.password_confirmation = "password", "password"
113 # login uniqueness
113 # login uniqueness
114 assert !user.save
114 assert !user.save
115 assert_equal 1, user.errors.count
115 assert_equal 1, user.errors.count
116
116
117 user.login = "newuser"
117 user.login = "newuser"
118 user.password, user.password_confirmation = "password", "pass"
118 user.password, user.password_confirmation = "password", "pass"
119 # password confirmation
119 # password confirmation
120 assert !user.save
120 assert !user.save
121 assert_equal 1, user.errors.count
121 assert_equal 1, user.errors.count
122
122
123 user.password, user.password_confirmation = "password", "password"
123 user.password, user.password_confirmation = "password", "password"
124 assert user.save
124 assert user.save
125 end
125 end
126
126
127 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting
127 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting
128 @user1 = User.generate!
128 @user1 = User.generate!
129 assert_equal 'only_my_events', @user1.mail_notification
129 assert_equal 'only_my_events', @user1.mail_notification
130 with_settings :default_notification_option => 'all' do
130 with_settings :default_notification_option => 'all' do
131 @user2 = User.generate!
131 @user2 = User.generate!
132 assert_equal 'all', @user2.mail_notification
132 assert_equal 'all', @user2.mail_notification
133 end
133 end
134 end
134 end
135
135
136 def test_user_login_should_be_case_insensitive
136 def test_user_login_should_be_case_insensitive
137 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
137 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
138 u.login = 'newuser'
138 u.login = 'newuser'
139 u.password, u.password_confirmation = "password", "password"
139 u.password, u.password_confirmation = "password", "password"
140 assert u.save
140 assert u.save
141 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
141 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
142 u.login = 'NewUser'
142 u.login = 'NewUser'
143 u.password, u.password_confirmation = "password", "password"
143 u.password, u.password_confirmation = "password", "password"
144 assert !u.save
144 assert !u.save
145 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
145 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
146 end
146 end
147
147
148 def test_mail_uniqueness_should_not_be_case_sensitive
148 def test_mail_uniqueness_should_not_be_case_sensitive
149 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
149 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
150 u.login = 'newuser1'
150 u.login = 'newuser1'
151 u.password, u.password_confirmation = "password", "password"
151 u.password, u.password_confirmation = "password", "password"
152 assert u.save
152 assert u.save
153
153
154 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
154 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
155 u.login = 'newuser2'
155 u.login = 'newuser2'
156 u.password, u.password_confirmation = "password", "password"
156 u.password, u.password_confirmation = "password", "password"
157 assert !u.save
157 assert !u.save
158 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
158 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
159 end
159 end
160
160
161 def test_update
161 def test_update
162 assert_equal "admin", @admin.login
162 assert_equal "admin", @admin.login
163 @admin.login = "john"
163 @admin.login = "john"
164 assert @admin.save, @admin.errors.full_messages.join("; ")
164 assert @admin.save, @admin.errors.full_messages.join("; ")
165 @admin.reload
165 @admin.reload
166 assert_equal "john", @admin.login
166 assert_equal "john", @admin.login
167 end
167 end
168
168
169 def test_update_should_not_fail_for_legacy_user_with_different_case_logins
169 def test_update_should_not_fail_for_legacy_user_with_different_case_logins
170 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
170 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
171 u1.login = 'newuser1'
171 u1.login = 'newuser1'
172 assert u1.save
172 assert u1.save
173
173
174 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
174 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
175 u2.login = 'newuser1'
175 u2.login = 'newuser1'
176 assert u2.save(:validate => false)
176 assert u2.save(:validate => false)
177
177
178 user = User.find(u2.id)
178 user = User.find(u2.id)
179 user.firstname = "firstname"
179 user.firstname = "firstname"
180 assert user.save, "Save failed"
180 assert user.save, "Save failed"
181 end
181 end
182
182
183 def test_destroy_should_delete_members_and_roles
183 def test_destroy_should_delete_members_and_roles
184 members = Member.find_all_by_user_id(2)
184 members = Member.find_all_by_user_id(2)
185 ms = members.size
185 ms = members.size
186 rs = members.collect(&:roles).flatten.size
186 rs = members.collect(&:roles).flatten.size
187
187
188 assert_difference 'Member.count', - ms do
188 assert_difference 'Member.count', - ms do
189 assert_difference 'MemberRole.count', - rs do
189 assert_difference 'MemberRole.count', - rs do
190 User.find(2).destroy
190 User.find(2).destroy
191 end
191 end
192 end
192 end
193
193
194 assert_nil User.find_by_id(2)
194 assert_nil User.find_by_id(2)
195 assert Member.find_all_by_user_id(2).empty?
195 assert Member.find_all_by_user_id(2).empty?
196 end
196 end
197
197
198 def test_destroy_should_update_attachments
198 def test_destroy_should_update_attachments
199 attachment = Attachment.create!(:container => Project.find(1),
199 attachment = Attachment.create!(:container => Project.find(1),
200 :file => uploaded_test_file("testfile.txt", "text/plain"),
200 :file => uploaded_test_file("testfile.txt", "text/plain"),
201 :author_id => 2)
201 :author_id => 2)
202
202
203 User.find(2).destroy
203 User.find(2).destroy
204 assert_nil User.find_by_id(2)
204 assert_nil User.find_by_id(2)
205 assert_equal User.anonymous, attachment.reload.author
205 assert_equal User.anonymous, attachment.reload.author
206 end
206 end
207
207
208 def test_destroy_should_update_comments
208 def test_destroy_should_update_comments
209 comment = Comment.create!(
209 comment = Comment.create!(
210 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
210 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
211 :author => User.find(2),
211 :author => User.find(2),
212 :comments => 'foo'
212 :comments => 'foo'
213 )
213 )
214
214
215 User.find(2).destroy
215 User.find(2).destroy
216 assert_nil User.find_by_id(2)
216 assert_nil User.find_by_id(2)
217 assert_equal User.anonymous, comment.reload.author
217 assert_equal User.anonymous, comment.reload.author
218 end
218 end
219
219
220 def test_destroy_should_update_issues
220 def test_destroy_should_update_issues
221 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
221 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
222
222
223 User.find(2).destroy
223 User.find(2).destroy
224 assert_nil User.find_by_id(2)
224 assert_nil User.find_by_id(2)
225 assert_equal User.anonymous, issue.reload.author
225 assert_equal User.anonymous, issue.reload.author
226 end
226 end
227
227
228 def test_destroy_should_unassign_issues
228 def test_destroy_should_unassign_issues
229 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
229 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
230
230
231 User.find(2).destroy
231 User.find(2).destroy
232 assert_nil User.find_by_id(2)
232 assert_nil User.find_by_id(2)
233 assert_nil issue.reload.assigned_to
233 assert_nil issue.reload.assigned_to
234 end
234 end
235
235
236 def test_destroy_should_update_journals
236 def test_destroy_should_update_journals
237 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
237 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
238 issue.init_journal(User.find(2), "update")
238 issue.init_journal(User.find(2), "update")
239 issue.save!
239 issue.save!
240
240
241 User.find(2).destroy
241 User.find(2).destroy
242 assert_nil User.find_by_id(2)
242 assert_nil User.find_by_id(2)
243 assert_equal User.anonymous, issue.journals.first.reload.user
243 assert_equal User.anonymous, issue.journals.first.reload.user
244 end
244 end
245
245
246 def test_destroy_should_update_journal_details_old_value
246 def test_destroy_should_update_journal_details_old_value
247 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
247 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
248 issue.init_journal(User.find(1), "update")
248 issue.init_journal(User.find(1), "update")
249 issue.assigned_to_id = nil
249 issue.assigned_to_id = nil
250 assert_difference 'JournalDetail.count' do
250 assert_difference 'JournalDetail.count' do
251 issue.save!
251 issue.save!
252 end
252 end
253 journal_detail = JournalDetail.first(:order => 'id DESC')
253 journal_detail = JournalDetail.first(:order => 'id DESC')
254 assert_equal '2', journal_detail.old_value
254 assert_equal '2', journal_detail.old_value
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_equal User.anonymous.id.to_s, journal_detail.reload.old_value
258 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
259 end
259 end
260
260
261 def test_destroy_should_update_journal_details_value
261 def test_destroy_should_update_journal_details_value
262 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
262 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
263 issue.init_journal(User.find(1), "update")
263 issue.init_journal(User.find(1), "update")
264 issue.assigned_to_id = 2
264 issue.assigned_to_id = 2
265 assert_difference 'JournalDetail.count' do
265 assert_difference 'JournalDetail.count' do
266 issue.save!
266 issue.save!
267 end
267 end
268 journal_detail = JournalDetail.first(:order => 'id DESC')
268 journal_detail = JournalDetail.first(:order => 'id DESC')
269 assert_equal '2', journal_detail.value
269 assert_equal '2', journal_detail.value
270
270
271 User.find(2).destroy
271 User.find(2).destroy
272 assert_nil User.find_by_id(2)
272 assert_nil User.find_by_id(2)
273 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
273 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
274 end
274 end
275
275
276 def test_destroy_should_update_messages
276 def test_destroy_should_update_messages
277 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
277 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
278 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
278 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
279
279
280 User.find(2).destroy
280 User.find(2).destroy
281 assert_nil User.find_by_id(2)
281 assert_nil User.find_by_id(2)
282 assert_equal User.anonymous, message.reload.author
282 assert_equal User.anonymous, message.reload.author
283 end
283 end
284
284
285 def test_destroy_should_update_news
285 def test_destroy_should_update_news
286 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
286 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
287
287
288 User.find(2).destroy
288 User.find(2).destroy
289 assert_nil User.find_by_id(2)
289 assert_nil User.find_by_id(2)
290 assert_equal User.anonymous, news.reload.author
290 assert_equal User.anonymous, news.reload.author
291 end
291 end
292
292
293 def test_destroy_should_delete_private_queries
293 def test_destroy_should_delete_private_queries
294 query = Query.new(:name => 'foo', :is_public => false)
294 query = Query.new(:name => 'foo', :is_public => false)
295 query.project_id = 1
295 query.project_id = 1
296 query.user_id = 2
296 query.user_id = 2
297 query.save!
297 query.save!
298
298
299 User.find(2).destroy
299 User.find(2).destroy
300 assert_nil User.find_by_id(2)
300 assert_nil User.find_by_id(2)
301 assert_nil Query.find_by_id(query.id)
301 assert_nil Query.find_by_id(query.id)
302 end
302 end
303
303
304 def test_destroy_should_update_public_queries
304 def test_destroy_should_update_public_queries
305 query = Query.new(:name => 'foo', :is_public => true)
305 query = Query.new(:name => 'foo', :is_public => true)
306 query.project_id = 1
306 query.project_id = 1
307 query.user_id = 2
307 query.user_id = 2
308 query.save!
308 query.save!
309
309
310 User.find(2).destroy
310 User.find(2).destroy
311 assert_nil User.find_by_id(2)
311 assert_nil User.find_by_id(2)
312 assert_equal User.anonymous, query.reload.user
312 assert_equal User.anonymous, query.reload.user
313 end
313 end
314
314
315 def test_destroy_should_update_time_entries
315 def test_destroy_should_update_time_entries
316 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
316 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
317 entry.project_id = 1
317 entry.project_id = 1
318 entry.user_id = 2
318 entry.user_id = 2
319 entry.save!
319 entry.save!
320
320
321 User.find(2).destroy
321 User.find(2).destroy
322 assert_nil User.find_by_id(2)
322 assert_nil User.find_by_id(2)
323 assert_equal User.anonymous, entry.reload.user
323 assert_equal User.anonymous, entry.reload.user
324 end
324 end
325
325
326 def test_destroy_should_delete_tokens
326 def test_destroy_should_delete_tokens
327 token = Token.create!(:user_id => 2, :value => 'foo')
327 token = Token.create!(:user_id => 2, :value => 'foo')
328
328
329 User.find(2).destroy
329 User.find(2).destroy
330 assert_nil User.find_by_id(2)
330 assert_nil User.find_by_id(2)
331 assert_nil Token.find_by_id(token.id)
331 assert_nil Token.find_by_id(token.id)
332 end
332 end
333
333
334 def test_destroy_should_delete_watchers
334 def test_destroy_should_delete_watchers
335 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
335 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
336 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
336 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
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 Watcher.find_by_id(watcher.id)
340 assert_nil Watcher.find_by_id(watcher.id)
341 end
341 end
342
342
343 def test_destroy_should_update_wiki_contents
343 def test_destroy_should_update_wiki_contents
344 wiki_content = WikiContent.create!(
344 wiki_content = WikiContent.create!(
345 :text => 'foo',
345 :text => 'foo',
346 :author_id => 2,
346 :author_id => 2,
347 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
347 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
348 )
348 )
349 wiki_content.text = 'bar'
349 wiki_content.text = 'bar'
350 assert_difference 'WikiContent::Version.count' do
350 assert_difference 'WikiContent::Version.count' do
351 wiki_content.save!
351 wiki_content.save!
352 end
352 end
353
353
354 User.find(2).destroy
354 User.find(2).destroy
355 assert_nil User.find_by_id(2)
355 assert_nil User.find_by_id(2)
356 assert_equal User.anonymous, wiki_content.reload.author
356 assert_equal User.anonymous, wiki_content.reload.author
357 wiki_content.versions.each do |version|
357 wiki_content.versions.each do |version|
358 assert_equal User.anonymous, version.reload.author
358 assert_equal User.anonymous, version.reload.author
359 end
359 end
360 end
360 end
361
361
362 def test_destroy_should_nullify_issue_categories
362 def test_destroy_should_nullify_issue_categories
363 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
363 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
364
364
365 User.find(2).destroy
365 User.find(2).destroy
366 assert_nil User.find_by_id(2)
366 assert_nil User.find_by_id(2)
367 assert_nil category.reload.assigned_to_id
367 assert_nil category.reload.assigned_to_id
368 end
368 end
369
369
370 def test_destroy_should_nullify_changesets
370 def test_destroy_should_nullify_changesets
371 changeset = Changeset.create!(
371 changeset = Changeset.create!(
372 :repository => Repository::Subversion.create!(
372 :repository => Repository::Subversion.create!(
373 :project_id => 1,
373 :project_id => 1,
374 :url => 'file:///tmp',
374 :url => 'file:///tmp',
375 :identifier => 'tmp'
375 :identifier => 'tmp'
376 ),
376 ),
377 :revision => '12',
377 :revision => '12',
378 :committed_on => Time.now,
378 :committed_on => Time.now,
379 :committer => 'jsmith'
379 :committer => 'jsmith'
380 )
380 )
381 assert_equal 2, changeset.user_id
381 assert_equal 2, changeset.user_id
382
382
383 User.find(2).destroy
383 User.find(2).destroy
384 assert_nil User.find_by_id(2)
384 assert_nil User.find_by_id(2)
385 assert_nil changeset.reload.user_id
385 assert_nil changeset.reload.user_id
386 end
386 end
387
387
388 def test_anonymous_user_should_not_be_destroyable
388 def test_anonymous_user_should_not_be_destroyable
389 assert_no_difference 'User.count' do
389 assert_no_difference 'User.count' do
390 assert_equal false, User.anonymous.destroy
390 assert_equal false, User.anonymous.destroy
391 end
391 end
392 end
392 end
393
393
394 def test_validate_login_presence
394 def test_validate_login_presence
395 @admin.login = ""
395 @admin.login = ""
396 assert !@admin.save
396 assert !@admin.save
397 assert_equal 1, @admin.errors.count
397 assert_equal 1, @admin.errors.count
398 end
398 end
399
399
400 def test_validate_mail_notification_inclusion
400 def test_validate_mail_notification_inclusion
401 u = User.new
401 u = User.new
402 u.mail_notification = 'foo'
402 u.mail_notification = 'foo'
403 u.save
403 u.save
404 assert_not_nil u.errors[:mail_notification]
404 assert_not_nil u.errors[:mail_notification]
405 end
405 end
406
406
407 test "User#try_to_login should fall-back to case-insensitive if user login is not found as-typed" do
408 user = User.try_to_login("AdMin", "admin")
409 assert_kind_of User, user
410 assert_equal "admin", user.login
411 end
412
413 test "User#try_to_login should select the exact matching user first" do
414 case_sensitive_user = User.generate! do |user|
415 user.password = "admin123"
416 end
417 # bypass validations to make it appear like existing data
418 case_sensitive_user.update_attribute(:login, 'ADMIN')
419
420 user = User.try_to_login("ADMIN", "admin123")
421 assert_kind_of User, user
422 assert_equal "ADMIN", user.login
423 end
424
425 def test_password
407 def test_password
426 user = User.try_to_login("admin", "admin")
408 user = User.try_to_login("admin", "admin")
427 assert_kind_of User, user
409 assert_kind_of User, user
428 assert_equal "admin", user.login
410 assert_equal "admin", user.login
429 user.password = "hello123"
411 user.password = "hello123"
430 assert user.save
412 assert user.save
431
413
432 user = User.try_to_login("admin", "hello123")
414 user = User.try_to_login("admin", "hello123")
433 assert_kind_of User, user
415 assert_kind_of User, user
434 assert_equal "admin", user.login
416 assert_equal "admin", user.login
435 end
417 end
436
418
437 def test_validate_password_length
419 def test_validate_password_length
438 with_settings :password_min_length => '100' do
420 with_settings :password_min_length => '100' do
439 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
421 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
440 user.login = "newuser100"
422 user.login = "newuser100"
441 user.password, user.password_confirmation = "password100", "password100"
423 user.password, user.password_confirmation = "password100", "password100"
442 assert !user.save
424 assert !user.save
443 assert_equal 1, user.errors.count
425 assert_equal 1, user.errors.count
444 end
426 end
445 end
427 end
446
428
447 def test_name_format
429 def test_name_format
448 assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
430 assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
449 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
431 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
450 with_settings :user_format => :firstname_lastname do
432 with_settings :user_format => :firstname_lastname do
451 assert_equal 'John Smith', @jsmith.reload.name
433 assert_equal 'John Smith', @jsmith.reload.name
452 end
434 end
453 with_settings :user_format => :username do
435 with_settings :user_format => :username do
454 assert_equal 'jsmith', @jsmith.reload.name
436 assert_equal 'jsmith', @jsmith.reload.name
455 end
437 end
456 with_settings :user_format => :lastname do
438 with_settings :user_format => :lastname do
457 assert_equal 'Smith', @jsmith.reload.name
439 assert_equal 'Smith', @jsmith.reload.name
458 end
440 end
459 end
441 end
460
442
461 def test_today_should_return_the_day_according_to_user_time_zone
443 def test_today_should_return_the_day_according_to_user_time_zone
462 preference = User.find(1).pref
444 preference = User.find(1).pref
463 date = Date.new(2012, 05, 15)
445 date = Date.new(2012, 05, 15)
464 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
446 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
465 Date.stubs(:today).returns(date)
447 Date.stubs(:today).returns(date)
466 Time.stubs(:now).returns(time)
448 Time.stubs(:now).returns(time)
467
449
468 preference.update_attribute :time_zone, 'Baku' # UTC+4
450 preference.update_attribute :time_zone, 'Baku' # UTC+4
469 assert_equal '2012-05-16', User.find(1).today.to_s
451 assert_equal '2012-05-16', User.find(1).today.to_s
470
452
471 preference.update_attribute :time_zone, 'La Paz' # UTC-4
453 preference.update_attribute :time_zone, 'La Paz' # UTC-4
472 assert_equal '2012-05-15', User.find(1).today.to_s
454 assert_equal '2012-05-15', User.find(1).today.to_s
473
455
474 preference.update_attribute :time_zone, ''
456 preference.update_attribute :time_zone, ''
475 assert_equal '2012-05-15', User.find(1).today.to_s
457 assert_equal '2012-05-15', User.find(1).today.to_s
476 end
458 end
477
459
478 def test_time_to_date_should_return_the_date_according_to_user_time_zone
460 def test_time_to_date_should_return_the_date_according_to_user_time_zone
479 preference = User.find(1).pref
461 preference = User.find(1).pref
480 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
462 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
481
463
482 preference.update_attribute :time_zone, 'Baku' # UTC+4
464 preference.update_attribute :time_zone, 'Baku' # UTC+4
483 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s
465 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s
484
466
485 preference.update_attribute :time_zone, 'La Paz' # UTC-4
467 preference.update_attribute :time_zone, 'La Paz' # UTC-4
486 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
468 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
487
469
488 preference.update_attribute :time_zone, ''
470 preference.update_attribute :time_zone, ''
489 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
471 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
490 end
472 end
491
473
492 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
474 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
493 with_settings :user_format => 'lastname_coma_firstname' do
475 with_settings :user_format => 'lastname_coma_firstname' do
494 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
476 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
495 end
477 end
496 end
478 end
497
479
498 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
480 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
499 with_settings :user_format => 'lastname_firstname' do
481 with_settings :user_format => 'lastname_firstname' do
500 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
482 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
501 end
483 end
502 end
484 end
503
485
504 def test_fields_for_order_statement_with_blank_format_should_return_default
486 def test_fields_for_order_statement_with_blank_format_should_return_default
505 with_settings :user_format => '' do
487 with_settings :user_format => '' do
506 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
488 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
507 end
489 end
508 end
490 end
509
491
510 def test_fields_for_order_statement_with_invalid_format_should_return_default
492 def test_fields_for_order_statement_with_invalid_format_should_return_default
511 with_settings :user_format => 'foo' do
493 with_settings :user_format => 'foo' do
512 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
494 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
513 end
495 end
514 end
496 end
515
497
516 def test_lock
498 test ".try_to_login with good credentials should return the user" do
517 user = User.try_to_login("jsmith", "jsmith")
499 user = User.try_to_login("admin", "admin")
518 assert_equal @jsmith, user
500 assert_kind_of User, user
501 assert_equal "admin", user.login
502 end
519
503
504 test ".try_to_login with wrong credentials should return nil" do
505 assert_nil User.try_to_login("admin", "foo")
506 end
507
508 def test_try_to_login_with_locked_user_should_return_nil
520 @jsmith.status = User::STATUS_LOCKED
509 @jsmith.status = User::STATUS_LOCKED
521 assert @jsmith.save
510 @jsmith.save!
522
511
523 user = User.try_to_login("jsmith", "jsmith")
512 user = User.try_to_login("jsmith", "jsmith")
524 assert_equal nil, user
513 assert_equal nil, user
525 end
514 end
526
515
527 test ".try_to_login with good credentials should return the user" do
516 def test_try_to_login_with_locked_user_and_not_active_only_should_return_user
528 user = User.try_to_login("admin", "admin")
517 @jsmith.status = User::STATUS_LOCKED
518 @jsmith.save!
519
520 user = User.try_to_login("jsmith", "jsmith", false)
521 assert_equal @jsmith, user
522 end
523
524 test ".try_to_login should fall-back to case-insensitive if user login is not found as-typed" do
525 user = User.try_to_login("AdMin", "admin")
529 assert_kind_of User, user
526 assert_kind_of User, user
530 assert_equal "admin", user.login
527 assert_equal "admin", user.login
531 end
528 end
532
529
533 test ".try_to_login with wrong credentials should return nil" do
530 test ".try_to_login should select the exact matching user first" do
534 assert_nil User.try_to_login("admin", "foo")
531 case_sensitive_user = User.generate! do |user|
532 user.password = "admin123"
533 end
534 # bypass validations to make it appear like existing data
535 case_sensitive_user.update_attribute(:login, 'ADMIN')
536
537 user = User.try_to_login("ADMIN", "admin123")
538 assert_kind_of User, user
539 assert_equal "ADMIN", user.login
535 end
540 end
536
541
537 if ldap_configured?
542 if ldap_configured?
538 context "#try_to_login using LDAP" do
543 context "#try_to_login using LDAP" do
539 context "with failed connection to the LDAP server" do
544 context "with failed connection to the LDAP server" do
540 should "return nil" do
545 should "return nil" do
541 @auth_source = AuthSourceLdap.find(1)
546 @auth_source = AuthSourceLdap.find(1)
542 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
547 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
543
548
544 assert_equal nil, User.try_to_login('edavis', 'wrong')
549 assert_equal nil, User.try_to_login('edavis', 'wrong')
545 end
550 end
546 end
551 end
547
552
548 context "with an unsuccessful authentication" do
553 context "with an unsuccessful authentication" do
549 should "return nil" do
554 should "return nil" do
550 assert_equal nil, User.try_to_login('edavis', 'wrong')
555 assert_equal nil, User.try_to_login('edavis', 'wrong')
551 end
556 end
552 end
557 end
553
558
554 context "binding with user's account" do
559 context "binding with user's account" do
555 setup do
560 setup do
556 @auth_source = AuthSourceLdap.find(1)
561 @auth_source = AuthSourceLdap.find(1)
557 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
562 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
558 @auth_source.account_password = ''
563 @auth_source.account_password = ''
559 @auth_source.save!
564 @auth_source.save!
560
565
561 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
566 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
562 @ldap_user.login = 'example1'
567 @ldap_user.login = 'example1'
563 @ldap_user.save!
568 @ldap_user.save!
564 end
569 end
565
570
566 context "with a successful authentication" do
571 context "with a successful authentication" do
567 should "return the user" do
572 should "return the user" do
568 assert_equal @ldap_user, User.try_to_login('example1', '123456')
573 assert_equal @ldap_user, User.try_to_login('example1', '123456')
569 end
574 end
570 end
575 end
571
576
572 context "with an unsuccessful authentication" do
577 context "with an unsuccessful authentication" do
573 should "return nil" do
578 should "return nil" do
574 assert_nil User.try_to_login('example1', '11111')
579 assert_nil User.try_to_login('example1', '11111')
575 end
580 end
576 end
581 end
577 end
582 end
578
583
579 context "on the fly registration" do
584 context "on the fly registration" do
580 setup do
585 setup do
581 @auth_source = AuthSourceLdap.find(1)
586 @auth_source = AuthSourceLdap.find(1)
582 @auth_source.update_attribute :onthefly_register, true
587 @auth_source.update_attribute :onthefly_register, true
583 end
588 end
584
589
585 context "with a successful authentication" do
590 context "with a successful authentication" do
586 should "create a new user account if it doesn't exist" do
591 should "create a new user account if it doesn't exist" do
587 assert_difference('User.count') do
592 assert_difference('User.count') do
588 user = User.try_to_login('edavis', '123456')
593 user = User.try_to_login('edavis', '123456')
589 assert !user.admin?
594 assert !user.admin?
590 end
595 end
591 end
596 end
592
597
593 should "retrieve existing user" do
598 should "retrieve existing user" do
594 user = User.try_to_login('edavis', '123456')
599 user = User.try_to_login('edavis', '123456')
595 user.admin = true
600 user.admin = true
596 user.save!
601 user.save!
597
602
598 assert_no_difference('User.count') do
603 assert_no_difference('User.count') do
599 user = User.try_to_login('edavis', '123456')
604 user = User.try_to_login('edavis', '123456')
600 assert user.admin?
605 assert user.admin?
601 end
606 end
602 end
607 end
603 end
608 end
604
609
605 context "binding with user's account" do
610 context "binding with user's account" do
606 setup do
611 setup do
607 @auth_source = AuthSourceLdap.find(1)
612 @auth_source = AuthSourceLdap.find(1)
608 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
613 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
609 @auth_source.account_password = ''
614 @auth_source.account_password = ''
610 @auth_source.save!
615 @auth_source.save!
611 end
616 end
612
617
613 context "with a successful authentication" do
618 context "with a successful authentication" do
614 should "create a new user account if it doesn't exist" do
619 should "create a new user account if it doesn't exist" do
615 assert_difference('User.count') do
620 assert_difference('User.count') do
616 user = User.try_to_login('example1', '123456')
621 user = User.try_to_login('example1', '123456')
617 assert_kind_of User, user
622 assert_kind_of User, user
618 end
623 end
619 end
624 end
620 end
625 end
621
626
622 context "with an unsuccessful authentication" do
627 context "with an unsuccessful authentication" do
623 should "return nil" do
628 should "return nil" do
624 assert_nil User.try_to_login('example1', '11111')
629 assert_nil User.try_to_login('example1', '11111')
625 end
630 end
626 end
631 end
627 end
632 end
628 end
633 end
629 end
634 end
630
635
631 else
636 else
632 puts "Skipping LDAP tests."
637 puts "Skipping LDAP tests."
633 end
638 end
634
639
635 def test_create_anonymous
640 def test_create_anonymous
636 AnonymousUser.delete_all
641 AnonymousUser.delete_all
637 anon = User.anonymous
642 anon = User.anonymous
638 assert !anon.new_record?
643 assert !anon.new_record?
639 assert_kind_of AnonymousUser, anon
644 assert_kind_of AnonymousUser, anon
640 end
645 end
641
646
642 def test_ensure_single_anonymous_user
647 def test_ensure_single_anonymous_user
643 AnonymousUser.delete_all
648 AnonymousUser.delete_all
644 anon1 = User.anonymous
649 anon1 = User.anonymous
645 assert !anon1.new_record?
650 assert !anon1.new_record?
646 assert_kind_of AnonymousUser, anon1
651 assert_kind_of AnonymousUser, anon1
647 anon2 = AnonymousUser.create(
652 anon2 = AnonymousUser.create(
648 :lastname => 'Anonymous', :firstname => '',
653 :lastname => 'Anonymous', :firstname => '',
649 :mail => '', :login => '', :status => 0)
654 :mail => '', :login => '', :status => 0)
650 assert_equal 1, anon2.errors.count
655 assert_equal 1, anon2.errors.count
651 end
656 end
652
657
653 def test_rss_key
658 def test_rss_key
654 assert_nil @jsmith.rss_token
659 assert_nil @jsmith.rss_token
655 key = @jsmith.rss_key
660 key = @jsmith.rss_key
656 assert_equal 40, key.length
661 assert_equal 40, key.length
657
662
658 @jsmith.reload
663 @jsmith.reload
659 assert_equal key, @jsmith.rss_key
664 assert_equal key, @jsmith.rss_key
660 end
665 end
661
666
662 def test_rss_key_should_not_be_generated_twice
667 def test_rss_key_should_not_be_generated_twice
663 assert_difference 'Token.count', 1 do
668 assert_difference 'Token.count', 1 do
664 key1 = @jsmith.rss_key
669 key1 = @jsmith.rss_key
665 key2 = @jsmith.rss_key
670 key2 = @jsmith.rss_key
666 assert_equal key1, key2
671 assert_equal key1, key2
667 end
672 end
668 end
673 end
669
674
670 def test_api_key_should_not_be_generated_twice
675 def test_api_key_should_not_be_generated_twice
671 assert_difference 'Token.count', 1 do
676 assert_difference 'Token.count', 1 do
672 key1 = @jsmith.api_key
677 key1 = @jsmith.api_key
673 key2 = @jsmith.api_key
678 key2 = @jsmith.api_key
674 assert_equal key1, key2
679 assert_equal key1, key2
675 end
680 end
676 end
681 end
677
682
678 test "#api_key should generate a new one if the user doesn't have one" do
683 test "#api_key should generate a new one if the user doesn't have one" do
679 user = User.generate!(:api_token => nil)
684 user = User.generate!(:api_token => nil)
680 assert_nil user.api_token
685 assert_nil user.api_token
681
686
682 key = user.api_key
687 key = user.api_key
683 assert_equal 40, key.length
688 assert_equal 40, key.length
684 user.reload
689 user.reload
685 assert_equal key, user.api_key
690 assert_equal key, user.api_key
686 end
691 end
687
692
688 test "#api_key should return the existing api token value" do
693 test "#api_key should return the existing api token value" do
689 user = User.generate!
694 user = User.generate!
690 token = Token.create!(:action => 'api')
695 token = Token.create!(:action => 'api')
691 user.api_token = token
696 user.api_token = token
692 assert user.save
697 assert user.save
693
698
694 assert_equal token.value, user.api_key
699 assert_equal token.value, user.api_key
695 end
700 end
696
701
697 test "#find_by_api_key should return nil if no matching key is found" do
702 test "#find_by_api_key should return nil if no matching key is found" do
698 assert_nil User.find_by_api_key('zzzzzzzzz')
703 assert_nil User.find_by_api_key('zzzzzzzzz')
699 end
704 end
700
705
701 test "#find_by_api_key should return nil if the key is found for an inactive user" do
706 test "#find_by_api_key should return nil if the key is found for an inactive user" do
702 user = User.generate!
707 user = User.generate!
703 user.status = User::STATUS_LOCKED
708 user.status = User::STATUS_LOCKED
704 token = Token.create!(:action => 'api')
709 token = Token.create!(:action => 'api')
705 user.api_token = token
710 user.api_token = token
706 user.save
711 user.save
707
712
708 assert_nil User.find_by_api_key(token.value)
713 assert_nil User.find_by_api_key(token.value)
709 end
714 end
710
715
711 test "#find_by_api_key should return the user if the key is found for an active user" do
716 test "#find_by_api_key should return the user if the key is found for an active user" do
712 user = User.generate!
717 user = User.generate!
713 token = Token.create!(:action => 'api')
718 token = Token.create!(:action => 'api')
714 user.api_token = token
719 user.api_token = token
715 user.save
720 user.save
716
721
717 assert_equal user, User.find_by_api_key(token.value)
722 assert_equal user, User.find_by_api_key(token.value)
718 end
723 end
719
724
720 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
725 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
721 user = User.find_by_login("admin")
726 user = User.find_by_login("admin")
722 user.password = "admin"
727 user.password = "admin"
723 assert user.save(:validate => false)
728 assert user.save(:validate => false)
724
729
725 assert_equal false, User.default_admin_account_changed?
730 assert_equal false, User.default_admin_account_changed?
726 end
731 end
727
732
728 def test_default_admin_account_changed_should_return_true_if_password_was_changed
733 def test_default_admin_account_changed_should_return_true_if_password_was_changed
729 user = User.find_by_login("admin")
734 user = User.find_by_login("admin")
730 user.password = "newpassword"
735 user.password = "newpassword"
731 user.save!
736 user.save!
732
737
733 assert_equal true, User.default_admin_account_changed?
738 assert_equal true, User.default_admin_account_changed?
734 end
739 end
735
740
736 def test_default_admin_account_changed_should_return_true_if_account_is_disabled
741 def test_default_admin_account_changed_should_return_true_if_account_is_disabled
737 user = User.find_by_login("admin")
742 user = User.find_by_login("admin")
738 user.password = "admin"
743 user.password = "admin"
739 user.status = User::STATUS_LOCKED
744 user.status = User::STATUS_LOCKED
740 assert user.save(:validate => false)
745 assert user.save(:validate => false)
741
746
742 assert_equal true, User.default_admin_account_changed?
747 assert_equal true, User.default_admin_account_changed?
743 end
748 end
744
749
745 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
750 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
746 user = User.find_by_login("admin")
751 user = User.find_by_login("admin")
747 user.destroy
752 user.destroy
748
753
749 assert_equal true, User.default_admin_account_changed?
754 assert_equal true, User.default_admin_account_changed?
750 end
755 end
751
756
752 def test_membership_with_project_should_return_membership
757 def test_membership_with_project_should_return_membership
753 project = Project.find(1)
758 project = Project.find(1)
754
759
755 membership = @jsmith.membership(project)
760 membership = @jsmith.membership(project)
756 assert_kind_of Member, membership
761 assert_kind_of Member, membership
757 assert_equal @jsmith, membership.user
762 assert_equal @jsmith, membership.user
758 assert_equal project, membership.project
763 assert_equal project, membership.project
759 end
764 end
760
765
761 def test_membership_with_project_id_should_return_membership
766 def test_membership_with_project_id_should_return_membership
762 project = Project.find(1)
767 project = Project.find(1)
763
768
764 membership = @jsmith.membership(1)
769 membership = @jsmith.membership(1)
765 assert_kind_of Member, membership
770 assert_kind_of Member, membership
766 assert_equal @jsmith, membership.user
771 assert_equal @jsmith, membership.user
767 assert_equal project, membership.project
772 assert_equal project, membership.project
768 end
773 end
769
774
770 def test_membership_for_non_member_should_return_nil
775 def test_membership_for_non_member_should_return_nil
771 project = Project.find(1)
776 project = Project.find(1)
772
777
773 user = User.generate!
778 user = User.generate!
774 membership = user.membership(1)
779 membership = user.membership(1)
775 assert_nil membership
780 assert_nil membership
776 end
781 end
777
782
778 def test_roles_for_project
783 def test_roles_for_project
779 # user with a role
784 # user with a role
780 roles = @jsmith.roles_for_project(Project.find(1))
785 roles = @jsmith.roles_for_project(Project.find(1))
781 assert_kind_of Role, roles.first
786 assert_kind_of Role, roles.first
782 assert_equal "Manager", roles.first.name
787 assert_equal "Manager", roles.first.name
783
788
784 # user with no role
789 # user with no role
785 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
790 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
786 end
791 end
787
792
788 def test_projects_by_role_for_user_with_role
793 def test_projects_by_role_for_user_with_role
789 user = User.find(2)
794 user = User.find(2)
790 assert_kind_of Hash, user.projects_by_role
795 assert_kind_of Hash, user.projects_by_role
791 assert_equal 2, user.projects_by_role.size
796 assert_equal 2, user.projects_by_role.size
792 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
797 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
793 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
798 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
794 end
799 end
795
800
796 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
801 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
797 user = User.find(2)
802 user = User.find(2)
798 assert_equal [], user.projects_by_role[Role.find(3)]
803 assert_equal [], user.projects_by_role[Role.find(3)]
799 # should not update the hash
804 # should not update the hash
800 assert_nil user.projects_by_role.values.detect(&:blank?)
805 assert_nil user.projects_by_role.values.detect(&:blank?)
801 end
806 end
802
807
803 def test_projects_by_role_for_user_with_no_role
808 def test_projects_by_role_for_user_with_no_role
804 user = User.generate!
809 user = User.generate!
805 assert_equal({}, user.projects_by_role)
810 assert_equal({}, user.projects_by_role)
806 end
811 end
807
812
808 def test_projects_by_role_for_anonymous
813 def test_projects_by_role_for_anonymous
809 assert_equal({}, User.anonymous.projects_by_role)
814 assert_equal({}, User.anonymous.projects_by_role)
810 end
815 end
811
816
812 def test_valid_notification_options
817 def test_valid_notification_options
813 # without memberships
818 # without memberships
814 assert_equal 5, User.find(7).valid_notification_options.size
819 assert_equal 5, User.find(7).valid_notification_options.size
815 # with memberships
820 # with memberships
816 assert_equal 6, User.find(2).valid_notification_options.size
821 assert_equal 6, User.find(2).valid_notification_options.size
817 end
822 end
818
823
819 def test_valid_notification_options_class_method
824 def test_valid_notification_options_class_method
820 assert_equal 5, User.valid_notification_options.size
825 assert_equal 5, User.valid_notification_options.size
821 assert_equal 5, User.valid_notification_options(User.find(7)).size
826 assert_equal 5, User.valid_notification_options(User.find(7)).size
822 assert_equal 6, User.valid_notification_options(User.find(2)).size
827 assert_equal 6, User.valid_notification_options(User.find(2)).size
823 end
828 end
824
829
825 def test_mail_notification_all
830 def test_mail_notification_all
826 @jsmith.mail_notification = 'all'
831 @jsmith.mail_notification = 'all'
827 @jsmith.notified_project_ids = []
832 @jsmith.notified_project_ids = []
828 @jsmith.save
833 @jsmith.save
829 @jsmith.reload
834 @jsmith.reload
830 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
835 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
831 end
836 end
832
837
833 def test_mail_notification_selected
838 def test_mail_notification_selected
834 @jsmith.mail_notification = 'selected'
839 @jsmith.mail_notification = 'selected'
835 @jsmith.notified_project_ids = [1]
840 @jsmith.notified_project_ids = [1]
836 @jsmith.save
841 @jsmith.save
837 @jsmith.reload
842 @jsmith.reload
838 assert Project.find(1).recipients.include?(@jsmith.mail)
843 assert Project.find(1).recipients.include?(@jsmith.mail)
839 end
844 end
840
845
841 def test_mail_notification_only_my_events
846 def test_mail_notification_only_my_events
842 @jsmith.mail_notification = 'only_my_events'
847 @jsmith.mail_notification = 'only_my_events'
843 @jsmith.notified_project_ids = []
848 @jsmith.notified_project_ids = []
844 @jsmith.save
849 @jsmith.save
845 @jsmith.reload
850 @jsmith.reload
846 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
851 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
847 end
852 end
848
853
849 def test_comments_sorting_preference
854 def test_comments_sorting_preference
850 assert !@jsmith.wants_comments_in_reverse_order?
855 assert !@jsmith.wants_comments_in_reverse_order?
851 @jsmith.pref.comments_sorting = 'asc'
856 @jsmith.pref.comments_sorting = 'asc'
852 assert !@jsmith.wants_comments_in_reverse_order?
857 assert !@jsmith.wants_comments_in_reverse_order?
853 @jsmith.pref.comments_sorting = 'desc'
858 @jsmith.pref.comments_sorting = 'desc'
854 assert @jsmith.wants_comments_in_reverse_order?
859 assert @jsmith.wants_comments_in_reverse_order?
855 end
860 end
856
861
857 def test_find_by_mail_should_be_case_insensitive
862 def test_find_by_mail_should_be_case_insensitive
858 u = User.find_by_mail('JSmith@somenet.foo')
863 u = User.find_by_mail('JSmith@somenet.foo')
859 assert_not_nil u
864 assert_not_nil u
860 assert_equal 'jsmith@somenet.foo', u.mail
865 assert_equal 'jsmith@somenet.foo', u.mail
861 end
866 end
862
867
863 def test_random_password
868 def test_random_password
864 u = User.new
869 u = User.new
865 u.random_password
870 u.random_password
866 assert !u.password.blank?
871 assert !u.password.blank?
867 assert !u.password_confirmation.blank?
872 assert !u.password_confirmation.blank?
868 end
873 end
869
874
870 test "#change_password_allowed? should be allowed if no auth source is set" do
875 test "#change_password_allowed? should be allowed if no auth source is set" do
871 user = User.generate!
876 user = User.generate!
872 assert user.change_password_allowed?
877 assert user.change_password_allowed?
873 end
878 end
874
879
875 test "#change_password_allowed? should delegate to the auth source" do
880 test "#change_password_allowed? should delegate to the auth source" do
876 user = User.generate!
881 user = User.generate!
877
882
878 allowed_auth_source = AuthSource.generate!
883 allowed_auth_source = AuthSource.generate!
879 def allowed_auth_source.allow_password_changes?; true; end
884 def allowed_auth_source.allow_password_changes?; true; end
880
885
881 denied_auth_source = AuthSource.generate!
886 denied_auth_source = AuthSource.generate!
882 def denied_auth_source.allow_password_changes?; false; end
887 def denied_auth_source.allow_password_changes?; false; end
883
888
884 assert user.change_password_allowed?
889 assert user.change_password_allowed?
885
890
886 user.auth_source = allowed_auth_source
891 user.auth_source = allowed_auth_source
887 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
892 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
888
893
889 user.auth_source = denied_auth_source
894 user.auth_source = denied_auth_source
890 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
895 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
891 end
896 end
892
897
893 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
898 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
894 with_settings :unsubscribe => '1' do
899 with_settings :unsubscribe => '1' do
895 assert_equal true, User.find(2).own_account_deletable?
900 assert_equal true, User.find(2).own_account_deletable?
896 end
901 end
897 end
902 end
898
903
899 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
904 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
900 with_settings :unsubscribe => '0' do
905 with_settings :unsubscribe => '0' do
901 assert_equal false, User.find(2).own_account_deletable?
906 assert_equal false, User.find(2).own_account_deletable?
902 end
907 end
903 end
908 end
904
909
905 def test_own_account_deletable_should_be_false_for_a_single_admin
910 def test_own_account_deletable_should_be_false_for_a_single_admin
906 User.delete_all(["admin = ? AND id <> ?", true, 1])
911 User.delete_all(["admin = ? AND id <> ?", true, 1])
907
912
908 with_settings :unsubscribe => '1' do
913 with_settings :unsubscribe => '1' do
909 assert_equal false, User.find(1).own_account_deletable?
914 assert_equal false, User.find(1).own_account_deletable?
910 end
915 end
911 end
916 end
912
917
913 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
918 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
914 User.generate! do |user|
919 User.generate! do |user|
915 user.admin = true
920 user.admin = true
916 end
921 end
917
922
918 with_settings :unsubscribe => '1' do
923 with_settings :unsubscribe => '1' do
919 assert_equal true, User.find(1).own_account_deletable?
924 assert_equal true, User.find(1).own_account_deletable?
920 end
925 end
921 end
926 end
922
927
923 context "#allowed_to?" do
928 context "#allowed_to?" do
924 context "with a unique project" do
929 context "with a unique project" do
925 should "return false if project is archived" do
930 should "return false if project is archived" do
926 project = Project.find(1)
931 project = Project.find(1)
927 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
932 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
928 assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1))
933 assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1))
929 end
934 end
930
935
931 should "return false for write action if project is closed" do
936 should "return false for write action if project is closed" do
932 project = Project.find(1)
937 project = Project.find(1)
933 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
938 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
934 assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1))
939 assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1))
935 end
940 end
936
941
937 should "return true for read action if project is closed" do
942 should "return true for read action if project is closed" do
938 project = Project.find(1)
943 project = Project.find(1)
939 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
944 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
940 assert_equal true, @admin.allowed_to?(:view_project, Project.find(1))
945 assert_equal true, @admin.allowed_to?(:view_project, Project.find(1))
941 end
946 end
942
947
943 should "return false if related module is disabled" do
948 should "return false if related module is disabled" do
944 project = Project.find(1)
949 project = Project.find(1)
945 project.enabled_module_names = ["issue_tracking"]
950 project.enabled_module_names = ["issue_tracking"]
946 assert_equal true, @admin.allowed_to?(:add_issues, project)
951 assert_equal true, @admin.allowed_to?(:add_issues, project)
947 assert_equal false, @admin.allowed_to?(:view_wiki_pages, project)
952 assert_equal false, @admin.allowed_to?(:view_wiki_pages, project)
948 end
953 end
949
954
950 should "authorize nearly everything for admin users" do
955 should "authorize nearly everything for admin users" do
951 project = Project.find(1)
956 project = Project.find(1)
952 assert ! @admin.member_of?(project)
957 assert ! @admin.member_of?(project)
953 %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p|
958 %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p|
954 assert_equal true, @admin.allowed_to?(p.to_sym, project)
959 assert_equal true, @admin.allowed_to?(p.to_sym, project)
955 end
960 end
956 end
961 end
957
962
958 should "authorize normal users depending on their roles" do
963 should "authorize normal users depending on their roles" do
959 project = Project.find(1)
964 project = Project.find(1)
960 assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager
965 assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager
961 assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper
966 assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper
962 end
967 end
963 end
968 end
964
969
965 context "with multiple projects" do
970 context "with multiple projects" do
966 should "return false if array is empty" do
971 should "return false if array is empty" do
967 assert_equal false, @admin.allowed_to?(:view_project, [])
972 assert_equal false, @admin.allowed_to?(:view_project, [])
968 end
973 end
969
974
970 should "return true only if user has permission on all these projects" do
975 should "return true only if user has permission on all these projects" do
971 assert_equal true, @admin.allowed_to?(:view_project, Project.all)
976 assert_equal true, @admin.allowed_to?(:view_project, Project.all)
972 assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
977 assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
973 assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
978 assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
974 assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
979 assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
975 end
980 end
976
981
977 should "behave correctly with arrays of 1 project" do
982 should "behave correctly with arrays of 1 project" do
978 assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first])
983 assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first])
979 end
984 end
980 end
985 end
981
986
982 context "with options[:global]" do
987 context "with options[:global]" do
983 should "authorize if user has at least one role that has this permission" do
988 should "authorize if user has at least one role that has this permission" do
984 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
989 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
985 @anonymous = User.find(6)
990 @anonymous = User.find(6)
986 assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
991 assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
987 assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
992 assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
988 assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true)
993 assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true)
989 assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true)
994 assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true)
990 assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true)
995 assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true)
991 end
996 end
992 end
997 end
993 end
998 end
994
999
995 context "User#notify_about?" do
1000 context "User#notify_about?" do
996 context "Issues" do
1001 context "Issues" do
997 setup do
1002 setup do
998 @project = Project.find(1)
1003 @project = Project.find(1)
999 @author = User.generate!
1004 @author = User.generate!
1000 @assignee = User.generate!
1005 @assignee = User.generate!
1001 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
1006 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
1002 end
1007 end
1003
1008
1004 should "be true for a user with :all" do
1009 should "be true for a user with :all" do
1005 @author.update_attribute(:mail_notification, 'all')
1010 @author.update_attribute(:mail_notification, 'all')
1006 assert @author.notify_about?(@issue)
1011 assert @author.notify_about?(@issue)
1007 end
1012 end
1008
1013
1009 should "be false for a user with :none" do
1014 should "be false for a user with :none" do
1010 @author.update_attribute(:mail_notification, 'none')
1015 @author.update_attribute(:mail_notification, 'none')
1011 assert ! @author.notify_about?(@issue)
1016 assert ! @author.notify_about?(@issue)
1012 end
1017 end
1013
1018
1014 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
1019 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
1015 @user = User.generate!(:mail_notification => 'only_my_events')
1020 @user = User.generate!(:mail_notification => 'only_my_events')
1016 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1021 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1017 assert ! @user.notify_about?(@issue)
1022 assert ! @user.notify_about?(@issue)
1018 end
1023 end
1019
1024
1020 should "be true for a user with :only_my_events and is the author" do
1025 should "be true for a user with :only_my_events and is the author" do
1021 @author.update_attribute(:mail_notification, 'only_my_events')
1026 @author.update_attribute(:mail_notification, 'only_my_events')
1022 assert @author.notify_about?(@issue)
1027 assert @author.notify_about?(@issue)
1023 end
1028 end
1024
1029
1025 should "be true for a user with :only_my_events and is the assignee" do
1030 should "be true for a user with :only_my_events and is the assignee" do
1026 @assignee.update_attribute(:mail_notification, 'only_my_events')
1031 @assignee.update_attribute(:mail_notification, 'only_my_events')
1027 assert @assignee.notify_about?(@issue)
1032 assert @assignee.notify_about?(@issue)
1028 end
1033 end
1029
1034
1030 should "be true for a user with :only_assigned and is the assignee" do
1035 should "be true for a user with :only_assigned and is the assignee" do
1031 @assignee.update_attribute(:mail_notification, 'only_assigned')
1036 @assignee.update_attribute(:mail_notification, 'only_assigned')
1032 assert @assignee.notify_about?(@issue)
1037 assert @assignee.notify_about?(@issue)
1033 end
1038 end
1034
1039
1035 should "be false for a user with :only_assigned and is not the assignee" do
1040 should "be false for a user with :only_assigned and is not the assignee" do
1036 @author.update_attribute(:mail_notification, 'only_assigned')
1041 @author.update_attribute(:mail_notification, 'only_assigned')
1037 assert ! @author.notify_about?(@issue)
1042 assert ! @author.notify_about?(@issue)
1038 end
1043 end
1039
1044
1040 should "be true for a user with :only_owner and is the author" do
1045 should "be true for a user with :only_owner and is the author" do
1041 @author.update_attribute(:mail_notification, 'only_owner')
1046 @author.update_attribute(:mail_notification, 'only_owner')
1042 assert @author.notify_about?(@issue)
1047 assert @author.notify_about?(@issue)
1043 end
1048 end
1044
1049
1045 should "be false for a user with :only_owner and is not the author" do
1050 should "be false for a user with :only_owner and is not the author" do
1046 @assignee.update_attribute(:mail_notification, 'only_owner')
1051 @assignee.update_attribute(:mail_notification, 'only_owner')
1047 assert ! @assignee.notify_about?(@issue)
1052 assert ! @assignee.notify_about?(@issue)
1048 end
1053 end
1049
1054
1050 should "be true for a user with :selected and is the author" do
1055 should "be true for a user with :selected and is the author" do
1051 @author.update_attribute(:mail_notification, 'selected')
1056 @author.update_attribute(:mail_notification, 'selected')
1052 assert @author.notify_about?(@issue)
1057 assert @author.notify_about?(@issue)
1053 end
1058 end
1054
1059
1055 should "be true for a user with :selected and is the assignee" do
1060 should "be true for a user with :selected and is the assignee" do
1056 @assignee.update_attribute(:mail_notification, 'selected')
1061 @assignee.update_attribute(:mail_notification, 'selected')
1057 assert @assignee.notify_about?(@issue)
1062 assert @assignee.notify_about?(@issue)
1058 end
1063 end
1059
1064
1060 should "be false for a user with :selected and is not the author or assignee" do
1065 should "be false for a user with :selected and is not the author or assignee" do
1061 @user = User.generate!(:mail_notification => 'selected')
1066 @user = User.generate!(:mail_notification => 'selected')
1062 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1067 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1063 assert ! @user.notify_about?(@issue)
1068 assert ! @user.notify_about?(@issue)
1064 end
1069 end
1065 end
1070 end
1066 end
1071 end
1067
1072
1068 def test_notify_about_news
1073 def test_notify_about_news
1069 user = User.generate!
1074 user = User.generate!
1070 news = News.new
1075 news = News.new
1071
1076
1072 User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option|
1077 User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option|
1073 user.mail_notification = option
1078 user.mail_notification = option
1074 assert_equal (option != 'none'), user.notify_about?(news)
1079 assert_equal (option != 'none'), user.notify_about?(news)
1075 end
1080 end
1076 end
1081 end
1077
1082
1078 def test_salt_unsalted_passwords
1083 def test_salt_unsalted_passwords
1079 # Restore a user with an unsalted password
1084 # Restore a user with an unsalted password
1080 user = User.find(1)
1085 user = User.find(1)
1081 user.salt = nil
1086 user.salt = nil
1082 user.hashed_password = User.hash_password("unsalted")
1087 user.hashed_password = User.hash_password("unsalted")
1083 user.save!
1088 user.save!
1084
1089
1085 User.salt_unsalted_passwords!
1090 User.salt_unsalted_passwords!
1086
1091
1087 user.reload
1092 user.reload
1088 # Salt added
1093 # Salt added
1089 assert !user.salt.blank?
1094 assert !user.salt.blank?
1090 # Password still valid
1095 # Password still valid
1091 assert user.check_password?("unsalted")
1096 assert user.check_password?("unsalted")
1092 assert_equal user, User.try_to_login(user.login, "unsalted")
1097 assert_equal user, User.try_to_login(user.login, "unsalted")
1093 end
1098 end
1094
1099
1095 if Object.const_defined?(:OpenID)
1100 if Object.const_defined?(:OpenID)
1096
1101
1097 def test_setting_identity_url
1102 def test_setting_identity_url
1098 normalized_open_id_url = 'http://example.com/'
1103 normalized_open_id_url = 'http://example.com/'
1099 u = User.new( :identity_url => 'http://example.com/' )
1104 u = User.new( :identity_url => 'http://example.com/' )
1100 assert_equal normalized_open_id_url, u.identity_url
1105 assert_equal normalized_open_id_url, u.identity_url
1101 end
1106 end
1102
1107
1103 def test_setting_identity_url_without_trailing_slash
1108 def test_setting_identity_url_without_trailing_slash
1104 normalized_open_id_url = 'http://example.com/'
1109 normalized_open_id_url = 'http://example.com/'
1105 u = User.new( :identity_url => 'http://example.com' )
1110 u = User.new( :identity_url => 'http://example.com' )
1106 assert_equal normalized_open_id_url, u.identity_url
1111 assert_equal normalized_open_id_url, u.identity_url
1107 end
1112 end
1108
1113
1109 def test_setting_identity_url_without_protocol
1114 def test_setting_identity_url_without_protocol
1110 normalized_open_id_url = 'http://example.com/'
1115 normalized_open_id_url = 'http://example.com/'
1111 u = User.new( :identity_url => 'example.com' )
1116 u = User.new( :identity_url => 'example.com' )
1112 assert_equal normalized_open_id_url, u.identity_url
1117 assert_equal normalized_open_id_url, u.identity_url
1113 end
1118 end
1114
1119
1115 def test_setting_blank_identity_url
1120 def test_setting_blank_identity_url
1116 u = User.new( :identity_url => 'example.com' )
1121 u = User.new( :identity_url => 'example.com' )
1117 u.identity_url = ''
1122 u.identity_url = ''
1118 assert u.identity_url.blank?
1123 assert u.identity_url.blank?
1119 end
1124 end
1120
1125
1121 def test_setting_invalid_identity_url
1126 def test_setting_invalid_identity_url
1122 u = User.new( :identity_url => 'this is not an openid url' )
1127 u = User.new( :identity_url => 'this is not an openid url' )
1123 assert u.identity_url.blank?
1128 assert u.identity_url.blank?
1124 end
1129 end
1125
1130
1126 else
1131 else
1127 puts "Skipping openid tests."
1132 puts "Skipping openid tests."
1128 end
1133 end
1129
1134
1130 end
1135 end
General Comments 0
You need to be logged in to leave comments. Login now