increate test coverage for user and project model

This commit is contained in:
Reinier Balt 2013-02-28 16:44:42 +01:00
parent 188baa938d
commit bf3a1e653a
4 changed files with 96 additions and 44 deletions

View file

@ -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

View file

@ -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.

View file

@ -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

View file

@ -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