diff --git a/app/models/user.rb b/app/models/user.rb index aaa43c0..ca05a7b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -95,18 +95,18 @@ class User < ActiveRecord::Base validates_presence_of :login validates_presence_of :email - validates_format_of :login, :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?}) + validates_format_of :login, :message => _('incorrect format'), :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?}) validates_presence_of :password, :if => :password_required? validates_presence_of :password_confirmation, :if => :password_required? - validates_length_of :password, :within => 4..40, :if => :password_required? + validates_length_of :password, :message => _('length must be within 4 to 40 characters'), :within => 4..40, :if => :password_required? validates_confirmation_of :password, :if => :password_required? - validates_length_of :login, :within => 2..40, :if => (lambda {|user| !user.login.blank?}) - validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?}) - validates_uniqueness_of :login, :message => _('login already taken') - validates_uniqueness_of :email, :message => _('email already taken'), :case_sensitive => false, :scope => :environment_id + validates_length_of :login, :message => _('length must be within 2 to 40 characters'), :within => 2..40, :if => (lambda {|user| !user.login.blank?}) + validates_length_of :email, :message => _('length must be within 3 to 100 characters'),:within => 3..100, :if => (lambda {|user| !user.email.blank?}) + validates_uniqueness_of :login, :case_sensitive => false, :scope => :environment_id + validates_uniqueness_of :email, :case_sensitive => false, :scope => :environment_id before_save :encrypt_password before_save :normalize_email, if: proc{ |u| u.email.present? } - validates_format_of :email, :message => _('incorrect email format'), :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?}) + validates_format_of :email, :message => _('incorrect format'), :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?}) validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('{fn} must be checked in order to signup.').fix_i18n diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 4551feb..a34502f 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -21,6 +21,14 @@ class UserTest < ActiveSupport::TestCase end end + def test_should_not_allow_duplicate_login + user1 = create_user('new_user', :email => 'new_user1@example.com', :password => 'test', :password_confirmation => 'test') + assert !user1.errors[:login].present? + user1.save! + user2 = new_user(:login => 'new_user') + assert user2.errors[:login].present? + end + def test_should_require_password assert_no_difference 'User.count' do u = new_user(:password => nil) @@ -42,6 +50,13 @@ class UserTest < ActiveSupport::TestCase end end + def test_email_format + assert_no_difference 'User.count' do + u = new_user(:email => 'test.email') + assert u.errors[:email].present? + end + end + def test_should_reset_password users(:johndoe).update_attributes(:password => 'new password', :password_confirmation => 'new password') assert_equal users(:johndoe), User.authenticate('johndoe', 'new password') -- libgit2 0.21.2