Removed outer tracks directory.

This commit is contained in:
bsag 2008-05-24 15:57:18 +01:00
parent 649f4a44a4
commit 20940ff348
2274 changed files with 0 additions and 0 deletions

117
test/unit/context_test.rb Normal file
View file

@ -0,0 +1,117 @@
require File.dirname(__FILE__) + '/../test_helper'
class ContextTest < Test::Rails::TestCase
fixtures :contexts, :todos, :users, :preferences
def setup
@agenda = contexts(:agenda)
@email = contexts(:email)
@library = contexts(:library)
end
def test_validate_presence_of_name
@agenda.name = ""
assert !@agenda.save
assert_equal 1, @agenda.errors.count
assert_equal "context must have a name", @agenda.errors.on(:name)
end
def test_validate_name_is_less_than_256
@agenda.name = "a"*256
assert !@agenda.save
assert_equal 1, @agenda.errors.count
assert_equal "context name must be less than 256 characters", @agenda.errors.on(:name)
end
def test_validate_name_is_unique
newcontext = Context.new
newcontext.name = contexts(:agenda).name
newcontext.user_id = contexts(:agenda).user_id
assert !newcontext.save
assert_equal 1, newcontext.errors.count
assert_equal "already exists", newcontext.errors.on(:name)
end
def test_validate_name_does_not_contain_comma
newcontext = Context.new
newcontext.name = "phone,telegraph"
assert !newcontext.save
assert_equal 1, newcontext.errors.count
assert_equal "cannot contain the comma (',') character", newcontext.errors.on(:name)
end
def test_find_by_namepart_with_exact_match
c = Context.find_by_namepart('agenda')
assert_not_nil c
assert_equal @agenda.id, c.id
end
def test_find_by_namepart_with_starts_with
c = Context.find_by_namepart('agen')
assert_not_nil c
assert_equal @agenda.id, c.id
end
def test_delete_context_deletes_todos_within_it
assert_equal 6, @agenda.todos.count
agenda_todo_ids = @agenda.todos.collect{|t| t.id }
@agenda.destroy
agenda_todo_ids.each do |todo_id|
assert !Todo.exists?(todo_id)
end
end
def test_not_done_todos
assert_equal 5, @agenda.not_done_todos.size
t = @agenda.not_done_todos[0]
t.complete!
t.save!
assert_equal 4, Context.find(@agenda.id).not_done_todos.size
end
def test_done_todos
assert_equal 1, @agenda.done_todos.size
t = @agenda.not_done_todos[0]
t.complete!
t.save!
assert_equal 2, Context.find(@agenda.id).done_todos.size
end
def test_to_param_returns_id
assert_equal '1', @agenda.to_param
end
def test_title_reader_returns_name
assert_equal @agenda.name, @agenda.title
end
def test_feed_options
opts = Context.feed_options(users(:admin_user))
assert_equal 'Tracks Contexts', opts[:title], 'Unexpected value for :title key of feed_options'
assert_equal 'Lists all the contexts for Admin Schmadmin', opts[:description], 'Unexpected value for :description key of feed_options'
end
def test_hidden_attr_reader
assert !@agenda.hidden?
@agenda.hide = true
@agenda.save!
@agenda.reload
assert_equal true, @agenda.hidden?
end
def test_summary
undone_todo_count = '5 actions'
assert_equal "<p>#{undone_todo_count}. Context is Active.</p>", @agenda.summary(undone_todo_count)
@agenda.hide = true
@agenda.save!
assert_equal "<p>#{undone_todo_count}. Context is Hidden.</p>", @agenda.summary(undone_todo_count)
end
def test_null_object
c = Context.null_object
assert c.nil?
assert_nil c.id
assert_equal '', c.name
end
end

14
test/unit/notes_test.rb Normal file
View file

@ -0,0 +1,14 @@
require File.dirname(__FILE__) + '/../test_helper'
class NotesTest < Test::Rails::TestCase
fixtures :notes
def setup
@notes = Note.find(1)
end
# Replace this with your real tests.
def test_truth
assert_kind_of Note, @notes
end
end

View file

