diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2010-12-18 13:38:05 -0800 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2010-12-18 13:38:05 -0800 |
commit | bcf4e4f2b02157cecc1f1727a95cdf5bfa471771 (patch) | |
tree | b38232fb127a25817aa25d3b8976d218948119e3 /activemodel/test | |
parent | d9b732fcadc001b6757e648761acf97833827b58 (diff) | |
download | rails-bcf4e4f2b02157cecc1f1727a95cdf5bfa471771.tar.gz rails-bcf4e4f2b02157cecc1f1727a95cdf5bfa471771.tar.bz2 rails-bcf4e4f2b02157cecc1f1727a95cdf5bfa471771.zip |
Added ActiveRecord::Base#has_secure_password (via ActiveModel::SecurePassword) to encapsulate dead-simple password usage with SHA2 encryption and salting
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/secure_password_test.rb | 42 | ||||
-rw-r--r-- | activemodel/test/models/user.rb | 8 |
2 files changed, 50 insertions, 0 deletions
diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb new file mode 100644 index 0000000000..7d7c51e6cb --- /dev/null +++ b/activemodel/test/cases/secure_password_test.rb @@ -0,0 +1,42 @@ +require 'cases/helper' +require 'models/user' + +class SecurePasswordTest < ActiveModel::TestCase + setup do + @user = User.new + end + + test "password must be present" do + assert !@user.valid? + assert_equal 1, @user.errors.size + end + + test "password must match confirmation" do + @user.password = "thiswillberight" + @user.password_confirmation = "wrong" + + assert !@user.valid? + + @user.password_confirmation = "thiswillberight" + + assert @user.valid? + end + + test "password must pass validation rules" do + @user.password = "password" + assert !@user.valid? + + @user.password = "short" + assert !@user.valid? + + @user.password = "plentylongenough" + assert @user.valid? + end + + test "authenticate" do + @user.password = "secret" + + assert !@user.authenticate("wrong") + assert @user.authenticate("secret") + end +end
\ No newline at end of file diff --git a/activemodel/test/models/user.rb b/activemodel/test/models/user.rb new file mode 100644 index 0000000000..e221bb8091 --- /dev/null +++ b/activemodel/test/models/user.rb @@ -0,0 +1,8 @@ +class User + include ActiveModel::Validations + include ActiveModel::SecurePassword + + has_secure_password + + attr_accessor :password_digest, :password_salt +end |