1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/test/models/validatable_test.rb
José Valim 7774accb6c Remove data_mapper support.
Devise 1.1.0 will be released soon. This new version will support activerecord and mongoid as default ORMs. From now on, Devise will prefer ORM extensions as gems since this is the best way to handle dependencies.

For example, to allow Devise to work with Datamapper, it requires at least activemodel, dm-rails and dm-timestamps. If the ORM support comes from Devise gem, we cannot add dm-rails and dm-timestamps as dependencies, relying on the developer and documentation to find these out and install them.

Other ORMs may still be added to Devise, as long as they are supported by the community, extend Devise test suite to have all tests passing and they necessarily use ActiveModel::Validations.
2010-07-04 17:22:57 +02:00

99 lines
3.3 KiB
Ruby

require 'test_helper'
class ValidatableTest < ActiveSupport::TestCase
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].join
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].join
user.email = existing_user.email
assert user.invalid?
assert_equal 'has already been taken', user.errors[:email].join
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].join
%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_equal 'is invalid', user.errors[:email].join
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_blank 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_equal 'can\'t be blank', user.errors[:password].join
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_equal 'doesn\'t match confirmation', user.errors[:password].join
end
test 'should require password when updating/reseting password' do
user = create_user
user.password = ''
user.password_confirmation = ''
assert user.invalid?
assert_equal 'can\'t be blank', user.errors[:password].join
end
test 'should require confirmation when updating/reseting password' do
user = create_user
user.password_confirmation = 'another_password'
assert user.invalid?
assert_equal 'doesn\'t match confirmation', user.errors[:password].join
end
test 'should require a password with minimum of 6 characters' do
user = new_user(:password => '12345', :password_confirmation => '12345')
assert user.invalid?
assert_equal 'is too short (minimum is 6 characters)', user.errors[:password].join
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_equal 'is too long (maximum is 20 characters)', user.errors[:password].join
end
test 'should not require password length when it\'s not changed' do
user = create_user.reload
user.password = user.password_confirmation = nil
assert user.valid?
user.password_confirmation = 'confirmation'
assert user.invalid?
assert_not (user.errors[:password].join =~ /is too long/)
end
test 'shuold not be included in objects with invalid API' do
assert_raise RuntimeError do
Class.new.send :include, Devise::Models::Validatable
end
end
end