Browse Source

Merge pull request #22 from rossta/extract_method_max_login_attempts

extract method #max_login_attempts
master
Dmitrii Golub 11 years ago
parent
commit
9f50283743
  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. 2
      spec/support/authenticated_model_helper.rb

6
lib/two_factor_authentication/models/two_factor_authenticatable.rb

@ -57,7 +57,11 @@ module Devise
end end
def max_login_attempts? 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
def populate_otp_column def populate_otp_column

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

@ -119,3 +119,36 @@ describe Devise::Models::TwoFactorAuthenticatable, '#populate_otp_column' do
expect(instance.otp_secret_key).to_not eq(original_key) expect(instance.otp_secret_key).to_not eq(original_key)
end end
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' require 'two_factor_authentication'
Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f} Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f}
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config| RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true config.treat_symbols_as_metadata_keys_with_true_values = true

2
spec/support/authenticated_model_helper.rb

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

Loading…
Cancel
Save