Browse Source

Spec: validate that send_two_factor_authentication_code can be overwritten

master
Matt Mueller 11 years ago
parent
commit
4d2dbe1c44
  1. 8
      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

8
lib/two_factor_authentication/models/two_factor_authenticatable.rb

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

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

@ -53,3 +53,18 @@ describe Devise::Models::TwoFactorAuthenticatable, '#authenticate_otp' do
expect(do_invoke(code)).to eq(false) expect(do_invoke(code)).to eq(false)
end end
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 has_one_time_password
end end
class UserWithOverrides < User
def send_two_factor_authentication_code
"Code sent"
end
end
def create_new_user def create_new_user
User.new User.new
end end
def create_new_user_with_overrides
UserWithOverrides.new
end
end end
Loading…
Cancel
Save