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