Commit 36528941c644a71f0f28fb9c1b9bd738c9c2e2c4

Authored by Carlos Purificação
2 parents 4932de43 c4f08ac2

Merged with user-messages

Showing 2 changed files with 24 additions and 7 deletions   Show diff stats
app/models/user.rb
... ... @@ -102,18 +102,20 @@ class User < ActiveRecord::Base
102 102 # Virtual attribute for the unencrypted password
103 103 attr_accessor :password, :name
104 104  
105   - validates_presence_of :login, :email
106   - validates_format_of :login, :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?})
  105 + validates_presence_of :login
  106 + validates_presence_of :email
  107 + validates_format_of :login, :message => _('incorrect format'), :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?})
107 108 validates_presence_of :password, :if => :password_required?
108 109 validates_presence_of :password_confirmation, :if => :password_required?
109   - validates_length_of :password, :within => 4..40, :if => :password_required?
  110 + validates_length_of :password, :message => _('length must be within 4 to 40 characters'), :within => 4..40, :if => :password_required?
110 111 validates_confirmation_of :password, :if => :password_required?
111   - validates_length_of :login, :within => 2..40, :if => (lambda {|user| !user.login.blank?})
112   - validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?})
113   - validates_uniqueness_of :login, :email, :case_sensitive => false, :scope => :environment_id
  112 + validates_length_of :login, :message => _('length must be within 2 to 40 characters'), :within => 2..40, :if => (lambda {|user| !user.login.blank?})
  113 + validates_length_of :email, :message => _('length must be within 3 to 100 characters'),:within => 3..100, :if => (lambda {|user| !user.email.blank?})
  114 + validates_uniqueness_of :login, :case_sensitive => false, :scope => :environment_id
  115 + validates_uniqueness_of :email, :case_sensitive => false, :scope => :environment_id
114 116 before_save :encrypt_password
115 117 before_save :normalize_email, if: proc{ |u| u.email.present? }
116   - validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})
  118 + validates_format_of :email, :message => _('incorrect format'), :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})
117 119  
118 120 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
119 121  
... ...
test/unit/user_test.rb
... ... @@ -21,6 +21,14 @@ class UserTest < ActiveSupport::TestCase
21 21 end
22 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 32 def test_should_require_password
25 33 assert_no_difference 'User.count' do
26 34 u = new_user(:password => nil)
... ... @@ -42,6 +50,13 @@ class UserTest < ActiveSupport::TestCase
42 50 end
43 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 60 def test_should_reset_password
46 61 users(:johndoe).update_attributes(:password => 'new password', :password_confirmation => 'new password')
47 62 assert_equal users(:johndoe), User.authenticate('johndoe', 'new password')
... ...