@ -0,0 +1,35 @@
require File.dirname(__FILE__) + '/../test_helper'
class PreferenceTest < Test::Rails::TestCase
fixtures :users, :preferences
def setup
assert_equal "test", ENV['RAILS_ENV']
assert_equal "change-me", Tracks::Config.salt
@admin_user = User.find(1)
@other_user = User.find(2)
end
def test_time_zone
assert_equal 'London', @admin_user.preference.time_zone
assert_equal @admin_user.preference.tz, TimeZone['London']
end
def test_show_project_on_todo_done
assert @other_user.preference.show_project_on_todo_done
assert !@admin_user.preference.show_project_on_todo_done
end
def test_parse_date
assert_equal Date.new(2007, 5, 20).to_s, @admin_user.preference.parse_date('20/5/2007').to_s
end
def test_parse_date_returns_nil_if_string_is_empty
assert_nil @admin_user.preference.parse_date('')
end
def test_parse_date_returns_nil_if_string_is_nil
assert_nil @admin_user.preference.parse_date(nil)
end
end

187
test/unit/project_test.rb Normal file
View file

@ -0,0 +1,187 @@
require File.dirname(__FILE__) + '/../test_helper'
class ProjectTest < Test::Rails::TestCase
fixtures :projects, :contexts, :todos, :users, :preferences
def setup
@timemachine = projects(:timemachine)
@moremoney = projects(:moremoney)
end
def test_validate_presence_of_name
@timemachine.name = ""
assert !@timemachine.save
assert_equal 1, @timemachine.errors.count
assert_equal "project must have a name", @timemachine.errors.on(:name)
end
def test_validate_name_is_less_than_256
@timemachine.name = "a"*256
assert !@timemachine.save
assert_equal 1, @timemachine.errors.count
assert_equal "project name must be less than 256 characters", @timemachine.errors.on(:name)
end
def test_validate_name_is_unique
newproj = Project.new
newproj.name = projects(:timemachine).name
newproj.user_id = projects(:timemachine).user_id
assert !newproj.save
assert_equal 1, newproj.errors.count
assert_equal "already exists", newproj.errors.on(:name)
end
def test_validate_name_does_not_contain_comma
newproj = Project.new
newproj.name = "Buy iPhones for Luke,bsag,David Allen"
assert !newproj.save
assert_equal 1, newproj.errors.count
assert_equal "cannot contain the comma (',') character", newproj.errors.on(:name)
end
def test_name_removes_extra_spaces
newproj = Project.new
newproj.name = "These Words Have Proximity Issues "
assert newproj.save
assert_equal 0, newproj.errors.count
assert_equal "These Words Have Proximity Issues", newproj.name
# and on update...
@timemachine.name = " a time machine needs lots of spaaaaaaace "
assert @timemachine.save
assert_equal "a time machine needs lots of spaaaaaaace", @timemachine.name
end
def test_project_initial_state_is_active
assert_equal :active, @timemachine.current_state
assert @timemachine.active?
end
def test_hide_project
@timemachine.hide!
assert_equal :hidden, @timemachine.current_state
assert @timemachine.hidden?
end
def test_activate_project
@timemachine.activate!
assert_equal :active, @timemachine.current_state
assert @timemachine.active?
end
def test_complete_project
assert_nil @timemachine.completed_at
@timemachine.complete!
assert_equal :completed, @timemachine.current_state
assert @timemachine.completed?
assert_not_nil @timemachine.completed_at, "completed_at not expected to be nil"
assert_in_delta Time.now, @timemachine.completed_at, 1
end
def test_find_project_by_namepart_with_exact_match
p = Project.find_by_namepart('Build a working time machine')
assert_not_nil p
assert_equal @timemachine.id, p.id
end
def test_find_project_by_namepart_with_starts_with
p = Project.find_by_namepart('Build a')
assert_not_nil p
assert_equal @timemachine.id, p.id
end
def test_delete_project_deletes_todos_within_it
assert_equal 3, @timemachine.todos.count
timemachine_todo_ids = @timemachine.todos.map{ |t| t.id }
@timemachine.destroy
timemachine_todo_ids.each do |t_id|
assert !Todo.exists?(t_id)
end
end
def test_not_done_todos
assert_equal 2, @timemachine.not_done_todos.size
t = @timemachine.not_done_todos[0]
t.complete!
t.save!
assert_equal 1, Project.find(@timemachine.id).not_done_todos.size
end
def test_done_todos
assert_equal 0, @timemachine.done_todos.size
t = @timemachine.not_done_todos[0]
t.complete!
t.save!
assert_equal 1, Project.find(@timemachine.id).done_todos.size
end
def test_deferred_todos
assert_equal 1, @timemachine.deferred_todos.size
t = @timemachine.not_done_todos[0]
t.show_from = 1.days.from_now.utc.to_date
t.save!
assert_equal 2, Project.find(@timemachine.id).deferred_todos.size
end
def test_to_param_returns_id
assert_equal '1', @timemachine.to_param
end
def test_null_object
p = Project.null_object
assert !p.hidden?
assert p.nil?
assert_nil p.id
end
def test_feed_options
opts = Project.feed_options(users(:admin_user))
assert_equal 'Tracks Projects', opts[:title], 'Unexpected value for :title key of feed_options'
assert_equal 'Lists all the projects for Admin Schmadmin', opts[:description], 'Unexpected value for :description key of feed_options'
end
def test_transition_to_another_state
assert_equal :active, @timemachine.current_state
@timemachine.transition_to(:hidden)
assert_equal :hidden, @timemachine.current_state
@timemachine.transition_to(:completed)
assert_equal :completed, @timemachine.current_state
@timemachine.transition_to(:active)
assert_equal :active, @timemachine.current_state
end
def test_transition_to_same_state
assert_equal :active, @timemachine.current_state
@timemachine.transition_to(:active)
assert_equal :active, @timemachine.current_state
end
def test_deferred_todo_count
assert_equal 1, @timemachine.deferred_todo_count
assert_equal 0, @moremoney.deferred_todo_count
@moremoney.todos[0].show_from = next_week
assert_equal 1, @moremoney.deferred_todo_count
end
def test_done_todo_count
assert_equal 0, @timemachine.done_todo_count
assert_equal 0, @moremoney.done_todo_count
@moremoney.todos[0].complete!
assert_equal 1, @moremoney.done_todo_count
end
def test_not_done_todo_count
assert_equal 2, @timemachine.not_done_todo_count
assert_equal 3, @moremoney.not_done_todo_count
@moremoney.todos[0].complete!
assert_equal 2, @moremoney.not_done_todo_count
end
def test_default_context_name
p = Project.new
assert_equal '', p.default_context.name
p.default_context = contexts(:agenda)
assert_equal 'agenda', p.default_context.name
end
end

