From bf3a1e653af14d26dfd0e379ad82c8c76984b1da Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Thu, 28 Feb 2013 16:44:42 +0100 Subject: [PATCH] increate test coverage for user and project model --- app/models/user.rb | 9 --- test/test_helper.rb | 2 +- test/unit/project_test.rb | 16 ++++++ test/unit/user_test.rb | 113 ++++++++++++++++++++++++++------------ 4 files changed, 96 insertions(+), 44 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index ee155bd6..36419d0c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -130,15 +130,6 @@ class User < ActiveRecord::Base candidate.password_matches? pass end - if Tracks::Config.auth_schemes.include?('ldap') - return candidate if candidate.auth_type == 'ldap' && SimpleLdapAuthenticator.valid?(login, pass) - end - - if Tracks::Config.auth_schemes.include?('cas') - # because we can not auth them with out thier real password we have to settle for this - return candidate if candidate.auth_type.eql?("cas") - end - return nil end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8779c868..7efb8388 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,7 +6,7 @@ require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' # set config for tests. Overwrite those read from config/site.yml. Use inject to avoid warning about changing CONSTANT -{"salt" => "change-me", "authentication_schemes" => ["database", "open_id", "ldap"], "prefered_auth" => "database"}.inject( SITE_CONFIG ) { |h, elem| h[elem[0]] = elem[1]; h } +{"salt" => "change-me", "authentication_schemes" => ["database"], "prefered_auth" => "database"}.inject( SITE_CONFIG ) { |h, elem| h[elem[0]] = elem[1]; h } class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index 4de18967..9a06a74f 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -174,5 +174,21 @@ class ProjectTest < ActiveSupport::TestCase assert_equal project.description, todo.notes assert_equal project.default_context, todo.context end + + def test_new_record_before_save + assert !@timemachine.new_record_before_save?, "existing records should not be new_record" + p = Project.where(:name => "I do not exist").first_or_create + assert p.new_record_before_save?, "newly created record should be new_record" + end + + def test_shortened_name + s = "project"*7 # len=49 + p = users(:admin_user).projects.create(:name => s) + assert_equal 49, p.name.length + assert_equal 40, p.shortened_name.length + assert_equal "project"*5+"pr...", p.shortened_name + + assert p.shortened_name.html_safe? + end end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index da51de18..42c8c472 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -1,20 +1,11 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper') -class SimpleLdapAuthenticator - cattr_accessor :fake_success - - def self.valid?(login, pass) - fake_success - end -end - class UserTest < ActiveSupport::TestCase fixtures :users, :preferences, :projects, :contexts, :todos, :recurring_todos def setup assert_equal "test", ENV['RAILS_ENV'] assert_equal "change-me", Tracks::Config.salt - assert Tracks::Config.auth_schemes.include?('ldap') @admin_user = User.find(1) @other_user = User.find(2) end @@ -187,16 +178,6 @@ class UserTest < ActiveSupport::TestCase assert_equal ["not a valid authentication type (dnacheck)"], @other_user.errors[:auth_type] end - def test_authenticate_can_use_ldap - u = @other_user - u.auth_type = 'ldap' - u.save! - SimpleLdapAuthenticator.fake_success = false - assert_nil User.authenticate(u.login, 'foobar') - SimpleLdapAuthenticator.fake_success = true - assert_equal @other_user, User.authenticate(u.login, 'foobar') - end - def test_find_context_by_params u = @admin_user c = u.contexts.find_by_params('id' => '1') @@ -333,19 +314,83 @@ class UserTest < ActiveSupport::TestCase assert_not_nil User.authenticate(users(:user_with_sha1_password).login, "foobar") assert_nil User.authenticate(users(:user_with_sha1_password).login, "wrong") end - - + + def test_update_positions_of_contexts + u = users(:admin_user) + assert_equal "1,2,3,4,5,6,7,8,9,12", u.contexts.map(&:id).join(",") + + u.contexts.update_positions [1,2,3,8,9,12,4,5,6,7] + assert_equal "1,2,3,8,9,12,4,5,6,7", u.contexts.reload.map(&:id).join(",") + end + + def test_cache_notes_count_for_projects + u = users(:admin_user) + u.projects.each do |p| + assert_nil p.cached_note_count, "notes count should not be there" + end + + u.projects.cache_note_counts + + u.projects.each do |p| + assert !p.cached_note_count.nil?, "notes count should be there" + end + end + + def test_actionize_projects + u = users(:admin_user) + assert_equal "1,2,3", u.projects.map(&:id).join(",") + + u.projects.actionize + + assert_equal "3,2,1", u.projects.reload.map(&:id).join(",") + end + + def test_remember_token + u = users(:admin_user) + assert_nil u.remember_token + assert_nil u.remember_token_expires_at + + # set token on 2013-feb-28 + Timecop.travel(Time.local(2013, 2, 28)) do + u.remember_me + assert_not_nil u.remember_token_expires_at + + assert u.remember_token? + end + + # token should be valid after 5 days + Timecop.travel(Time.local(2013, 3, 5)) do + assert u.remember_token? + end + + # token should not be valid after more than 2 weeks + Timecop.travel(Time.local(2013, 3, 28)) do + assert !u.remember_token? + end + end + + def test_count_todos_by_group + u = users(:admin_user) + + # test group counts for projects and contexts + project_counts = u.todos.count_by_group(:project_id) + assert_equal "6,3,4,4", project_counts.map{|pc|pc[1]}.join(",") + + context_counts = u.todos.count_by_group(:context_id) + assert_equal "7,3,1,3,1,2", context_counts.map{|cc|cc[1]}.join(",") + + # add a todo to the first context and check that the count is increased + u.todos.create!(:description => "yet another todo", :context => u.contexts.first) + + context_counts = u.todos.reload.count_by_group(:context_id) + assert_equal "8,3,1,3,1,2", context_counts.map{|cc|cc[1]}.join(",") + end + protected - def create_user(options = {}) - options[:password_confirmation] = options[:password] unless options.has_key?(:password_confirmation) || !options.has_key?(:password) - User.create({ :login => 'quire', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) - end - - def assert_open_id_url_normalized_on_save(initial, expected) - u = users(:other_user) - u.open_id_url = initial - u.save - assert_equal expected, u.open_id_url - end - -end + + def create_user(options = {}) + options[:password_confirmation] = options[:password] unless options.has_key?(:password_confirmation) || !options.has_key?(:password) + User.create({ :login => 'quire', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) + end + +end \ No newline at end of file