Skip to content

Commit

Permalink
Model testing appears to be working
Browse files Browse the repository at this point in the history
  • Loading branch information
xntrik committed Jan 26, 2012
1 parent 95fbb35 commit c81ec62
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 2 deletions.
Expand Up @@ -29,6 +29,7 @@ def assign_tmp
end

def validate_token(token)
return false if self.gauth_tmp_datetime.nil?
if self.gauth_tmp_datetime < self.class.ga_timeout.ago
return false
else
Expand Down
9 changes: 8 additions & 1 deletion test/generators_test.rb
Expand Up @@ -8,6 +8,13 @@ class GeneratorsTest < ActiveSupport::TestCase

test "rails g should include the 3 generators" do
@output = `cd #{RAILS_APP_PATH} && rails g`
assert @output.match(%r|DeviseInvitable:\n devise_invitable\n devise_invitable:install\n devise_invitable:views|)
assert @output.match(%r|DeviseGoogleAuthenticator:\n devise_google_authenticator\n devise_google_authenticator:install\n devise_google_authenticator:views|)
end

test "rails g devise_google_authenticator:install" do
@output = `cd #{RAILS_APP_PATH} && rails g devise_google_authenticator:install -p`
assert @output.match(%r^(inject|insert).+config/initializers/devise\.rb\n^)
assert @output.match(%r|create.+config/locales/devise\.google_authenticator\.en\.yml\n|)
end

end
21 changes: 21 additions & 0 deletions test/model_tests_helper.rb
@@ -0,0 +1,21 @@
class ActiveSupport::TestCase

# Helpers for creating new users
#
def generate_unique_email
@@email_count ||= 0
@@email_count += 1
"test#{@@email_count}@email.com"
end

def valid_attributes(attributes={})
{ :email => generate_unique_email,
:password => '123456',
:password_confirmation => '123456' }.update(attributes)
end

def new_user(attributes={})
User.new(valid_attributes(attributes)).save
end

end
64 changes: 64 additions & 0 deletions test/models/google_authenticatable_test.rb
@@ -0,0 +1,64 @@
require 'test_helper'
require 'model_tests_helper'

class GoogleAuthenticatableTest < ActiveSupport::TestCase

def setup
new_user
end

test 'new users have a non-nil secret set' do
assert_not_nil User.find(1).gauth_secret
end

test 'new users should have gauth_enabled disabled by default' do
assert_equal 0, User.find(1).gauth_enabled.to_i
end

test 'get_qr method works' do
assert_not_nil User.find(1).get_qr
end

test 'updating gauth_enabled to true' do
User.find(1).set_gauth_enabled(:gauth_enabled => 1)
assert_equal 1, User.find(1).gauth_enabled.to_i
end

test 'updating gauth_enabled back to false' do
User.find(1).set_gauth_enabled(:gauth_enabled => 0)
assert_equal 0, User.find(1).gauth_enabled.to_i
end

test 'updating the gauth_tmp key' do
User.find(1).assign_tmp

assert_not_nil User.find(1).gauth_tmp
assert_not_nil User.find(1).gauth_tmp_datetime

sleep(1)

old_tmp = User.find(1).gauth_tmp
old_dt = User.find(1).gauth_tmp_datetime

User.find(1).assign_tmp

assert_not_equal old_tmp, User.find(1).gauth_tmp
assert_not_equal old_dt, User.find(1).gauth_tmp_datetime
end

test 'testing class method for finding by tmp key' do
assert User.find_by_gauth_tmp('invalid').nil?
assert !User.find_by_gauth_tmp(User.find(1).gauth_tmp).nil?
end

test 'testing token validation' do
assert !User.find(1).validate_token('1')
assert !User.find(1).validate_token(ROTP::TOTP.new(User.find(1).get_qr).at(Time.now))

User.find(1).assign_tmp

assert !User.find(1).validate_token('1')
assert User.find(1).validate_token(ROTP::TOTP.new(User.find(1).get_qr).at(Time.now))
end

end
54 changes: 54 additions & 0 deletions test/models_test.rb
@@ -0,0 +1,54 @@
require 'test_helper'

class ModelsTest < ActiveSupport::TestCase
def include_module?(klass, mod)
klass.devise_modules.include?(mod) &&
klass.included_modules.include?(Devise::Models::const_get(mod.to_s.classify))
end

def assert_include_modules(klass, *modules)
modules.each do |mod|
assert include_module?(klass, mod), "#{klass} not include #{mod}"
end

(Devise::ALL - modules).each do |mod|
assert !include_module?(klass, mod), "#{klass} include #{mod}"
end
end

test 'should include Devise modules' do
assert_include_modules User, :database_authenticatable, :registerable, :validatable, :rememberable, :trackable, :google_authenticatable, :recoverable
end

test 'should have a default value for ga_timeout' do
assert_equal 3.minutes, User.ga_timeout
end

test 'should have a default value for ga_timedrift' do
assert_equal 3, User.ga_timedrift
end

test 'should set a new value for ga_timeout' do
old_ga_timeout = User.ga_timeout
User.ga_timeout = 1.minutes

assert_equal 1.minutes, User.ga_timeout

User.ga_timeout = old_ga_timeout
end

test 'should set a new value for ga_timedrift' do
old_ga_timedrift = User.ga_timedrift
User.ga_timedrift = 2

assert_equal 2, User.ga_timedrift

User.ga_timedrift = old_ga_timedrift
end

test 'google_authenticatable attributes' do
assert_equal 'f', User.new.gauth_enabled
assert_nil User.new.gauth_tmp
assert_nil User.new.gauth_tmp_datetime
end
end
2 changes: 1 addition & 1 deletion test/rails_app/config/initializers/devise.rb
Expand Up @@ -13,7 +13,7 @@
# Load and configure the ORM. Supports :active_record (default) and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
require 'devise/orm/#{DEVISE_ORM}'
require "devise/orm/#{DEVISE_ORM}"

# ==> Configuration for any authentication mechanism
# Configure which keys are used when authenticating a user. The default is
Expand Down
201 changes: 201 additions & 0 deletions test/rails_app/log/test.log
@@ -0,0 +1,201 @@
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
WARNING: Can't mass-assign protected attributes: user
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
WARNING: Can't mass-assign protected attributes: user
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
WARNING: Can't mass-assign protected attributes: user
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
WARNING: Can't mass-assign protected attributes: user
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `encrypted_password`

0 comments on commit c81ec62

Please sign in to comment.