View file

@ -0,0 +1,72 @@
require File.dirname(__FILE__) + '/../test_helper'
require File.dirname(__FILE__) + '/../../lib/prototype_helper_extensions'
class PrototypeHelperExtensionsTest < Test::Unit::TestCase
include ActionView::Helpers::JavaScriptHelper
include ActionView::Helpers::PrototypeHelper
include ActionView::Helpers::ScriptaculousHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormHelper
include ActionView::Helpers::CaptureHelper
def setup
@template = nil
@controller = Class.new do
def url_for(options, *parameters_for_method_reference)
if options.is_a?(String)
options
elsif options.is_a?(Hash)
url = "http://www.example.com/"
url << options[:action].to_s if options and options[:action]
url << "?a=#{options[:a]}" if options && options[:a]
url << "&b=#{options[:b]}" if options && options[:a] && options[:b]
url
elsif options.is_a?(JavaScriptRef)
url = options.to_json
else
raise 'unhandled type' + options.class.inspect
end
end
end.new
@generator = create_generator
end
def test_confirming
@generator.confirming("Please confirm.") do
@generator.alert 'foo'
end
assert_equal "if (confirm('Please confirm.')) {\nalert(\"foo\");\n}", @generator.to_s
end
def test_confirming_with_javascript
@generator.confirming("'Please confirm ' + this.title + '.'") do
@generator.alert 'foo'
end
assert_equal "if (confirm('Please confirm ' + this.title + '.')) {\nalert(\"foo\");\n}", @generator.to_s
end
def test_remote_to_href
assert_equal "new Ajax.Request(this.href, {asynchronous:true, evalScripts:true})\n", remote_to_href
end
def test_remote_to_href_with_options
assert_equal "new Ajax.Request(this.href, {asynchronous:true, evalScripts:true, method:'put'})\n", remote_to_href(:method => 'put')
end
protected
def create_generator
block = Proc.new { |*args| yield *args if block_given? }
JavaScriptGenerator.new self, &block
end
def protect_against_forgery?
false
end
end

