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