Browse Source

extract method #populate_otp_column

useful for populating column values for pre-existing devise-enabled records
master
Ross Kaffenberger 11 years ago
parent
commit
9ef373482a
  1. 6
      lib/two_factor_authentication/models/two_factor_authenticatable.rb
  2. 25
      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
before_create { self.otp_column = ROTP::Base32.random_base32 }
before_create { populate_otp_column }
if respond_to?(:attributes_protected_by_default)
def self.attributes_protected_by_default #:nodoc:
@ -60,6 +60,10 @@ module Devise
second_factor_attempts_count >= self.class.max_login_attempts
end
def populate_otp_column
self.otp_column = ROTP::Base32.random_base32
end
end
end
end

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

@ -1,7 +1,6 @@
require 'spec_helper'
include AuthenticatedModelHelper
describe Devise::Models::TwoFactorAuthenticatable, '#otp_code' do
let(:instance) { AuthenticatedModelHelper.create_new_user }
subject { instance.otp_code(time) }
@ -67,4 +66,26 @@ describe Devise::Models::TwoFactorAuthenticatable, '#send_two_factor_authenticat
instance = AuthenticatedModelHelper.create_new_user_with_overrides
expect(instance.send_two_factor_authentication_code).to eq("Code sent")
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

Loading…
Cancel
Save