##// END OF EJS Templates
Removed unused fixtures....
Jean-Philippe Lang -
r11091:4fa7f621823e
parent child
Show More
@@ -1,130 +1,128
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class GroupTest < ActiveSupport::TestCase
20 class GroupTest < ActiveSupport::TestCase
21 fixtures :projects, :trackers, :issue_statuses, :issues,
21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 :enumerations, :users, :issue_categories,
22 :enumerations, :users,
23 :projects_trackers,
23 :projects_trackers,
24 :roles,
24 :roles,
25 :member_roles,
25 :member_roles,
26 :members,
26 :members,
27 :enabled_modules,
28 :workflows,
29 :groups_users
27 :groups_users
30
28
31 include Redmine::I18n
29 include Redmine::I18n
32
30
33 def test_create
31 def test_create
34 g = Group.new(:name => 'New group')
32 g = Group.new(:name => 'New group')
35 assert g.save
33 assert g.save
36 g.reload
34 g.reload
37 assert_equal 'New group', g.name
35 assert_equal 'New group', g.name
38 end
36 end
39
37
40 def test_blank_name_error_message
38 def test_blank_name_error_message
41 set_language_if_valid 'en'
39 set_language_if_valid 'en'
42 g = Group.new
40 g = Group.new
43 assert !g.save
41 assert !g.save
44 assert_include "Name can't be blank", g.errors.full_messages
42 assert_include "Name can't be blank", g.errors.full_messages
45 end
43 end
46
44
47 def test_blank_name_error_message_fr
45 def test_blank_name_error_message_fr
48 set_language_if_valid 'fr'
46 set_language_if_valid 'fr'
49 str = "Nom doit \xc3\xaatre renseign\xc3\xa9(e)"
47 str = "Nom doit \xc3\xaatre renseign\xc3\xa9(e)"
50 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
48 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
51 g = Group.new
49 g = Group.new
52 assert !g.save
50 assert !g.save
53 assert_include str, g.errors.full_messages
51 assert_include str, g.errors.full_messages
54 end
52 end
55
53
56 def test_group_roles_should_be_given_to_added_user
54 def test_group_roles_should_be_given_to_added_user
57 group = Group.find(11)
55 group = Group.find(11)
58 user = User.find(9)
56 user = User.find(9)
59 project = Project.first
57 project = Project.first
60
58
61 Member.create!(:principal => group, :project => project, :role_ids => [1, 2])
59 Member.create!(:principal => group, :project => project, :role_ids => [1, 2])
62 group.users << user
60 group.users << user
63 assert user.member_of?(project)
61 assert user.member_of?(project)
64 end
62 end
65
63
66 def test_new_roles_should_be_given_to_existing_user
64 def test_new_roles_should_be_given_to_existing_user
67 group = Group.find(11)
65 group = Group.find(11)
68 user = User.find(9)
66 user = User.find(9)
69 project = Project.first
67 project = Project.first
70
68
71 group.users << user
69 group.users << user
72 m = Member.create!(:principal => group, :project => project, :role_ids => [1, 2])
70 m = Member.create!(:principal => group, :project => project, :role_ids => [1, 2])
73 assert user.member_of?(project)
71 assert user.member_of?(project)
74 end
72 end
75
73
76 def test_user_roles_should_updated_when_updating_user_ids
74 def test_user_roles_should_updated_when_updating_user_ids
77 group = Group.find(11)
75 group = Group.find(11)
78 user = User.find(9)
76 user = User.find(9)
79 project = Project.first
77 project = Project.first
80
78
81 Member.create!(:principal => group, :project => project, :role_ids => [1, 2])
79 Member.create!(:principal => group, :project => project, :role_ids => [1, 2])
82 group.user_ids = [user.id]
80 group.user_ids = [user.id]
83 group.save!
81 group.save!
84 assert User.find(9).member_of?(project)
82 assert User.find(9).member_of?(project)
85
83
86 group.user_ids = [1]
84 group.user_ids = [1]
87 group.save!
85 group.save!
88 assert !User.find(9).member_of?(project)
86 assert !User.find(9).member_of?(project)
89 end
87 end
90
88
91 def test_user_roles_should_updated_when_updating_group_roles
89 def test_user_roles_should_updated_when_updating_group_roles
92 group = Group.find(11)
90 group = Group.find(11)
93 user = User.find(9)
91 user = User.find(9)
94 project = Project.first
92 project = Project.first
95 group.users << user
93 group.users << user
96 m = Member.create!(:principal => group, :project => project, :role_ids => [1])
94 m = Member.create!(:principal => group, :project => project, :role_ids => [1])
97 assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort
95 assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort
98
96
99 m.role_ids = [1, 2]
97 m.role_ids = [1, 2]
100 assert_equal [1, 2], user.reload.roles_for_project(project).collect(&:id).sort
98 assert_equal [1, 2], user.reload.roles_for_project(project).collect(&:id).sort
101
99
102 m.role_ids = [2]
100 m.role_ids = [2]
103 assert_equal [2], user.reload.roles_for_project(project).collect(&:id).sort
101 assert_equal [2], user.reload.roles_for_project(project).collect(&:id).sort
104
102
105 m.role_ids = [1]
103 m.role_ids = [1]
106 assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort
104 assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort
107 end
105 end
108
106
109 def test_user_memberships_should_be_removed_when_removing_group_membership
107 def test_user_memberships_should_be_removed_when_removing_group_membership
110 assert User.find(8).member_of?(Project.find(5))
108 assert User.find(8).member_of?(Project.find(5))
111 Member.find_by_project_id_and_user_id(5, 10).destroy
109 Member.find_by_project_id_and_user_id(5, 10).destroy
112 assert !User.find(8).member_of?(Project.find(5))
110 assert !User.find(8).member_of?(Project.find(5))
113 end
111 end
114
112
115 def test_user_roles_should_be_removed_when_removing_user_from_group
113 def test_user_roles_should_be_removed_when_removing_user_from_group
116 assert User.find(8).member_of?(Project.find(5))
114 assert User.find(8).member_of?(Project.find(5))
117 User.find(8).groups = []
115 User.find(8).groups = []
118 assert !User.find(8).member_of?(Project.find(5))
116 assert !User.find(8).member_of?(Project.find(5))
119 end
117 end
120
118
121 def test_destroy_should_unassign_issues
119 def test_destroy_should_unassign_issues
122 group = Group.first
120 group = Group.first
123 Issue.update_all(["assigned_to_id = ?", group.id], 'id = 1')
121 Issue.update_all(["assigned_to_id = ?", group.id], 'id = 1')
124
122
125 assert group.destroy
123 assert group.destroy
126 assert group.destroyed?
124 assert group.destroyed?
127
125
128 assert_equal nil, Issue.find(1).assigned_to_id
126 assert_equal nil, Issue.find(1).assigned_to_id
129 end
127 end
130 end
128 end
@@ -1,77 +1,76
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../test_helper', __FILE__)
18 require File.expand_path('../../../test_helper', __FILE__)
19
19
20 class ProjectsHelperTest < ActionView::TestCase
20 class ProjectsHelperTest < ActionView::TestCase
21 include ApplicationHelper
21 include ApplicationHelper
22 include ProjectsHelper
22 include ProjectsHelper
23 include ERB::Util
23 include ERB::Util
24
24
25 fixtures :projects, :trackers, :issue_statuses, :issues,
25 fixtures :projects, :trackers, :issue_statuses, :issues,
26 :enumerations, :users, :issue_categories,
26 :enumerations, :users, :issue_categories,
27 :versions,
27 :versions,
28 :projects_trackers,
28 :projects_trackers,
29 :member_roles,
29 :member_roles,
30 :members,
30 :members,
31 :groups_users,
31 :groups_users,
32 :enabled_modules,
32 :enabled_modules
33 :workflows
34
33
35 def setup
34 def setup
36 super
35 super
37 set_language_if_valid('en')
36 set_language_if_valid('en')
38 User.current = nil
37 User.current = nil
39 end
38 end
40
39
41 def test_link_to_version_within_project
40 def test_link_to_version_within_project
42 @project = Project.find(2)
41 @project = Project.find(2)
43 User.current = User.find(1)
42 User.current = User.find(1)
44 assert_equal '<a href="/versions/5">Alpha</a>', link_to_version(Version.find(5))
43 assert_equal '<a href="/versions/5">Alpha</a>', link_to_version(Version.find(5))
45 end
44 end
46
45
47 def test_link_to_version
46 def test_link_to_version
48 User.current = User.find(1)
47 User.current = User.find(1)
49 assert_equal '<a href="/versions/5">OnlineStore - Alpha</a>', link_to_version(Version.find(5))
48 assert_equal '<a href="/versions/5">OnlineStore - Alpha</a>', link_to_version(Version.find(5))
50 end
49 end
51
50
52 def test_link_to_private_version
51 def test_link_to_private_version
53 assert_equal 'OnlineStore - Alpha', link_to_version(Version.find(5))
52 assert_equal 'OnlineStore - Alpha', link_to_version(Version.find(5))
54 end
53 end
55
54
56 def test_link_to_version_invalid_version
55 def test_link_to_version_invalid_version
57 assert_equal '', link_to_version(Object)
56 assert_equal '', link_to_version(Object)
58 end
57 end
59
58
60 def test_format_version_name_within_project
59 def test_format_version_name_within_project
61 @project = Project.find(1)
60 @project = Project.find(1)
62 assert_equal "0.1", format_version_name(Version.find(1))
61 assert_equal "0.1", format_version_name(Version.find(1))
63 end
62 end
64
63
65 def test_format_version_name
64 def test_format_version_name
66 assert_equal "eCookbook - 0.1", format_version_name(Version.find(1))
65 assert_equal "eCookbook - 0.1", format_version_name(Version.find(1))
67 end
66 end
68
67
69 def test_format_version_name_for_system_version
68 def test_format_version_name_for_system_version
70 assert_equal "OnlineStore - Systemwide visible version", format_version_name(Version.find(7))
69 assert_equal "OnlineStore - Systemwide visible version", format_version_name(Version.find(7))
71 end
70 end
72
71
73 def test_version_options_for_select_with_no_versions
72 def test_version_options_for_select_with_no_versions
74 assert_equal '', version_options_for_select([])
73 assert_equal '', version_options_for_select([])
75 assert_equal '', version_options_for_select([], Version.find(1))
74 assert_equal '', version_options_for_select([], Version.find(1))
76 end
75 end
77 end
76 end
@@ -1,377 +1,374
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class IssueNestedSetTest < ActiveSupport::TestCase
20 class IssueNestedSetTest < ActiveSupport::TestCase
21 fixtures :projects, :users, :members, :member_roles, :roles,
21 fixtures :projects, :users, :roles,
22 :trackers, :projects_trackers,
22 :trackers, :projects_trackers,
23 :versions,
23 :issue_statuses, :issue_categories, :issue_relations,
24 :issue_statuses, :issue_categories, :issue_relations, :workflows,
25 :enumerations,
24 :enumerations,
26 :issues,
25 :issues
27 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
28 :time_entries
29
26
30 def test_create_root_issue
27 def test_create_root_issue
31 issue1 = Issue.generate!
28 issue1 = Issue.generate!
32 issue2 = Issue.generate!
29 issue2 = Issue.generate!
33 issue1.reload
30 issue1.reload
34 issue2.reload
31 issue2.reload
35
32
36 assert_equal [issue1.id, nil, 1, 2], [issue1.root_id, issue1.parent_id, issue1.lft, issue1.rgt]
33 assert_equal [issue1.id, nil, 1, 2], [issue1.root_id, issue1.parent_id, issue1.lft, issue1.rgt]
37 assert_equal [issue2.id, nil, 1, 2], [issue2.root_id, issue2.parent_id, issue2.lft, issue2.rgt]
34 assert_equal [issue2.id, nil, 1, 2], [issue2.root_id, issue2.parent_id, issue2.lft, issue2.rgt]
38 end
35 end
39
36
40 def test_create_child_issue
37 def test_create_child_issue
41 parent = Issue.generate!
38 parent = Issue.generate!
42 child = Issue.generate!(:parent_issue_id => parent.id)
39 child = Issue.generate!(:parent_issue_id => parent.id)
43 parent.reload
40 parent.reload
44 child.reload
41 child.reload
45
42
46 assert_equal [parent.id, nil, 1, 4], [parent.root_id, parent.parent_id, parent.lft, parent.rgt]
43 assert_equal [parent.id, nil, 1, 4], [parent.root_id, parent.parent_id, parent.lft, parent.rgt]
47 assert_equal [parent.id, parent.id, 2, 3], [child.root_id, child.parent_id, child.lft, child.rgt]
44 assert_equal [parent.id, parent.id, 2, 3], [child.root_id, child.parent_id, child.lft, child.rgt]
48 end
45 end
49
46
50 def test_creating_a_child_in_a_subproject_should_validate
47 def test_creating_a_child_in_a_subproject_should_validate
51 issue = Issue.generate!
48 issue = Issue.generate!
52 child = Issue.new(:project_id => 3, :tracker_id => 2, :author_id => 1,
49 child = Issue.new(:project_id => 3, :tracker_id => 2, :author_id => 1,
53 :subject => 'child', :parent_issue_id => issue.id)
50 :subject => 'child', :parent_issue_id => issue.id)
54 assert_save child
51 assert_save child
55 assert_equal issue, child.reload.parent
52 assert_equal issue, child.reload.parent
56 end
53 end
57
54
58 def test_creating_a_child_in_an_invalid_project_should_not_validate
55 def test_creating_a_child_in_an_invalid_project_should_not_validate
59 issue = Issue.generate!
56 issue = Issue.generate!
60 child = Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
57 child = Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
61 :subject => 'child', :parent_issue_id => issue.id)
58 :subject => 'child', :parent_issue_id => issue.id)
62 assert !child.save
59 assert !child.save
63 assert_not_nil child.errors[:parent_issue_id]
60 assert_not_nil child.errors[:parent_issue_id]
64 end
61 end
65
62
66 def test_move_a_root_to_child
63 def test_move_a_root_to_child
67 parent1 = Issue.generate!
64 parent1 = Issue.generate!
68 parent2 = Issue.generate!
65 parent2 = Issue.generate!
69 child = Issue.generate!(:parent_issue_id => parent1.id)
66 child = Issue.generate!(:parent_issue_id => parent1.id)
70
67
71 parent2.parent_issue_id = parent1.id
68 parent2.parent_issue_id = parent1.id
72 parent2.save!
69 parent2.save!
73 child.reload
70 child.reload
74 parent1.reload
71 parent1.reload
75 parent2.reload
72 parent2.reload
76
73
77 assert_equal [parent1.id, 1, 6], [parent1.root_id, parent1.lft, parent1.rgt]
74 assert_equal [parent1.id, 1, 6], [parent1.root_id, parent1.lft, parent1.rgt]
78 assert_equal [parent1.id, 4, 5], [parent2.root_id, parent2.lft, parent2.rgt]
75 assert_equal [parent1.id, 4, 5], [parent2.root_id, parent2.lft, parent2.rgt]
79 assert_equal [parent1.id, 2, 3], [child.root_id, child.lft, child.rgt]
76 assert_equal [parent1.id, 2, 3], [child.root_id, child.lft, child.rgt]
80 end
77 end
81
78
82 def test_move_a_child_to_root
79 def test_move_a_child_to_root
83 parent1 = Issue.generate!
80 parent1 = Issue.generate!
84 parent2 = Issue.generate!
81 parent2 = Issue.generate!
85 child = Issue.generate!(:parent_issue_id => parent1.id)
82 child = Issue.generate!(:parent_issue_id => parent1.id)
86
83
87 child.parent_issue_id = nil
84 child.parent_issue_id = nil
88 child.save!
85 child.save!
89 child.reload
86 child.reload
90 parent1.reload
87 parent1.reload
91 parent2.reload
88 parent2.reload
92
89
93 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
90 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
94 assert_equal [parent2.id, 1, 2], [parent2.root_id, parent2.lft, parent2.rgt]
91 assert_equal [parent2.id, 1, 2], [parent2.root_id, parent2.lft, parent2.rgt]
95 assert_equal [child.id, 1, 2], [child.root_id, child.lft, child.rgt]
92 assert_equal [child.id, 1, 2], [child.root_id, child.lft, child.rgt]
96 end
93 end
97
94
98 def test_move_a_child_to_another_issue
95 def test_move_a_child_to_another_issue
99 parent1 = Issue.generate!
96 parent1 = Issue.generate!
100 parent2 = Issue.generate!
97 parent2 = Issue.generate!
101 child = Issue.generate!(:parent_issue_id => parent1.id)
98 child = Issue.generate!(:parent_issue_id => parent1.id)
102
99
103 child.parent_issue_id = parent2.id
100 child.parent_issue_id = parent2.id
104 child.save!
101 child.save!
105 child.reload
102 child.reload
106 parent1.reload
103 parent1.reload
107 parent2.reload
104 parent2.reload
108
105
109 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
106 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
110 assert_equal [parent2.id, 1, 4], [parent2.root_id, parent2.lft, parent2.rgt]
107 assert_equal [parent2.id, 1, 4], [parent2.root_id, parent2.lft, parent2.rgt]
111 assert_equal [parent2.id, 2, 3], [child.root_id, child.lft, child.rgt]
108 assert_equal [parent2.id, 2, 3], [child.root_id, child.lft, child.rgt]
112 end
109 end
113
110
114 def test_move_a_child_with_descendants_to_another_issue
111 def test_move_a_child_with_descendants_to_another_issue
115 parent1 = Issue.generate!
112 parent1 = Issue.generate!
116 parent2 = Issue.generate!
113 parent2 = Issue.generate!
117 child = Issue.generate!(:parent_issue_id => parent1.id)
114 child = Issue.generate!(:parent_issue_id => parent1.id)
118 grandchild = Issue.generate!(:parent_issue_id => child.id)
115 grandchild = Issue.generate!(:parent_issue_id => child.id)
119
116
120 parent1.reload
117 parent1.reload
121 parent2.reload
118 parent2.reload
122 child.reload
119 child.reload
123 grandchild.reload
120 grandchild.reload
124
121
125 assert_equal [parent1.id, 1, 6], [parent1.root_id, parent1.lft, parent1.rgt]
122 assert_equal [parent1.id, 1, 6], [parent1.root_id, parent1.lft, parent1.rgt]
126 assert_equal [parent2.id, 1, 2], [parent2.root_id, parent2.lft, parent2.rgt]
123 assert_equal [parent2.id, 1, 2], [parent2.root_id, parent2.lft, parent2.rgt]
127 assert_equal [parent1.id, 2, 5], [child.root_id, child.lft, child.rgt]
124 assert_equal [parent1.id, 2, 5], [child.root_id, child.lft, child.rgt]
128 assert_equal [parent1.id, 3, 4], [grandchild.root_id, grandchild.lft, grandchild.rgt]
125 assert_equal [parent1.id, 3, 4], [grandchild.root_id, grandchild.lft, grandchild.rgt]
129
126
130 child.reload.parent_issue_id = parent2.id
127 child.reload.parent_issue_id = parent2.id
131 child.save!
128 child.save!
132 child.reload
129 child.reload
133 grandchild.reload
130 grandchild.reload
134 parent1.reload
131 parent1.reload
135 parent2.reload
132 parent2.reload
136
133
137 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
134 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
138 assert_equal [parent2.id, 1, 6], [parent2.root_id, parent2.lft, parent2.rgt]
135 assert_equal [parent2.id, 1, 6], [parent2.root_id, parent2.lft, parent2.rgt]
139 assert_equal [parent2.id, 2, 5], [child.root_id, child.lft, child.rgt]
136 assert_equal [parent2.id, 2, 5], [child.root_id, child.lft, child.rgt]
140 assert_equal [parent2.id, 3, 4], [grandchild.root_id, grandchild.lft, grandchild.rgt]
137 assert_equal [parent2.id, 3, 4], [grandchild.root_id, grandchild.lft, grandchild.rgt]
141 end
138 end
142
139
143 def test_move_a_child_with_descendants_to_another_project
140 def test_move_a_child_with_descendants_to_another_project
144 parent1 = Issue.generate!
141 parent1 = Issue.generate!
145 child = Issue.generate!(:parent_issue_id => parent1.id)
142 child = Issue.generate!(:parent_issue_id => parent1.id)
146 grandchild = Issue.generate!(:parent_issue_id => child.id)
143 grandchild = Issue.generate!(:parent_issue_id => child.id)
147
144
148 child.reload
145 child.reload
149 child.project = Project.find(2)
146 child.project = Project.find(2)
150 assert child.save
147 assert child.save
151 child.reload
148 child.reload
152 grandchild.reload
149 grandchild.reload
153 parent1.reload
150 parent1.reload
154
151
155 assert_equal [1, parent1.id, 1, 2], [parent1.project_id, parent1.root_id, parent1.lft, parent1.rgt]
152 assert_equal [1, parent1.id, 1, 2], [parent1.project_id, parent1.root_id, parent1.lft, parent1.rgt]
156 assert_equal [2, child.id, 1, 4], [child.project_id, child.root_id, child.lft, child.rgt]
153 assert_equal [2, child.id, 1, 4], [child.project_id, child.root_id, child.lft, child.rgt]
157 assert_equal [2, child.id, 2, 3], [grandchild.project_id, grandchild.root_id, grandchild.lft, grandchild.rgt]
154 assert_equal [2, child.id, 2, 3], [grandchild.project_id, grandchild.root_id, grandchild.lft, grandchild.rgt]
158 end
155 end
159
156
160 def test_moving_an_issue_to_a_descendant_should_not_validate
157 def test_moving_an_issue_to_a_descendant_should_not_validate
161 parent1 = Issue.generate!
158 parent1 = Issue.generate!
162 parent2 = Issue.generate!
159 parent2 = Issue.generate!
163 child = Issue.generate!(:parent_issue_id => parent1.id)
160 child = Issue.generate!(:parent_issue_id => parent1.id)
164 grandchild = Issue.generate!(:parent_issue_id => child.id)
161 grandchild = Issue.generate!(:parent_issue_id => child.id)
165
162
166 child.reload
163 child.reload
167 child.parent_issue_id = grandchild.id
164 child.parent_issue_id = grandchild.id
168 assert !child.save
165 assert !child.save
169 assert_not_nil child.errors[:parent_issue_id]
166 assert_not_nil child.errors[:parent_issue_id]
170 end
167 end
171
168
172 def test_moving_an_issue_should_keep_valid_relations_only
169 def test_moving_an_issue_should_keep_valid_relations_only
173 issue1 = Issue.generate!
170 issue1 = Issue.generate!
174 issue2 = Issue.generate!
171 issue2 = Issue.generate!
175 issue3 = Issue.generate!(:parent_issue_id => issue2.id)
172 issue3 = Issue.generate!(:parent_issue_id => issue2.id)
176 issue4 = Issue.generate!
173 issue4 = Issue.generate!
177 r1 = IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
174 r1 = IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
178 r2 = IssueRelation.create!(:issue_from => issue1, :issue_to => issue3, :relation_type => IssueRelation::TYPE_PRECEDES)
175 r2 = IssueRelation.create!(:issue_from => issue1, :issue_to => issue3, :relation_type => IssueRelation::TYPE_PRECEDES)
179 r3 = IssueRelation.create!(:issue_from => issue2, :issue_to => issue4, :relation_type => IssueRelation::TYPE_PRECEDES)
176 r3 = IssueRelation.create!(:issue_from => issue2, :issue_to => issue4, :relation_type => IssueRelation::TYPE_PRECEDES)
180 issue2.reload
177 issue2.reload
181 issue2.parent_issue_id = issue1.id
178 issue2.parent_issue_id = issue1.id
182 issue2.save!
179 issue2.save!
183 assert !IssueRelation.exists?(r1.id)
180 assert !IssueRelation.exists?(r1.id)
184 assert !IssueRelation.exists?(r2.id)
181 assert !IssueRelation.exists?(r2.id)
185 assert IssueRelation.exists?(r3.id)
182 assert IssueRelation.exists?(r3.id)
186 end
183 end
187
184
188 def test_destroy_should_destroy_children
185 def test_destroy_should_destroy_children
189 issue1 = Issue.generate!
186 issue1 = Issue.generate!
190 issue2 = Issue.generate!
187 issue2 = Issue.generate!
191 issue3 = Issue.generate!(:parent_issue_id => issue2.id)
188 issue3 = Issue.generate!(:parent_issue_id => issue2.id)
192 issue4 = Issue.generate!(:parent_issue_id => issue1.id)
189 issue4 = Issue.generate!(:parent_issue_id => issue1.id)
193
190
194 issue3.init_journal(User.find(2))
191 issue3.init_journal(User.find(2))
195 issue3.subject = 'child with journal'
192 issue3.subject = 'child with journal'
196 issue3.save!
193 issue3.save!
197
194
198 assert_difference 'Issue.count', -2 do
195 assert_difference 'Issue.count', -2 do
199 assert_difference 'Journal.count', -1 do
196 assert_difference 'Journal.count', -1 do
200 assert_difference 'JournalDetail.count', -1 do
197 assert_difference 'JournalDetail.count', -1 do
201 Issue.find(issue2.id).destroy
198 Issue.find(issue2.id).destroy
202 end
199 end
203 end
200 end
204 end
201 end
205
202
206 issue1.reload
203 issue1.reload
207 issue4.reload
204 issue4.reload
208 assert !Issue.exists?(issue2.id)
205 assert !Issue.exists?(issue2.id)
209 assert !Issue.exists?(issue3.id)
206 assert !Issue.exists?(issue3.id)
210 assert_equal [issue1.id, 1, 4], [issue1.root_id, issue1.lft, issue1.rgt]
207 assert_equal [issue1.id, 1, 4], [issue1.root_id, issue1.lft, issue1.rgt]
211 assert_equal [issue1.id, 2, 3], [issue4.root_id, issue4.lft, issue4.rgt]
208 assert_equal [issue1.id, 2, 3], [issue4.root_id, issue4.lft, issue4.rgt]
212 end
209 end
213
210
214 def test_destroy_child_should_update_parent
211 def test_destroy_child_should_update_parent
215 issue = Issue.generate!
212 issue = Issue.generate!
216 child1 = Issue.generate!(:parent_issue_id => issue.id)
213 child1 = Issue.generate!(:parent_issue_id => issue.id)
217 child2 = Issue.generate!(:parent_issue_id => issue.id)
214 child2 = Issue.generate!(:parent_issue_id => issue.id)
218
215
219 issue.reload
216 issue.reload
220 assert_equal [issue.id, 1, 6], [issue.root_id, issue.lft, issue.rgt]
217 assert_equal [issue.id, 1, 6], [issue.root_id, issue.lft, issue.rgt]
221
218
222 child2.reload.destroy
219 child2.reload.destroy
223
220
224 issue.reload
221 issue.reload
225 assert_equal [issue.id, 1, 4], [issue.root_id, issue.lft, issue.rgt]
222 assert_equal [issue.id, 1, 4], [issue.root_id, issue.lft, issue.rgt]
226 end
223 end
227
224
228 def test_destroy_parent_issue_updated_during_children_destroy
225 def test_destroy_parent_issue_updated_during_children_destroy
229 parent = Issue.generate!
226 parent = Issue.generate!
230 Issue.generate!(:start_date => Date.today, :parent_issue_id => parent.id)
227 Issue.generate!(:start_date => Date.today, :parent_issue_id => parent.id)
231 Issue.generate!(:start_date => 2.days.from_now, :parent_issue_id => parent.id)
228 Issue.generate!(:start_date => 2.days.from_now, :parent_issue_id => parent.id)
232
229
233 assert_difference 'Issue.count', -3 do
230 assert_difference 'Issue.count', -3 do
234 Issue.find(parent.id).destroy
231 Issue.find(parent.id).destroy
235 end
232 end
236 end
233 end
237
234
238 def test_destroy_child_issue_with_children
235 def test_destroy_child_issue_with_children
239 root = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'root')
236 root = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'root')
240 child = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => root.id)
237 child = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => root.id)
241 leaf = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'leaf', :parent_issue_id => child.id)
238 leaf = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'leaf', :parent_issue_id => child.id)
242 leaf.init_journal(User.find(2))
239 leaf.init_journal(User.find(2))
243 leaf.subject = 'leaf with journal'
240 leaf.subject = 'leaf with journal'
244 leaf.save!
241 leaf.save!
245
242
246 assert_difference 'Issue.count', -2 do
243 assert_difference 'Issue.count', -2 do
247 assert_difference 'Journal.count', -1 do
244 assert_difference 'Journal.count', -1 do
248 assert_difference 'JournalDetail.count', -1 do
245 assert_difference 'JournalDetail.count', -1 do
249 Issue.find(child.id).destroy
246 Issue.find(child.id).destroy
250 end
247 end
251 end
248 end
252 end
249 end
253
250
254 root = Issue.find(root.id)
251 root = Issue.find(root.id)
255 assert root.leaf?, "Root issue is not a leaf (lft: #{root.lft}, rgt: #{root.rgt})"
252 assert root.leaf?, "Root issue is not a leaf (lft: #{root.lft}, rgt: #{root.rgt})"
256 end
253 end
257
254
258 def test_destroy_issue_with_grand_child
255 def test_destroy_issue_with_grand_child
259 parent = Issue.generate!
256 parent = Issue.generate!
260 issue = Issue.generate!(:parent_issue_id => parent.id)
257 issue = Issue.generate!(:parent_issue_id => parent.id)
261 child = Issue.generate!(:parent_issue_id => issue.id)
258 child = Issue.generate!(:parent_issue_id => issue.id)
262 grandchild1 = Issue.generate!(:parent_issue_id => child.id)
259 grandchild1 = Issue.generate!(:parent_issue_id => child.id)
263 grandchild2 = Issue.generate!(:parent_issue_id => child.id)
260 grandchild2 = Issue.generate!(:parent_issue_id => child.id)
264
261
265 assert_difference 'Issue.count', -4 do
262 assert_difference 'Issue.count', -4 do
266 Issue.find(issue.id).destroy
263 Issue.find(issue.id).destroy
267 parent.reload
264 parent.reload
268 assert_equal [1, 2], [parent.lft, parent.rgt]
265 assert_equal [1, 2], [parent.lft, parent.rgt]
269 end
266 end
270 end
267 end
271
268
272 def test_parent_priority_should_be_the_highest_child_priority
269 def test_parent_priority_should_be_the_highest_child_priority
273 parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
270 parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
274 # Create children
271 # Create children
275 child1 = Issue.generate!(:priority => IssuePriority.find_by_name('High'), :parent_issue_id => parent.id)
272 child1 = Issue.generate!(:priority => IssuePriority.find_by_name('High'), :parent_issue_id => parent.id)
276 assert_equal 'High', parent.reload.priority.name
273 assert_equal 'High', parent.reload.priority.name
277 child2 = Issue.generate!(:priority => IssuePriority.find_by_name('Immediate'), :parent_issue_id => child1.id)
274 child2 = Issue.generate!(:priority => IssuePriority.find_by_name('Immediate'), :parent_issue_id => child1.id)
278 assert_equal 'Immediate', child1.reload.priority.name
275 assert_equal 'Immediate', child1.reload.priority.name
279 assert_equal 'Immediate', parent.reload.priority.name
276 assert_equal 'Immediate', parent.reload.priority.name
280 child3 = Issue.generate!(:priority => IssuePriority.find_by_name('Low'), :parent_issue_id => parent.id)
277 child3 = Issue.generate!(:priority => IssuePriority.find_by_name('Low'), :parent_issue_id => parent.id)
281 assert_equal 'Immediate', parent.reload.priority.name
278 assert_equal 'Immediate', parent.reload.priority.name
282 # Destroy a child
279 # Destroy a child
283 child1.destroy
280 child1.destroy
284 assert_equal 'Low', parent.reload.priority.name
281 assert_equal 'Low', parent.reload.priority.name
285 # Update a child
282 # Update a child
286 child3.reload.priority = IssuePriority.find_by_name('Normal')
283 child3.reload.priority = IssuePriority.find_by_name('Normal')
287 child3.save!
284 child3.save!
288 assert_equal 'Normal', parent.reload.priority.name
285 assert_equal 'Normal', parent.reload.priority.name
289 end
286 end
290
287
291 def test_parent_dates_should_be_lowest_start_and_highest_due_dates
288 def test_parent_dates_should_be_lowest_start_and_highest_due_dates
292 parent = Issue.generate!
289 parent = Issue.generate!
293 Issue.generate!(:start_date => '2010-01-25', :due_date => '2010-02-15', :parent_issue_id => parent.id)
290 Issue.generate!(:start_date => '2010-01-25', :due_date => '2010-02-15', :parent_issue_id => parent.id)
294 Issue.generate!( :due_date => '2010-02-13', :parent_issue_id => parent.id)
291 Issue.generate!( :due_date => '2010-02-13', :parent_issue_id => parent.id)
295 Issue.generate!(:start_date => '2010-02-01', :due_date => '2010-02-22', :parent_issue_id => parent.id)
292 Issue.generate!(:start_date => '2010-02-01', :due_date => '2010-02-22', :parent_issue_id => parent.id)
296 parent.reload
293 parent.reload
297 assert_equal Date.parse('2010-01-25'), parent.start_date
294 assert_equal Date.parse('2010-01-25'), parent.start_date
298 assert_equal Date.parse('2010-02-22'), parent.due_date
295 assert_equal Date.parse('2010-02-22'), parent.due_date
299 end
296 end
300
297
301 def test_parent_done_ratio_should_be_average_done_ratio_of_leaves
298 def test_parent_done_ratio_should_be_average_done_ratio_of_leaves
302 parent = Issue.generate!
299 parent = Issue.generate!
303 Issue.generate!(:done_ratio => 20, :parent_issue_id => parent.id)
300 Issue.generate!(:done_ratio => 20, :parent_issue_id => parent.id)
304 assert_equal 20, parent.reload.done_ratio
301 assert_equal 20, parent.reload.done_ratio
305 Issue.generate!(:done_ratio => 70, :parent_issue_id => parent.id)
302 Issue.generate!(:done_ratio => 70, :parent_issue_id => parent.id)
306 assert_equal 45, parent.reload.done_ratio
303 assert_equal 45, parent.reload.done_ratio
307
304
308 child = Issue.generate!(:done_ratio => 0, :parent_issue_id => parent.id)
305 child = Issue.generate!(:done_ratio => 0, :parent_issue_id => parent.id)
309 assert_equal 30, parent.reload.done_ratio
306 assert_equal 30, parent.reload.done_ratio
310
307
311 Issue.generate!(:done_ratio => 30, :parent_issue_id => child.id)
308 Issue.generate!(:done_ratio => 30, :parent_issue_id => child.id)
312 assert_equal 30, child.reload.done_ratio
309 assert_equal 30, child.reload.done_ratio
313 assert_equal 40, parent.reload.done_ratio
310 assert_equal 40, parent.reload.done_ratio
314 end
311 end
315
312
316 def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any
313 def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any
317 parent = Issue.generate!
314 parent = Issue.generate!
318 Issue.generate!(:estimated_hours => 10, :done_ratio => 20, :parent_issue_id => parent.id)
315 Issue.generate!(:estimated_hours => 10, :done_ratio => 20, :parent_issue_id => parent.id)
319 assert_equal 20, parent.reload.done_ratio
316 assert_equal 20, parent.reload.done_ratio
320 Issue.generate!(:estimated_hours => 20, :done_ratio => 50, :parent_issue_id => parent.id)
317 Issue.generate!(:estimated_hours => 20, :done_ratio => 50, :parent_issue_id => parent.id)
321 assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio
318 assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio
322 end
319 end
323
320
324 def test_parent_estimate_should_be_sum_of_leaves
321 def test_parent_estimate_should_be_sum_of_leaves
325 parent = Issue.generate!
322 parent = Issue.generate!
326 Issue.generate!(:estimated_hours => nil, :parent_issue_id => parent.id)
323 Issue.generate!(:estimated_hours => nil, :parent_issue_id => parent.id)
327 assert_equal nil, parent.reload.estimated_hours
324 assert_equal nil, parent.reload.estimated_hours
328 Issue.generate!(:estimated_hours => 5, :parent_issue_id => parent.id)
325 Issue.generate!(:estimated_hours => 5, :parent_issue_id => parent.id)
329 assert_equal 5, parent.reload.estimated_hours
326 assert_equal 5, parent.reload.estimated_hours
330 Issue.generate!(:estimated_hours => 7, :parent_issue_id => parent.id)
327 Issue.generate!(:estimated_hours => 7, :parent_issue_id => parent.id)
331 assert_equal 12, parent.reload.estimated_hours
328 assert_equal 12, parent.reload.estimated_hours
332 end
329 end
333
330
334 def test_move_parent_updates_old_parent_attributes
331 def test_move_parent_updates_old_parent_attributes
335 first_parent = Issue.generate!
332 first_parent = Issue.generate!
336 second_parent = Issue.generate!
333 second_parent = Issue.generate!
337 child = Issue.generate!(:estimated_hours => 5, :parent_issue_id => first_parent.id)
334 child = Issue.generate!(:estimated_hours => 5, :parent_issue_id => first_parent.id)
338 assert_equal 5, first_parent.reload.estimated_hours
335 assert_equal 5, first_parent.reload.estimated_hours
339 child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id)
336 child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id)
340 assert_equal 7, second_parent.reload.estimated_hours
337 assert_equal 7, second_parent.reload.estimated_hours
341 assert_nil first_parent.reload.estimated_hours
338 assert_nil first_parent.reload.estimated_hours
342 end
339 end
343
340
344 def test_reschuling_a_parent_should_reschedule_subtasks
341 def test_reschuling_a_parent_should_reschedule_subtasks
345 parent = Issue.generate!
342 parent = Issue.generate!
346 c1 = Issue.generate!(:start_date => '2010-05-12', :due_date => '2010-05-18', :parent_issue_id => parent.id)
343 c1 = Issue.generate!(:start_date => '2010-05-12', :due_date => '2010-05-18', :parent_issue_id => parent.id)
347 c2 = Issue.generate!(:start_date => '2010-06-03', :due_date => '2010-06-10', :parent_issue_id => parent.id)
344 c2 = Issue.generate!(:start_date => '2010-06-03', :due_date => '2010-06-10', :parent_issue_id => parent.id)
348 parent.reload
345 parent.reload
349 parent.reschedule_on!(Date.parse('2010-06-02'))
346 parent.reschedule_on!(Date.parse('2010-06-02'))
350 c1.reload
347 c1.reload
351 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-08')], [c1.start_date, c1.due_date]
348 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-08')], [c1.start_date, c1.due_date]
352 c2.reload
349 c2.reload
353 assert_equal [Date.parse('2010-06-03'), Date.parse('2010-06-10')], [c2.start_date, c2.due_date] # no change
350 assert_equal [Date.parse('2010-06-03'), Date.parse('2010-06-10')], [c2.start_date, c2.due_date] # no change
354 parent.reload
351 parent.reload
355 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-10')], [parent.start_date, parent.due_date]
352 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-10')], [parent.start_date, parent.due_date]
356 end
353 end
357
354
358 def test_project_copy_should_copy_issue_tree
355 def test_project_copy_should_copy_issue_tree
359 p = Project.create!(:name => 'Tree copy', :identifier => 'tree-copy', :tracker_ids => [1, 2])
356 p = Project.create!(:name => 'Tree copy', :identifier => 'tree-copy', :tracker_ids => [1, 2])
360 i1 = Issue.generate!(:project => p, :subject => 'i1')
357 i1 = Issue.generate!(:project => p, :subject => 'i1')
361 i2 = Issue.generate!(:project => p, :subject => 'i2', :parent_issue_id => i1.id)
358 i2 = Issue.generate!(:project => p, :subject => 'i2', :parent_issue_id => i1.id)
362 i3 = Issue.generate!(:project => p, :subject => 'i3', :parent_issue_id => i1.id)
359 i3 = Issue.generate!(:project => p, :subject => 'i3', :parent_issue_id => i1.id)
363 i4 = Issue.generate!(:project => p, :subject => 'i4', :parent_issue_id => i2.id)
360 i4 = Issue.generate!(:project => p, :subject => 'i4', :parent_issue_id => i2.id)
364 i5 = Issue.generate!(:project => p, :subject => 'i5')
361 i5 = Issue.generate!(:project => p, :subject => 'i5')
365 c = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1, 2])
362 c = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1, 2])
366 c.copy(p, :only => 'issues')
363 c.copy(p, :only => 'issues')
367 c.reload
364 c.reload
368
365
369 assert_equal 5, c.issues.count
366 assert_equal 5, c.issues.count
370 ic1, ic2, ic3, ic4, ic5 = c.issues.order('subject').all
367 ic1, ic2, ic3, ic4, ic5 = c.issues.order('subject').all
371 assert ic1.root?
368 assert ic1.root?
372 assert_equal ic1, ic2.parent
369 assert_equal ic1, ic2.parent
373 assert_equal ic1, ic3.parent
370 assert_equal ic1, ic3.parent
374 assert_equal ic2, ic4.parent
371 assert_equal ic2, ic4.parent
375 assert ic5.root?
372 assert ic5.root?
376 end
373 end
377 end
374 end
@@ -1,125 +1,124
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class MemberTest < ActiveSupport::TestCase
20 class MemberTest < ActiveSupport::TestCase
21 fixtures :projects, :trackers, :issue_statuses, :issues,
21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 :enumerations, :users, :issue_categories,
22 :enumerations, :users, :issue_categories,
23 :projects_trackers,
23 :projects_trackers,
24 :roles,
24 :roles,
25 :member_roles,
25 :member_roles,
26 :members,
26 :members,
27 :enabled_modules,
27 :enabled_modules,
28 :workflows,
29 :groups_users,
28 :groups_users,
30 :watchers,
29 :watchers,
31 :journals, :journal_details,
30 :journals, :journal_details,
32 :messages,
31 :messages,
33 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
32 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
34 :boards
33 :boards
35
34
36 include Redmine::I18n
35 include Redmine::I18n
37
36
38 def setup
37 def setup
39 @jsmith = Member.find(1)
38 @jsmith = Member.find(1)
40 end
39 end
41
40
42 def test_create
41 def test_create
43 member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2])
42 member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2])
44 assert member.save
43 assert member.save
45 member.reload
44 member.reload
46
45
47 assert_equal 2, member.roles.size
46 assert_equal 2, member.roles.size
48 assert_equal Role.find(1), member.roles.sort.first
47 assert_equal Role.find(1), member.roles.sort.first
49 end
48 end
50
49
51 def test_update
50 def test_update
52 assert_equal "eCookbook", @jsmith.project.name
51 assert_equal "eCookbook", @jsmith.project.name
53 assert_equal "Manager", @jsmith.roles.first.name
52 assert_equal "Manager", @jsmith.roles.first.name
54 assert_equal "jsmith", @jsmith.user.login
53 assert_equal "jsmith", @jsmith.user.login
55
54
56 @jsmith.mail_notification = !@jsmith.mail_notification
55 @jsmith.mail_notification = !@jsmith.mail_notification
57 assert @jsmith.save
56 assert @jsmith.save
58 end
57 end
59
58
60 def test_update_roles
59 def test_update_roles
61 assert_equal 1, @jsmith.roles.size
60 assert_equal 1, @jsmith.roles.size
62 @jsmith.role_ids = [1, 2]
61 @jsmith.role_ids = [1, 2]
63 assert @jsmith.save
62 assert @jsmith.save
64 assert_equal 2, @jsmith.reload.roles.size
63 assert_equal 2, @jsmith.reload.roles.size
65 end
64 end
66
65
67 def test_validate
66 def test_validate
68 member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [2])
67 member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [2])
69 # same use can't have more than one membership for a project
68 # same use can't have more than one membership for a project
70 assert !member.save
69 assert !member.save
71
70
72 # must have one role at least
71 # must have one role at least
73 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
72 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
74 user.login = "test_validate"
73 user.login = "test_validate"
75 user.password, user.password_confirmation = "password", "password"
74 user.password, user.password_confirmation = "password", "password"
76 assert user.save
75 assert user.save
77
76
78 set_language_if_valid 'fr'
77 set_language_if_valid 'fr'
79 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [])
78 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [])
80 assert !member.save
79 assert !member.save
81 assert_include I18n.translate('activerecord.errors.messages.empty'), member.errors[:role]
80 assert_include I18n.translate('activerecord.errors.messages.empty'), member.errors[:role]
82 str = "R\xc3\xb4le doit \xc3\xaatre renseign\xc3\xa9(e)"
81 str = "R\xc3\xb4le doit \xc3\xaatre renseign\xc3\xa9(e)"
83 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
82 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
84 assert_equal str, [member.errors.full_messages].flatten.join
83 assert_equal str, [member.errors.full_messages].flatten.join
85 end
84 end
86
85
87 def test_validate_member_role
86 def test_validate_member_role
88 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
87 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
89 user.login = "test_validate_member_role"
88 user.login = "test_validate_member_role"
90 user.password, user.password_confirmation = "password", "password"
89 user.password, user.password_confirmation = "password", "password"
91 assert user.save
90 assert user.save
92 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [5])
91 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [5])
93 assert !member.save
92 assert !member.save
94 end
93 end
95
94
96 def test_destroy
95 def test_destroy
97 category1 = IssueCategory.find(1)
96 category1 = IssueCategory.find(1)
98 assert_equal @jsmith.user.id, category1.assigned_to_id
97 assert_equal @jsmith.user.id, category1.assigned_to_id
99 assert_difference 'Member.count', -1 do
98 assert_difference 'Member.count', -1 do
100 assert_difference 'MemberRole.count', -1 do
99 assert_difference 'MemberRole.count', -1 do
101 @jsmith.destroy
100 @jsmith.destroy
102 end
101 end
103 end
102 end
104 assert_raise(ActiveRecord::RecordNotFound) { Member.find(@jsmith.id) }
103 assert_raise(ActiveRecord::RecordNotFound) { Member.find(@jsmith.id) }
105 category1.reload
104 category1.reload
106 assert_nil category1.assigned_to_id
105 assert_nil category1.assigned_to_id
107 end
106 end
108
107
109 def test_sort_without_roles
108 def test_sort_without_roles
110 a = Member.new(:roles => [Role.first])
109 a = Member.new(:roles => [Role.first])
111 b = Member.new
110 b = Member.new
112
111
113 assert_equal -1, a <=> b
112 assert_equal -1, a <=> b
114 assert_equal 1, b <=> a
113 assert_equal 1, b <=> a
115 end
114 end
116
115
117 def test_sort_without_principal
116 def test_sort_without_principal
118 role = Role.first
117 role = Role.first
119 a = Member.new(:roles => [role], :principal => User.first)
118 a = Member.new(:roles => [role], :principal => User.first)
120 b = Member.new(:roles => [role])
119 b = Member.new(:roles => [role])
121
120
122 assert_equal -1, a <=> b
121 assert_equal -1, a <=> b
123 assert_equal 1, b <=> a
122 assert_equal 1, b <=> a
124 end
123 end
125 end
124 end
@@ -1,129 +1,128
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class TimeEntryTest < ActiveSupport::TestCase
20 class TimeEntryTest < ActiveSupport::TestCase
21 fixtures :issues, :projects, :users, :time_entries,
21 fixtures :issues, :projects, :users, :time_entries,
22 :members, :roles, :member_roles,
22 :members, :roles, :member_roles,
23 :trackers, :issue_statuses,
23 :trackers, :issue_statuses,
24 :projects_trackers,
24 :projects_trackers,
25 :journals, :journal_details,
25 :journals, :journal_details,
26 :issue_categories, :enumerations,
26 :issue_categories, :enumerations,
27 :groups_users,
27 :groups_users,
28 :enabled_modules,
28 :enabled_modules
29 :workflows
30
29
31 def test_hours_format
30 def test_hours_format
32 assertions = { "2" => 2.0,
31 assertions = { "2" => 2.0,
33 "21.1" => 21.1,
32 "21.1" => 21.1,
34 "2,1" => 2.1,
33 "2,1" => 2.1,
35 "1,5h" => 1.5,
34 "1,5h" => 1.5,
36 "7:12" => 7.2,
35 "7:12" => 7.2,
37 "10h" => 10.0,
36 "10h" => 10.0,
38 "10 h" => 10.0,
37 "10 h" => 10.0,
39 "45m" => 0.75,
38 "45m" => 0.75,
40 "45 m" => 0.75,
39 "45 m" => 0.75,
41 "3h15" => 3.25,
40 "3h15" => 3.25,
42 "3h 15" => 3.25,
41 "3h 15" => 3.25,
43 "3 h 15" => 3.25,
42 "3 h 15" => 3.25,
44 "3 h 15m" => 3.25,
43 "3 h 15m" => 3.25,
45 "3 h 15 m" => 3.25,
44 "3 h 15 m" => 3.25,
46 "3 hours" => 3.0,
45 "3 hours" => 3.0,
47 "12min" => 0.2,
46 "12min" => 0.2,
48 "12 Min" => 0.2,
47 "12 Min" => 0.2,
49 }
48 }
50
49
51 assertions.each do |k, v|
50 assertions.each do |k, v|
52 t = TimeEntry.new(:hours => k)
51 t = TimeEntry.new(:hours => k)
53 assert_equal v, t.hours, "Converting #{k} failed:"
52 assert_equal v, t.hours, "Converting #{k} failed:"
54 end
53 end
55 end
54 end
56
55
57 def test_hours_should_default_to_nil
56 def test_hours_should_default_to_nil
58 assert_nil TimeEntry.new.hours
57 assert_nil TimeEntry.new.hours
59 end
58 end
60
59
61 def test_spent_on_with_blank
60 def test_spent_on_with_blank
62 c = TimeEntry.new
61 c = TimeEntry.new
63 c.spent_on = ''
62 c.spent_on = ''
64 assert_nil c.spent_on
63 assert_nil c.spent_on
65 end
64 end
66
65
67 def test_spent_on_with_nil
66 def test_spent_on_with_nil
68 c = TimeEntry.new
67 c = TimeEntry.new
69 c.spent_on = nil
68 c.spent_on = nil
70 assert_nil c.spent_on
69 assert_nil c.spent_on
71 end
70 end
72
71
73 def test_spent_on_with_string
72 def test_spent_on_with_string
74 c = TimeEntry.new
73 c = TimeEntry.new
75 c.spent_on = "2011-01-14"
74 c.spent_on = "2011-01-14"
76 assert_equal Date.parse("2011-01-14"), c.spent_on
75 assert_equal Date.parse("2011-01-14"), c.spent_on
77 end
76 end
78
77
79 def test_spent_on_with_invalid_string
78 def test_spent_on_with_invalid_string
80 c = TimeEntry.new
79 c = TimeEntry.new
81 c.spent_on = "foo"
80 c.spent_on = "foo"
82 assert_nil c.spent_on
81 assert_nil c.spent_on
83 end
82 end
84
83
85 def test_spent_on_with_date
84 def test_spent_on_with_date
86 c = TimeEntry.new
85 c = TimeEntry.new
87 c.spent_on = Date.today
86 c.spent_on = Date.today
88 assert_equal Date.today, c.spent_on
87 assert_equal Date.today, c.spent_on
89 end
88 end
90
89
91 def test_spent_on_with_time
90 def test_spent_on_with_time
92 c = TimeEntry.new
91 c = TimeEntry.new
93 c.spent_on = Time.now
92 c.spent_on = Time.now
94 assert_equal Date.today, c.spent_on
93 assert_equal Date.today, c.spent_on
95 end
94 end
96
95
97 def test_validate_time_entry
96 def test_validate_time_entry
98 anon = User.anonymous
97 anon = User.anonymous
99 project = Project.find(1)
98 project = Project.find(1)
100 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
99 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
101 :priority => IssuePriority.all.first, :subject => 'test_create',
100 :priority => IssuePriority.all.first, :subject => 'test_create',
102 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
101 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
103 assert issue.save
102 assert issue.save
104 activity = TimeEntryActivity.find_by_name('Design')
103 activity = TimeEntryActivity.find_by_name('Design')
105 te = TimeEntry.create(:spent_on => '2010-01-01',
104 te = TimeEntry.create(:spent_on => '2010-01-01',
106 :hours => 100000,
105 :hours => 100000,
107 :issue => issue,
106 :issue => issue,
108 :project => project,
107 :project => project,
109 :user => anon,
108 :user => anon,
110 :activity => activity)
109 :activity => activity)
111 assert_equal 1, te.errors.count
110 assert_equal 1, te.errors.count
112 end
111 end
113
112
114 def test_set_project_if_nil
113 def test_set_project_if_nil
115 anon = User.anonymous
114 anon = User.anonymous
116 project = Project.find(1)
115 project = Project.find(1)
117 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
116 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
118 :priority => IssuePriority.all.first, :subject => 'test_create',
117 :priority => IssuePriority.all.first, :subject => 'test_create',
119 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
118 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
120 assert issue.save
119 assert issue.save
121 activity = TimeEntryActivity.find_by_name('Design')
120 activity = TimeEntryActivity.find_by_name('Design')
122 te = TimeEntry.create(:spent_on => '2010-01-01',
121 te = TimeEntry.create(:spent_on => '2010-01-01',
123 :hours => 10,
122 :hours => 10,
124 :issue => issue,
123 :issue => issue,
125 :user => anon,
124 :user => anon,
126 :activity => activity)
125 :activity => activity)
127 assert_equal project.id, te.project.id
126 assert_equal project.id, te.project.id
128 end
127 end
129 end
128 end
@@ -1,1085 +1,1084
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class UserTest < ActiveSupport::TestCase
20 class UserTest < ActiveSupport::TestCase
21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
22 :trackers, :issue_statuses,
22 :trackers, :issue_statuses,
23 :projects_trackers,
23 :projects_trackers,
24 :watchers,
24 :watchers,
25 :issue_categories, :enumerations, :issues,
25 :issue_categories, :enumerations, :issues,
26 :journals, :journal_details,
26 :journals, :journal_details,
27 :groups_users,
27 :groups_users,
28 :enabled_modules,
28 :enabled_modules
29 :workflows
30
29
31 def setup
30 def setup
32 @admin = User.find(1)
31 @admin = User.find(1)
33 @jsmith = User.find(2)
32 @jsmith = User.find(2)
34 @dlopper = User.find(3)
33 @dlopper = User.find(3)
35 end
34 end
36
35
37 def test_sorted_scope_should_sort_user_by_display_name
36 def test_sorted_scope_should_sort_user_by_display_name
38 assert_equal User.all.map(&:name).map(&:downcase).sort, User.sorted.all.map(&:name).map(&:downcase)
37 assert_equal User.all.map(&:name).map(&:downcase).sort, User.sorted.all.map(&:name).map(&:downcase)
39 end
38 end
40
39
41 def test_generate
40 def test_generate
42 User.generate!(:firstname => 'Testing connection')
41 User.generate!(:firstname => 'Testing connection')
43 User.generate!(:firstname => 'Testing connection')
42 User.generate!(:firstname => 'Testing connection')
44 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
43 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
45 end
44 end
46
45
47 def test_truth
46 def test_truth
48 assert_kind_of User, @jsmith
47 assert_kind_of User, @jsmith
49 end
48 end
50
49
51 def test_mail_should_be_stripped
50 def test_mail_should_be_stripped
52 u = User.new
51 u = User.new
53 u.mail = " foo@bar.com "
52 u.mail = " foo@bar.com "
54 assert_equal "foo@bar.com", u.mail
53 assert_equal "foo@bar.com", u.mail
55 end
54 end
56
55
57 def test_mail_validation
56 def test_mail_validation
58 u = User.new
57 u = User.new
59 u.mail = ''
58 u.mail = ''
60 assert !u.valid?
59 assert !u.valid?
61 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
60 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
62 end
61 end
63
62
64 def test_login_length_validation
63 def test_login_length_validation
65 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
64 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
66 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
65 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
67 assert !user.valid?
66 assert !user.valid?
68
67
69 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
68 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
70 assert user.valid?
69 assert user.valid?
71 assert user.save
70 assert user.save
72 end
71 end
73
72
74 def test_create
73 def test_create
75 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
74 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
76
75
77 user.login = "jsmith"
76 user.login = "jsmith"
78 user.password, user.password_confirmation = "password", "password"
77 user.password, user.password_confirmation = "password", "password"
79 # login uniqueness
78 # login uniqueness
80 assert !user.save
79 assert !user.save
81 assert_equal 1, user.errors.count
80 assert_equal 1, user.errors.count
82
81
83 user.login = "newuser"
82 user.login = "newuser"
84 user.password, user.password_confirmation = "password", "pass"
83 user.password, user.password_confirmation = "password", "pass"
85 # password confirmation
84 # password confirmation
86 assert !user.save
85 assert !user.save
87 assert_equal 1, user.errors.count
86 assert_equal 1, user.errors.count
88
87
89 user.password, user.password_confirmation = "password", "password"
88 user.password, user.password_confirmation = "password", "password"
90 assert user.save
89 assert user.save
91 end
90 end
92
91
93 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting
92 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting
94 @user1 = User.generate!
93 @user1 = User.generate!
95 assert_equal 'only_my_events', @user1.mail_notification
94 assert_equal 'only_my_events', @user1.mail_notification
96 with_settings :default_notification_option => 'all' do
95 with_settings :default_notification_option => 'all' do
97 @user2 = User.generate!
96 @user2 = User.generate!
98 assert_equal 'all', @user2.mail_notification
97 assert_equal 'all', @user2.mail_notification
99 end
98 end
100 end
99 end
101
100
102 def test_user_login_should_be_case_insensitive
101 def test_user_login_should_be_case_insensitive
103 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
102 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
104 u.login = 'newuser'
103 u.login = 'newuser'
105 u.password, u.password_confirmation = "password", "password"
104 u.password, u.password_confirmation = "password", "password"
106 assert u.save
105 assert u.save
107 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
106 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
108 u.login = 'NewUser'
107 u.login = 'NewUser'
109 u.password, u.password_confirmation = "password", "password"
108 u.password, u.password_confirmation = "password", "password"
110 assert !u.save
109 assert !u.save
111 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
110 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
112 end
111 end
113
112
114 def test_mail_uniqueness_should_not_be_case_sensitive
113 def test_mail_uniqueness_should_not_be_case_sensitive
115 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
114 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
116 u.login = 'newuser1'
115 u.login = 'newuser1'
117 u.password, u.password_confirmation = "password", "password"
116 u.password, u.password_confirmation = "password", "password"
118 assert u.save
117 assert u.save
119
118
120 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
119 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
121 u.login = 'newuser2'
120 u.login = 'newuser2'
122 u.password, u.password_confirmation = "password", "password"
121 u.password, u.password_confirmation = "password", "password"
123 assert !u.save
122 assert !u.save
124 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
123 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
125 end
124 end
126
125
127 def test_update
126 def test_update
128 assert_equal "admin", @admin.login
127 assert_equal "admin", @admin.login
129 @admin.login = "john"
128 @admin.login = "john"
130 assert @admin.save, @admin.errors.full_messages.join("; ")
129 assert @admin.save, @admin.errors.full_messages.join("; ")
131 @admin.reload
130 @admin.reload
132 assert_equal "john", @admin.login
131 assert_equal "john", @admin.login
133 end
132 end
134
133
135 def test_update_should_not_fail_for_legacy_user_with_different_case_logins
134 def test_update_should_not_fail_for_legacy_user_with_different_case_logins
136 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
135 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
137 u1.login = 'newuser1'
136 u1.login = 'newuser1'
138 assert u1.save
137 assert u1.save
139
138
140 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
139 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
141 u2.login = 'newuser1'
140 u2.login = 'newuser1'
142 assert u2.save(:validate => false)
141 assert u2.save(:validate => false)
143
142
144 user = User.find(u2.id)
143 user = User.find(u2.id)
145 user.firstname = "firstname"
144 user.firstname = "firstname"
146 assert user.save, "Save failed"
145 assert user.save, "Save failed"
147 end
146 end
148
147
149 def test_destroy_should_delete_members_and_roles
148 def test_destroy_should_delete_members_and_roles
150 members = Member.find_all_by_user_id(2)
149 members = Member.find_all_by_user_id(2)
151 ms = members.size
150 ms = members.size
152 rs = members.collect(&:roles).flatten.size
151 rs = members.collect(&:roles).flatten.size
153
152
154 assert_difference 'Member.count', - ms do
153 assert_difference 'Member.count', - ms do
155 assert_difference 'MemberRole.count', - rs do
154 assert_difference 'MemberRole.count', - rs do
156 User.find(2).destroy
155 User.find(2).destroy
157 end
156 end
158 end
157 end
159
158
160 assert_nil User.find_by_id(2)
159 assert_nil User.find_by_id(2)
161 assert Member.find_all_by_user_id(2).empty?
160 assert Member.find_all_by_user_id(2).empty?
162 end
161 end
163
162
164 def test_destroy_should_update_attachments
163 def test_destroy_should_update_attachments
165 attachment = Attachment.create!(:container => Project.find(1),
164 attachment = Attachment.create!(:container => Project.find(1),
166 :file => uploaded_test_file("testfile.txt", "text/plain"),
165 :file => uploaded_test_file("testfile.txt", "text/plain"),
167 :author_id => 2)
166 :author_id => 2)
168
167
169 User.find(2).destroy
168 User.find(2).destroy
170 assert_nil User.find_by_id(2)
169 assert_nil User.find_by_id(2)
171 assert_equal User.anonymous, attachment.reload.author
170 assert_equal User.anonymous, attachment.reload.author
172 end
171 end
173
172
174 def test_destroy_should_update_comments
173 def test_destroy_should_update_comments
175 comment = Comment.create!(
174 comment = Comment.create!(
176 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
175 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
177 :author => User.find(2),
176 :author => User.find(2),
178 :comments => 'foo'
177 :comments => 'foo'
179 )
178 )
180
179
181 User.find(2).destroy
180 User.find(2).destroy
182 assert_nil User.find_by_id(2)
181 assert_nil User.find_by_id(2)
183 assert_equal User.anonymous, comment.reload.author
182 assert_equal User.anonymous, comment.reload.author
184 end
183 end
185
184
186 def test_destroy_should_update_issues
185 def test_destroy_should_update_issues
187 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
186 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
188
187
189 User.find(2).destroy
188 User.find(2).destroy
190 assert_nil User.find_by_id(2)
189 assert_nil User.find_by_id(2)
191 assert_equal User.anonymous, issue.reload.author
190 assert_equal User.anonymous, issue.reload.author
192 end
191 end
193
192
194 def test_destroy_should_unassign_issues
193 def test_destroy_should_unassign_issues
195 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
194 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
196
195
197 User.find(2).destroy
196 User.find(2).destroy
198 assert_nil User.find_by_id(2)
197 assert_nil User.find_by_id(2)
199 assert_nil issue.reload.assigned_to
198 assert_nil issue.reload.assigned_to
200 end
199 end
201
200
202 def test_destroy_should_update_journals
201 def test_destroy_should_update_journals
203 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
202 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
204 issue.init_journal(User.find(2), "update")
203 issue.init_journal(User.find(2), "update")
205 issue.save!
204 issue.save!
206
205
207 User.find(2).destroy
206 User.find(2).destroy
208 assert_nil User.find_by_id(2)
207 assert_nil User.find_by_id(2)
209 assert_equal User.anonymous, issue.journals.first.reload.user
208 assert_equal User.anonymous, issue.journals.first.reload.user
210 end
209 end
211
210
212 def test_destroy_should_update_journal_details_old_value
211 def test_destroy_should_update_journal_details_old_value
213 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
212 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
214 issue.init_journal(User.find(1), "update")
213 issue.init_journal(User.find(1), "update")
215 issue.assigned_to_id = nil
214 issue.assigned_to_id = nil
216 assert_difference 'JournalDetail.count' do
215 assert_difference 'JournalDetail.count' do
217 issue.save!
216 issue.save!
218 end
217 end
219 journal_detail = JournalDetail.first(:order => 'id DESC')
218 journal_detail = JournalDetail.first(:order => 'id DESC')
220 assert_equal '2', journal_detail.old_value
219 assert_equal '2', journal_detail.old_value
221
220
222 User.find(2).destroy
221 User.find(2).destroy
223 assert_nil User.find_by_id(2)
222 assert_nil User.find_by_id(2)
224 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
223 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
225 end
224 end
226
225
227 def test_destroy_should_update_journal_details_value
226 def test_destroy_should_update_journal_details_value
228 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
227 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
229 issue.init_journal(User.find(1), "update")
228 issue.init_journal(User.find(1), "update")
230 issue.assigned_to_id = 2
229 issue.assigned_to_id = 2
231 assert_difference 'JournalDetail.count' do
230 assert_difference 'JournalDetail.count' do
232 issue.save!
231 issue.save!
233 end
232 end
234 journal_detail = JournalDetail.first(:order => 'id DESC')
233 journal_detail = JournalDetail.first(:order => 'id DESC')
235 assert_equal '2', journal_detail.value
234 assert_equal '2', journal_detail.value
236
235
237 User.find(2).destroy
236 User.find(2).destroy
238 assert_nil User.find_by_id(2)
237 assert_nil User.find_by_id(2)
239 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
238 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
240 end
239 end
241
240
242 def test_destroy_should_update_messages
241 def test_destroy_should_update_messages
243 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
242 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
244 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
243 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
245
244
246 User.find(2).destroy
245 User.find(2).destroy
247 assert_nil User.find_by_id(2)
246 assert_nil User.find_by_id(2)
248 assert_equal User.anonymous, message.reload.author
247 assert_equal User.anonymous, message.reload.author
249 end
248 end
250
249
251 def test_destroy_should_update_news
250 def test_destroy_should_update_news
252 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
251 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
253
252
254 User.find(2).destroy
253 User.find(2).destroy
255 assert_nil User.find_by_id(2)
254 assert_nil User.find_by_id(2)
256 assert_equal User.anonymous, news.reload.author
255 assert_equal User.anonymous, news.reload.author
257 end
256 end
258
257
259 def test_destroy_should_delete_private_queries
258 def test_destroy_should_delete_private_queries
260 query = Query.new(:name => 'foo', :is_public => false)
259 query = Query.new(:name => 'foo', :is_public => false)
261 query.project_id = 1
260 query.project_id = 1
262 query.user_id = 2
261 query.user_id = 2
263 query.save!
262 query.save!
264
263
265 User.find(2).destroy
264 User.find(2).destroy
266 assert_nil User.find_by_id(2)
265 assert_nil User.find_by_id(2)
267 assert_nil Query.find_by_id(query.id)
266 assert_nil Query.find_by_id(query.id)
268 end
267 end
269
268
270 def test_destroy_should_update_public_queries
269 def test_destroy_should_update_public_queries
271 query = Query.new(:name => 'foo', :is_public => true)
270 query = Query.new(:name => 'foo', :is_public => true)
272 query.project_id = 1
271 query.project_id = 1
273 query.user_id = 2
272 query.user_id = 2
274 query.save!
273 query.save!
275
274
276 User.find(2).destroy
275 User.find(2).destroy
277 assert_nil User.find_by_id(2)
276 assert_nil User.find_by_id(2)
278 assert_equal User.anonymous, query.reload.user
277 assert_equal User.anonymous, query.reload.user
279 end
278 end
280
279
281 def test_destroy_should_update_time_entries
280 def test_destroy_should_update_time_entries
282 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
281 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
283 entry.project_id = 1
282 entry.project_id = 1
284 entry.user_id = 2
283 entry.user_id = 2
285 entry.save!
284 entry.save!
286
285
287 User.find(2).destroy
286 User.find(2).destroy
288 assert_nil User.find_by_id(2)
287 assert_nil User.find_by_id(2)
289 assert_equal User.anonymous, entry.reload.user
288 assert_equal User.anonymous, entry.reload.user
290 end
289 end
291
290
292 def test_destroy_should_delete_tokens
291 def test_destroy_should_delete_tokens
293 token = Token.create!(:user_id => 2, :value => 'foo')
292 token = Token.create!(:user_id => 2, :value => 'foo')
294
293
295 User.find(2).destroy
294 User.find(2).destroy
296 assert_nil User.find_by_id(2)
295 assert_nil User.find_by_id(2)
297 assert_nil Token.find_by_id(token.id)
296 assert_nil Token.find_by_id(token.id)
298 end
297 end
299
298
300 def test_destroy_should_delete_watchers
299 def test_destroy_should_delete_watchers
301 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
300 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
302 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
301 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
303
302
304 User.find(2).destroy
303 User.find(2).destroy
305 assert_nil User.find_by_id(2)
304 assert_nil User.find_by_id(2)
306 assert_nil Watcher.find_by_id(watcher.id)
305 assert_nil Watcher.find_by_id(watcher.id)
307 end
306 end
308
307
309 def test_destroy_should_update_wiki_contents
308 def test_destroy_should_update_wiki_contents
310 wiki_content = WikiContent.create!(
309 wiki_content = WikiContent.create!(
311 :text => 'foo',
310 :text => 'foo',
312 :author_id => 2,
311 :author_id => 2,
313 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
312 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
314 )
313 )
315 wiki_content.text = 'bar'
314 wiki_content.text = 'bar'
316 assert_difference 'WikiContent::Version.count' do
315 assert_difference 'WikiContent::Version.count' do
317 wiki_content.save!
316 wiki_content.save!
318 end
317 end
319
318
320 User.find(2).destroy
319 User.find(2).destroy
321 assert_nil User.find_by_id(2)
320 assert_nil User.find_by_id(2)
322 assert_equal User.anonymous, wiki_content.reload.author
321 assert_equal User.anonymous, wiki_content.reload.author
323 wiki_content.versions.each do |version|
322 wiki_content.versions.each do |version|
324 assert_equal User.anonymous, version.reload.author
323 assert_equal User.anonymous, version.reload.author
325 end
324 end
326 end
325 end
327
326
328 def test_destroy_should_nullify_issue_categories
327 def test_destroy_should_nullify_issue_categories
329 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
328 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
330
329
331 User.find(2).destroy
330 User.find(2).destroy
332 assert_nil User.find_by_id(2)
331 assert_nil User.find_by_id(2)
333 assert_nil category.reload.assigned_to_id
332 assert_nil category.reload.assigned_to_id
334 end
333 end
335
334
336 def test_destroy_should_nullify_changesets
335 def test_destroy_should_nullify_changesets
337 changeset = Changeset.create!(
336 changeset = Changeset.create!(
338 :repository => Repository::Subversion.create!(
337 :repository => Repository::Subversion.create!(
339 :project_id => 1,
338 :project_id => 1,
340 :url => 'file:///tmp',
339 :url => 'file:///tmp',
341 :identifier => 'tmp'
340 :identifier => 'tmp'
342 ),
341 ),
343 :revision => '12',
342 :revision => '12',
344 :committed_on => Time.now,
343 :committed_on => Time.now,
345 :committer => 'jsmith'
344 :committer => 'jsmith'
346 )
345 )
347 assert_equal 2, changeset.user_id
346 assert_equal 2, changeset.user_id
348
347
349 User.find(2).destroy
348 User.find(2).destroy
350 assert_nil User.find_by_id(2)
349 assert_nil User.find_by_id(2)
351 assert_nil changeset.reload.user_id
350 assert_nil changeset.reload.user_id
352 end
351 end
353
352
354 def test_anonymous_user_should_not_be_destroyable
353 def test_anonymous_user_should_not_be_destroyable
355 assert_no_difference 'User.count' do
354 assert_no_difference 'User.count' do
356 assert_equal false, User.anonymous.destroy
355 assert_equal false, User.anonymous.destroy
357 end
356 end
358 end
357 end
359
358
360 def test_validate_login_presence
359 def test_validate_login_presence
361 @admin.login = ""
360 @admin.login = ""
362 assert !@admin.save
361 assert !@admin.save
363 assert_equal 1, @admin.errors.count
362 assert_equal 1, @admin.errors.count
364 end
363 end
365
364
366 def test_validate_mail_notification_inclusion
365 def test_validate_mail_notification_inclusion
367 u = User.new
366 u = User.new
368 u.mail_notification = 'foo'
367 u.mail_notification = 'foo'
369 u.save
368 u.save
370 assert_not_nil u.errors[:mail_notification]
369 assert_not_nil u.errors[:mail_notification]
371 end
370 end
372
371
373 context "User#try_to_login" do
372 context "User#try_to_login" do
374 should "fall-back to case-insensitive if user login is not found as-typed." do
373 should "fall-back to case-insensitive if user login is not found as-typed." do
375 user = User.try_to_login("AdMin", "admin")
374 user = User.try_to_login("AdMin", "admin")
376 assert_kind_of User, user
375 assert_kind_of User, user
377 assert_equal "admin", user.login
376 assert_equal "admin", user.login
378 end
377 end
379
378
380 should "select the exact matching user first" do
379 should "select the exact matching user first" do
381 case_sensitive_user = User.generate! do |user|
380 case_sensitive_user = User.generate! do |user|
382 user.password = "admin123"
381 user.password = "admin123"
383 end
382 end
384 # bypass validations to make it appear like existing data
383 # bypass validations to make it appear like existing data
385 case_sensitive_user.update_attribute(:login, 'ADMIN')
384 case_sensitive_user.update_attribute(:login, 'ADMIN')
386
385
387 user = User.try_to_login("ADMIN", "admin123")
386 user = User.try_to_login("ADMIN", "admin123")
388 assert_kind_of User, user
387 assert_kind_of User, user
389 assert_equal "ADMIN", user.login
388 assert_equal "ADMIN", user.login
390
389
391 end
390 end
392 end
391 end
393
392
394 def test_password
393 def test_password
395 user = User.try_to_login("admin", "admin")
394 user = User.try_to_login("admin", "admin")
396 assert_kind_of User, user
395 assert_kind_of User, user
397 assert_equal "admin", user.login
396 assert_equal "admin", user.login
398 user.password = "hello123"
397 user.password = "hello123"
399 assert user.save
398 assert user.save
400
399
401 user = User.try_to_login("admin", "hello123")
400 user = User.try_to_login("admin", "hello123")
402 assert_kind_of User, user
401 assert_kind_of User, user
403 assert_equal "admin", user.login
402 assert_equal "admin", user.login
404 end
403 end
405
404
406 def test_validate_password_length
405 def test_validate_password_length
407 with_settings :password_min_length => '100' do
406 with_settings :password_min_length => '100' do
408 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
407 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
409 user.login = "newuser100"
408 user.login = "newuser100"
410 user.password, user.password_confirmation = "password100", "password100"
409 user.password, user.password_confirmation = "password100", "password100"
411 assert !user.save
410 assert !user.save
412 assert_equal 1, user.errors.count
411 assert_equal 1, user.errors.count
413 end
412 end
414 end
413 end
415
414
416 def test_name_format
415 def test_name_format
417 assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
416 assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
418 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
417 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
419 with_settings :user_format => :firstname_lastname do
418 with_settings :user_format => :firstname_lastname do
420 assert_equal 'John Smith', @jsmith.reload.name
419 assert_equal 'John Smith', @jsmith.reload.name
421 end
420 end
422 with_settings :user_format => :username do
421 with_settings :user_format => :username do
423 assert_equal 'jsmith', @jsmith.reload.name
422 assert_equal 'jsmith', @jsmith.reload.name
424 end
423 end
425 with_settings :user_format => :lastname do
424 with_settings :user_format => :lastname do
426 assert_equal 'Smith', @jsmith.reload.name
425 assert_equal 'Smith', @jsmith.reload.name
427 end
426 end
428 end
427 end
429
428
430 def test_today_should_return_the_day_according_to_user_time_zone
429 def test_today_should_return_the_day_according_to_user_time_zone
431 preference = User.find(1).pref
430 preference = User.find(1).pref
432 date = Date.new(2012, 05, 15)
431 date = Date.new(2012, 05, 15)
433 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
432 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
434 Date.stubs(:today).returns(date)
433 Date.stubs(:today).returns(date)
435 Time.stubs(:now).returns(time)
434 Time.stubs(:now).returns(time)
436
435
437 preference.update_attribute :time_zone, 'Baku' # UTC+4
436 preference.update_attribute :time_zone, 'Baku' # UTC+4
438 assert_equal '2012-05-16', User.find(1).today.to_s
437 assert_equal '2012-05-16', User.find(1).today.to_s
439
438
440 preference.update_attribute :time_zone, 'La Paz' # UTC-4
439 preference.update_attribute :time_zone, 'La Paz' # UTC-4
441 assert_equal '2012-05-15', User.find(1).today.to_s
440 assert_equal '2012-05-15', User.find(1).today.to_s
442
441
443 preference.update_attribute :time_zone, ''
442 preference.update_attribute :time_zone, ''
444 assert_equal '2012-05-15', User.find(1).today.to_s
443 assert_equal '2012-05-15', User.find(1).today.to_s
445 end
444 end
446
445
447 def test_time_to_date_should_return_the_date_according_to_user_time_zone
446 def test_time_to_date_should_return_the_date_according_to_user_time_zone
448 preference = User.find(1).pref
447 preference = User.find(1).pref
449 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
448 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
450
449
451 preference.update_attribute :time_zone, 'Baku' # UTC+4
450 preference.update_attribute :time_zone, 'Baku' # UTC+4
452 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s
451 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s
453
452
454 preference.update_attribute :time_zone, 'La Paz' # UTC-4
453 preference.update_attribute :time_zone, 'La Paz' # UTC-4
455 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
454 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
456
455
457 preference.update_attribute :time_zone, ''
456 preference.update_attribute :time_zone, ''
458 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
457 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
459 end
458 end
460
459
461 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
460 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
462 with_settings :user_format => 'lastname_coma_firstname' do
461 with_settings :user_format => 'lastname_coma_firstname' do
463 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
462 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
464 end
463 end
465 end
464 end
466
465
467 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
466 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
468 with_settings :user_format => 'lastname_firstname' do
467 with_settings :user_format => 'lastname_firstname' do
469 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
468 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
470 end
469 end
471 end
470 end
472
471
473 def test_fields_for_order_statement_with_blank_format_should_return_default
472 def test_fields_for_order_statement_with_blank_format_should_return_default
474 with_settings :user_format => '' do
473 with_settings :user_format => '' do
475 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
474 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
476 end
475 end
477 end
476 end
478
477
479 def test_fields_for_order_statement_with_invalid_format_should_return_default
478 def test_fields_for_order_statement_with_invalid_format_should_return_default
480 with_settings :user_format => 'foo' do
479 with_settings :user_format => 'foo' do
481 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
480 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
482 end
481 end
483 end
482 end
484
483
485 def test_lock
484 def test_lock
486 user = User.try_to_login("jsmith", "jsmith")
485 user = User.try_to_login("jsmith", "jsmith")
487 assert_equal @jsmith, user
486 assert_equal @jsmith, user
488
487
489 @jsmith.status = User::STATUS_LOCKED
488 @jsmith.status = User::STATUS_LOCKED
490 assert @jsmith.save
489 assert @jsmith.save
491
490
492 user = User.try_to_login("jsmith", "jsmith")
491 user = User.try_to_login("jsmith", "jsmith")
493 assert_equal nil, user
492 assert_equal nil, user
494 end
493 end
495
494
496 context ".try_to_login" do
495 context ".try_to_login" do
497 context "with good credentials" do
496 context "with good credentials" do
498 should "return the user" do
497 should "return the user" do
499 user = User.try_to_login("admin", "admin")
498 user = User.try_to_login("admin", "admin")
500 assert_kind_of User, user
499 assert_kind_of User, user
501 assert_equal "admin", user.login
500 assert_equal "admin", user.login
502 end
501 end
503 end
502 end
504
503
505 context "with wrong credentials" do
504 context "with wrong credentials" do
506 should "return nil" do
505 should "return nil" do
507 assert_nil User.try_to_login("admin", "foo")
506 assert_nil User.try_to_login("admin", "foo")
508 end
507 end
509 end
508 end
510 end
509 end
511
510
512 if ldap_configured?
511 if ldap_configured?
513 context "#try_to_login using LDAP" do
512 context "#try_to_login using LDAP" do
514 context "with failed connection to the LDAP server" do
513 context "with failed connection to the LDAP server" do
515 should "return nil" do
514 should "return nil" do
516 @auth_source = AuthSourceLdap.find(1)
515 @auth_source = AuthSourceLdap.find(1)
517 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
516 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
518
517
519 assert_equal nil, User.try_to_login('edavis', 'wrong')
518 assert_equal nil, User.try_to_login('edavis', 'wrong')
520 end
519 end
521 end
520 end
522
521
523 context "with an unsuccessful authentication" do
522 context "with an unsuccessful authentication" do
524 should "return nil" do
523 should "return nil" do
525 assert_equal nil, User.try_to_login('edavis', 'wrong')
524 assert_equal nil, User.try_to_login('edavis', 'wrong')
526 end
525 end
527 end
526 end
528
527
529 context "binding with user's account" do
528 context "binding with user's account" do
530 setup do
529 setup do
531 @auth_source = AuthSourceLdap.find(1)
530 @auth_source = AuthSourceLdap.find(1)
532 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
531 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
533 @auth_source.account_password = ''
532 @auth_source.account_password = ''
534 @auth_source.save!
533 @auth_source.save!
535
534
536 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
535 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
537 @ldap_user.login = 'example1'
536 @ldap_user.login = 'example1'
538 @ldap_user.save!
537 @ldap_user.save!
539 end
538 end
540
539
541 context "with a successful authentication" do
540 context "with a successful authentication" do
542 should "return the user" do
541 should "return the user" do
543 assert_equal @ldap_user, User.try_to_login('example1', '123456')
542 assert_equal @ldap_user, User.try_to_login('example1', '123456')
544 end
543 end
545 end
544 end
546
545
547 context "with an unsuccessful authentication" do
546 context "with an unsuccessful authentication" do
548 should "return nil" do
547 should "return nil" do
549 assert_nil User.try_to_login('example1', '11111')
548 assert_nil User.try_to_login('example1', '11111')
550 end
549 end
551 end
550 end
552 end
551 end
553
552
554 context "on the fly registration" do
553 context "on the fly registration" do
555 setup do
554 setup do
556 @auth_source = AuthSourceLdap.find(1)
555 @auth_source = AuthSourceLdap.find(1)
557 @auth_source.update_attribute :onthefly_register, true
556 @auth_source.update_attribute :onthefly_register, true
558 end
557 end
559
558
560 context "with a successful authentication" do
559 context "with a successful authentication" do
561 should "create a new user account if it doesn't exist" do
560 should "create a new user account if it doesn't exist" do
562 assert_difference('User.count') do
561 assert_difference('User.count') do
563 user = User.try_to_login('edavis', '123456')
562 user = User.try_to_login('edavis', '123456')
564 assert !user.admin?
563 assert !user.admin?
565 end
564 end
566 end
565 end
567
566
568 should "retrieve existing user" do
567 should "retrieve existing user" do
569 user = User.try_to_login('edavis', '123456')
568 user = User.try_to_login('edavis', '123456')
570 user.admin = true
569 user.admin = true
571 user.save!
570 user.save!
572
571
573 assert_no_difference('User.count') do
572 assert_no_difference('User.count') do
574 user = User.try_to_login('edavis', '123456')
573 user = User.try_to_login('edavis', '123456')
575 assert user.admin?
574 assert user.admin?
576 end
575 end
577 end
576 end
578 end
577 end
579
578
580 context "binding with user's account" do
579 context "binding with user's account" do
581 setup do
580 setup do
582 @auth_source = AuthSourceLdap.find(1)
581 @auth_source = AuthSourceLdap.find(1)
583 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
582 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
584 @auth_source.account_password = ''
583 @auth_source.account_password = ''
585 @auth_source.save!
584 @auth_source.save!
586 end
585 end
587
586
588 context "with a successful authentication" do
587 context "with a successful authentication" do
589 should "create a new user account if it doesn't exist" do
588 should "create a new user account if it doesn't exist" do
590 assert_difference('User.count') do
589 assert_difference('User.count') do
591 user = User.try_to_login('example1', '123456')
590 user = User.try_to_login('example1', '123456')
592 assert_kind_of User, user
591 assert_kind_of User, user
593 end
592 end
594 end
593 end
595 end
594 end
596
595
597 context "with an unsuccessful authentication" do
596 context "with an unsuccessful authentication" do
598 should "return nil" do
597 should "return nil" do
599 assert_nil User.try_to_login('example1', '11111')
598 assert_nil User.try_to_login('example1', '11111')
600 end
599 end
601 end
600 end
602 end
601 end
603 end
602 end
604 end
603 end
605
604
606 else
605 else
607 puts "Skipping LDAP tests."
606 puts "Skipping LDAP tests."
608 end
607 end
609
608
610 def test_create_anonymous
609 def test_create_anonymous
611 AnonymousUser.delete_all
610 AnonymousUser.delete_all
612 anon = User.anonymous
611 anon = User.anonymous
613 assert !anon.new_record?
612 assert !anon.new_record?
614 assert_kind_of AnonymousUser, anon
613 assert_kind_of AnonymousUser, anon
615 end
614 end
616
615
617 def test_ensure_single_anonymous_user
616 def test_ensure_single_anonymous_user
618 AnonymousUser.delete_all
617 AnonymousUser.delete_all
619 anon1 = User.anonymous
618 anon1 = User.anonymous
620 assert !anon1.new_record?
619 assert !anon1.new_record?
621 assert_kind_of AnonymousUser, anon1
620 assert_kind_of AnonymousUser, anon1
622 anon2 = AnonymousUser.create(
621 anon2 = AnonymousUser.create(
623 :lastname => 'Anonymous', :firstname => '',
622 :lastname => 'Anonymous', :firstname => '',
624 :mail => '', :login => '', :status => 0)
623 :mail => '', :login => '', :status => 0)
625 assert_equal 1, anon2.errors.count
624 assert_equal 1, anon2.errors.count
626 end
625 end
627
626
628 def test_rss_key
627 def test_rss_key
629 assert_nil @jsmith.rss_token
628 assert_nil @jsmith.rss_token
630 key = @jsmith.rss_key
629 key = @jsmith.rss_key
631 assert_equal 40, key.length
630 assert_equal 40, key.length
632
631
633 @jsmith.reload
632 @jsmith.reload
634 assert_equal key, @jsmith.rss_key
633 assert_equal key, @jsmith.rss_key
635 end
634 end
636
635
637 def test_rss_key_should_not_be_generated_twice
636 def test_rss_key_should_not_be_generated_twice
638 assert_difference 'Token.count', 1 do
637 assert_difference 'Token.count', 1 do
639 key1 = @jsmith.rss_key
638 key1 = @jsmith.rss_key
640 key2 = @jsmith.rss_key
639 key2 = @jsmith.rss_key
641 assert_equal key1, key2
640 assert_equal key1, key2
642 end
641 end
643 end
642 end
644
643
645 def test_api_key_should_not_be_generated_twice
644 def test_api_key_should_not_be_generated_twice
646 assert_difference 'Token.count', 1 do
645 assert_difference 'Token.count', 1 do
647 key1 = @jsmith.api_key
646 key1 = @jsmith.api_key
648 key2 = @jsmith.api_key
647 key2 = @jsmith.api_key
649 assert_equal key1, key2
648 assert_equal key1, key2
650 end
649 end
651 end
650 end
652
651
653 context "User#api_key" do
652 context "User#api_key" do
654 should "generate a new one if the user doesn't have one" do
653 should "generate a new one if the user doesn't have one" do
655 user = User.generate!(:api_token => nil)
654 user = User.generate!(:api_token => nil)
656 assert_nil user.api_token
655 assert_nil user.api_token
657
656
658 key = user.api_key
657 key = user.api_key
659 assert_equal 40, key.length
658 assert_equal 40, key.length
660 user.reload
659 user.reload
661 assert_equal key, user.api_key
660 assert_equal key, user.api_key
662 end
661 end
663
662
664 should "return the existing api token value" do
663 should "return the existing api token value" do
665 user = User.generate!
664 user = User.generate!
666 token = Token.create!(:action => 'api')
665 token = Token.create!(:action => 'api')
667 user.api_token = token
666 user.api_token = token
668 assert user.save
667 assert user.save
669
668
670 assert_equal token.value, user.api_key
669 assert_equal token.value, user.api_key
671 end
670 end
672 end
671 end
673
672
674 context "User#find_by_api_key" do
673 context "User#find_by_api_key" do
675 should "return nil if no matching key is found" do
674 should "return nil if no matching key is found" do
676 assert_nil User.find_by_api_key('zzzzzzzzz')
675 assert_nil User.find_by_api_key('zzzzzzzzz')
677 end
676 end
678
677
679 should "return nil if the key is found for an inactive user" do
678 should "return nil if the key is found for an inactive user" do
680 user = User.generate!
679 user = User.generate!
681 user.status = User::STATUS_LOCKED
680 user.status = User::STATUS_LOCKED
682 token = Token.create!(:action => 'api')
681 token = Token.create!(:action => 'api')
683 user.api_token = token
682 user.api_token = token
684 user.save
683 user.save
685
684
686 assert_nil User.find_by_api_key(token.value)
685 assert_nil User.find_by_api_key(token.value)
687 end
686 end
688
687
689 should "return the user if the key is found for an active user" do
688 should "return the user if the key is found for an active user" do
690 user = User.generate!
689 user = User.generate!
691 token = Token.create!(:action => 'api')
690 token = Token.create!(:action => 'api')
692 user.api_token = token
691 user.api_token = token
693 user.save
692 user.save
694
693
695 assert_equal user, User.find_by_api_key(token.value)
694 assert_equal user, User.find_by_api_key(token.value)
696 end
695 end
697 end
696 end
698
697
699 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
698 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
700 user = User.find_by_login("admin")
699 user = User.find_by_login("admin")
701 user.password = "admin"
700 user.password = "admin"
702 assert user.save(:validate => false)
701 assert user.save(:validate => false)
703
702
704 assert_equal false, User.default_admin_account_changed?
703 assert_equal false, User.default_admin_account_changed?
705 end
704 end
706
705
707 def test_default_admin_account_changed_should_return_true_if_password_was_changed
706 def test_default_admin_account_changed_should_return_true_if_password_was_changed
708 user = User.find_by_login("admin")
707 user = User.find_by_login("admin")
709 user.password = "newpassword"
708 user.password = "newpassword"
710 user.save!
709 user.save!
711
710
712 assert_equal true, User.default_admin_account_changed?
711 assert_equal true, User.default_admin_account_changed?
713 end
712 end
714
713
715 def test_default_admin_account_changed_should_return_true_if_account_is_disabled
714 def test_default_admin_account_changed_should_return_true_if_account_is_disabled
716 user = User.find_by_login("admin")
715 user = User.find_by_login("admin")
717 user.password = "admin"
716 user.password = "admin"
718 user.status = User::STATUS_LOCKED
717 user.status = User::STATUS_LOCKED
719 assert user.save(:validate => false)
718 assert user.save(:validate => false)
720
719
721 assert_equal true, User.default_admin_account_changed?
720 assert_equal true, User.default_admin_account_changed?
722 end
721 end
723
722
724 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
723 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
725 user = User.find_by_login("admin")
724 user = User.find_by_login("admin")
726 user.destroy
725 user.destroy
727
726
728 assert_equal true, User.default_admin_account_changed?
727 assert_equal true, User.default_admin_account_changed?
729 end
728 end
730
729
731 def test_roles_for_project
730 def test_roles_for_project
732 # user with a role
731 # user with a role
733 roles = @jsmith.roles_for_project(Project.find(1))
732 roles = @jsmith.roles_for_project(Project.find(1))
734 assert_kind_of Role, roles.first
733 assert_kind_of Role, roles.first
735 assert_equal "Manager", roles.first.name
734 assert_equal "Manager", roles.first.name
736
735
737 # user with no role
736 # user with no role
738 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
737 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
739 end
738 end
740
739
741 def test_projects_by_role_for_user_with_role
740 def test_projects_by_role_for_user_with_role
742 user = User.find(2)
741 user = User.find(2)
743 assert_kind_of Hash, user.projects_by_role
742 assert_kind_of Hash, user.projects_by_role
744 assert_equal 2, user.projects_by_role.size
743 assert_equal 2, user.projects_by_role.size
745 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
744 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
746 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
745 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
747 end
746 end
748
747
749 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
748 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
750 user = User.find(2)
749 user = User.find(2)
751 assert_equal [], user.projects_by_role[Role.find(3)]
750 assert_equal [], user.projects_by_role[Role.find(3)]
752 # should not update the hash
751 # should not update the hash
753 assert_nil user.projects_by_role.values.detect(&:blank?)
752 assert_nil user.projects_by_role.values.detect(&:blank?)
754 end
753 end
755
754
756 def test_projects_by_role_for_user_with_no_role
755 def test_projects_by_role_for_user_with_no_role
757 user = User.generate!
756 user = User.generate!
758 assert_equal({}, user.projects_by_role)
757 assert_equal({}, user.projects_by_role)
759 end
758 end
760
759
761 def test_projects_by_role_for_anonymous
760 def test_projects_by_role_for_anonymous
762 assert_equal({}, User.anonymous.projects_by_role)
761 assert_equal({}, User.anonymous.projects_by_role)
763 end
762 end
764
763
765 def test_valid_notification_options
764 def test_valid_notification_options
766 # without memberships
765 # without memberships
767 assert_equal 5, User.find(7).valid_notification_options.size
766 assert_equal 5, User.find(7).valid_notification_options.size
768 # with memberships
767 # with memberships
769 assert_equal 6, User.find(2).valid_notification_options.size
768 assert_equal 6, User.find(2).valid_notification_options.size
770 end
769 end
771
770
772 def test_valid_notification_options_class_method
771 def test_valid_notification_options_class_method
773 assert_equal 5, User.valid_notification_options.size
772 assert_equal 5, User.valid_notification_options.size
774 assert_equal 5, User.valid_notification_options(User.find(7)).size
773 assert_equal 5, User.valid_notification_options(User.find(7)).size
775 assert_equal 6, User.valid_notification_options(User.find(2)).size
774 assert_equal 6, User.valid_notification_options(User.find(2)).size
776 end
775 end
777
776
778 def test_mail_notification_all
777 def test_mail_notification_all
779 @jsmith.mail_notification = 'all'
778 @jsmith.mail_notification = 'all'
780 @jsmith.notified_project_ids = []
779 @jsmith.notified_project_ids = []
781 @jsmith.save
780 @jsmith.save
782 @jsmith.reload
781 @jsmith.reload
783 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
782 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
784 end
783 end
785
784
786 def test_mail_notification_selected
785 def test_mail_notification_selected
787 @jsmith.mail_notification = 'selected'
786 @jsmith.mail_notification = 'selected'
788 @jsmith.notified_project_ids = [1]
787 @jsmith.notified_project_ids = [1]
789 @jsmith.save
788 @jsmith.save
790 @jsmith.reload
789 @jsmith.reload
791 assert Project.find(1).recipients.include?(@jsmith.mail)
790 assert Project.find(1).recipients.include?(@jsmith.mail)
792 end
791 end
793
792
794 def test_mail_notification_only_my_events
793 def test_mail_notification_only_my_events
795 @jsmith.mail_notification = 'only_my_events'
794 @jsmith.mail_notification = 'only_my_events'
796 @jsmith.notified_project_ids = []
795 @jsmith.notified_project_ids = []
797 @jsmith.save
796 @jsmith.save
798 @jsmith.reload
797 @jsmith.reload
799 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
798 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
800 end
799 end
801
800
802 def test_comments_sorting_preference
801 def test_comments_sorting_preference
803 assert !@jsmith.wants_comments_in_reverse_order?
802 assert !@jsmith.wants_comments_in_reverse_order?
804 @jsmith.pref.comments_sorting = 'asc'
803 @jsmith.pref.comments_sorting = 'asc'
805 assert !@jsmith.wants_comments_in_reverse_order?
804 assert !@jsmith.wants_comments_in_reverse_order?
806 @jsmith.pref.comments_sorting = 'desc'
805 @jsmith.pref.comments_sorting = 'desc'
807 assert @jsmith.wants_comments_in_reverse_order?
806 assert @jsmith.wants_comments_in_reverse_order?
808 end
807 end
809
808
810 def test_find_by_mail_should_be_case_insensitive
809 def test_find_by_mail_should_be_case_insensitive
811 u = User.find_by_mail('JSmith@somenet.foo')
810 u = User.find_by_mail('JSmith@somenet.foo')
812 assert_not_nil u
811 assert_not_nil u
813 assert_equal 'jsmith@somenet.foo', u.mail
812 assert_equal 'jsmith@somenet.foo', u.mail
814 end
813 end
815
814
816 def test_random_password
815 def test_random_password
817 u = User.new
816 u = User.new
818 u.random_password
817 u.random_password
819 assert !u.password.blank?
818 assert !u.password.blank?
820 assert !u.password_confirmation.blank?
819 assert !u.password_confirmation.blank?
821 end
820 end
822
821
823 context "#change_password_allowed?" do
822 context "#change_password_allowed?" do
824 should "be allowed if no auth source is set" do
823 should "be allowed if no auth source is set" do
825 user = User.generate!
824 user = User.generate!
826 assert user.change_password_allowed?
825 assert user.change_password_allowed?
827 end
826 end
828
827
829 should "delegate to the auth source" do
828 should "delegate to the auth source" do
830 user = User.generate!
829 user = User.generate!
831
830
832 allowed_auth_source = AuthSource.generate!
831 allowed_auth_source = AuthSource.generate!
833 def allowed_auth_source.allow_password_changes?; true; end
832 def allowed_auth_source.allow_password_changes?; true; end
834
833
835 denied_auth_source = AuthSource.generate!
834 denied_auth_source = AuthSource.generate!
836 def denied_auth_source.allow_password_changes?; false; end
835 def denied_auth_source.allow_password_changes?; false; end
837
836
838 assert user.change_password_allowed?
837 assert user.change_password_allowed?
839
838
840 user.auth_source = allowed_auth_source
839 user.auth_source = allowed_auth_source
841 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
840 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
842
841
843 user.auth_source = denied_auth_source
842 user.auth_source = denied_auth_source
844 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
843 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
845 end
844 end
846 end
845 end
847
846
848 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
847 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
849 with_settings :unsubscribe => '1' do
848 with_settings :unsubscribe => '1' do
850 assert_equal true, User.find(2).own_account_deletable?
849 assert_equal true, User.find(2).own_account_deletable?
851 end
850 end
852 end
851 end
853
852
854 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
853 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
855 with_settings :unsubscribe => '0' do
854 with_settings :unsubscribe => '0' do
856 assert_equal false, User.find(2).own_account_deletable?
855 assert_equal false, User.find(2).own_account_deletable?
857 end
856 end
858 end
857 end
859
858
860 def test_own_account_deletable_should_be_false_for_a_single_admin
859 def test_own_account_deletable_should_be_false_for_a_single_admin
861 User.delete_all(["admin = ? AND id <> ?", true, 1])
860 User.delete_all(["admin = ? AND id <> ?", true, 1])
862
861
863 with_settings :unsubscribe => '1' do
862 with_settings :unsubscribe => '1' do
864 assert_equal false, User.find(1).own_account_deletable?
863 assert_equal false, User.find(1).own_account_deletable?
865 end
864 end
866 end
865 end
867
866
868 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
867 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
869 User.generate! do |user|
868 User.generate! do |user|
870 user.admin = true
869 user.admin = true
871 end
870 end
872
871
873 with_settings :unsubscribe => '1' do
872 with_settings :unsubscribe => '1' do
874 assert_equal true, User.find(1).own_account_deletable?
873 assert_equal true, User.find(1).own_account_deletable?
875 end
874 end
876 end
875 end
877
876
878 context "#allowed_to?" do
877 context "#allowed_to?" do
879 context "with a unique project" do
878 context "with a unique project" do
880 should "return false if project is archived" do
879 should "return false if project is archived" do
881 project = Project.find(1)
880 project = Project.find(1)
882 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
881 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
883 assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1))
882 assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1))
884 end
883 end
885
884
886 should "return false for write action if project is closed" do
885 should "return false for write action if project is closed" do
887 project = Project.find(1)
886 project = Project.find(1)
888 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
887 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
889 assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1))
888 assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1))
890 end
889 end
891
890
892 should "return true for read action if project is closed" do
891 should "return true for read action if project is closed" do
893 project = Project.find(1)
892 project = Project.find(1)
894 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
893 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
895 assert_equal true, @admin.allowed_to?(:view_project, Project.find(1))
894 assert_equal true, @admin.allowed_to?(:view_project, Project.find(1))
896 end
895 end
897
896
898 should "return false if related module is disabled" do
897 should "return false if related module is disabled" do
899 project = Project.find(1)
898 project = Project.find(1)
900 project.enabled_module_names = ["issue_tracking"]
899 project.enabled_module_names = ["issue_tracking"]
901 assert_equal true, @admin.allowed_to?(:add_issues, project)
900 assert_equal true, @admin.allowed_to?(:add_issues, project)
902 assert_equal false, @admin.allowed_to?(:view_wiki_pages, project)
901 assert_equal false, @admin.allowed_to?(:view_wiki_pages, project)
903 end
902 end
904
903
905 should "authorize nearly everything for admin users" do
904 should "authorize nearly everything for admin users" do
906 project = Project.find(1)
905 project = Project.find(1)
907 assert ! @admin.member_of?(project)
906 assert ! @admin.member_of?(project)
908 %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p|
907 %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p|
909 assert_equal true, @admin.allowed_to?(p.to_sym, project)
908 assert_equal true, @admin.allowed_to?(p.to_sym, project)
910 end
909 end
911 end
910 end
912
911
913 should "authorize normal users depending on their roles" do
912 should "authorize normal users depending on their roles" do
914 project = Project.find(1)
913 project = Project.find(1)
915 assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager
914 assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager
916 assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper
915 assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper
917 end
916 end
918 end
917 end
919
918
920 context "with multiple projects" do
919 context "with multiple projects" do
921 should "return false if array is empty" do
920 should "return false if array is empty" do
922 assert_equal false, @admin.allowed_to?(:view_project, [])
921 assert_equal false, @admin.allowed_to?(:view_project, [])
923 end
922 end
924
923
925 should "return true only if user has permission on all these projects" do
924 should "return true only if user has permission on all these projects" do
926 assert_equal true, @admin.allowed_to?(:view_project, Project.all)
925 assert_equal true, @admin.allowed_to?(:view_project, Project.all)
927 assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
926 assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
928 assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
927 assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
929 assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
928 assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
930 end
929 end
931
930
932 should "behave correctly with arrays of 1 project" do
931 should "behave correctly with arrays of 1 project" do
933 assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first])
932 assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first])
934 end
933 end
935 end
934 end
936
935
937 context "with options[:global]" do
936 context "with options[:global]" do
938 should "authorize if user has at least one role that has this permission" do
937 should "authorize if user has at least one role that has this permission" do
939 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
938 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
940 @anonymous = User.find(6)
939 @anonymous = User.find(6)
941 assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
940 assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
942 assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
941 assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
943 assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true)
942 assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true)
944 assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true)
943 assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true)
945 assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true)
944 assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true)
946 end
945 end
947 end
946 end
948 end
947 end
949
948
950 context "User#notify_about?" do
949 context "User#notify_about?" do
951 context "Issues" do
950 context "Issues" do
952 setup do
951 setup do
953 @project = Project.find(1)
952 @project = Project.find(1)
954 @author = User.generate!
953 @author = User.generate!
955 @assignee = User.generate!
954 @assignee = User.generate!
956 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
955 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
957 end
956 end
958
957
959 should "be true for a user with :all" do
958 should "be true for a user with :all" do
960 @author.update_attribute(:mail_notification, 'all')
959 @author.update_attribute(:mail_notification, 'all')
961 assert @author.notify_about?(@issue)
960 assert @author.notify_about?(@issue)
962 end
961 end
963
962
964 should "be false for a user with :none" do
963 should "be false for a user with :none" do
965 @author.update_attribute(:mail_notification, 'none')
964 @author.update_attribute(:mail_notification, 'none')
966 assert ! @author.notify_about?(@issue)
965 assert ! @author.notify_about?(@issue)
967 end
966 end
968
967
969 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
968 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
970 @user = User.generate!(:mail_notification => 'only_my_events')
969 @user = User.generate!(:mail_notification => 'only_my_events')
971 Member.create!(:user => @user, :project => @project, :role_ids => [1])
970 Member.create!(:user => @user, :project => @project, :role_ids => [1])
972 assert ! @user.notify_about?(@issue)
971 assert ! @user.notify_about?(@issue)
973 end
972 end
974
973
975 should "be true for a user with :only_my_events and is the author" do
974 should "be true for a user with :only_my_events and is the author" do
976 @author.update_attribute(:mail_notification, 'only_my_events')
975 @author.update_attribute(:mail_notification, 'only_my_events')
977 assert @author.notify_about?(@issue)
976 assert @author.notify_about?(@issue)
978 end
977 end
979
978
980 should "be true for a user with :only_my_events and is the assignee" do
979 should "be true for a user with :only_my_events and is the assignee" do
981 @assignee.update_attribute(:mail_notification, 'only_my_events')
980 @assignee.update_attribute(:mail_notification, 'only_my_events')
982 assert @assignee.notify_about?(@issue)
981 assert @assignee.notify_about?(@issue)
983 end
982 end
984
983
985 should "be true for a user with :only_assigned and is the assignee" do
984 should "be true for a user with :only_assigned and is the assignee" do
986 @assignee.update_attribute(:mail_notification, 'only_assigned')
985 @assignee.update_attribute(:mail_notification, 'only_assigned')
987 assert @assignee.notify_about?(@issue)
986 assert @assignee.notify_about?(@issue)
988 end
987 end
989
988
990 should "be false for a user with :only_assigned and is not the assignee" do
989 should "be false for a user with :only_assigned and is not the assignee" do
991 @author.update_attribute(:mail_notification, 'only_assigned')
990 @author.update_attribute(:mail_notification, 'only_assigned')
992 assert ! @author.notify_about?(@issue)
991 assert ! @author.notify_about?(@issue)
993 end
992 end
994
993
995 should "be true for a user with :only_owner and is the author" do
994 should "be true for a user with :only_owner and is the author" do
996 @author.update_attribute(:mail_notification, 'only_owner')
995 @author.update_attribute(:mail_notification, 'only_owner')
997 assert @author.notify_about?(@issue)
996 assert @author.notify_about?(@issue)
998 end
997 end
999
998
1000 should "be false for a user with :only_owner and is not the author" do
999 should "be false for a user with :only_owner and is not the author" do
1001 @assignee.update_attribute(:mail_notification, 'only_owner')
1000 @assignee.update_attribute(:mail_notification, 'only_owner')
1002 assert ! @assignee.notify_about?(@issue)
1001 assert ! @assignee.notify_about?(@issue)
1003 end
1002 end
1004
1003
1005 should "be true for a user with :selected and is the author" do
1004 should "be true for a user with :selected and is the author" do
1006 @author.update_attribute(:mail_notification, 'selected')
1005 @author.update_attribute(:mail_notification, 'selected')
1007 assert @author.notify_about?(@issue)
1006 assert @author.notify_about?(@issue)
1008 end
1007 end
1009
1008
1010 should "be true for a user with :selected and is the assignee" do
1009 should "be true for a user with :selected and is the assignee" do
1011 @assignee.update_attribute(:mail_notification, 'selected')
1010 @assignee.update_attribute(:mail_notification, 'selected')
1012 assert @assignee.notify_about?(@issue)
1011 assert @assignee.notify_about?(@issue)
1013 end
1012 end
1014
1013
1015 should "be false for a user with :selected and is not the author or assignee" do
1014 should "be false for a user with :selected and is not the author or assignee" do
1016 @user = User.generate!(:mail_notification => 'selected')
1015 @user = User.generate!(:mail_notification => 'selected')
1017 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1016 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1018 assert ! @user.notify_about?(@issue)
1017 assert ! @user.notify_about?(@issue)
1019 end
1018 end
1020 end
1019 end
1021 end
1020 end
1022
1021
1023 def test_notify_about_news
1022 def test_notify_about_news
1024 user = User.generate!
1023 user = User.generate!
1025 news = News.new
1024 news = News.new
1026
1025
1027 User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option|
1026 User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option|
1028 user.mail_notification = option
1027 user.mail_notification = option
1029 assert_equal (option != 'none'), user.notify_about?(news)
1028 assert_equal (option != 'none'), user.notify_about?(news)
1030 end
1029 end
1031 end
1030 end
1032
1031
1033 def test_salt_unsalted_passwords
1032 def test_salt_unsalted_passwords
1034 # Restore a user with an unsalted password
1033 # Restore a user with an unsalted password
1035 user = User.find(1)
1034 user = User.find(1)
1036 user.salt = nil
1035 user.salt = nil
1037 user.hashed_password = User.hash_password("unsalted")
1036 user.hashed_password = User.hash_password("unsalted")
1038 user.save!
1037 user.save!
1039
1038
1040 User.salt_unsalted_passwords!
1039 User.salt_unsalted_passwords!
1041
1040
1042 user.reload
1041 user.reload
1043 # Salt added
1042 # Salt added
1044 assert !user.salt.blank?
1043 assert !user.salt.blank?
1045 # Password still valid
1044 # Password still valid
1046 assert user.check_password?("unsalted")
1045 assert user.check_password?("unsalted")
1047 assert_equal user, User.try_to_login(user.login, "unsalted")
1046 assert_equal user, User.try_to_login(user.login, "unsalted")
1048 end
1047 end
1049
1048
1050 if Object.const_defined?(:OpenID)
1049 if Object.const_defined?(:OpenID)
1051
1050
1052 def test_setting_identity_url
1051 def test_setting_identity_url
1053 normalized_open_id_url = 'http://example.com/'
1052 normalized_open_id_url = 'http://example.com/'
1054 u = User.new( :identity_url => 'http://example.com/' )
1053 u = User.new( :identity_url => 'http://example.com/' )
1055 assert_equal normalized_open_id_url, u.identity_url
1054 assert_equal normalized_open_id_url, u.identity_url
1056 end
1055 end
1057
1056
1058 def test_setting_identity_url_without_trailing_slash
1057 def test_setting_identity_url_without_trailing_slash
1059 normalized_open_id_url = 'http://example.com/'
1058 normalized_open_id_url = 'http://example.com/'
1060 u = User.new( :identity_url => 'http://example.com' )
1059 u = User.new( :identity_url => 'http://example.com' )
1061 assert_equal normalized_open_id_url, u.identity_url
1060 assert_equal normalized_open_id_url, u.identity_url
1062 end
1061 end
1063
1062
1064 def test_setting_identity_url_without_protocol
1063 def test_setting_identity_url_without_protocol
1065 normalized_open_id_url = 'http://example.com/'
1064 normalized_open_id_url = 'http://example.com/'
1066 u = User.new( :identity_url => 'example.com' )
1065 u = User.new( :identity_url => 'example.com' )
1067 assert_equal normalized_open_id_url, u.identity_url
1066 assert_equal normalized_open_id_url, u.identity_url
1068 end
1067 end
1069
1068
1070 def test_setting_blank_identity_url
1069 def test_setting_blank_identity_url
1071 u = User.new( :identity_url => 'example.com' )
1070 u = User.new( :identity_url => 'example.com' )
1072 u.identity_url = ''
1071 u.identity_url = ''
1073 assert u.identity_url.blank?
1072 assert u.identity_url.blank?
1074 end
1073 end
1075
1074
1076 def test_setting_invalid_identity_url
1075 def test_setting_invalid_identity_url
1077 u = User.new( :identity_url => 'this is not an openid url' )
1076 u = User.new( :identity_url => 'this is not an openid url' )
1078 assert u.identity_url.blank?
1077 assert u.identity_url.blank?
1079 end
1078 end
1080
1079
1081 else
1080 else
1082 puts "Skipping openid tests."
1081 puts "Skipping openid tests."
1083 end
1082 end
1084
1083
1085 end
1084 end
General Comments 0
You need to be logged in to leave comments. Login now