Browse Source

Merge pull request #21 from rossta/extract_method_populate_otp_column

master
Dmitrii Golub 11 years ago
parent
commit
f294ad7358
  1. 6
      lib/two_factor_authentication/models/two_factor_authenticatable.rb
  2. 21
      spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb

6
lib/two_factor_authentication/models/two_factor_authenticatable.rb

@ -12,7 +12,7 @@ module Devise
include InstanceMethodsOnActivation include InstanceMethodsOnActivation
before_create { self.otp_column = ROTP::Base32.random_base32 } before_create { populate_otp_column }
if respond_to?(:attributes_protected_by_default) if respond_to?(:attributes_protected_by_default)
def self.attributes_protected_by_default #:nodoc: def self.attributes_protected_by_default #:nodoc:
@ -60,6 +60,10 @@ module Devise
second_factor_attempts_count >= self.class.max_login_attempts second_factor_attempts_count >= self.class.max_login_attempts
end end
def populate_otp_column
self.otp_column = ROTP::Base32.random_base32
end
end end
end end
end end

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

@ -96,5 +96,26 @@ describe Devise::Models::TwoFactorAuthenticatable, '#provisioning_uri' do
expect(params['issuer'].shift).to eq('Magic') expect(params['issuer'].shift).to eq('Magic')
expect(params['secret'].shift).to match(%r{\w{16}}) expect(params['secret'].shift).to match(%r{\w{16}})
end end
end
describe Devise::Models::TwoFactorAuthenticatable, '#populate_otp_column' do
let(:instance) { AuthenticatedModelHelper.create_new_user }
it "populates otp_column on create" do
expect(instance.otp_secret_key).to be_nil
instance.run_callbacks :create # populate_otp_column called via before_create
expect(instance.otp_secret_key).to match(%r{\w{16}})
end
it "repopulates otp_column" do
instance.run_callbacks :create
original_key = instance.otp_secret_key
instance.populate_otp_column
expect(instance.otp_secret_key).to match(%r{\w{16}})
expect(instance.otp_secret_key).to_not eq(original_key)
end
end end

Loading…
Cancel
Save