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