Skip to content

Commit

Permalink
* Stop using errors.on(key) since that is now deprecated in Rails. Us…
Browse files Browse the repository at this point in the history
…e errors[key] instead.
  • Loading branch information
binarylogic committed Jun 10, 2009
1 parent 6e901ba commit bcb0d4a
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
@@ -1,6 +1,8 @@
== 2.0.14

* Fixed issue with using brute force protection AND generalize_credentials_error_messages. Brute force protection was looking to see if there were password errors, which generalize_credentials_error_messages was obfuscating. This is all fixed now though thanks to a handy article on Microsoft (http://support.microsoft.com/kb/168702), method #2, it works every time.
* Added db_setup? method to avoid errors during rake tasks where the db might not be set up. Ex: migrations
* Stop using errors.on(key) since that is now deprecated in Rails. Use errors[key] instead.

== 2.0.13 released 2009-5-13

Expand Down
1 change: 1 addition & 0 deletions README.rdoc
Expand Up @@ -66,6 +66,7 @@ If you find a bug or a problem please post it on lighthouse. If you need help wi

* <b>Authlogic OpenID addon:</b> http://github.com/binarylogic/authlogic_openid
* <b>Authlogic LDAP addon:</b> http://github.com/binarylogic/authlogic_ldap
* <b>Authlogic Facebook Connect:</b> http://github.com/kalasjocke/authlogic_facebook_connect

If you create one of your own, please let me know about it so I can add it to this list. Or just fork the project, add your link, and send me a pull request.

Expand Down
2 changes: 1 addition & 1 deletion lib/authlogic/session/brute_force_protection.rb
Expand Up @@ -76,7 +76,7 @@ def reset_failed_login_count

def validate_failed_logins
errors.clear # Clear all other error messages, as they are irrelevant at this point and can only provide additional information that is not needed
errors.add_to_base(I18n.t('error_messages.consecutive_failed_logins_limit_exceeded', :default => "Consecutive failed logins limit exceeded, account is disabled."))
errors.add(:base, I18n.t('error_messages.consecutive_failed_logins_limit_exceeded', :default => "Consecutive failed logins limit exceeded, account is disabled."))
end

def consecutive_failed_logins_limit
Expand Down
2 changes: 1 addition & 1 deletion lib/authlogic/session/magic_states.rb
Expand Up @@ -46,7 +46,7 @@ def validate_magic_states
return true if attempted_record.nil?
[:active, :approved, :confirmed].each do |required_status|
if attempted_record.respond_to?("#{required_status}?") && !attempted_record.send("#{required_status}?")
errors.add_to_base(I18n.t("error_messages.not_#{required_status}", :default => "Your account is not #{required_status}"))
errors.add(:base, I18n.t("error_messages.not_#{required_status}", :default => "Your account is not #{required_status}"))
return false
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/authlogic/session/password.rb
Expand Up @@ -198,7 +198,7 @@ def login_field
end

def add_general_credentials_error
errors.add_to_base(I18n.t('error_messages.general_credentials_error', :default => "#{login_field.to_s.humanize}/Password combination is not valid"))
errors.add(:base, I18n.t('error_messages.general_credentials_error', :default => "#{login_field.to_s.humanize}/Password combination is not valid"))
end

def generalize_credentials_error_messages?
Expand Down
14 changes: 9 additions & 5 deletions lib/authlogic/session/validation.rb
Expand Up @@ -10,10 +10,14 @@ module Validation
# private
# def check_if_awesome
# errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
# errors.add_to_base("You must be awesome to log in") unless attempted_record.awesome?
# errors.add(:base, "You must be awesome to log in") unless attempted_record.awesome?
# end
# end
class Errors < ::ActiveRecord::Errors
def [](key)
value = super
value.is_a?(Array) ? value : [value].compact
end
end

# You should use this as a place holder for any records that you find during validation. The main reason for this is to
Expand All @@ -39,7 +43,7 @@ def attempted_record=(value)
# private
# def check_if_awesome
# errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
# errors.add_to_base("You must be awesome to log in") unless attempted_record.awesome?
# errors.add(:base, "You must be awesome to log in") unless attempted_record.awesome?
# end
# end
def errors
Expand All @@ -58,18 +62,18 @@ def valid?
validate
ensure_authentication_attempted

if errors.empty?
if errors.size == 0
new_session? ? after_validation_on_create : after_validation_on_update
after_validation
end

save_record(attempted_record)
errors.empty?
errors.size == 0
end

private
def ensure_authentication_attempted
errors.add_to_base(I18n.t('error_messages.no_authentication_details', :default => "You did not provide any details for authentication.")) if errors.empty? && attempted_record.nil?
errors.add(:base, I18n.t('error_messages.no_authentication_details', :default => "You did not provide any details for authentication.")) if errors.empty? && attempted_record.nil?
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions test/acts_as_authentic_test/email_test.rb
Expand Up @@ -57,41 +57,41 @@ def test_validates_length_of_email_field
u = User.new
u.email = "a@a.a"
assert !u.valid?
assert u.errors.on(:email)
assert u.errors[:email].size > 0

u.email = "a@a.com"
assert !u.valid?
assert !u.errors.on(:email)
assert u.errors[:email].size == 0
end

def test_validates_format_of_email_field
u = User.new
u.email = "aaaaaaaaaaaaa"
assert !u.valid?
assert u.errors.on(:email)
assert u.errors[:email].size > 0

u.email = "a@a.com"
assert !u.valid?
assert !u.errors.on(:email)
assert u.errors[:email].size == 0

u.email = "dakota.dux+1@gmail.com"
assert !u.valid?
assert !u.errors.on(:email)
assert u.errors[:email].size == 0
end

def test_validates_uniqueness_of_email_field
u = User.new
u.email = "bjohnson@binarylogic.com"
assert !u.valid?
assert u.errors.on(:email)
assert u.errors[:email].size > 0

u.email = "BJOHNSON@binarylogic.com"
assert !u.valid?
assert u.errors.on(:email)
assert u.errors[:email].size > 0

u.email = "a@a.com"
assert !u.valid?
assert !u.errors.on(:email)
assert u.errors[:email].size == 0
end
end
end
16 changes: 8 additions & 8 deletions test/acts_as_authentic_test/login_test.rb
Expand Up @@ -57,41 +57,41 @@ def test_validates_length_of_login_field
u = User.new
u.login = "a"
assert !u.valid?
assert u.errors.on(:login)
assert u.errors[:login].size > 0

u.login = "aaaaaaaaaa"
assert !u.valid?
assert !u.errors.on(:login)
assert u.errors[:login].size == 0
end

def test_validates_format_of_login_field
u = User.new
u.login = "fdsf@^&*"
assert !u.valid?
assert u.errors.on(:login)
assert u.errors[:login].size > 0

u.login = "fdsfdsfdsfdsfs"
assert !u.valid?
assert !u.errors.on(:login)
assert u.errors[:login].size == 0

u.login = "dakota.dux+1@gmail.com"
assert !u.valid?
assert !u.errors.on(:login)
assert u.errors[:login].size == 0
end

def test_validates_uniqueness_of_login_field
u = User.new
u.login = "bjohnson"
assert !u.valid?
assert u.errors.on(:login)
assert u.errors[:login].size > 0

u.login = "BJOHNSON"
assert !u.valid?
assert u.errors.on(:login)
assert u.errors[:login].size > 0

u.login = "fdsfdsf"
assert !u.valid?
assert !u.errors.on(:login)
assert u.errors[:login].size == 0
end

def test_find_by_smart_case_login_field
Expand Down
8 changes: 4 additions & 4 deletions test/acts_as_authentic_test/magic_columns_test.rb
Expand Up @@ -6,22 +6,22 @@ def test_validates_numericality_of_login_count
u = User.new
u.login_count = -1
assert !u.valid?
assert u.errors.on(:login_count)
assert u.errors[:login_count].size > 0

u.login_count = 0
assert !u.valid?
assert !u.errors.on(:login_count)
assert u.errors[:login_count].size == 0
end

def test_validates_numericality_of_failed_login_count
u = User.new
u.failed_login_count = -1
assert !u.valid?
assert u.errors.on(:failed_login_count)
assert u.errors[:failed_login_count].size > 0

u.failed_login_count = 0
assert !u.valid?
assert !u.errors.on(:failed_login_count)
assert u.errors[:failed_login_count].size == 0
end
end
end
14 changes: 7 additions & 7 deletions test/acts_as_authentic_test/password_test.rb
Expand Up @@ -107,23 +107,23 @@ def test_validates_length_of_password
u = User.new
u.password_confirmation = "test2"
assert !u.valid?
assert u.errors.on(:password)
assert u.errors[:password].size > 0

u.password = "test"
assert !u.valid?
assert !u.errors.on(:password_confirmation)
assert u.errors[:password_confirmation].size == 0
end

def test_validates_confirmation_of_password
u = User.new
u.password = "test"
u.password_confirmation = "test2"
assert !u.valid?
assert u.errors.on(:password)
assert u.errors[:password].size > 0

u.password_confirmation = "test"
assert !u.valid?
assert !u.errors.on(:password)
assert u.errors[:password].size == 0
end

def test_validates_length_of_password_confirmation
Expand All @@ -132,18 +132,18 @@ def test_validates_length_of_password_confirmation
u.password = "test"
u.password_confirmation = ""
assert !u.valid?
assert u.errors.on(:password_confirmation)
assert u.errors[:password_confirmation].size > 0

u.password_confirmation = "test"
assert !u.valid?
assert !u.errors.on(:password_confirmation)
assert u.errors[:password_confirmation].size == 0

ben = users(:ben)
assert ben.valid?

ben.password = "newpass"
assert !ben.valid?
assert ben.errors.on(:password_confirmation)
assert ben.errors[:password_confirmation].size > 0

ben.password_confirmation = "newpass"
assert ben.valid?
Expand Down
2 changes: 1 addition & 1 deletion test/acts_as_authentic_test/perishable_token_test.rb
Expand Up @@ -26,7 +26,7 @@ def test_validates_uniqueness_of_perishable_token
u = User.new
u.perishable_token = users(:ben).perishable_token
assert !u.valid?
assert u.errors.on(:perishable_token)
assert u.errors[:perishable_token].size > 0
end

def test_before_save_reset_perishable_token
Expand Down
2 changes: 1 addition & 1 deletion test/acts_as_authentic_test/single_access_test.rb
Expand Up @@ -16,7 +16,7 @@ def test_validates_uniqueness_of_single_access_token
u = User.new
u.single_access_token = users(:ben).single_access_token
assert !u.valid?
assert u.errors.on(:single_access_token)
assert u.errors[:single_access_token].size > 0
end

def test_before_validation_reset_single_access_token
Expand Down
6 changes: 3 additions & 3 deletions test/session_test/brute_force_protection_test.rb
Expand Up @@ -45,13 +45,13 @@ def test_exceeding_failed_logins_limit
2.times do |i|
session = UserSession.new(:login => ben.login, :password => "badpassword1")
assert !session.save
assert session.errors.on(:password)
assert session.errors[:password].size > 0
assert_equal i + 1, ben.reload.failed_login_count
end

session = UserSession.new(:login => ben.login, :password => "badpassword2")
assert !session.save
assert !session.errors.on(:password)
assert session.errors[:password].size == 0
assert_equal 3, ben.reload.failed_login_count

UserSession.consecutive_failed_logins_limit = 50
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_exceeded_ban_and_failed_doesnt_ban_again
2.times do |i|
session = UserSession.new(:login => ben.login, :password => "badpassword1")
assert !session.save
assert session.errors.on(:password)
assert session.errors[:password].size > 0
assert_equal i + 1, ben.reload.failed_login_count
end

Expand Down
6 changes: 3 additions & 3 deletions test/session_test/magic_states_test.rb
Expand Up @@ -31,7 +31,7 @@ def test_validate_validate_magic_states_active

ben.update_attribute(:active, false)
assert !session.valid?
assert session.errors.on_base.size > 0
assert session.errors[:base].size > 0
end

def test_validate_validate_magic_states_approved
Expand All @@ -42,7 +42,7 @@ def test_validate_validate_magic_states_approved

ben.update_attribute(:approved, false)
assert !session.valid?
assert session.errors.on_base.size > 0
assert session.errors[:base].size > 0
end

def test_validate_validate_magic_states_confirmed
Expand All @@ -53,7 +53,7 @@ def test_validate_validate_magic_states_confirmed

ben.update_attribute(:confirmed, false)
assert !session.valid?
assert session.errors.on_base.size > 0
assert session.errors[:base].size > 0
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/test_helper.rb
Expand Up @@ -4,6 +4,16 @@
require "active_record"
require "active_record/fixtures"

# A temporary fix to bring active record errors up to speed with rails edge.
# I need to remove this once the new gem is released. This is only here so my tests pass.
class ActiveRecord::Errors
def [](key)
value = on(key)
value.is_a?(Array) ? value : [value].compact
end
end


ActiveRecord::Schema.verbose = false
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
ActiveRecord::Base.configurations = true
Expand Down

0 comments on commit bcb0d4a

Please sign in to comment.