10
test/unit/tag_test.rb Normal file
View file

@ -0,0 +1,10 @@
require File.dirname(__FILE__) + '/../test_helper'
class TagTest < Test::Rails::TestCase
fixtures :tags
# Replace this with your real tests.
def test_truth
assert true
end
end

10
test/unit/tagging_test.rb Normal file
View file

@ -0,0 +1,10 @@
require File.dirname(__FILE__) + '/../test_helper'
class TaggingTest < Test::Rails::TestCase
fixtures :taggings
# Replace this with your real tests.
def test_truth
assert true
end
end

View file

@ -0,0 +1,136 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'todos_controller'
class TodoCreateParamsHelperTest < Test::Rails::TestCase
def test_works_with_request_as_root_hash_entry
params = {'request' => { 'todo' => { 'description' => 'foo'}}}
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal({'description' => 'foo'}, params_helper.attributes)
end
def test_works_with_todo_as_root_hash_entry
params = { 'todo' => { 'description' => 'foo'}}
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal({'description' => 'foo'}, params_helper.attributes)
end
def test_show_from_accessor
expected_date = Time.now.to_date
params = { 'todo' => { 'show_from' => expected_date}}
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal(expected_date, params_helper.show_from)
end
def test_due_accessor
expected_date = Time.now.to_date
params = { 'todo' => { 'due' => expected_date}}
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal(expected_date, params_helper.due)
end
def test_tag_list_accessor
params = { 'todo' => { }, 'tag_list' => 'foo, bar'}
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal('foo, bar', params_helper.tag_list)
end
def test_parse_dates_parses_show_from_date_based_on_prefs
params = { 'todo' => { 'show_from' => '5/20/07', 'due' => '5/23/07'}}
prefs = flexmock()
prefs.should_receive(:parse_date).with('5/20/07').and_return(Date.new(2007, 5, 20))
prefs.should_receive(:parse_date).with('5/23/07').and_return(Date.new(2007, 5, 23))
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
params_helper.parse_dates()
assert_equal Date.new(2007, 5, 20), params_helper.show_from
end
def test_parse_dates_parses_due_date_based_on_prefs
params = { 'todo' => { 'show_from' => '5/20/07', 'due' => '5/23/07'}}
prefs = flexmock()
prefs.should_receive(:parse_date).with('5/20/07').and_return(Date.new(2007, 5, 20))
prefs.should_receive(:parse_date).with('5/23/07').and_return(Date.new(2007, 5, 23))
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
params_helper.parse_dates()
assert_equal Date.new(2007, 5, 23), params_helper.due
end
def test_parse_dates_sets_due_to_empty_string_if_nil
params = { 'todo' => { 'show_from' => '5/20/07', 'due' => nil}}
prefs = flexmock()
prefs.should_receive(:parse_date).with('5/20/07').and_return(Date.new(2007, 5, 20))
prefs.should_receive(:parse_date).with(nil).and_return(nil)
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
params_helper.parse_dates()
assert_equal '', params_helper.due
end
def test_project_name_is_stripped_of_leading_and_trailing_whitespace
params = { 'project_name' => ' Visit New Orleans ' }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal 'Visit New Orleans', params_helper.project_name
end
def test_project_name_is_nil_when_unspecified
params = { }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_nil params_helper.project_name
end
def test_context_name_is_stripped_of_leading_and_trailing_whitespace
params = { 'context_name' => ' mobile phone ' }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal 'mobile phone', params_helper.context_name
end
def test_context_name_is_nil_when_unspecified
params = { }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_nil params_helper.context_name
end
def test_project_specified_by_name_is_false_when_project_id_is_specified
params = { 'todo' => { 'project_id' => 2 } }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal false, params_helper.project_specified_by_name?
end
def test_project_specified_by_name_is_false_when_project_name_is_blank
params = { 'project_name' => nil, 'todo' => {} }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal false, params_helper.project_specified_by_name?
end
def test_project_specified_by_name_is_false_when_project_name_is_none
params = { 'project_name' => 'None', 'todo' => {} }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal false, params_helper.project_specified_by_name?
end
def test_context_specified_by_name_is_false_when_context_id_is_specified
params = { 'todo' => { 'context_id' => 3 } }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal false, params_helper.context_specified_by_name?
end
def test_context_specified_by_name_is_false_when_context_name_is_blank
params = { 'context_name' => nil, 'todo' => {} }
prefs = flexmock()
params_helper = TodosController::TodoCreateParamsHelper.new(params, prefs)
assert_equal false, params_helper.context_specified_by_name?
end
end

