Browse Source

Introducing first spec to test otp code.

master
Matt Mueller 11 years ago
parent
commit
d00f0ae35e
  1. 1
      lib/two_factor_authentication.rb
  2. 33
      spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb
  3. 1
      spec/spec_helper.rb
  4. 18
      spec/support/authenticated_model_helper.rb

1
lib/two_factor_authentication.rb

@ -2,6 +2,7 @@ require 'two_factor_authentication/version'
require 'devise' require 'devise'
require 'active_support/concern' require 'active_support/concern'
require "active_model" require "active_model"
require "active_record"
require "active_support/core_ext/class/attribute_accessors" require "active_support/core_ext/class/attribute_accessors"
require "cgi" require "cgi"
require "rotp" require "rotp"

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

@ -0,0 +1,33 @@
require 'spec_helper'
include AuthenticatedModelHelper
describe Devise::Models::TwoFactorAuthenticatable, '#otp_code' do
let(:instance) { AuthenticatedModelHelper.create_new_user }
subject { instance.otp_code(time) }
let(:time) { 1392852456 }
it "should return an error if no secret is set" do
expect {
subject
}.to raise_error
end
context "secret is set" do
before :each do
instance.otp_secret_key = "2z6hxkdwi3uvrnpn"
end
it "should not return an error" do
subject
end
context "with a known time" do
let(:time) { 1392852756 }
it "should return a known result" do
expect(subject).to eq(562202)
end
end
end
end

1
spec/spec_helper.rb

@ -3,6 +3,7 @@ require "bundler/setup"
require 'two_factor_authentication' require 'two_factor_authentication'
Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f} Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f}

18
spec/support/authenticated_model_helper.rb

@ -0,0 +1,18 @@
module AuthenticatedModelHelper
class User
extend ActiveModel::Callbacks
include ActiveModel::Validations
include Devise::Models::TwoFactorAuthenticatable
define_model_callbacks :create
attr_accessor :otp_secret_key, :email
has_one_time_password
end
def create_new_user
User.new
end
end
Loading…
Cancel
Save