Guest User

Untitled

a guest
Apr 13th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. require 'password'
  3.  
  4. class UserTest < Test::Unit::TestCase
  5. fixtures :users, :roles
  6.  
  7. # Replace this with your real tests.
  8. def test_invalid_with_empty_attributes
  9. user = User.new
  10. assert !user.valid?
  11. end
  12.  
  13. def test_invalid_role
  14. user = User.new(
  15. :username => 'yyy',
  16. :password => 'update',
  17. :role_id => '6'
  18. )
  19. assert !user.save
  20. #assert_equal "is invalid", user.errors.on(:role_id)
  21. end
  22. end
  23.  
  24. class User < ActiveRecord::Base
  25. belongs_to :role
  26.  
  27. validates_length_of :username,
  28. :within => 3..40
  29. validates_length_of :password,
  30. :minimum => 6,
  31. :if => :perform_password_validation?
  32. validates_confirmation_of :password, :if => :perform_password_validation?
  33. validates_presence_of :username, :password, :role_id
  34. validates_uniqueness_of :username
  35. validates_format_of :email_address,
  36. :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
  37. :if => Proc.new {|c| not c.email_address.blank?},
  38. :message => "is invalid"
  39. validates_associated :role
  40.  
  41. before_save :hash_password
  42. attr_accessor :password
  43.  
  44. def self.authenticate(username,password)
  45. user = find(:first, :conditions => ['username = ?', username])
  46.  
  47. if Password::check(password, user.password_hash)
  48. user
  49. else
  50. return false
  51. end
  52. end
  53.  
  54. protected
  55.  
  56. def hash_password
  57. self.password_hash = Password::update(self.password) unless self.password.blank?
  58. end
  59.  
  60. def perform_password_validation?
  61. self.new_record? ? true : !self.password.blank?
  62. end
  63. end
Add Comment
Please, Sign In to add comment