Browse Source

extract method #max_login_attempts

Respects Law of Demeter, useful for stubbing out methods on instance or
for apps that use the NullObject pattern for guest user accounts.
master
Ross Kaffenberger 11 years ago
parent
commit
153a4531ca
  1. 6
      lib/two_factor_authentication/models/two_factor_authenticatable.rb
  2. 33
      spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb
  3. 2
      spec/spec_helper.rb
  4. 4
      spec/support/authenticated_model_helper.rb

6
lib/two_factor_authentication/models/two_factor_authenticatable.rb

@ -57,7 +57,11 @@ module Devise
end
def max_login_attempts?
second_factor_attempts_count >= self.class.max_login_attempts
second_factor_attempts_count.to_i >= max_login_attempts.to_i
end
def max_login_attempts
self.class.max_login_attempts
end
end

33
spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb

@ -98,3 +98,36 @@ describe Devise::Models::TwoFactorAuthenticatable, '#provisioning_uri' do
end
end
describe Devise::Models::TwoFactorAuthenticatable, '#max_login_attempts' do
let(:instance) { AuthenticatedModelHelper.create_new_user }
before do
@original_max_login_attempts = User.max_login_attempts
User.max_login_attempts = 3
end
after { User.max_login_attempts = @original_max_login_attempts }
it "returns class setting" do
expect(instance.max_login_attempts).to eq(3)
end
it "returns false as boolean" do
instance.second_factor_attempts_count = nil
expect(instance.max_login_attempts?).to be_false
instance.second_factor_attempts_count = 0
expect(instance.max_login_attempts?).to be_false
instance.second_factor_attempts_count = 1
expect(instance.max_login_attempts?).to be_false
instance.second_factor_attempts_count = 2
expect(instance.max_login_attempts?).to be_false
end
it "returns true as boolean after too many attempts" do
instance.second_factor_attempts_count = 3
expect(instance.max_login_attempts?).to be_true
instance.second_factor_attempts_count = 4
expect(instance.max_login_attempts?).to be_true
end
end

2
spec/spec_helper.rb

@ -3,10 +3,8 @@ require "bundler/setup"
require 'two_factor_authentication'
Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f}
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true

4
spec/support/authenticated_model_helper.rb

@ -6,7 +6,7 @@ module AuthenticatedModelHelper
include Devise::Models::TwoFactorAuthenticatable
define_model_callbacks :create
attr_accessor :otp_secret_key, :email
attr_accessor :otp_secret_key, :email, :second_factor_attempts_count
has_one_time_password
end
@ -26,4 +26,4 @@ module AuthenticatedModelHelper
UserWithOverrides.new
end
end
end

Loading…
Cancel
Save