Browse Source

Spec: validate that send_two_factor_authentication_code can be overwritten

master
Matt Mueller 11 years ago
parent
commit
4d2dbe1c44
  1. 18
      lib/two_factor_authentication/models/two_factor_authenticatable.rb
  2. 15
      spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb
  3. 11
      spec/support/authenticated_model_helper.rb

18
lib/two_factor_authentication/models/two_factor_authenticatable.rb

@ -50,18 +50,18 @@ module Devise
self.send("#{self.class.otp_column_name}=", attr)
end
end
def need_two_factor_authentication?(request)
true
end
def need_two_factor_authentication?(request)
true
end
def send_two_factor_authentication_code
raise NotImplementedError.new("No default implementation - please define in your class.")
end
def send_two_factor_authentication_code(code)
p "Code is #{code}"
end
def max_login_attempts?
second_factor_attempts_count >= self.class.max_login_attempts
end
def max_login_attempts?
second_factor_attempts_count >= self.class.max_login_attempts
end
end
end

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

@ -52,4 +52,19 @@ describe Devise::Models::TwoFactorAuthenticatable, '#authenticate_otp' do
code = instance.otp_code(1.minutes.ago.to_i)
expect(do_invoke(code)).to eq(false)
end
end
describe Devise::Models::TwoFactorAuthenticatable, '#send_two_factor_authentication_code' do
it "should raise an error by default" do
instance = AuthenticatedModelHelper.create_new_user
expect {
instance.send_two_factor_authentication_code
}.to raise_error(NotImplementedError)
end
it "should be overrideable" do
instance = AuthenticatedModelHelper.create_new_user_with_overrides
expect(instance.send_two_factor_authentication_code).to eq("Code sent")
end
end

11
spec/support/authenticated_model_helper.rb

@ -11,8 +11,19 @@ module AuthenticatedModelHelper
has_one_time_password
end
class UserWithOverrides < User
def send_two_factor_authentication_code
"Code sent"
end
end
def create_new_user
User.new
end
def create_new_user_with_overrides
UserWithOverrides.new
end
end
Loading…
Cancel
Save