182
test/unit/todo_test.rb Normal file
View file

@ -0,0 +1,182 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'date'
class TodoTest < Test::Rails::TestCase
fixtures :todos, :users, :contexts, :preferences, :tags, :taggings
def setup
@not_completed1 = Todo.find(1).reload
@not_completed2 = Todo.find(2).reload
@completed = Todo.find(8).reload
end
# Test loading a todo item
def test_load
assert_kind_of Todo, @not_completed1
assert_equal 1, @not_completed1.id
assert_equal 1, @not_completed1.context_id
assert_equal 2, @not_completed1.project_id
assert_equal "Call Bill Gates to find out how much he makes per day", @not_completed1.description
assert_nil @not_completed1.notes
assert @not_completed1.completed? == false
assert_equal 1.week.ago.utc.beginning_of_day.strftime("%Y-%m-%d %H:%M"), @not_completed1.created_at.strftime("%Y-%m-%d %H:%M")
assert_equal 2.week.from_now.utc.beginning_of_day.strftime("%Y-%m-%d"), @not_completed1.due.strftime("%Y-%m-%d")
assert_nil @not_completed1.completed_at
assert_equal 1, @not_completed1.user_id
end
def test_completed
assert_kind_of Todo, @completed
assert @completed.completed?
assert_not_nil @completed.completed_at
end
def test_completed_at_cleared_after_toggle_to_active
assert_kind_of Todo, @completed
assert @completed.completed?
@completed.toggle_completion!
assert @completed.active?
assert_nil @completed.completed_at
end
# Validation tests
#
def test_validate_presence_of_description
assert_equal "Call dinosaur exterminator", @not_completed2.description
@not_completed2.description = ""
assert !@not_completed2.save
assert_equal 1, @not_completed2.errors.count
assert_equal "can't be blank", @not_completed2.errors.on(:description)
end
def test_validate_length_of_description
assert_equal "Call dinosaur exterminator", @not_completed2.description
@not_completed2.description = generate_random_string(101)
assert !@not_completed2.save
assert_equal 1, @not_completed2.errors.count
assert_equal "is too long (maximum is 100 characters)", @not_completed2.errors.on(:description)
end
def test_validate_length_of_notes
assert_equal "Ask him if I need to hire a skip for the corpses.", @not_completed2.notes
@not_completed2.notes = generate_random_string(60001)
assert !@not_completed2.save
assert_equal 1, @not_completed2.errors.count
assert_equal "is too long (maximum is 60000 characters)", @not_completed2.errors.on(:notes)
end
def test_validate_show_from_must_be_a_date_in_the_future
t = @not_completed2
t[:show_from] = 1.week.ago.to_date # we have to set this via the indexer because show_from=() updates the state
# and actual show_from value appropriately based on the date
assert_equal 1.week.ago.to_date, t.show_from
assert !t.save
assert_equal 1, t.errors.count
assert_equal "must be a date in the future", t.errors.on(:show_from)
end
def test_defer_an_existing_todo
@not_completed2
assert_equal :active, @not_completed2.current_state
@not_completed2.show_from = next_week
assert @not_completed2.save, "should have saved successfully" + @not_completed2.errors.to_xml
assert_equal :deferred, @not_completed2.current_state
end
def test_create_a_new_deferred_todo
user = users(:other_user)
todo = user.todos.build
todo.show_from = next_week
todo.context_id = 1
todo.description = 'foo'
assert todo.save, "should have saved successfully" + todo.errors.to_xml
assert_equal :deferred, todo.current_state
end
def test_create_a_new_deferred_todo_by_passing_attributes
user = users(:other_user)
todo = user.todos.build(:show_from => next_week, :context_id => 1, :description => 'foo')
assert todo.save, "should have saved successfully" + todo.errors.to_xml
assert_equal :deferred, todo.current_state
end
def test_feed_options
opts = Todo.feed_options(users(:admin_user))
assert_equal 'Tracks Actions', opts[:title], 'Unexpected value for :title key of feed_options'
assert_equal 'Actions for Admin Schmadmin', opts[:description], 'Unexpected value for :description key of feed_options'
end
def test_toggle_completion
t = @not_completed1
assert_equal :active, t.current_state
t.toggle_completion!
assert_equal :completed, t.current_state
t.toggle_completion!
assert_equal :active, t.current_state
end
def test_activate_also_saves
t = @not_completed1
t.show_from = 1.week.from_now.to_date
t.save!
assert t.deferred?
t.reload
t.activate!
assert t.active?
t.reload
assert t.active?
end
def test_project_returns_null_object_when_nil
t = @not_completed1
assert !t.project.is_a?(NullProject)
t.project = nil
assert t.project.is_a?(NullProject)
end
def test_initial_state_defaults_to_active
t = Todo.new
t.description = 'foo'
t.context_id = 1
t.save!
t.reload
assert_equal :active, t.current_state
end
def test_initial_state_is_deferred_when_show_from_in_future
t = Todo.new
t.user = users(:admin_user)
t.description = 'foo'
t.context_id = 1
t.show_from = 1.week.from_now.to_date
t.save!
t.reload
assert_equal :deferred, t.current_state
end
def test_todo_is_not_starred
assert !@not_completed1.starred?
end
def test_todo_2_is_not_starred
assert !Todo.find(2).starred?
end
def test_todo_is_starred_after_starred_tag_is_added
@not_completed1.add_tag('starred')
assert @not_completed1.starred?
end
def test_todo_is_starred_after_toggle_starred
@not_completed1.toggle_star!
assert @not_completed1.starred?
end
def test_todo_is_not_starred_after_toggle_starred_twice
@not_completed1.toggle_star!
@not_completed1.toggle_star!
assert !@not_completed1.starred?
end
end

