Commit c4f08ac291a0ce08746d84e4448f490073c4ee8f

Authored by Carlos Purificação
1 parent 80c4b310

Individualized format messages and added tests

Showing 2 changed files with 22 additions and 7 deletions   Show diff stats
app/models/user.rb
@@ -95,18 +95,18 @@ class User < ActiveRecord::Base @@ -95,18 +95,18 @@ class User < ActiveRecord::Base
95 95
96 validates_presence_of :login 96 validates_presence_of :login
97 validates_presence_of :email 97 validates_presence_of :email
98 - validates_format_of :login, :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?}) 98 + validates_format_of :login, :message => _('incorrect format'), :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?})
99 validates_presence_of :password, :if => :password_required? 99 validates_presence_of :password, :if => :password_required?
100 validates_presence_of :password_confirmation, :if => :password_required? 100 validates_presence_of :password_confirmation, :if => :password_required?
101 - validates_length_of :password, :within => 4..40, :if => :password_required? 101 + validates_length_of :password, :message => _('length must be within 4 to 40 characters'), :within => 4..40, :if => :password_required?
102 validates_confirmation_of :password, :if => :password_required? 102 validates_confirmation_of :password, :if => :password_required?
103 - validates_length_of :login, :within => 2..40, :if => (lambda {|user| !user.login.blank?})  
104 - validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?})  
105 - validates_uniqueness_of :login, :message => _('login already taken')  
106 - validates_uniqueness_of :email, :message => _('email already taken'), :case_sensitive => false, :scope => :environment_id 103 + validates_length_of :login, :message => _('length must be within 2 to 40 characters'), :within => 2..40, :if => (lambda {|user| !user.login.blank?})
  104 + validates_length_of :email, :message => _('length must be within 3 to 100 characters'),:within => 3..100, :if => (lambda {|user| !user.email.blank?})
  105 + validates_uniqueness_of :login, :case_sensitive => false, :scope => :environment_id
  106 + validates_uniqueness_of :email, :case_sensitive => false, :scope => :environment_id
107 before_save :encrypt_password 107 before_save :encrypt_password
108 before_save :normalize_email, if: proc{ |u| u.email.present? } 108 before_save :normalize_email, if: proc{ |u| u.email.present? }
109 - validates_format_of :email, :message => _('incorrect email format'), :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?}) 109 + validates_format_of :email, :message => _('incorrect format'), :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})
110 110
111 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 111 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
112 112
test/unit/user_test.rb
@@ -21,6 +21,14 @@ class UserTest < ActiveSupport::TestCase @@ -21,6 +21,14 @@ class UserTest < ActiveSupport::TestCase
21 end 21 end
22 end 22 end
23 23
  24 + def test_should_not_allow_duplicate_login
  25 + user1 = create_user('new_user', :email => 'new_user1@example.com', :password => 'test', :password_confirmation => 'test')
  26 + assert !user1.errors[:login].present?
  27 + user1.save!
  28 + user2 = new_user(:login => 'new_user')
  29 + assert user2.errors[:login].present?
  30 + end
  31 +
24 def test_should_require_password 32 def test_should_require_password
25 assert_no_difference 'User.count' do 33 assert_no_difference 'User.count' do
26 u = new_user(:password => nil) 34 u = new_user(:password => nil)
@@ -42,6 +50,13 @@ class UserTest < ActiveSupport::TestCase @@ -42,6 +50,13 @@ class UserTest < ActiveSupport::TestCase
42 end 50 end
43 end 51 end
44 52
  53 + def test_email_format
  54 + assert_no_difference 'User.count' do
  55 + u = new_user(:email => 'test.email')
  56 + assert u.errors[:email].present?
  57 + end
  58 + end
  59 +
45 def test_should_reset_password 60 def test_should_reset_password
46 users(:johndoe).update_attributes(:password => 'new password', :password_confirmation => 'new password') 61 users(:johndoe).update_attributes(:password => 'new password', :password_confirmation => 'new password')
47 assert_equal users(:johndoe), User.authenticate('johndoe', 'new password') 62 assert_equal users(:johndoe), User.authenticate('johndoe', 'new password')