1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Adding validatable module containing some default email and password validations.

This commit is contained in:
Carlos A. da Silva 2009-09-18 12:03:41 -03:00
parent 91b19bd44f
commit b82717a96c
7 changed files with 139 additions and 8 deletions

View file

@ -2,6 +2,7 @@ require 'devise/authenticable'
require 'devise/perishable_token'
require 'devise/confirmable'
require 'devise/recoverable'
require 'devise/validatable'
require 'devise/notifier'

View file

@ -4,6 +4,7 @@ module Devise
def self.included(base)
base.class_eval do
extend ClassMethods
include ::Devise::PerishableToken
end
end
@ -43,6 +44,11 @@ module Devise
recoverable
end
# Attempt to find a user by it's perishable_token to reset it's password.
# If a user is found, reset it's password and automatically try saving the
# record. If not user is found, returns a new user containing an error
# in perishable_token attribute
#
def find_and_reset_password(perishable_token, password=nil, password_confirmation=nil)
recoverable = find_or_initialize_by_perishable_token(perishable_token)
unless recoverable.new_record?

30
lib/devise/validatable.rb Normal file
View file

@ -0,0 +1,30 @@
module Devise
module Validatable
# Email regex used to validate email formats
#
EMAIL_REGEX = /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)\z/i
def self.included(base)
base.class_eval do
validates_presence_of :email
validates_uniqueness_of :email, :allow_blank => true
validates_format_of :email, :with => EMAIL_REGEX, :allow_blank => true
validates_presence_of :password, :if => :password_required?
validates_confirmation_of :password, :if => :password_required?
validates_length_of :password, :within => 6..20, :allow_blank => true
end
end
private
# Checks whether a password is needed or not. For validations only.
#
def password_required?
new_record? || !password.nil? || !password_confirmation.nil?
end
end
end

View file

@ -51,8 +51,8 @@ class AuthenticableTest < ActiveSupport::TestCase
now = Time.now
Time.stubs(:now).returns(now)
User.any_instance.stubs(:random_string).returns('random_string')
expected_salt = ::Digest::SHA1.hexdigest("--#{now.utc}--random_string--12345--")
user = create_user
expected_salt = ::Digest::SHA1.hexdigest("--#{now.utc}--random_string--123456--")
assert_equal expected_salt, user.password_salt
end
@ -79,7 +79,7 @@ class AuthenticableTest < ActiveSupport::TestCase
user = create_user
encrypted_password = user.encrypted_password
user.expects(:encrypted_password=).never
user.password = '12345'
user.password = '123456'
assert_equal encrypted_password, user.encrypted_password
end
@ -93,14 +93,14 @@ class AuthenticableTest < ActiveSupport::TestCase
test 'should encrypt password using a sha1 hash' do
digest_key = Devise::Authenticable::SECURE_AUTH_SITE_KEY
user = create_user
expected_password = ::Digest::SHA1.hexdigest("--#{user.password_salt}--#{digest_key}--#{12345}--")
expected_password = ::Digest::SHA1.hexdigest("--#{user.password_salt}--#{digest_key}--123456--")
assert_equal expected_password, user.encrypted_password
end
test 'should test for a valid password' do
user = create_user
assert user.valid_password?('12345')
assert_not user.valid_password?('54321')
assert user.valid_password?('123456')
assert_not user.valid_password?('654321')
end
test 'should authenticate a valid user with email and password and return it' do

View file

@ -57,7 +57,7 @@ class PerishableTokenTest < ActiveSupport::TestCase
now = Time.now
Time.stubs(:now).returns(now)
User.any_instance.stubs(:random_string).returns('random_string')
expected_token = ::Digest::SHA1.hexdigest("--#{now.utc}--random_string--12345--")
expected_token = ::Digest::SHA1.hexdigest("--#{now.utc}--random_string--123456--")
user = create_user
assert_equal expected_token, user.perishable_token
end

View file

@ -64,8 +64,8 @@ class ActiveSupport::TestCase
def valid_attributes(attributes={})
{ :email => generate_unique_email,
:password => '12345',
:password_confirmation => '12345' }.update(attributes)
:password => '123456',
:password_confirmation => '123456' }.update(attributes)
end
def new_user(attributes={})

94
test/validatable_test.rb Normal file
View file

@ -0,0 +1,94 @@
require 'test_helper'
class ValidatableTest < ActiveSupport::TestCase
def setup
User.send :include, ::Devise::Validatable unless User.included_modules.include?(::Devise::Validatable)
end
test 'should require email to be set' do
user = new_user(:email => nil)
assert user.invalid?
assert user.errors[:email]
assert_equal 'can\'t be blank', user.errors[:email]
end
test 'should require uniqueness of email, allowing blank' do
existing_user = create_user
user = new_user(:email => '')
assert user.invalid?
assert_not_equal 'has already been taken', user.errors[:email]
user.email = existing_user.email
assert user.invalid?
assert user.errors[:email]
assert_equal 1, user.errors[:email].to_a.size
assert_equal 'has already been taken', user.errors[:email]
end
test 'should require correct email format, allowing blank' do
user = new_user(:email => '')
assert user.invalid?
assert_not_equal 'is invalid', user.errors[:email]
%w(invalid_email_format email@invalid invalid$character@mail.com other@not 123).each do |email|
user.email = email
assert user.invalid?, 'should be invalid with email ' << email
assert user.errors[:email]
assert_equal 1, user.errors[:email].to_a.size
assert_equal 'is invalid', user.errors[:email]
end
end
test 'should accept valid emails' do
%w(a.b.c@example.com test_mail@gmail.com any@any.net email@test.br 123@mail.test).each do |email|
user = new_user(:email => email)
assert user.valid?, 'should be valid with email ' << email
assert_nil user.errors[:email]
end
end
test 'should require password to be set when creating a new record' do
user = new_user(:password => '', :password_confirmation => '')
assert user.invalid?
assert user.errors[:password]
assert_equal 'can\'t be blank', user.errors[:password]
end
test 'should require confirmation to be set when creating a new record' do
user = new_user(:password => 'new_password', :password_confirmation => 'blabla')
assert user.invalid?
assert user.errors[:password]
assert_equal 'doesn\'t match confirmation', user.errors[:password]
end
test 'should require password when updating/reseting password' do
user = create_user
user.password = ''
user.password_confirmation = ''
assert user.invalid?
assert user.errors[:password]
assert_equal 'can\'t be blank', user.errors[:password]
end
test 'should require confirmation when updating/reseting password' do
user = create_user
user.password_confirmation = 'another_password'
assert user.invalid?
assert user.errors[:password]
assert_equal 'doesn\'t match confirmation', user.errors[:password]
end
test 'should require a password with minimum of 6 characters' do
user = new_user(:password => '12345', :password_confirmation => '12345')
assert user.invalid?
assert user.errors[:password]
assert_equal 'is too short (minimum is 6 characters)', user.errors[:password]
end
test 'should require a password with maximum of 20 characters long' do
user = new_user(:password => 'x'*21, :password_confirmation => 'x'*21)
assert user.invalid?
assert user.errors[:password]
assert_equal 'is too long (maximum is 20 characters)', user.errors[:password]
end
end