2005-01-09 11:59:57 +00:00
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
|
|
|
|
|
|
|
|
|
class UserTest < Test::Unit::TestCase
|
|
|
|
|
fixtures :users
|
|
|
|
|
|
2005-08-08 01:54:05 +00:00
|
|
|
def setup
|
2006-02-26 10:58:39 +00:00
|
|
|
assert_equal "test", ENV['RAILS_ENV']
|
2006-11-05 10:41:59 +00:00
|
|
|
assert_equal "change-me", Tracks::Config.salt
|
2005-08-08 01:54:05 +00:00
|
|
|
@admin_user = User.find(1)
|
|
|
|
|
@other_user = User.find(2)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Test an admin user model
|
2006-02-26 10:58:39 +00:00
|
|
|
#
|
2005-08-08 01:54:05 +00:00
|
|
|
def test_admin
|
|
|
|
|
assert_kind_of User, @admin_user
|
|
|
|
|
assert_equal 1, @admin_user.id
|
|
|
|
|
assert_equal "admin", @admin_user.login
|
2006-11-05 10:41:59 +00:00
|
|
|
assert_equal "#{Digest::SHA1.hexdigest("#{Tracks::Config.salt}--abracadabra--")}", @admin_user.password
|
2006-02-26 10:58:39 +00:00
|
|
|
assert_not_nil @admin_user.word
|
2006-06-19 04:04:20 +00:00
|
|
|
assert @admin_user.is_admin
|
2005-08-08 01:54:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Test a non-admin user model
|
|
|
|
|
def test_non_admin
|
|
|
|
|
assert_kind_of User, @other_user
|
|
|
|
|
assert_equal 2, @other_user.id
|
|
|
|
|
assert_equal "jane", @other_user.login
|
2006-11-05 10:41:59 +00:00
|
|
|
assert_equal "#{Digest::SHA1.hexdigest("#{Tracks::Config.salt}--sesame--")}", @other_user.password
|
2006-02-26 10:58:39 +00:00
|
|
|
assert_not_nil @other_user.word
|
2006-08-01 07:03:31 +00:00
|
|
|
assert @other_user.is_admin == false || @other_user.is_admin == 0
|
2005-08-08 01:54:05 +00:00
|
|
|
end
|
|
|
|
|
|
2006-02-26 10:58:39 +00:00
|
|
|
# ============================================
|
|
|
|
|
# Validations
|
|
|
|
|
# ============================================
|
|
|
|
|
|
|
|
|
|
# Test a password shorter than 5 characters
|
|
|
|
|
#
|
2005-08-08 01:54:05 +00:00
|
|
|
def test_validate_short_password
|
2006-11-05 10:41:59 +00:00
|
|
|
assert_equal "#{Digest::SHA1.hexdigest("#{Tracks::Config.salt}--sesame--")}", @other_user.password
|
2005-08-08 01:54:05 +00:00
|
|
|
@other_user.password = "four"
|
|
|
|
|
assert !@other_user.save
|
|
|
|
|
assert_equal 1, @other_user.errors.count
|
2006-04-08 17:46:41 +00:00
|
|
|
assert_equal "is too short (minimum is 5 characters)", @other_user.errors.on(:password)
|
2005-08-08 01:54:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Test a password longer than 40 characters
|
2006-02-26 10:58:39 +00:00
|
|
|
#
|
2005-08-08 01:54:05 +00:00
|
|
|
def test_validate_long_password
|
2006-11-05 10:41:59 +00:00
|
|
|
assert_equal "#{Digest::SHA1.hexdigest("#{Tracks::Config.salt}--sesame--")}", @other_user.password
|
2005-08-08 01:54:05 +00:00
|
|
|
@other_user.password = generate_random_string(41)
|
|
|
|
|
assert !@other_user.save
|
|
|
|
|
assert_equal 1, @other_user.errors.count
|
2006-04-08 17:46:41 +00:00
|
|
|
assert_equal "is too long (maximum is 40 characters)", @other_user.errors.on(:password)
|
2006-02-26 10:58:39 +00:00
|
|
|
end
|
2005-08-08 01:54:05 +00:00
|
|
|
|
2006-02-26 10:58:39 +00:00
|
|
|
# Test that correct length password is valid
|
|
|
|
|
#
|
2005-08-08 01:54:05 +00:00
|
|
|
def test_validate_correct_length_password
|
2006-11-05 10:41:59 +00:00
|
|
|
assert_equal "#{Digest::SHA1.hexdigest("#{Tracks::Config.salt}--sesame--")}", @other_user.password
|
2005-08-08 01:54:05 +00:00
|
|
|
@other_user.password = generate_random_string(6)
|
|
|
|
|
assert @other_user.save
|
|
|
|
|
end
|
|
|
|
|
|
2006-02-26 10:58:39 +00:00
|
|
|
# Test a missing password
|
|
|
|
|
#
|
2005-08-08 01:54:05 +00:00
|
|
|
def test_validate_missing_password
|
|
|
|
|
assert_equal 2, @other_user.id
|
|
|
|
|
@other_user.password = ""
|
|
|
|
|
assert !@other_user.save
|
|
|
|
|
assert_equal 2, @other_user.errors.count
|
2006-04-08 17:46:41 +00:00
|
|
|
assert_equal ["is too short (minimum is 5 characters)", "can't be blank"], @other_user.errors.on(:password)
|
2005-01-09 11:59:57 +00:00
|
|
|
end
|
2006-02-26 10:58:39 +00:00
|
|
|
|
|
|
|
|
# Test a login shorter than 3 characters
|
|
|
|
|
#
|
|
|
|
|
def test_validate_short_login
|
|
|
|
|
assert_equal "jane", @other_user.login
|
|
|
|
|
@other_user.login = "ba"
|
|
|
|
|
assert !@other_user.save
|
|
|
|
|
assert_equal 1, @other_user.errors.count
|
2006-04-08 17:46:41 +00:00
|
|
|
assert_equal "is too short (minimum is 3 characters)", @other_user.errors.on(:login)
|
2006-02-26 10:58:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Test a login longer than 80 characters
|
|
|
|
|
#
|
|
|
|
|
def test_validate_long_login
|
|
|
|
|
assert_equal "jane", @other_user.login
|
|
|
|
|
@other_user.login = generate_random_string(81)
|
|
|
|
|
assert !@other_user.save
|
|
|
|
|
assert_equal 1, @other_user.errors.count
|
2006-04-08 17:46:41 +00:00
|
|
|
assert_equal "is too long (maximum is 80 characters)", @other_user.errors.on(:login)
|
2006-02-26 10:58:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Test that correct length login is valid
|
|
|
|
|
#
|
|
|
|
|
def test_validate_correct_length_login
|
|
|
|
|
assert_equal "jane", @other_user.login
|
|
|
|
|
@other_user.login = generate_random_string(6)
|
|
|
|
|
assert @other_user.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Test a missing login
|
|
|
|
|
#
|
|
|
|
|
def test_validate_missing_login
|
|
|
|
|
assert_equal 2, @other_user.id
|
|
|
|
|
@other_user.login = ""
|
|
|
|
|
assert !@other_user.save
|
|
|
|
|
assert_equal 2, @other_user.errors.count
|
2006-04-08 17:46:41 +00:00
|
|
|
assert_equal ["is too short (minimum is 3 characters)", "can't be blank"], @other_user.errors.on(:login)
|
2006-02-26 10:58:39 +00:00
|
|
|
end
|
2006-09-16 16:01:29 +00:00
|
|
|
|
|
|
|
|
def test_display_name_with_first_and_last_name_set
|
|
|
|
|
@other_user.first_name = "Jane"
|
|
|
|
|
@other_user.last_name = "Doe"
|
|
|
|
|
assert_equal "Jane Doe", @other_user.display_name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_display_name_with_first_name_set
|
|
|
|
|
@other_user.first_name = "Jane"
|
|
|
|
|
@other_user.last_name = nil
|
|
|
|
|
assert_equal "Jane", @other_user.display_name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_display_name_with_last_name_set
|
|
|
|
|
@other_user.first_name = nil
|
|
|
|
|
@other_user.last_name = "Doe"
|
|
|
|
|
assert_equal "Doe", @other_user.display_name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_display_name_with_neither_first_nor_last_name_set
|
|
|
|
|
@other_user.first_name = nil
|
|
|
|
|
@other_user.last_name = nil
|
|
|
|
|
assert_equal @other_user.login, @other_user.display_name
|
|
|
|
|
end
|
2006-02-26 10:58:39 +00:00
|
|
|
|
2005-01-09 11:59:57 +00:00
|
|
|
end
|