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