Guest User

Untitled

a guest
Mar 9th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. ## my validation code
  2.  
  3. def validate_on_update
  4. puts("#{self.password} : #{User.obscure(old_password)}")
  5. if new_password and not new_password.empty? and User.obscure(old_password) != self.password
  6. puts 'adding error'
  7. errors.add(:password, "does not match old password")
  8. end
  9. end
  10.  
  11. def after_validation_on_update
  12. self.password = User.obscure(self.new_password) if self.new_password and not self.new_password.empty?
  13. puts 'in after validation'
  14. end
  15.  
  16. ## my test
  17.  
  18. it 'should not fail if the given old password is correct' do
  19. puts "starting update"
  20. User.update(1, {:new_password => 'frank',
  21. :new_password_confirmation => 'frank',
  22. :old_password => 'pass'}).should have(0).errors_on(:password)
  23. end
  24.  
  25. ## output from the test
  26.  
  27. "starting update"
  28. "9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684 : 9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684"
  29. "in after validation"
  30. "86a8c2da8527a1c6978bdca6d7986fe14ae147fe : 9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684"
  31. "adding an error"
  32. "in after validation"
  33.  
  34.  
  35. ## in console
  36.  
  37. >> User.update(2, {:new_password => 'frank', :new_password_confirmation => 'frank', :old_password => 'test'})
  38. a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 : a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
  39. in after validation
  40. => #<User:0xb72da854 @new_password_confirmation="frank", @new_record_before_save=nil, @errors=#<ActiveRecord::Errors:0xb72d9d00 @errors={}, @base=#<Rider:0xb72da854 ...>>, @old_password="test", @new_password="frank", @attributes={"name"=>"evan", "id"=>"2", "password"=>"86a8c2da8527a1c6978bdca6d7986fe14ae147fe", "email"=>"evan"}>
Add Comment
Please, Sign In to add comment