teensyweb/
teensyweb/app/models/
teensyweb/app/views/account/
teensyweb/app/views/layouts/
teensyweb/app/views/notifications/
teensyweb/app/views/repository/
teensyweb/config/
teensyweb/config/environments/
teensyweb/db/migrate/
teensyweb/doc/
teensyweb/lib/
teensyweb/public/images/
teensyweb/public/javascripts/
teensyweb/public/stylesheets/
teensyweb/script/
teensyweb/script/performance/
teensyweb/script/process/
teensyweb/test/
teensyweb/test/fixtures/
teensyweb/test/fixtures/notifications/
require File.dirname(__FILE__) + '/../test_helper'

class UserTest < Test::Unit::TestCase

  fixtures :users

  def test_auth

    assert_equal  @bob, User.authenticate("bob", "test")
    assert_nil    User.authenticate("nonbob", "test")

  end

  def test_disallowed_passwords

    u = User.new
    u.login = "nonbob"

    u.password = u.password_confirmation = "tiny"
    assert !u.save
    assert u.errors.invalid?('password')

    u.password = u.password_confirmation = "hugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehuge"
    assert !u.save
    assert u.errors.invalid?('password')

    u.password = u.password_confirmation = ""
    assert !u.save
    assert u.errors.invalid?('password')

    u.password = u.password_confirmation = "bobs_secure_password"
    assert u.save
    assert u.errors.empty?

  end

  def test_bad_logins

    u = User.new
    u.password = u.password_confirmation = "bobs_secure_password"

    u.login = "x"
    assert !u.save
    assert u.errors.invalid?('login')

    u.login = "hugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhug"
    assert !u.save
    assert u.errors.invalid?('login')

    u.login = ""
    assert !u.save
    assert u.errors.invalid?('login')

    u.login = "okbob"
    assert u.save
    assert u.errors.empty?

  end


  def test_collision
    u = User.new
    u.login      = "existingbob"
    u.password = u.password_confirmation = "bobs_secure_password"
    assert !u.save
  end


  def test_create
    u = User.new
    u.login      = "nonexistingbob"
    u.password = u.password_confirmation = "bobs_secure_password"

    assert u.save

  end

  def test_sha1
    u = User.new
    u.login      = "nonexistingbob"
    u.password = u.password_confirmation = "bobs_secure_password"
    assert u.save

    assert_equal '98740ff87bade6d895010bceebbd9f718e7856bb', u.password
  end


end