Skip to content

Commit

Permalink
extract method #populate_otp_column
Browse files Browse the repository at this point in the history
useful for populating column values for pre-existing devise-enabled records
  • Loading branch information
rossta committed Apr 2, 2014
1 parent f918008 commit 9ef3734
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def has_one_time_password(options = {})

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:
Expand Down Expand Up @@ -60,6 +60,10 @@ def max_login_attempts?
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down Expand Up @@ -67,4 +66,26 @@ def do_invoke code, options = {}
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

0 comments on commit 9ef3734

Please sign in to comment.