diff --git a/test/object_helpers.rb b/test/object_helpers.rb index 2f29793f2..dd68fbc8a 100644 --- a/test/object_helpers.rb +++ b/test/object_helpers.rb @@ -99,6 +99,7 @@ module ObjectHelpers @generated_version_name.succ! version = Version.new(attributes) version.name = @generated_version_name.dup if version.name.blank? + version.project ||= Project.find(1) yield version if block_given? version.save! version diff --git a/test/unit/initializers/patches_test.rb b/test/unit/initializers/patches_test.rb index 1fc234d70..f46c905e1 100644 --- a/test/unit/initializers/patches_test.rb +++ b/test/unit/initializers/patches_test.rb @@ -20,21 +20,19 @@ require File.expand_path('../../../test_helper', __FILE__) class PatchesTest < ActiveSupport::TestCase include Redmine::I18n - context "ActiveRecord::Base.human_attribute_name" do - setup do - Setting.default_language = 'en' - end + def setup + Setting.default_language = 'en' + end - should "transform name to field_name" do - assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on') - end + test "ActiveRecord::Base.human_attribute_name should transform name to field_name" do + assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on') + end - should "cut extra _id suffix for better validation" do - assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on_id') - end + test "ActiveRecord::Base.human_attribute_name should cut extra _id suffix for better validation" do + assert_equal l('field_last_login_on'), ActiveRecord::Base.human_attribute_name('last_login_on_id') + end - should "default to humanized value if no translation has been found (useful for custom fields)" do - assert_equal 'Patch name', ActiveRecord::Base.human_attribute_name('Patch name') - end + test "ActiveRecord::Base.human_attribute_name should default to humanized value if no translation has been found (useful for custom fields)" do + assert_equal 'Patch name', ActiveRecord::Base.human_attribute_name('Patch name') end end diff --git a/test/unit/lib/redmine/menu_manager/mapper_test.rb b/test/unit/lib/redmine/menu_manager/mapper_test.rb index 80d294254..9ff34c839 100644 --- a/test/unit/lib/redmine/menu_manager/mapper_test.rb +++ b/test/unit/lib/redmine/menu_manager/mapper_test.rb @@ -18,19 +18,17 @@ require File.expand_path('../../../../../test_helper', __FILE__) class Redmine::MenuManager::MapperTest < ActiveSupport::TestCase - context "Mapper#initialize" do - should "define a root MenuNode if menu is not present in items" do - menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) - node = menu_mapper.menu_items - assert_not_nil node - assert_equal :root, node.name - end + test "Mapper#initialize should define a root MenuNode if menu is not present in items" do + menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) + node = menu_mapper.menu_items + assert_not_nil node + assert_equal :root, node.name + end - should "use existing MenuNode if present" do - node = "foo" # just an arbitrary reference - menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {:test_menu => node}) - assert_equal node, menu_mapper.menu_items - end + test "Mapper#initialize should use existing MenuNode if present" do + node = "foo" # just an arbitrary reference + menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {:test_menu => node}) + assert_equal node, menu_mapper.menu_items end def test_push_onto_root diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 230e5b140..5c6400298 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -404,25 +404,22 @@ class UserTest < ActiveSupport::TestCase assert_not_nil u.errors[:mail_notification] end - context "User#try_to_login" do - should "fall-back to case-insensitive if user login is not found as-typed." do - user = User.try_to_login("AdMin", "admin") - assert_kind_of User, user - assert_equal "admin", user.login + test "User#try_to_login should fall-back to case-insensitive if user login is not found as-typed" do + user = User.try_to_login("AdMin", "admin") + assert_kind_of User, user + assert_equal "admin", user.login + end + + test "User#try_to_login should select the exact matching user first" do + case_sensitive_user = User.generate! do |user| + user.password = "admin123" end + # bypass validations to make it appear like existing data + case_sensitive_user.update_attribute(:login, 'ADMIN') - should "select the exact matching user first" do - case_sensitive_user = User.generate! do |user| - user.password = "admin123" - end - # bypass validations to make it appear like existing data - case_sensitive_user.update_attribute(:login, 'ADMIN') - - user = User.try_to_login("ADMIN", "admin123") - assert_kind_of User, user - assert_equal "ADMIN", user.login - - end + user = User.try_to_login("ADMIN", "admin123") + assert_kind_of User, user + assert_equal "ADMIN", user.login end def test_password @@ -527,20 +524,14 @@ class UserTest < ActiveSupport::TestCase assert_equal nil, user end - context ".try_to_login" do - context "with good credentials" do - should "return the user" do - user = User.try_to_login("admin", "admin") - assert_kind_of User, user - assert_equal "admin", user.login - end - end + test ".try_to_login with good credentials should return the user" do + user = User.try_to_login("admin", "admin") + assert_kind_of User, user + assert_equal "admin", user.login + end - context "with wrong credentials" do - should "return nil" do - assert_nil User.try_to_login("admin", "foo") - end - end + test ".try_to_login with wrong credentials should return nil" do + assert_nil User.try_to_login("admin", "foo") end if ldap_configured? @@ -684,50 +675,46 @@ class UserTest < ActiveSupport::TestCase end end - context "User#api_key" do - should "generate a new one if the user doesn't have one" do - user = User.generate!(:api_token => nil) - assert_nil user.api_token + test "#api_key should generate a new one if the user doesn't have one" do + user = User.generate!(:api_token => nil) + assert_nil user.api_token - key = user.api_key - assert_equal 40, key.length - user.reload - assert_equal key, user.api_key - end - - should "return the existing api token value" do - user = User.generate! - token = Token.create!(:action => 'api') - user.api_token = token - assert user.save - - assert_equal token.value, user.api_key - end + key = user.api_key + assert_equal 40, key.length + user.reload + assert_equal key, user.api_key end - context "User#find_by_api_key" do - should "return nil if no matching key is found" do - assert_nil User.find_by_api_key('zzzzzzzzz') - end + test "#api_key should return the existing api token value" do + user = User.generate! + token = Token.create!(:action => 'api') + user.api_token = token + assert user.save - should "return nil if the key is found for an inactive user" do - user = User.generate! - user.status = User::STATUS_LOCKED - token = Token.create!(:action => 'api') - user.api_token = token - user.save + assert_equal token.value, user.api_key + end - assert_nil User.find_by_api_key(token.value) - end + test "#find_by_api_key should return nil if no matching key is found" do + assert_nil User.find_by_api_key('zzzzzzzzz') + end - should "return the user if the key is found for an active user" do - user = User.generate! - token = Token.create!(:action => 'api') - user.api_token = token - user.save + test "#find_by_api_key should return nil if the key is found for an inactive user" do + user = User.generate! + user.status = User::STATUS_LOCKED + token = Token.create!(:action => 'api') + user.api_token = token + user.save - assert_equal user, User.find_by_api_key(token.value) - end + assert_nil User.find_by_api_key(token.value) + end + + test "#find_by_api_key should return the user if the key is found for an active user" do + user = User.generate! + token = Token.create!(:action => 'api') + user.api_token = token + user.save + + assert_equal user, User.find_by_api_key(token.value) end def test_default_admin_account_changed_should_return_false_if_account_was_not_changed @@ -880,29 +867,27 @@ class UserTest < ActiveSupport::TestCase assert !u.password_confirmation.blank? end - context "#change_password_allowed?" do - should "be allowed if no auth source is set" do - user = User.generate! - assert user.change_password_allowed? - end + test "#change_password_allowed? should be allowed if no auth source is set" do + user = User.generate! + assert user.change_password_allowed? + end - should "delegate to the auth source" do - user = User.generate! + test "#change_password_allowed? should delegate to the auth source" do + user = User.generate! - allowed_auth_source = AuthSource.generate! - def allowed_auth_source.allow_password_changes?; true; end + allowed_auth_source = AuthSource.generate! + def allowed_auth_source.allow_password_changes?; true; end - denied_auth_source = AuthSource.generate! - def denied_auth_source.allow_password_changes?; false; end + denied_auth_source = AuthSource.generate! + def denied_auth_source.allow_password_changes?; false; end - assert user.change_password_allowed? + assert user.change_password_allowed? - user.auth_source = allowed_auth_source - assert user.change_password_allowed?, "User not allowed to change password, though auth source does" + user.auth_source = allowed_auth_source + assert user.change_password_allowed?, "User not allowed to change password, though auth source does" - user.auth_source = denied_auth_source - assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" - end + user.auth_source = denied_auth_source + assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" end def test_own_account_deletable_should_be_true_with_unsubscrive_enabled diff --git a/test/unit/version_test.rb b/test/unit/version_test.rb index 2e0686b27..6564dcfd7 100644 --- a/test/unit/version_test.rb +++ b/test/unit/version_test.rb @@ -132,75 +132,64 @@ class VersionTest < ActiveSupport::TestCase assert_equal false, version.completed? end - context "#behind_schedule?" do - setup do - ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests - @project = Project.create!(:name => 'test0', :identifier => 'test0') - @project.trackers << Tracker.create!(:name => 'track') - - @version = Version.create!(:project => @project, :effective_date => nil, :name => 'version') - end - - should "be false if there are no issues assigned" do - @version.update_attribute(:effective_date, Date.yesterday) - assert_equal false, @version.behind_schedule? - end - - should "be false if there is no effective_date" do - assert_equal false, @version.behind_schedule? - end - - should "be false if all of the issues are ahead of schedule" do - @version.update_attribute(:effective_date, 7.days.from_now.to_date) - add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left - add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left - assert_equal 60, @version.completed_percent - assert_equal false, @version.behind_schedule? - end - - should "be true if any of the issues are behind schedule" do - @version.update_attribute(:effective_date, 7.days.from_now.to_date) - add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left - add_issue(@version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left - assert_equal 40, @version.completed_percent - assert_equal true, @version.behind_schedule? - end - - should "be false if all of the issues are complete" do - @version.update_attribute(:effective_date, 7.days.from_now.to_date) - add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span - add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span - assert_equal 100, @version.completed_percent - assert_equal false, @version.behind_schedule? - end + test "#behind_schedule? should be false if there are no issues assigned" do + version = Version.generate!(:effective_date => Date.yesterday) + assert_equal false, version.behind_schedule? end - context "#estimated_hours" do - setup do - @version = Version.create!(:project_id => 1, :name => '#estimated_hours') - end + test "#behind_schedule? should be false if there is no effective_date" do + version = Version.generate!(:effective_date => nil) + assert_equal false, version.behind_schedule? + end - should "return 0 with no assigned issues" do - assert_equal 0, @version.estimated_hours - end + test "#behind_schedule? should be false if all of the issues are ahead of schedule" do + version = Version.generate!(:effective_date => 7.days.from_now.to_date) + add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left + add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left + assert_equal 60, version.completed_percent + assert_equal false, version.behind_schedule? + end - should "return 0 with no estimated hours" do - add_issue(@version) - assert_equal 0, @version.estimated_hours - end + test "#behind_schedule? should be true if any of the issues are behind schedule" do + version = Version.generate!(:effective_date => 7.days.from_now.to_date) + add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left + add_issue(version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left + assert_equal 40, version.completed_percent + assert_equal true, version.behind_schedule? + end - should "return the sum of estimated hours" do - add_issue(@version, :estimated_hours => 2.5) - add_issue(@version, :estimated_hours => 5) - assert_equal 7.5, @version.estimated_hours - end + test "#behind_schedule? should be false if all of the issues are complete" do + version = Version.generate!(:effective_date => 7.days.from_now.to_date) + add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span + add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span + assert_equal 100, version.completed_percent + assert_equal false, version.behind_schedule? + end - should "return the sum of leaves estimated hours" do - parent = add_issue(@version) - add_issue(@version, :estimated_hours => 2.5, :parent_issue_id => parent.id) - add_issue(@version, :estimated_hours => 5, :parent_issue_id => parent.id) - assert_equal 7.5, @version.estimated_hours - end + test "#estimated_hours should return 0 with no assigned issues" do + version = Version.generate! + assert_equal 0, version.estimated_hours + end + + test "#estimated_hours should return 0 with no estimated hours" do + version = Version.generate! + add_issue(version) + assert_equal 0, version.estimated_hours + end + + test "#estimated_hours should return return the sum of estimated hours" do + version = Version.generate! + add_issue(version, :estimated_hours => 2.5) + add_issue(version, :estimated_hours => 5) + assert_equal 7.5, version.estimated_hours + end + + test "#estimated_hours should return the sum of leaves estimated hours" do + version = Version.generate! + parent = add_issue(version) + add_issue(version, :estimated_hours => 2.5, :parent_issue_id => parent.id) + add_issue(version, :estimated_hours => 5, :parent_issue_id => parent.id) + assert_equal 7.5, version.estimated_hours end test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do