Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## MODEL
- equire 'digest/sha1'
- class User < ActiveRecord::Base
- #model relationships
- belongs_to :province
- belongs_to :country
- belongs_to :rank
- validates_length_of :username, :within => 3..40
- validates_length_of :password, :within => 4..40
- validates_presence_of :username, :password, :password_confirmation, :salt
- validates_uniqueness_of :login, :email
- validates_confirmation_of :password
- validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => 'Invalid email'
- # Makes sure values cannot set them with a post
- attr_protected :id, :salt, :rank_id
- # always prepend 'self.' in the model method declarations.
- # It will help troubleshoot if a migration breaks,
- # Anytime a password is assigned to a User this method is called.
- # the password assigned is stored as @password, I think
- !! def password=(pass)
- !! @password=pass
- !! # if it doesn't have any salt, give it some
- !! self.salt = User.random_string(10) if !self.salt?
- !! self.hashed_password = User.encrypt(@password, self.salt)
- !! end
- def self.authenticate(login, pass)
- u=find(:first, :conditions => ["login = ?", login]) #return the first user with that login
- return nil if u.nil?
- return u if User.encrypt(pass, u.salt) == u.hashed_password
- nil
- end
- def send_new_password
- new_pass = User.random_string(10)
- # store new password as both for model verification
- self.password = self.password_confirmation = new_pass
- self.save
- # make sure to write thisn Notifications mailer!
- Notifications.deliver_forgot_password(self.email, self.login, new_pass)
- end
- protected
- #Everything after here not accesible outside of this model
- def self.random_string(len)
- # generate a random alphanumeric string for the password
- chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
- newpass = ''
- # for the length or the string, a random character is concat onto the newpass
- 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
- return newpass
- end
- def self.encrypt(pass, salt)
- Digest::SHA1.hexdigest(pass+salt)
- end
- end
- ## A sample test that fails when the model saves. It fails where highlighted on .save
- ##
- ## NoMethodError: undefined method `password' for #<User:0x475e690>
- def test_create
- #check create works and we can authenticate after creation
- u = User.new
- u.username = "nonexistingbob"
- u.password = u.password_confirmation = "bobs_secure_password"
- u.email="nonexistingbob@mcbob.com"
- assert_not_nil u.salt
- assert u.save
- assert_equal 10, u.salt.length
- assert_equal u, User.authenticate(u.username, u.password)
- u = User.new(:username => "newbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "newbob@mcbob.com" )
- assert_not_nil u.salt
- assert_not_nil u.password
- assert_not_nil u.hashed_password
- !! assert u.save
- assert_equal u, User.authenticate(u.username, u.password)
- end
- ## STACK TRACE
- 4) Error:
- test_create(UserTest):
- NoMethodError: undefined method `password' for #<User:0x475e690>
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:300:in `send'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:300:in `validates_each'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:299:in `each'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:299:in `validates_each'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:822:in `call'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:822:in `run_validations'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:816:in `each'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:816:in `run_validations'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:780:in `valid_without_callbacks?'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:299:in `valid?'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:751:in `save_without_transactions'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:129:in `save'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:59:in `transaction'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:95:in `transaction'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:121:in `transaction'
- c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:129:in `save'
- test/unit/user_test.rb:111:in `test_create'
Add Comment
Please, Sign In to add comment