Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require File.dirname(__FILE__) + '/../test_helper'
- require 'password'
- class UserTest < Test::Unit::TestCase
- fixtures :users, :roles
- # Replace this with your real tests.
- def test_invalid_with_empty_attributes
- user = User.new
- assert !user.valid?
- end
- def test_invalid_role
- user = User.new(
- :username => 'yyy',
- :password => 'update',
- :role_id => '6'
- )
- assert !user.save
- #assert_equal "is invalid", user.errors.on(:role_id)
- end
- end
- class User < ActiveRecord::Base
- belongs_to :role
- validates_length_of :username,
- :within => 3..40
- validates_length_of :password,
- :minimum => 6,
- :if => :perform_password_validation?
- validates_confirmation_of :password, :if => :perform_password_validation?
- validates_presence_of :username, :password, :role_id
- validates_uniqueness_of :username
- validates_format_of :email_address,
- :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
- :if => Proc.new {|c| not c.email_address.blank?},
- :message => "is invalid"
- validates_associated :role
- before_save :hash_password
- attr_accessor :password
- def self.authenticate(username,password)
- user = find(:first, :conditions => ['username = ?', username])
- if Password::check(password, user.password_hash)
- user
- else
- return false
- end
- end
- protected
- def hash_password
- self.password_hash = Password::update(self.password) unless self.password.blank?
- end
- def perform_password_validation?
- self.new_record? ? true : !self.password.blank?
- end
- end
Add Comment
Please, Sign In to add comment