From c81ec625543003f1b4f532e7cb5caf3cd9668e12 Mon Sep 17 00:00:00 2001 From: Christian Frichot Date: Thu, 26 Jan 2012 21:08:01 +0800 Subject: [PATCH] Model testing appears to be working --- .../models/google_authenticatable.rb | 1 + test/generators_test.rb | 9 +- test/model_tests_helper.rb | 21 ++ test/models/google_authenticatable_test.rb | 64 ++++++ test/models_test.rb | 54 +++++ test/rails_app/config/initializers/devise.rb | 2 +- test/rails_app/log/test.log | 201 ++++++++++++++++++ 7 files changed, 350 insertions(+), 2 deletions(-) create mode 100644 test/model_tests_helper.rb create mode 100644 test/models/google_authenticatable_test.rb create mode 100644 test/models_test.rb diff --git a/lib/devise_google_authenticatable/models/google_authenticatable.rb b/lib/devise_google_authenticatable/models/google_authenticatable.rb index 21531dc..1b1094c 100644 --- a/lib/devise_google_authenticatable/models/google_authenticatable.rb +++ b/lib/devise_google_authenticatable/models/google_authenticatable.rb @@ -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 diff --git a/test/generators_test.rb b/test/generators_test.rb index 99dfdfe..54d4281 100644 --- a/test/generators_test.rb +++ b/test/generators_test.rb @@ -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 \ No newline at end of file diff --git a/test/model_tests_helper.rb b/test/model_tests_helper.rb new file mode 100644 index 0000000..488c14f --- /dev/null +++ b/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 diff --git a/test/models/google_authenticatable_test.rb b/test/models/google_authenticatable_test.rb new file mode 100644 index 0000000..815486f --- /dev/null +++ b/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 \ No newline at end of file diff --git a/test/models_test.rb b/test/models_test.rb new file mode 100644 index 0000000..6e46b9c --- /dev/null +++ b/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 \ No newline at end of file diff --git a/test/rails_app/config/initializers/devise.rb b/test/rails_app/config/initializers/devise.rb index 7cf99ea..360d5ca 100644 --- a/test/rails_app/config/initializers/devise.rb +++ b/test/rails_app/config/initializers/devise.rb @@ -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 diff --git a/test/rails_app/log/test.log b/test/rails_app/log/test.log index e69de29..1be94bc 100644 --- a/test/rails_app/log/test.log +++ b/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`