358
test/unit/user_test.rb Normal file
View file

@ -0,0 +1,358 @@
require File.dirname(__FILE__) + '/../test_helper'
module Tracks
class Config
def self.auth_schemes
['database', 'ldap']
end
end
end
class SimpleLdapAuthenticator
cattr_accessor :fake_success
def self.valid?(login, pass)
fake_success
end
end
class UserTest < Test::Rails::TestCase
fixtures :users, :preferences, :projects, :contexts, :todos
def setup
assert_equal "test", ENV['RAILS_ENV']
assert_equal "change-me", Tracks::Config.salt
assert_equal ['database', 'ldap'], Tracks::Config.auth_schemes
@admin_user = User.find(1)
@other_user = User.find(2)
end
# Test an admin user model
#
def test_admin
assert_kind_of User, @admin_user
assert_equal 1, @admin_user.id
assert_equal "admin", @admin_user.login
assert_equal "#{Digest::SHA1.hexdigest("#{Tracks::Config.salt}--abracadabra--")}", @admin_user.crypted_password
assert_not_nil @admin_user.token
assert @admin_user.is_admin
end
# Test a non-admin user model
def test_non_admin
assert_kind_of User, @other_user
assert_equal 2, @other_user.id
assert_equal "jane", @other_user.login
assert_equal "#{Digest::SHA1.hexdigest("#{Tracks::Config.salt}--sesame--")}", @other_user.crypted_password
assert_not_nil @other_user.token
assert @other_user.is_admin == false || @other_user.is_admin == 0
end
# ============================================
# Validations
# ============================================
# Test a password shorter than 5 characters
#
def test_validate_short_password
assert_no_difference User, :count do
u = create_user :password => generate_random_string(4)
assert_error_on u, :password, "is too short (minimum is 5 characters)"
end
end
def test_validate_long_password
assert_no_difference User, :count do
u = create_user :password => generate_random_string(41)
assert_error_on u, :password, "is too long (maximum is 40 characters)"
end
end
def test_validate_correct_length_password
assert_difference User, :count do
create_user :password => generate_random_string(6)
end
end
def test_validate_missing_password
assert_no_difference User, :count do
u = create_user :password => ''
assert_errors_on u, :password, ["can't be blank", "is too short (minimum is 5 characters)"]
end
end
def test_validate_short_login
assert_no_difference User, :count do
u = create_user :login => 'ba'
assert_error_on u, :login, "is too short (minimum is 3 characters)"
end
end
def test_validate_long_login
assert_no_difference User, :count do
u = create_user :login => generate_random_string(81)
assert_error_on u, :login, "is too long (maximum is 80 characters)"
end
end
def test_validate_correct_length_login
assert_difference User, :count do
create_user :login => generate_random_string(6)
end
end
def test_validate_missing_login
assert_no_difference User, :count do
u = create_user :login => ''
assert_errors_on u, :login, ["can't be blank", "is too short (minimum is 3 characters)"]
end
end
def test_display_name_with_first_and_last_name_set
@other_user.first_name = "Jane"
@other_user.last_name = "Doe"
assert_equal "Jane Doe", @other_user.display_name
end
def test_display_name_with_first_name_set
@other_user.first_name = "Jane"
@other_user.last_name = nil
assert_equal "Jane", @other_user.display_name
end
def test_display_name_with_last_name_set
@other_user.first_name = nil
@other_user.last_name = "Doe"
assert_equal "Doe", @other_user.display_name
end
def test_display_name_with_neither_first_nor_last_name_set
@other_user.first_name = nil
@other_user.last_name = nil
assert_equal @other_user.login, @other_user.display_name
end
def test_prefs_is_short_for_preference
assert_equal @admin_user.preference, @admin_user.prefs
end
def test_to_param_returns_login
assert_equal @admin_user.login, @admin_user.to_param
end
def test_change_password
assert_not_nil User.authenticate(@admin_user.login, "abracadabra")
@admin_user.change_password("foobar", "foobar")
@admin_user.reload
assert_nil User.authenticate(@admin_user.login, "abracadabra")
assert_not_nil User.authenticate(@admin_user.login, "foobar")
end
def test_projects_next_project
moremoney = projects(:moremoney)
next_project = @admin_user.projects.next_from(moremoney)
assert_equal projects(:gardenclean), next_project
end
def test_projects_previous_project
moremoney = projects(:moremoney)
previous_project = @admin_user.projects.previous_from(moremoney)
assert_equal projects(:timemachine), previous_project
end
def test_projects_next_project_nil
gardenclean = projects(:gardenclean)
next_project = @admin_user.projects.next_from(gardenclean)
assert_nil next_project
end
def test_projects_previous_project_nil
timemachine = projects(:timemachine)
previous_project = @admin_user.projects.previous_from(timemachine)
assert_nil previous_project
end
def test_no_users_yet
assert !User.no_users_yet?
User.delete_all
assert User.no_users_yet?
end
def test_generate_token_updates_token
assert_value_changed @admin_user, :token do
@admin_user.send :generate_token
end
end
def test_find_admin
assert_equal @admin_user, User.find_admin
end
def test_validates_auth_type
@other_user.auth_type = 'dnacheck'
assert !@other_user.save
assert_equal 1, @other_user.errors.count
assert_equal "not a valid authentication type (dnacheck)", @other_user.errors.on(: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')
assert_equal contexts(:agenda), c
c = u.contexts.find_by_params('context_id' => '1')
assert_equal contexts(:agenda), c
end
def test_find_project_by_params
u = @admin_user
p = u.projects.find_by_params('id' => '1')
assert_equal projects(:timemachine), p
p = u.projects.find_by_params('project_id' => '1')
assert_equal projects(:timemachine), p
end
def test_update_project_positions
assert_equal 1, Project.find(1).position
assert_equal 2, Project.find(2).position
assert_equal 3, Project.find(3).position
@admin_user.projects.update_positions([2,1,3])
assert_equal 2, Project.find(1).position
assert_equal 1, Project.find(2).position
assert_equal 3, Project.find(3).position
end
def test_find_and_activate_deferred_todos_that_are_ready
assert_equal 1, @admin_user.deferred_todos.count
@admin_user.deferred_todos[0].show_from = @admin_user.time.to_date
@admin_user.deferred_todos[0].save
@admin_user.deferred_todos.reload
@admin_user.deferred_todos.find_and_activate_ready
@admin_user.deferred_todos.reload
assert_equal 0, @admin_user.deferred_todos.count
end
def test_completed_todos_completed_within
todos = @admin_user.completed_todos.completed_within(@admin_user.time - 1.day)
assert_equal 3, todos.length
end
def test_completed_todos_complete_more_than
todos = @admin_user.completed_todos.completed_more_than(@admin_user.time - 1.day)
assert_equal 1, todos.length
end
def test_sort_active_projects_alphabetically
u = users(:admin_user)
u.projects.alphabetize(:state => "active")
assert_equal 1, projects(:timemachine).position
assert_equal 2, projects(:gardenclean).position
assert_equal 3, projects(:moremoney).position
end
def test_sort_active_projects_alphabetically_case_insensitive
u = users(:admin_user)
projects(:timemachine).name = projects(:timemachine).name.downcase
projects(:timemachine).save!
u.projects.alphabetize(:state => "active")
assert_equal 1, projects(:timemachine).position
assert_equal 2, projects(:gardenclean).position
assert_equal 3, projects(:moremoney).position
end
def test_should_create_user
assert_difference User, :count do
user = create_user
assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
end
end
def test_should_require_login
assert_no_difference User, :count do
u = create_user(:login => nil)
assert u.errors.on(:login)
end
end
def test_should_require_password
assert_no_difference User, :count do
u = create_user(:password => nil)
assert u.errors.on(:password)
end
end
def test_should_require_password_confirmation
assert_no_difference User, :count do
u = create_user(:password_confirmation => nil)
assert u.errors.on(:password_confirmation)
end
end
def test_should_reset_password
users(:other_user).update_attributes(:password => 'new password', :password_confirmation => 'new password')
assert_equal users(:other_user), User.authenticate('jane', 'new password')
end
def test_should_not_rehash_password
users(:other_user).update_attributes(:login => 'jane2')
assert_equal users(:other_user), User.authenticate('jane2', 'sesame')
end
def test_should_authenticate_user
assert_equal users(:other_user), User.authenticate('jane', 'sesame')
end
def test_should_set_remember_token
users(:other_user).remember_me
assert_not_nil users(:other_user).remember_token
assert_not_nil users(:other_user).remember_token_expires_at
end
def test_should_unset_remember_token
users(:other_user).remember_me
assert_not_nil users(:other_user).remember_token
users(:other_user).forget_me
assert_nil users(:other_user).remember_token
end
def test_normalizes_open_id_url_on_save
['www.johndoe.com', 'WWW.JOHNDOE.COM', 'http://www.johndoe.com/', 'http://www.johndoe.com'].each do |initial|
assert_open_id_url_normalized_on_save initial, 'http://www.johndoe.com'
end
end
def test_normalizes_open_id_url_on_find
u = users(:other_user)
u.open_id_url = 'http://www.johndoe.com'
u.save
['www.johndoe.com', 'WWW.JOHNDOE.COM', 'http://www.johndoe.com/', 'http://www.johndoe.com'].each do |raw_open_id_url|
assert_equal u.id, User.find_by_open_id_url(raw_open_id_url).id
end
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