@@ -1,1135 +1,1152 | |||||
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 |
|
29 | |||
30 | def setup |
|
30 | def setup | |
31 | @admin = User.find(1) |
|
31 | @admin = User.find(1) | |
32 | @jsmith = User.find(2) |
|
32 | @jsmith = User.find(2) | |
33 | @dlopper = User.find(3) |
|
33 | @dlopper = User.find(3) | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | def test_sorted_scope_should_sort_user_by_display_name |
|
36 | def test_sorted_scope_should_sort_user_by_display_name | |
37 |
assert_equal User.all.map(&:name).map(&:downcase).sort, |
|
37 | assert_equal User.all.map(&:name).map(&:downcase).sort, | |
|
38 | User.sorted.all.map(&:name).map(&:downcase) | |||
38 | end |
|
39 | end | |
39 |
|
40 | |||
40 | def test_generate |
|
41 | def test_generate | |
41 | User.generate!(:firstname => 'Testing connection') |
|
42 | User.generate!(:firstname => 'Testing connection') | |
42 | User.generate!(:firstname => 'Testing connection') |
|
43 | User.generate!(:firstname => 'Testing connection') | |
43 | assert_equal 2, User.where(:firstname => 'Testing connection').count |
|
44 | assert_equal 2, User.where(:firstname => 'Testing connection').count | |
44 | end |
|
45 | end | |
45 |
|
46 | |||
46 | def test_truth |
|
47 | def test_truth | |
47 | assert_kind_of User, @jsmith |
|
48 | assert_kind_of User, @jsmith | |
48 | end |
|
49 | end | |
49 |
|
50 | |||
50 | def test_mail_should_be_stripped |
|
51 | def test_mail_should_be_stripped | |
51 | u = User.new |
|
52 | u = User.new | |
52 | u.mail = " foo@bar.com " |
|
53 | u.mail = " foo@bar.com " | |
53 | assert_equal "foo@bar.com", u.mail |
|
54 | assert_equal "foo@bar.com", u.mail | |
54 | end |
|
55 | end | |
55 |
|
56 | |||
56 | def test_mail_validation |
|
57 | def test_mail_validation | |
57 | u = User.new |
|
58 | u = User.new | |
58 | u.mail = '' |
|
59 | u.mail = '' | |
59 | assert !u.valid? |
|
60 | assert !u.valid? | |
60 | assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail] |
|
61 | assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail] | |
61 | end |
|
62 | end | |
62 |
|
63 | |||
63 | def test_login_length_validation |
|
64 | def test_login_length_validation | |
64 | user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
|
65 | user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | |
65 | user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1) |
|
66 | user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1) | |
66 | assert !user.valid? |
|
67 | assert !user.valid? | |
67 |
|
68 | |||
68 | user.login = "x" * (User::LOGIN_LENGTH_LIMIT) |
|
69 | user.login = "x" * (User::LOGIN_LENGTH_LIMIT) | |
69 | assert user.valid? |
|
70 | assert user.valid? | |
70 | assert user.save |
|
71 | assert user.save | |
71 | end |
|
72 | end | |
72 |
|
73 | |||
73 | def test_generate_password_should_respect_minimum_password_length |
|
74 | def test_generate_password_should_respect_minimum_password_length | |
74 | with_settings :password_min_length => 15 do |
|
75 | with_settings :password_min_length => 15 do | |
75 | user = User.generate!(:generate_password => true) |
|
76 | user = User.generate!(:generate_password => true) | |
76 | assert user.password.length >= 15 |
|
77 | assert user.password.length >= 15 | |
77 | end |
|
78 | end | |
78 | end |
|
79 | end | |
79 |
|
80 | |||
80 | def test_generate_password_should_not_generate_password_with_less_than_10_characters |
|
81 | def test_generate_password_should_not_generate_password_with_less_than_10_characters | |
81 | with_settings :password_min_length => 4 do |
|
82 | with_settings :password_min_length => 4 do | |
82 | user = User.generate!(:generate_password => true) |
|
83 | user = User.generate!(:generate_password => true) | |
83 | assert user.password.length >= 10 |
|
84 | assert user.password.length >= 10 | |
84 | end |
|
85 | end | |
85 | end |
|
86 | end | |
86 |
|
87 | |||
87 | def test_generate_password_on_create_should_set_password |
|
88 | def test_generate_password_on_create_should_set_password | |
88 | user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
|
89 | user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | |
89 | user.login = "newuser" |
|
90 | user.login = "newuser" | |
90 | user.generate_password = true |
|
91 | user.generate_password = true | |
91 | assert user.save |
|
92 | assert user.save | |
92 |
|
93 | |||
93 | password = user.password |
|
94 | password = user.password | |
94 | assert user.check_password?(password) |
|
95 | assert user.check_password?(password) | |
95 | end |
|
96 | end | |
96 |
|
97 | |||
97 | def test_generate_password_on_update_should_update_password |
|
98 | def test_generate_password_on_update_should_update_password | |
98 | user = User.find(2) |
|
99 | user = User.find(2) | |
99 | hash = user.hashed_password |
|
100 | hash = user.hashed_password | |
100 | user.generate_password = true |
|
101 | user.generate_password = true | |
101 | assert user.save |
|
102 | assert user.save | |
102 |
|
103 | |||
103 | password = user.password |
|
104 | password = user.password | |
104 | assert user.check_password?(password) |
|
105 | assert user.check_password?(password) | |
105 | assert_not_equal hash, user.reload.hashed_password |
|
106 | assert_not_equal hash, user.reload.hashed_password | |
106 | end |
|
107 | end | |
107 |
|
108 | |||
108 | def test_create |
|
109 | def test_create | |
109 | user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
|
110 | user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | |
110 |
|
111 | |||
111 | user.login = "jsmith" |
|
112 | user.login = "jsmith" | |
112 | user.password, user.password_confirmation = "password", "password" |
|
113 | user.password, user.password_confirmation = "password", "password" | |
113 | # login uniqueness |
|
114 | # login uniqueness | |
114 | assert !user.save |
|
115 | assert !user.save | |
115 | assert_equal 1, user.errors.count |
|
116 | assert_equal 1, user.errors.count | |
116 |
|
117 | |||
117 | user.login = "newuser" |
|
118 | user.login = "newuser" | |
118 | user.password, user.password_confirmation = "password", "pass" |
|
119 | user.password, user.password_confirmation = "password", "pass" | |
119 | # password confirmation |
|
120 | # password confirmation | |
120 | assert !user.save |
|
121 | assert !user.save | |
121 | assert_equal 1, user.errors.count |
|
122 | assert_equal 1, user.errors.count | |
122 |
|
123 | |||
123 | user.password, user.password_confirmation = "password", "password" |
|
124 | user.password, user.password_confirmation = "password", "password" | |
124 | assert user.save |
|
125 | assert user.save | |
125 | end |
|
126 | end | |
126 |
|
127 | |||
127 | def test_user_before_create_should_set_the_mail_notification_to_the_default_setting |
|
128 | def test_user_before_create_should_set_the_mail_notification_to_the_default_setting | |
128 | @user1 = User.generate! |
|
129 | @user1 = User.generate! | |
129 | assert_equal 'only_my_events', @user1.mail_notification |
|
130 | assert_equal 'only_my_events', @user1.mail_notification | |
130 | with_settings :default_notification_option => 'all' do |
|
131 | with_settings :default_notification_option => 'all' do | |
131 | @user2 = User.generate! |
|
132 | @user2 = User.generate! | |
132 | assert_equal 'all', @user2.mail_notification |
|
133 | assert_equal 'all', @user2.mail_notification | |
133 | end |
|
134 | end | |
134 | end |
|
135 | end | |
135 |
|
136 | |||
136 | def test_user_login_should_be_case_insensitive |
|
137 | def test_user_login_should_be_case_insensitive | |
137 | u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
|
138 | u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | |
138 | u.login = 'newuser' |
|
139 | u.login = 'newuser' | |
139 | u.password, u.password_confirmation = "password", "password" |
|
140 | u.password, u.password_confirmation = "password", "password" | |
140 | assert u.save |
|
141 | assert u.save | |
141 |
u = User.new(:firstname => "Similar", :lastname => "User", |
|
142 | u = User.new(:firstname => "Similar", :lastname => "User", | |
|
143 | :mail => "similaruser@somenet.foo") | |||
142 | u.login = 'NewUser' |
|
144 | u.login = 'NewUser' | |
143 | u.password, u.password_confirmation = "password", "password" |
|
145 | u.password, u.password_confirmation = "password", "password" | |
144 | assert !u.save |
|
146 | assert !u.save | |
145 | assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login] |
|
147 | assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login] | |
146 | end |
|
148 | end | |
147 |
|
149 | |||
148 | def test_mail_uniqueness_should_not_be_case_sensitive |
|
150 | def test_mail_uniqueness_should_not_be_case_sensitive | |
149 | u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
|
151 | u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | |
150 | u.login = 'newuser1' |
|
152 | u.login = 'newuser1' | |
151 | u.password, u.password_confirmation = "password", "password" |
|
153 | u.password, u.password_confirmation = "password", "password" | |
152 | assert u.save |
|
154 | assert u.save | |
153 |
|
155 | |||
154 | u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo") |
|
156 | u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo") | |
155 | u.login = 'newuser2' |
|
157 | u.login = 'newuser2' | |
156 | u.password, u.password_confirmation = "password", "password" |
|
158 | u.password, u.password_confirmation = "password", "password" | |
157 | assert !u.save |
|
159 | assert !u.save | |
158 | assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail] |
|
160 | assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail] | |
159 | end |
|
161 | end | |
160 |
|
162 | |||
161 | def test_update |
|
163 | def test_update | |
162 | assert_equal "admin", @admin.login |
|
164 | assert_equal "admin", @admin.login | |
163 | @admin.login = "john" |
|
165 | @admin.login = "john" | |
164 | assert @admin.save, @admin.errors.full_messages.join("; ") |
|
166 | assert @admin.save, @admin.errors.full_messages.join("; ") | |
165 | @admin.reload |
|
167 | @admin.reload | |
166 | assert_equal "john", @admin.login |
|
168 | assert_equal "john", @admin.login | |
167 | end |
|
169 | end | |
168 |
|
170 | |||
169 | def test_update_should_not_fail_for_legacy_user_with_different_case_logins |
|
171 | def test_update_should_not_fail_for_legacy_user_with_different_case_logins | |
170 | u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo") |
|
172 | u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo") | |
171 | u1.login = 'newuser1' |
|
173 | u1.login = 'newuser1' | |
172 | assert u1.save |
|
174 | assert u1.save | |
173 |
|
175 | |||
174 | u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo") |
|
176 | u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo") | |
175 | u2.login = 'newuser1' |
|
177 | u2.login = 'newuser1' | |
176 | assert u2.save(:validate => false) |
|
178 | assert u2.save(:validate => false) | |
177 |
|
179 | |||
178 | user = User.find(u2.id) |
|
180 | user = User.find(u2.id) | |
179 | user.firstname = "firstname" |
|
181 | user.firstname = "firstname" | |
180 | assert user.save, "Save failed" |
|
182 | assert user.save, "Save failed" | |
181 | end |
|
183 | end | |
182 |
|
184 | |||
183 | def test_destroy_should_delete_members_and_roles |
|
185 | def test_destroy_should_delete_members_and_roles | |
184 | members = Member.find_all_by_user_id(2) |
|
186 | members = Member.find_all_by_user_id(2) | |
185 | ms = members.size |
|
187 | ms = members.size | |
186 | rs = members.collect(&:roles).flatten.size |
|
188 | rs = members.collect(&:roles).flatten.size | |
187 |
|
189 | |||
188 | assert_difference 'Member.count', - ms do |
|
190 | assert_difference 'Member.count', - ms do | |
189 | assert_difference 'MemberRole.count', - rs do |
|
191 | assert_difference 'MemberRole.count', - rs do | |
190 | User.find(2).destroy |
|
192 | User.find(2).destroy | |
191 | end |
|
193 | end | |
192 | end |
|
194 | end | |
193 |
|
195 | |||
194 | assert_nil User.find_by_id(2) |
|
196 | assert_nil User.find_by_id(2) | |
195 | assert Member.find_all_by_user_id(2).empty? |
|
197 | assert Member.find_all_by_user_id(2).empty? | |
196 | end |
|
198 | end | |
197 |
|
199 | |||
198 | def test_destroy_should_update_attachments |
|
200 | def test_destroy_should_update_attachments | |
199 | attachment = Attachment.create!(:container => Project.find(1), |
|
201 | attachment = Attachment.create!(:container => Project.find(1), | |
200 | :file => uploaded_test_file("testfile.txt", "text/plain"), |
|
202 | :file => uploaded_test_file("testfile.txt", "text/plain"), | |
201 | :author_id => 2) |
|
203 | :author_id => 2) | |
202 |
|
204 | |||
203 | User.find(2).destroy |
|
205 | User.find(2).destroy | |
204 | assert_nil User.find_by_id(2) |
|
206 | assert_nil User.find_by_id(2) | |
205 | assert_equal User.anonymous, attachment.reload.author |
|
207 | assert_equal User.anonymous, attachment.reload.author | |
206 | end |
|
208 | end | |
207 |
|
209 | |||
208 | def test_destroy_should_update_comments |
|
210 | def test_destroy_should_update_comments | |
209 | comment = Comment.create!( |
|
211 | comment = Comment.create!( | |
210 |
:commented => News.create!(:project_id => 1, |
|
212 | :commented => News.create!(:project_id => 1, | |
|
213 | :author_id => 1, :title => 'foo', :description => 'foo'), | |||
211 | :author => User.find(2), |
|
214 | :author => User.find(2), | |
212 | :comments => 'foo' |
|
215 | :comments => 'foo' | |
213 | ) |
|
216 | ) | |
214 |
|
217 | |||
215 | User.find(2).destroy |
|
218 | User.find(2).destroy | |
216 | assert_nil User.find_by_id(2) |
|
219 | assert_nil User.find_by_id(2) | |
217 | assert_equal User.anonymous, comment.reload.author |
|
220 | assert_equal User.anonymous, comment.reload.author | |
218 | end |
|
221 | end | |
219 |
|
222 | |||
220 | def test_destroy_should_update_issues |
|
223 | def test_destroy_should_update_issues | |
221 |
issue = Issue.create!(:project_id => 1, :author_id => 2, |
|
224 | issue = Issue.create!(:project_id => 1, :author_id => 2, | |
|
225 | :tracker_id => 1, :subject => 'foo') | |||
222 |
|
226 | |||
223 | User.find(2).destroy |
|
227 | User.find(2).destroy | |
224 | assert_nil User.find_by_id(2) |
|
228 | assert_nil User.find_by_id(2) | |
225 | assert_equal User.anonymous, issue.reload.author |
|
229 | assert_equal User.anonymous, issue.reload.author | |
226 | end |
|
230 | end | |
227 |
|
231 | |||
228 | def test_destroy_should_unassign_issues |
|
232 | def test_destroy_should_unassign_issues | |
229 |
issue = Issue.create!(:project_id => 1, :author_id => 1, |
|
233 | issue = Issue.create!(:project_id => 1, :author_id => 1, | |
|
234 | :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2) | |||
230 |
|
235 | |||
231 | User.find(2).destroy |
|
236 | User.find(2).destroy | |
232 | assert_nil User.find_by_id(2) |
|
237 | assert_nil User.find_by_id(2) | |
233 | assert_nil issue.reload.assigned_to |
|
238 | assert_nil issue.reload.assigned_to | |
234 | end |
|
239 | end | |
235 |
|
240 | |||
236 | def test_destroy_should_update_journals |
|
241 | def test_destroy_should_update_journals | |
237 |
issue = Issue.create!(:project_id => 1, :author_id => 2, |
|
242 | issue = Issue.create!(:project_id => 1, :author_id => 2, | |
|
243 | :tracker_id => 1, :subject => 'foo') | |||
238 | issue.init_journal(User.find(2), "update") |
|
244 | issue.init_journal(User.find(2), "update") | |
239 | issue.save! |
|
245 | issue.save! | |
240 |
|
246 | |||
241 | User.find(2).destroy |
|
247 | User.find(2).destroy | |
242 | assert_nil User.find_by_id(2) |
|
248 | assert_nil User.find_by_id(2) | |
243 | assert_equal User.anonymous, issue.journals.first.reload.user |
|
249 | assert_equal User.anonymous, issue.journals.first.reload.user | |
244 | end |
|
250 | end | |
245 |
|
251 | |||
246 | def test_destroy_should_update_journal_details_old_value |
|
252 | def test_destroy_should_update_journal_details_old_value | |
247 |
issue = Issue.create!(:project_id => 1, :author_id => 1, |
|
253 | issue = Issue.create!(:project_id => 1, :author_id => 1, | |
|
254 | :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2) | |||
248 | issue.init_journal(User.find(1), "update") |
|
255 | issue.init_journal(User.find(1), "update") | |
249 | issue.assigned_to_id = nil |
|
256 | issue.assigned_to_id = nil | |
250 | assert_difference 'JournalDetail.count' do |
|
257 | assert_difference 'JournalDetail.count' do | |
251 | issue.save! |
|
258 | issue.save! | |
252 | end |
|
259 | end | |
253 | journal_detail = JournalDetail.first(:order => 'id DESC') |
|
260 | journal_detail = JournalDetail.first(:order => 'id DESC') | |
254 | assert_equal '2', journal_detail.old_value |
|
261 | assert_equal '2', journal_detail.old_value | |
255 |
|
262 | |||
256 | User.find(2).destroy |
|
263 | User.find(2).destroy | |
257 | assert_nil User.find_by_id(2) |
|
264 | assert_nil User.find_by_id(2) | |
258 | assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value |
|
265 | assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value | |
259 | end |
|
266 | end | |
260 |
|
267 | |||
261 | def test_destroy_should_update_journal_details_value |
|
268 | def test_destroy_should_update_journal_details_value | |
262 |
issue = Issue.create!(:project_id => 1, :author_id => 1, |
|
269 | issue = Issue.create!(:project_id => 1, :author_id => 1, | |
|
270 | :tracker_id => 1, :subject => 'foo') | |||
263 | issue.init_journal(User.find(1), "update") |
|
271 | issue.init_journal(User.find(1), "update") | |
264 | issue.assigned_to_id = 2 |
|
272 | issue.assigned_to_id = 2 | |
265 | assert_difference 'JournalDetail.count' do |
|
273 | assert_difference 'JournalDetail.count' do | |
266 | issue.save! |
|
274 | issue.save! | |
267 | end |
|
275 | end | |
268 | journal_detail = JournalDetail.first(:order => 'id DESC') |
|
276 | journal_detail = JournalDetail.first(:order => 'id DESC') | |
269 | assert_equal '2', journal_detail.value |
|
277 | assert_equal '2', journal_detail.value | |
270 |
|
278 | |||
271 | User.find(2).destroy |
|
279 | User.find(2).destroy | |
272 | assert_nil User.find_by_id(2) |
|
280 | assert_nil User.find_by_id(2) | |
273 | assert_equal User.anonymous.id.to_s, journal_detail.reload.value |
|
281 | assert_equal User.anonymous.id.to_s, journal_detail.reload.value | |
274 | end |
|
282 | end | |
275 |
|
283 | |||
276 | def test_destroy_should_update_messages |
|
284 | def test_destroy_should_update_messages | |
277 | board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board') |
|
285 | board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board') | |
278 |
message = Message.create!(:board_id => board.id, :author_id => 2, |
|
286 | message = Message.create!(:board_id => board.id, :author_id => 2, | |
279 |
|
287 | :subject => 'foo', :content => 'foo') | ||
280 | User.find(2).destroy |
|
288 | User.find(2).destroy | |
281 | assert_nil User.find_by_id(2) |
|
289 | assert_nil User.find_by_id(2) | |
282 | assert_equal User.anonymous, message.reload.author |
|
290 | assert_equal User.anonymous, message.reload.author | |
283 | end |
|
291 | end | |
284 |
|
292 | |||
285 | def test_destroy_should_update_news |
|
293 | def test_destroy_should_update_news | |
286 |
news = News.create!(:project_id => 1, :author_id => 2, |
|
294 | news = News.create!(:project_id => 1, :author_id => 2, | |
287 |
|
295 | :title => 'foo', :description => 'foo') | ||
288 | User.find(2).destroy |
|
296 | User.find(2).destroy | |
289 | assert_nil User.find_by_id(2) |
|
297 | assert_nil User.find_by_id(2) | |
290 | assert_equal User.anonymous, news.reload.author |
|
298 | assert_equal User.anonymous, news.reload.author | |
291 | end |
|
299 | end | |
292 |
|
300 | |||
293 | def test_destroy_should_delete_private_queries |
|
301 | def test_destroy_should_delete_private_queries | |
294 | query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PRIVATE) |
|
302 | query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PRIVATE) | |
295 | query.project_id = 1 |
|
303 | query.project_id = 1 | |
296 | query.user_id = 2 |
|
304 | query.user_id = 2 | |
297 | query.save! |
|
305 | query.save! | |
298 |
|
306 | |||
299 | User.find(2).destroy |
|
307 | User.find(2).destroy | |
300 | assert_nil User.find_by_id(2) |
|
308 | assert_nil User.find_by_id(2) | |
301 | assert_nil Query.find_by_id(query.id) |
|
309 | assert_nil Query.find_by_id(query.id) | |
302 | end |
|
310 | end | |
303 |
|
311 | |||
304 | def test_destroy_should_update_public_queries |
|
312 | def test_destroy_should_update_public_queries | |
305 | query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PUBLIC) |
|
313 | query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PUBLIC) | |
306 | query.project_id = 1 |
|
314 | query.project_id = 1 | |
307 | query.user_id = 2 |
|
315 | query.user_id = 2 | |
308 | query.save! |
|
316 | query.save! | |
309 |
|
317 | |||
310 | User.find(2).destroy |
|
318 | User.find(2).destroy | |
311 | assert_nil User.find_by_id(2) |
|
319 | assert_nil User.find_by_id(2) | |
312 | assert_equal User.anonymous, query.reload.user |
|
320 | assert_equal User.anonymous, query.reload.user | |
313 | end |
|
321 | end | |
314 |
|
322 | |||
315 | def test_destroy_should_update_time_entries |
|
323 | def test_destroy_should_update_time_entries | |
316 |
entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, |
|
324 | entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, | |
|
325 | :activity => TimeEntryActivity.create!(:name => 'foo')) | |||
317 | entry.project_id = 1 |
|
326 | entry.project_id = 1 | |
318 | entry.user_id = 2 |
|
327 | entry.user_id = 2 | |
319 | entry.save! |
|
328 | entry.save! | |
320 |
|
329 | |||
321 | User.find(2).destroy |
|
330 | User.find(2).destroy | |
322 | assert_nil User.find_by_id(2) |
|
331 | assert_nil User.find_by_id(2) | |
323 | assert_equal User.anonymous, entry.reload.user |
|
332 | assert_equal User.anonymous, entry.reload.user | |
324 | end |
|
333 | end | |
325 |
|
334 | |||
326 | def test_destroy_should_delete_tokens |
|
335 | def test_destroy_should_delete_tokens | |
327 | token = Token.create!(:user_id => 2, :value => 'foo') |
|
336 | token = Token.create!(:user_id => 2, :value => 'foo') | |
328 |
|
337 | |||
329 | User.find(2).destroy |
|
338 | User.find(2).destroy | |
330 | assert_nil User.find_by_id(2) |
|
339 | assert_nil User.find_by_id(2) | |
331 | assert_nil Token.find_by_id(token.id) |
|
340 | assert_nil Token.find_by_id(token.id) | |
332 | end |
|
341 | end | |
333 |
|
342 | |||
334 | def test_destroy_should_delete_watchers |
|
343 | def test_destroy_should_delete_watchers | |
335 |
issue = Issue.create!(:project_id => 1, :author_id => 1, |
|
344 | issue = Issue.create!(:project_id => 1, :author_id => 1, | |
|
345 | :tracker_id => 1, :subject => 'foo') | |||
336 | watcher = Watcher.create!(:user_id => 2, :watchable => issue) |
|
346 | watcher = Watcher.create!(:user_id => 2, :watchable => issue) | |
337 |
|
347 | |||
338 | User.find(2).destroy |
|
348 | User.find(2).destroy | |
339 | assert_nil User.find_by_id(2) |
|
349 | assert_nil User.find_by_id(2) | |
340 | assert_nil Watcher.find_by_id(watcher.id) |
|
350 | assert_nil Watcher.find_by_id(watcher.id) | |
341 | end |
|
351 | end | |
342 |
|
352 | |||
343 | def test_destroy_should_update_wiki_contents |
|
353 | def test_destroy_should_update_wiki_contents | |
344 | wiki_content = WikiContent.create!( |
|
354 | wiki_content = WikiContent.create!( | |
345 | :text => 'foo', |
|
355 | :text => 'foo', | |
346 | :author_id => 2, |
|
356 | :author_id => 2, | |
347 | :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 3, :start_page => 'Start')) |
|
357 | :page => WikiPage.create!(:title => 'Foo', | |
|
358 | :wiki => Wiki.create!(:project_id => 3, | |||
|
359 | :start_page => 'Start')) | |||
348 | ) |
|
360 | ) | |
349 | wiki_content.text = 'bar' |
|
361 | wiki_content.text = 'bar' | |
350 | assert_difference 'WikiContent::Version.count' do |
|
362 | assert_difference 'WikiContent::Version.count' do | |
351 | wiki_content.save! |
|
363 | wiki_content.save! | |
352 | end |
|
364 | end | |
353 |
|
365 | |||
354 | User.find(2).destroy |
|
366 | User.find(2).destroy | |
355 | assert_nil User.find_by_id(2) |
|
367 | assert_nil User.find_by_id(2) | |
356 | assert_equal User.anonymous, wiki_content.reload.author |
|
368 | assert_equal User.anonymous, wiki_content.reload.author | |
357 | wiki_content.versions.each do |version| |
|
369 | wiki_content.versions.each do |version| | |
358 | assert_equal User.anonymous, version.reload.author |
|
370 | assert_equal User.anonymous, version.reload.author | |
359 | end |
|
371 | end | |
360 | end |
|
372 | end | |
361 |
|
373 | |||
362 | def test_destroy_should_nullify_issue_categories |
|
374 | def test_destroy_should_nullify_issue_categories | |
363 | category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo') |
|
375 | category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo') | |
364 |
|
376 | |||
365 | User.find(2).destroy |
|
377 | User.find(2).destroy | |
366 | assert_nil User.find_by_id(2) |
|
378 | assert_nil User.find_by_id(2) | |
367 | assert_nil category.reload.assigned_to_id |
|
379 | assert_nil category.reload.assigned_to_id | |
368 | end |
|
380 | end | |
369 |
|
381 | |||
370 | def test_destroy_should_nullify_changesets |
|
382 | def test_destroy_should_nullify_changesets | |
371 | changeset = Changeset.create!( |
|
383 | changeset = Changeset.create!( | |
372 | :repository => Repository::Subversion.create!( |
|
384 | :repository => Repository::Subversion.create!( | |
373 | :project_id => 1, |
|
385 | :project_id => 1, | |
374 | :url => 'file:///tmp', |
|
386 | :url => 'file:///tmp', | |
375 | :identifier => 'tmp' |
|
387 | :identifier => 'tmp' | |
376 | ), |
|
388 | ), | |
377 | :revision => '12', |
|
389 | :revision => '12', | |
378 | :committed_on => Time.now, |
|
390 | :committed_on => Time.now, | |
379 | :committer => 'jsmith' |
|
391 | :committer => 'jsmith' | |
380 | ) |
|
392 | ) | |
381 | assert_equal 2, changeset.user_id |
|
393 | assert_equal 2, changeset.user_id | |
382 |
|
394 | |||
383 | User.find(2).destroy |
|
395 | User.find(2).destroy | |
384 | assert_nil User.find_by_id(2) |
|
396 | assert_nil User.find_by_id(2) | |
385 | assert_nil changeset.reload.user_id |
|
397 | assert_nil changeset.reload.user_id | |
386 | end |
|
398 | end | |
387 |
|
399 | |||
388 | def test_anonymous_user_should_not_be_destroyable |
|
400 | def test_anonymous_user_should_not_be_destroyable | |
389 | assert_no_difference 'User.count' do |
|
401 | assert_no_difference 'User.count' do | |
390 | assert_equal false, User.anonymous.destroy |
|
402 | assert_equal false, User.anonymous.destroy | |
391 | end |
|
403 | end | |
392 | end |
|
404 | end | |
393 |
|
405 | |||
394 | def test_validate_login_presence |
|
406 | def test_validate_login_presence | |
395 | @admin.login = "" |
|
407 | @admin.login = "" | |
396 | assert !@admin.save |
|
408 | assert !@admin.save | |
397 | assert_equal 1, @admin.errors.count |
|
409 | assert_equal 1, @admin.errors.count | |
398 | end |
|
410 | end | |
399 |
|
411 | |||
400 | def test_validate_mail_notification_inclusion |
|
412 | def test_validate_mail_notification_inclusion | |
401 | u = User.new |
|
413 | u = User.new | |
402 | u.mail_notification = 'foo' |
|
414 | u.mail_notification = 'foo' | |
403 | u.save |
|
415 | u.save | |
404 | assert_not_equal [], u.errors[:mail_notification] |
|
416 | assert_not_equal [], u.errors[:mail_notification] | |
405 | end |
|
417 | end | |
406 |
|
418 | |||
407 | def test_password |
|
419 | def test_password | |
408 | user = User.try_to_login("admin", "admin") |
|
420 | user = User.try_to_login("admin", "admin") | |
409 | assert_kind_of User, user |
|
421 | assert_kind_of User, user | |
410 | assert_equal "admin", user.login |
|
422 | assert_equal "admin", user.login | |
411 | user.password = "hello123" |
|
423 | user.password = "hello123" | |
412 | assert user.save |
|
424 | assert user.save | |
413 |
|
425 | |||
414 | user = User.try_to_login("admin", "hello123") |
|
426 | user = User.try_to_login("admin", "hello123") | |
415 | assert_kind_of User, user |
|
427 | assert_kind_of User, user | |
416 | assert_equal "admin", user.login |
|
428 | assert_equal "admin", user.login | |
417 | end |
|
429 | end | |
418 |
|
430 | |||
419 | def test_validate_password_length |
|
431 | def test_validate_password_length | |
420 | with_settings :password_min_length => '100' do |
|
432 | with_settings :password_min_length => '100' do | |
421 |
user = User.new(:firstname => "new100", |
|
433 | user = User.new(:firstname => "new100", | |
|
434 | :lastname => "user100", :mail => "newuser100@somenet.foo") | |||
422 | user.login = "newuser100" |
|
435 | user.login = "newuser100" | |
423 | user.password, user.password_confirmation = "password100", "password100" |
|
436 | user.password, user.password_confirmation = "password100", "password100" | |
424 | assert !user.save |
|
437 | assert !user.save | |
425 | assert_equal 1, user.errors.count |
|
438 | assert_equal 1, user.errors.count | |
426 | end |
|
439 | end | |
427 | end |
|
440 | end | |
428 |
|
441 | |||
429 | def test_name_format |
|
442 | def test_name_format | |
430 | assert_equal 'John S.', @jsmith.name(:firstname_lastinitial) |
|
443 | assert_equal 'John S.', @jsmith.name(:firstname_lastinitial) | |
431 | assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname) |
|
444 | assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname) | |
432 | with_settings :user_format => :firstname_lastname do |
|
445 | with_settings :user_format => :firstname_lastname do | |
433 | assert_equal 'John Smith', @jsmith.reload.name |
|
446 | assert_equal 'John Smith', @jsmith.reload.name | |
434 | end |
|
447 | end | |
435 | with_settings :user_format => :username do |
|
448 | with_settings :user_format => :username do | |
436 | assert_equal 'jsmith', @jsmith.reload.name |
|
449 | assert_equal 'jsmith', @jsmith.reload.name | |
437 | end |
|
450 | end | |
438 | with_settings :user_format => :lastname do |
|
451 | with_settings :user_format => :lastname do | |
439 | assert_equal 'Smith', @jsmith.reload.name |
|
452 | assert_equal 'Smith', @jsmith.reload.name | |
440 | end |
|
453 | end | |
441 | end |
|
454 | end | |
442 |
|
455 | |||
443 | def test_today_should_return_the_day_according_to_user_time_zone |
|
456 | def test_today_should_return_the_day_according_to_user_time_zone | |
444 | preference = User.find(1).pref |
|
457 | preference = User.find(1).pref | |
445 | date = Date.new(2012, 05, 15) |
|
458 | date = Date.new(2012, 05, 15) | |
446 | time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC |
|
459 | time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC | |
447 | Date.stubs(:today).returns(date) |
|
460 | Date.stubs(:today).returns(date) | |
448 | Time.stubs(:now).returns(time) |
|
461 | Time.stubs(:now).returns(time) | |
449 |
|
462 | |||
450 | preference.update_attribute :time_zone, 'Baku' # UTC+4 |
|
463 | preference.update_attribute :time_zone, 'Baku' # UTC+4 | |
451 | assert_equal '2012-05-16', User.find(1).today.to_s |
|
464 | assert_equal '2012-05-16', User.find(1).today.to_s | |
452 |
|
465 | |||
453 | preference.update_attribute :time_zone, 'La Paz' # UTC-4 |
|
466 | preference.update_attribute :time_zone, 'La Paz' # UTC-4 | |
454 | assert_equal '2012-05-15', User.find(1).today.to_s |
|
467 | assert_equal '2012-05-15', User.find(1).today.to_s | |
455 |
|
468 | |||
456 | preference.update_attribute :time_zone, '' |
|
469 | preference.update_attribute :time_zone, '' | |
457 | assert_equal '2012-05-15', User.find(1).today.to_s |
|
470 | assert_equal '2012-05-15', User.find(1).today.to_s | |
458 | end |
|
471 | end | |
459 |
|
472 | |||
460 | def test_time_to_date_should_return_the_date_according_to_user_time_zone |
|
473 | def test_time_to_date_should_return_the_date_according_to_user_time_zone | |
461 | preference = User.find(1).pref |
|
474 | preference = User.find(1).pref | |
462 | time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC |
|
475 | time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC | |
463 |
|
476 | |||
464 | preference.update_attribute :time_zone, 'Baku' # UTC+4 |
|
477 | preference.update_attribute :time_zone, 'Baku' # UTC+4 | |
465 | assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s |
|
478 | assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s | |
466 |
|
479 | |||
467 | preference.update_attribute :time_zone, 'La Paz' # UTC-4 |
|
480 | preference.update_attribute :time_zone, 'La Paz' # UTC-4 | |
468 | assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s |
|
481 | assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s | |
469 |
|
482 | |||
470 | preference.update_attribute :time_zone, '' |
|
483 | preference.update_attribute :time_zone, '' | |
471 | assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s |
|
484 | assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s | |
472 | end |
|
485 | end | |
473 |
|
486 | |||
474 | def test_fields_for_order_statement_should_return_fields_according_user_format_setting |
|
487 | def test_fields_for_order_statement_should_return_fields_according_user_format_setting | |
475 | with_settings :user_format => 'lastname_coma_firstname' do |
|
488 | with_settings :user_format => 'lastname_coma_firstname' do | |
476 |
assert_equal ['users.lastname', 'users.firstname', 'users.id'], |
|
489 | assert_equal ['users.lastname', 'users.firstname', 'users.id'], | |
|
490 | User.fields_for_order_statement | |||
477 | end |
|
491 | end | |
478 | end |
|
492 | end | |
479 |
|
493 | |||
480 | def test_fields_for_order_statement_width_table_name_should_prepend_table_name |
|
494 | def test_fields_for_order_statement_width_table_name_should_prepend_table_name | |
481 | with_settings :user_format => 'lastname_firstname' do |
|
495 | with_settings :user_format => 'lastname_firstname' do | |
482 |
assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], |
|
496 | assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], | |
|
497 | User.fields_for_order_statement('authors') | |||
483 | end |
|
498 | end | |
484 | end |
|
499 | end | |
485 |
|
500 | |||
486 | def test_fields_for_order_statement_with_blank_format_should_return_default |
|
501 | def test_fields_for_order_statement_with_blank_format_should_return_default | |
487 | with_settings :user_format => '' do |
|
502 | with_settings :user_format => '' do | |
488 |
assert_equal ['users.firstname', 'users.lastname', 'users.id'], |
|
503 | assert_equal ['users.firstname', 'users.lastname', 'users.id'], | |
|
504 | User.fields_for_order_statement | |||
489 | end |
|
505 | end | |
490 | end |
|
506 | end | |
491 |
|
507 | |||
492 | def test_fields_for_order_statement_with_invalid_format_should_return_default |
|
508 | def test_fields_for_order_statement_with_invalid_format_should_return_default | |
493 | with_settings :user_format => 'foo' do |
|
509 | with_settings :user_format => 'foo' do | |
494 |
assert_equal ['users.firstname', 'users.lastname', 'users.id'], |
|
510 | assert_equal ['users.firstname', 'users.lastname', 'users.id'], | |
|
511 | User.fields_for_order_statement | |||
495 | end |
|
512 | end | |
496 | end |
|
513 | end | |
497 |
|
514 | |||
498 | test ".try_to_login with good credentials should return the user" do |
|
515 | test ".try_to_login with good credentials should return the user" do | |
499 | user = User.try_to_login("admin", "admin") |
|
516 | user = User.try_to_login("admin", "admin") | |
500 | assert_kind_of User, user |
|
517 | assert_kind_of User, user | |
501 | assert_equal "admin", user.login |
|
518 | assert_equal "admin", user.login | |
502 | end |
|
519 | end | |
503 |
|
520 | |||
504 | test ".try_to_login with wrong credentials should return nil" do |
|
521 | test ".try_to_login with wrong credentials should return nil" do | |
505 | assert_nil User.try_to_login("admin", "foo") |
|
522 | assert_nil User.try_to_login("admin", "foo") | |
506 | end |
|
523 | end | |
507 |
|
524 | |||
508 | def test_try_to_login_with_locked_user_should_return_nil |
|
525 | def test_try_to_login_with_locked_user_should_return_nil | |
509 | @jsmith.status = User::STATUS_LOCKED |
|
526 | @jsmith.status = User::STATUS_LOCKED | |
510 | @jsmith.save! |
|
527 | @jsmith.save! | |
511 |
|
528 | |||
512 | user = User.try_to_login("jsmith", "jsmith") |
|
529 | user = User.try_to_login("jsmith", "jsmith") | |
513 | assert_equal nil, user |
|
530 | assert_equal nil, user | |
514 | end |
|
531 | end | |
515 |
|
532 | |||
516 | def test_try_to_login_with_locked_user_and_not_active_only_should_return_user |
|
533 | def test_try_to_login_with_locked_user_and_not_active_only_should_return_user | |
517 | @jsmith.status = User::STATUS_LOCKED |
|
534 | @jsmith.status = User::STATUS_LOCKED | |
518 | @jsmith.save! |
|
535 | @jsmith.save! | |
519 |
|
536 | |||
520 | user = User.try_to_login("jsmith", "jsmith", false) |
|
537 | user = User.try_to_login("jsmith", "jsmith", false) | |
521 | assert_equal @jsmith, user |
|
538 | assert_equal @jsmith, user | |
522 | end |
|
539 | end | |
523 |
|
540 | |||
524 | test ".try_to_login should fall-back to case-insensitive if user login is not found as-typed" do |
|
541 | test ".try_to_login should fall-back to case-insensitive if user login is not found as-typed" do | |
525 | user = User.try_to_login("AdMin", "admin") |
|
542 | user = User.try_to_login("AdMin", "admin") | |
526 | assert_kind_of User, user |
|
543 | assert_kind_of User, user | |
527 | assert_equal "admin", user.login |
|
544 | assert_equal "admin", user.login | |
528 | end |
|
545 | end | |
529 |
|
546 | |||
530 | test ".try_to_login should select the exact matching user first" do |
|
547 | test ".try_to_login should select the exact matching user first" do | |
531 | case_sensitive_user = User.generate! do |user| |
|
548 | case_sensitive_user = User.generate! do |user| | |
532 | user.password = "admin123" |
|
549 | user.password = "admin123" | |
533 | end |
|
550 | end | |
534 | # bypass validations to make it appear like existing data |
|
551 | # bypass validations to make it appear like existing data | |
535 | case_sensitive_user.update_attribute(:login, 'ADMIN') |
|
552 | case_sensitive_user.update_attribute(:login, 'ADMIN') | |
536 |
|
553 | |||
537 | user = User.try_to_login("ADMIN", "admin123") |
|
554 | user = User.try_to_login("ADMIN", "admin123") | |
538 | assert_kind_of User, user |
|
555 | assert_kind_of User, user | |
539 | assert_equal "ADMIN", user.login |
|
556 | assert_equal "ADMIN", user.login | |
540 | end |
|
557 | end | |
541 |
|
558 | |||
542 | if ldap_configured? |
|
559 | if ldap_configured? | |
543 | context "#try_to_login using LDAP" do |
|
560 | context "#try_to_login using LDAP" do | |
544 | context "with failed connection to the LDAP server" do |
|
561 | context "with failed connection to the LDAP server" do | |
545 | should "return nil" do |
|
562 | should "return nil" do | |
546 | @auth_source = AuthSourceLdap.find(1) |
|
563 | @auth_source = AuthSourceLdap.find(1) | |
547 | AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect') |
|
564 | AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect') | |
548 |
|
565 | |||
549 | assert_equal nil, User.try_to_login('edavis', 'wrong') |
|
566 | assert_equal nil, User.try_to_login('edavis', 'wrong') | |
550 | end |
|
567 | end | |
551 | end |
|
568 | end | |
552 |
|
569 | |||
553 | context "with an unsuccessful authentication" do |
|
570 | context "with an unsuccessful authentication" do | |
554 | should "return nil" do |
|
571 | should "return nil" do | |
555 | assert_equal nil, User.try_to_login('edavis', 'wrong') |
|
572 | assert_equal nil, User.try_to_login('edavis', 'wrong') | |
556 | end |
|
573 | end | |
557 | end |
|
574 | end | |
558 |
|
575 | |||
559 | context "binding with user's account" do |
|
576 | context "binding with user's account" do | |
560 | setup do |
|
577 | setup do | |
561 | @auth_source = AuthSourceLdap.find(1) |
|
578 | @auth_source = AuthSourceLdap.find(1) | |
562 | @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org" |
|
579 | @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org" | |
563 | @auth_source.account_password = '' |
|
580 | @auth_source.account_password = '' | |
564 | @auth_source.save! |
|
581 | @auth_source.save! | |
565 |
|
582 | |||
566 | @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1) |
|
583 | @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1) | |
567 | @ldap_user.login = 'example1' |
|
584 | @ldap_user.login = 'example1' | |
568 | @ldap_user.save! |
|
585 | @ldap_user.save! | |
569 | end |
|
586 | end | |
570 |
|
587 | |||
571 | context "with a successful authentication" do |
|
588 | context "with a successful authentication" do | |
572 | should "return the user" do |
|
589 | should "return the user" do | |
573 | assert_equal @ldap_user, User.try_to_login('example1', '123456') |
|
590 | assert_equal @ldap_user, User.try_to_login('example1', '123456') | |
574 | end |
|
591 | end | |
575 | end |
|
592 | end | |
576 |
|
593 | |||
577 | context "with an unsuccessful authentication" do |
|
594 | context "with an unsuccessful authentication" do | |
578 | should "return nil" do |
|
595 | should "return nil" do | |
579 | assert_nil User.try_to_login('example1', '11111') |
|
596 | assert_nil User.try_to_login('example1', '11111') | |
580 | end |
|
597 | end | |
581 | end |
|
598 | end | |
582 | end |
|
599 | end | |
583 |
|
600 | |||
584 | context "on the fly registration" do |
|
601 | context "on the fly registration" do | |
585 | setup do |
|
602 | setup do | |
586 | @auth_source = AuthSourceLdap.find(1) |
|
603 | @auth_source = AuthSourceLdap.find(1) | |
587 | @auth_source.update_attribute :onthefly_register, true |
|
604 | @auth_source.update_attribute :onthefly_register, true | |
588 | end |
|
605 | end | |
589 |
|
606 | |||
590 | context "with a successful authentication" do |
|
607 | context "with a successful authentication" do | |
591 | should "create a new user account if it doesn't exist" do |
|
608 | should "create a new user account if it doesn't exist" do | |
592 | assert_difference('User.count') do |
|
609 | assert_difference('User.count') do | |
593 | user = User.try_to_login('edavis', '123456') |
|
610 | user = User.try_to_login('edavis', '123456') | |
594 | assert !user.admin? |
|
611 | assert !user.admin? | |
595 | end |
|
612 | end | |
596 | end |
|
613 | end | |
597 |
|
614 | |||
598 | should "retrieve existing user" do |
|
615 | should "retrieve existing user" do | |
599 | user = User.try_to_login('edavis', '123456') |
|
616 | user = User.try_to_login('edavis', '123456') | |
600 | user.admin = true |
|
617 | user.admin = true | |
601 | user.save! |
|
618 | user.save! | |
602 |
|
619 | |||
603 | assert_no_difference('User.count') do |
|
620 | assert_no_difference('User.count') do | |
604 | user = User.try_to_login('edavis', '123456') |
|
621 | user = User.try_to_login('edavis', '123456') | |
605 | assert user.admin? |
|
622 | assert user.admin? | |
606 | end |
|
623 | end | |
607 | end |
|
624 | end | |
608 | end |
|
625 | end | |
609 |
|
626 | |||
610 | context "binding with user's account" do |
|
627 | context "binding with user's account" do | |
611 | setup do |
|
628 | setup do | |
612 | @auth_source = AuthSourceLdap.find(1) |
|
629 | @auth_source = AuthSourceLdap.find(1) | |
613 | @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org" |
|
630 | @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org" | |
614 | @auth_source.account_password = '' |
|
631 | @auth_source.account_password = '' | |
615 | @auth_source.save! |
|
632 | @auth_source.save! | |
616 | end |
|
633 | end | |
617 |
|
634 | |||
618 | context "with a successful authentication" do |
|
635 | context "with a successful authentication" do | |
619 | should "create a new user account if it doesn't exist" do |
|
636 | should "create a new user account if it doesn't exist" do | |
620 | assert_difference('User.count') do |
|
637 | assert_difference('User.count') do | |
621 | user = User.try_to_login('example1', '123456') |
|
638 | user = User.try_to_login('example1', '123456') | |
622 | assert_kind_of User, user |
|
639 | assert_kind_of User, user | |
623 | end |
|
640 | end | |
624 | end |
|
641 | end | |
625 | end |
|
642 | end | |
626 |
|
643 | |||
627 | context "with an unsuccessful authentication" do |
|
644 | context "with an unsuccessful authentication" do | |
628 | should "return nil" do |
|
645 | should "return nil" do | |
629 | assert_nil User.try_to_login('example1', '11111') |
|
646 | assert_nil User.try_to_login('example1', '11111') | |
630 | end |
|
647 | end | |
631 | end |
|
648 | end | |
632 | end |
|
649 | end | |
633 | end |
|
650 | end | |
634 | end |
|
651 | end | |
635 |
|
652 | |||
636 | else |
|
653 | else | |
637 | puts "Skipping LDAP tests." |
|
654 | puts "Skipping LDAP tests." | |
638 | end |
|
655 | end | |
639 |
|
656 | |||
640 | def test_create_anonymous |
|
657 | def test_create_anonymous | |
641 | AnonymousUser.delete_all |
|
658 | AnonymousUser.delete_all | |
642 | anon = User.anonymous |
|
659 | anon = User.anonymous | |
643 | assert !anon.new_record? |
|
660 | assert !anon.new_record? | |
644 | assert_kind_of AnonymousUser, anon |
|
661 | assert_kind_of AnonymousUser, anon | |
645 | end |
|
662 | end | |
646 |
|
663 | |||
647 | def test_ensure_single_anonymous_user |
|
664 | def test_ensure_single_anonymous_user | |
648 | AnonymousUser.delete_all |
|
665 | AnonymousUser.delete_all | |
649 | anon1 = User.anonymous |
|
666 | anon1 = User.anonymous | |
650 | assert !anon1.new_record? |
|
667 | assert !anon1.new_record? | |
651 | assert_kind_of AnonymousUser, anon1 |
|
668 | assert_kind_of AnonymousUser, anon1 | |
652 | anon2 = AnonymousUser.create( |
|
669 | anon2 = AnonymousUser.create( | |
653 | :lastname => 'Anonymous', :firstname => '', |
|
670 | :lastname => 'Anonymous', :firstname => '', | |
654 | :mail => '', :login => '', :status => 0) |
|
671 | :mail => '', :login => '', :status => 0) | |
655 | assert_equal 1, anon2.errors.count |
|
672 | assert_equal 1, anon2.errors.count | |
656 | end |
|
673 | end | |
657 |
|
674 | |||
658 | def test_rss_key |
|
675 | def test_rss_key | |
659 | assert_nil @jsmith.rss_token |
|
676 | assert_nil @jsmith.rss_token | |
660 | key = @jsmith.rss_key |
|
677 | key = @jsmith.rss_key | |
661 | assert_equal 40, key.length |
|
678 | assert_equal 40, key.length | |
662 |
|
679 | |||
663 | @jsmith.reload |
|
680 | @jsmith.reload | |
664 | assert_equal key, @jsmith.rss_key |
|
681 | assert_equal key, @jsmith.rss_key | |
665 | end |
|
682 | end | |
666 |
|
683 | |||
667 | def test_rss_key_should_not_be_generated_twice |
|
684 | def test_rss_key_should_not_be_generated_twice | |
668 | assert_difference 'Token.count', 1 do |
|
685 | assert_difference 'Token.count', 1 do | |
669 | key1 = @jsmith.rss_key |
|
686 | key1 = @jsmith.rss_key | |
670 | key2 = @jsmith.rss_key |
|
687 | key2 = @jsmith.rss_key | |
671 | assert_equal key1, key2 |
|
688 | assert_equal key1, key2 | |
672 | end |
|
689 | end | |
673 | end |
|
690 | end | |
674 |
|
691 | |||
675 | def test_api_key_should_not_be_generated_twice |
|
692 | def test_api_key_should_not_be_generated_twice | |
676 | assert_difference 'Token.count', 1 do |
|
693 | assert_difference 'Token.count', 1 do | |
677 | key1 = @jsmith.api_key |
|
694 | key1 = @jsmith.api_key | |
678 | key2 = @jsmith.api_key |
|
695 | key2 = @jsmith.api_key | |
679 | assert_equal key1, key2 |
|
696 | assert_equal key1, key2 | |
680 | end |
|
697 | end | |
681 | end |
|
698 | end | |
682 |
|
699 | |||
683 | test "#api_key should generate a new one if the user doesn't have one" do |
|
700 | test "#api_key should generate a new one if the user doesn't have one" do | |
684 | user = User.generate!(:api_token => nil) |
|
701 | user = User.generate!(:api_token => nil) | |
685 | assert_nil user.api_token |
|
702 | assert_nil user.api_token | |
686 |
|
703 | |||
687 | key = user.api_key |
|
704 | key = user.api_key | |
688 | assert_equal 40, key.length |
|
705 | assert_equal 40, key.length | |
689 | user.reload |
|
706 | user.reload | |
690 | assert_equal key, user.api_key |
|
707 | assert_equal key, user.api_key | |
691 | end |
|
708 | end | |
692 |
|
709 | |||
693 | test "#api_key should return the existing api token value" do |
|
710 | test "#api_key should return the existing api token value" do | |
694 | user = User.generate! |
|
711 | user = User.generate! | |
695 | token = Token.create!(:action => 'api') |
|
712 | token = Token.create!(:action => 'api') | |
696 | user.api_token = token |
|
713 | user.api_token = token | |
697 | assert user.save |
|
714 | assert user.save | |
698 |
|
715 | |||
699 | assert_equal token.value, user.api_key |
|
716 | assert_equal token.value, user.api_key | |
700 | end |
|
717 | end | |
701 |
|
718 | |||
702 | test "#find_by_api_key should return nil if no matching key is found" do |
|
719 | test "#find_by_api_key should return nil if no matching key is found" do | |
703 | assert_nil User.find_by_api_key('zzzzzzzzz') |
|
720 | assert_nil User.find_by_api_key('zzzzzzzzz') | |
704 | end |
|
721 | end | |
705 |
|
722 | |||
706 | test "#find_by_api_key should return nil if the key is found for an inactive user" do |
|
723 | test "#find_by_api_key should return nil if the key is found for an inactive user" do | |
707 | user = User.generate! |
|
724 | user = User.generate! | |
708 | user.status = User::STATUS_LOCKED |
|
725 | user.status = User::STATUS_LOCKED | |
709 | token = Token.create!(:action => 'api') |
|
726 | token = Token.create!(:action => 'api') | |
710 | user.api_token = token |
|
727 | user.api_token = token | |
711 | user.save |
|
728 | user.save | |
712 |
|
729 | |||
713 | assert_nil User.find_by_api_key(token.value) |
|
730 | assert_nil User.find_by_api_key(token.value) | |
714 | end |
|
731 | end | |
715 |
|
732 | |||
716 | test "#find_by_api_key should return the user if the key is found for an active user" do |
|
733 | test "#find_by_api_key should return the user if the key is found for an active user" do | |
717 | user = User.generate! |
|
734 | user = User.generate! | |
718 | token = Token.create!(:action => 'api') |
|
735 | token = Token.create!(:action => 'api') | |
719 | user.api_token = token |
|
736 | user.api_token = token | |
720 | user.save |
|
737 | user.save | |
721 |
|
738 | |||
722 | assert_equal user, User.find_by_api_key(token.value) |
|
739 | assert_equal user, User.find_by_api_key(token.value) | |
723 | end |
|
740 | end | |
724 |
|
741 | |||
725 | def test_default_admin_account_changed_should_return_false_if_account_was_not_changed |
|
742 | def test_default_admin_account_changed_should_return_false_if_account_was_not_changed | |
726 | user = User.find_by_login("admin") |
|
743 | user = User.find_by_login("admin") | |
727 | user.password = "admin" |
|
744 | user.password = "admin" | |
728 | assert user.save(:validate => false) |
|
745 | assert user.save(:validate => false) | |
729 |
|
746 | |||
730 | assert_equal false, User.default_admin_account_changed? |
|
747 | assert_equal false, User.default_admin_account_changed? | |
731 | end |
|
748 | end | |
732 |
|
749 | |||
733 | def test_default_admin_account_changed_should_return_true_if_password_was_changed |
|
750 | def test_default_admin_account_changed_should_return_true_if_password_was_changed | |
734 | user = User.find_by_login("admin") |
|
751 | user = User.find_by_login("admin") | |
735 | user.password = "newpassword" |
|
752 | user.password = "newpassword" | |
736 | user.save! |
|
753 | user.save! | |
737 |
|
754 | |||
738 | assert_equal true, User.default_admin_account_changed? |
|
755 | assert_equal true, User.default_admin_account_changed? | |
739 | end |
|
756 | end | |
740 |
|
757 | |||
741 | def test_default_admin_account_changed_should_return_true_if_account_is_disabled |
|
758 | def test_default_admin_account_changed_should_return_true_if_account_is_disabled | |
742 | user = User.find_by_login("admin") |
|
759 | user = User.find_by_login("admin") | |
743 | user.password = "admin" |
|
760 | user.password = "admin" | |
744 | user.status = User::STATUS_LOCKED |
|
761 | user.status = User::STATUS_LOCKED | |
745 | assert user.save(:validate => false) |
|
762 | assert user.save(:validate => false) | |
746 |
|
763 | |||
747 | assert_equal true, User.default_admin_account_changed? |
|
764 | assert_equal true, User.default_admin_account_changed? | |
748 | end |
|
765 | end | |
749 |
|
766 | |||
750 | def test_default_admin_account_changed_should_return_true_if_account_does_not_exist |
|
767 | def test_default_admin_account_changed_should_return_true_if_account_does_not_exist | |
751 | user = User.find_by_login("admin") |
|
768 | user = User.find_by_login("admin") | |
752 | user.destroy |
|
769 | user.destroy | |
753 |
|
770 | |||
754 | assert_equal true, User.default_admin_account_changed? |
|
771 | assert_equal true, User.default_admin_account_changed? | |
755 | end |
|
772 | end | |
756 |
|
773 | |||
757 | def test_membership_with_project_should_return_membership |
|
774 | def test_membership_with_project_should_return_membership | |
758 | project = Project.find(1) |
|
775 | project = Project.find(1) | |
759 |
|
776 | |||
760 | membership = @jsmith.membership(project) |
|
777 | membership = @jsmith.membership(project) | |
761 | assert_kind_of Member, membership |
|
778 | assert_kind_of Member, membership | |
762 | assert_equal @jsmith, membership.user |
|
779 | assert_equal @jsmith, membership.user | |
763 | assert_equal project, membership.project |
|
780 | assert_equal project, membership.project | |
764 | end |
|
781 | end | |
765 |
|
782 | |||
766 | def test_membership_with_project_id_should_return_membership |
|
783 | def test_membership_with_project_id_should_return_membership | |
767 | project = Project.find(1) |
|
784 | project = Project.find(1) | |
768 |
|
785 | |||
769 | membership = @jsmith.membership(1) |
|
786 | membership = @jsmith.membership(1) | |
770 | assert_kind_of Member, membership |
|
787 | assert_kind_of Member, membership | |
771 | assert_equal @jsmith, membership.user |
|
788 | assert_equal @jsmith, membership.user | |
772 | assert_equal project, membership.project |
|
789 | assert_equal project, membership.project | |
773 | end |
|
790 | end | |
774 |
|
791 | |||
775 | def test_membership_for_non_member_should_return_nil |
|
792 | def test_membership_for_non_member_should_return_nil | |
776 | project = Project.find(1) |
|
793 | project = Project.find(1) | |
777 |
|
794 | |||
778 | user = User.generate! |
|
795 | user = User.generate! | |
779 | membership = user.membership(1) |
|
796 | membership = user.membership(1) | |
780 | assert_nil membership |
|
797 | assert_nil membership | |
781 | end |
|
798 | end | |
782 |
|
799 | |||
783 | def test_roles_for_project |
|
800 | def test_roles_for_project | |
784 | # user with a role |
|
801 | # user with a role | |
785 | roles = @jsmith.roles_for_project(Project.find(1)) |
|
802 | roles = @jsmith.roles_for_project(Project.find(1)) | |
786 | assert_kind_of Role, roles.first |
|
803 | assert_kind_of Role, roles.first | |
787 | assert_equal "Manager", roles.first.name |
|
804 | assert_equal "Manager", roles.first.name | |
788 |
|
805 | |||
789 | # user with no role |
|
806 | # user with no role | |
790 | assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?} |
|
807 | assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?} | |
791 | end |
|
808 | end | |
792 |
|
809 | |||
793 | def test_projects_by_role_for_user_with_role |
|
810 | def test_projects_by_role_for_user_with_role | |
794 | user = User.find(2) |
|
811 | user = User.find(2) | |
795 | assert_kind_of Hash, user.projects_by_role |
|
812 | assert_kind_of Hash, user.projects_by_role | |
796 | assert_equal 2, user.projects_by_role.size |
|
813 | assert_equal 2, user.projects_by_role.size | |
797 | assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort |
|
814 | assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort | |
798 | assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort |
|
815 | assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort | |
799 | end |
|
816 | end | |
800 |
|
817 | |||
801 | def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array |
|
818 | def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array | |
802 | user = User.find(2) |
|
819 | user = User.find(2) | |
803 | assert_equal [], user.projects_by_role[Role.find(3)] |
|
820 | assert_equal [], user.projects_by_role[Role.find(3)] | |
804 | # should not update the hash |
|
821 | # should not update the hash | |
805 | assert_nil user.projects_by_role.values.detect(&:blank?) |
|
822 | assert_nil user.projects_by_role.values.detect(&:blank?) | |
806 | end |
|
823 | end | |
807 |
|
824 | |||
808 | def test_projects_by_role_for_user_with_no_role |
|
825 | def test_projects_by_role_for_user_with_no_role | |
809 | user = User.generate! |
|
826 | user = User.generate! | |
810 | assert_equal({}, user.projects_by_role) |
|
827 | assert_equal({}, user.projects_by_role) | |
811 | end |
|
828 | end | |
812 |
|
829 | |||
813 | def test_projects_by_role_for_anonymous |
|
830 | def test_projects_by_role_for_anonymous | |
814 | assert_equal({}, User.anonymous.projects_by_role) |
|
831 | assert_equal({}, User.anonymous.projects_by_role) | |
815 | end |
|
832 | end | |
816 |
|
833 | |||
817 | def test_valid_notification_options |
|
834 | def test_valid_notification_options | |
818 | # without memberships |
|
835 | # without memberships | |
819 | assert_equal 5, User.find(7).valid_notification_options.size |
|
836 | assert_equal 5, User.find(7).valid_notification_options.size | |
820 | # with memberships |
|
837 | # with memberships | |
821 | assert_equal 6, User.find(2).valid_notification_options.size |
|
838 | assert_equal 6, User.find(2).valid_notification_options.size | |
822 | end |
|
839 | end | |
823 |
|
840 | |||
824 | def test_valid_notification_options_class_method |
|
841 | def test_valid_notification_options_class_method | |
825 | assert_equal 5, User.valid_notification_options.size |
|
842 | assert_equal 5, User.valid_notification_options.size | |
826 | assert_equal 5, User.valid_notification_options(User.find(7)).size |
|
843 | assert_equal 5, User.valid_notification_options(User.find(7)).size | |
827 | assert_equal 6, User.valid_notification_options(User.find(2)).size |
|
844 | assert_equal 6, User.valid_notification_options(User.find(2)).size | |
828 | end |
|
845 | end | |
829 |
|
846 | |||
830 | def test_mail_notification_all |
|
847 | def test_mail_notification_all | |
831 | @jsmith.mail_notification = 'all' |
|
848 | @jsmith.mail_notification = 'all' | |
832 | @jsmith.notified_project_ids = [] |
|
849 | @jsmith.notified_project_ids = [] | |
833 | @jsmith.save |
|
850 | @jsmith.save | |
834 | @jsmith.reload |
|
851 | @jsmith.reload | |
835 | assert @jsmith.projects.first.recipients.include?(@jsmith.mail) |
|
852 | assert @jsmith.projects.first.recipients.include?(@jsmith.mail) | |
836 | end |
|
853 | end | |
837 |
|
854 | |||
838 | def test_mail_notification_selected |
|
855 | def test_mail_notification_selected | |
839 | @jsmith.mail_notification = 'selected' |
|
856 | @jsmith.mail_notification = 'selected' | |
840 | @jsmith.notified_project_ids = [1] |
|
857 | @jsmith.notified_project_ids = [1] | |
841 | @jsmith.save |
|
858 | @jsmith.save | |
842 | @jsmith.reload |
|
859 | @jsmith.reload | |
843 | assert Project.find(1).recipients.include?(@jsmith.mail) |
|
860 | assert Project.find(1).recipients.include?(@jsmith.mail) | |
844 | end |
|
861 | end | |
845 |
|
862 | |||
846 | def test_mail_notification_only_my_events |
|
863 | def test_mail_notification_only_my_events | |
847 | @jsmith.mail_notification = 'only_my_events' |
|
864 | @jsmith.mail_notification = 'only_my_events' | |
848 | @jsmith.notified_project_ids = [] |
|
865 | @jsmith.notified_project_ids = [] | |
849 | @jsmith.save |
|
866 | @jsmith.save | |
850 | @jsmith.reload |
|
867 | @jsmith.reload | |
851 | assert !@jsmith.projects.first.recipients.include?(@jsmith.mail) |
|
868 | assert !@jsmith.projects.first.recipients.include?(@jsmith.mail) | |
852 | end |
|
869 | end | |
853 |
|
870 | |||
854 | def test_comments_sorting_preference |
|
871 | def test_comments_sorting_preference | |
855 | assert !@jsmith.wants_comments_in_reverse_order? |
|
872 | assert !@jsmith.wants_comments_in_reverse_order? | |
856 | @jsmith.pref.comments_sorting = 'asc' |
|
873 | @jsmith.pref.comments_sorting = 'asc' | |
857 | assert !@jsmith.wants_comments_in_reverse_order? |
|
874 | assert !@jsmith.wants_comments_in_reverse_order? | |
858 | @jsmith.pref.comments_sorting = 'desc' |
|
875 | @jsmith.pref.comments_sorting = 'desc' | |
859 | assert @jsmith.wants_comments_in_reverse_order? |
|
876 | assert @jsmith.wants_comments_in_reverse_order? | |
860 | end |
|
877 | end | |
861 |
|
878 | |||
862 | def test_find_by_mail_should_be_case_insensitive |
|
879 | def test_find_by_mail_should_be_case_insensitive | |
863 | u = User.find_by_mail('JSmith@somenet.foo') |
|
880 | u = User.find_by_mail('JSmith@somenet.foo') | |
864 | assert_not_nil u |
|
881 | assert_not_nil u | |
865 | assert_equal 'jsmith@somenet.foo', u.mail |
|
882 | assert_equal 'jsmith@somenet.foo', u.mail | |
866 | end |
|
883 | end | |
867 |
|
884 | |||
868 | def test_random_password |
|
885 | def test_random_password | |
869 | u = User.new |
|
886 | u = User.new | |
870 | u.random_password |
|
887 | u.random_password | |
871 | assert !u.password.blank? |
|
888 | assert !u.password.blank? | |
872 | assert !u.password_confirmation.blank? |
|
889 | assert !u.password_confirmation.blank? | |
873 | end |
|
890 | end | |
874 |
|
891 | |||
875 | test "#change_password_allowed? should be allowed if no auth source is set" do |
|
892 | test "#change_password_allowed? should be allowed if no auth source is set" do | |
876 | user = User.generate! |
|
893 | user = User.generate! | |
877 | assert user.change_password_allowed? |
|
894 | assert user.change_password_allowed? | |
878 | end |
|
895 | end | |
879 |
|
896 | |||
880 | test "#change_password_allowed? should delegate to the auth source" do |
|
897 | test "#change_password_allowed? should delegate to the auth source" do | |
881 | user = User.generate! |
|
898 | user = User.generate! | |
882 |
|
899 | |||
883 | allowed_auth_source = AuthSource.generate! |
|
900 | allowed_auth_source = AuthSource.generate! | |
884 | def allowed_auth_source.allow_password_changes?; true; end |
|
901 | def allowed_auth_source.allow_password_changes?; true; end | |
885 |
|
902 | |||
886 | denied_auth_source = AuthSource.generate! |
|
903 | denied_auth_source = AuthSource.generate! | |
887 | def denied_auth_source.allow_password_changes?; false; end |
|
904 | def denied_auth_source.allow_password_changes?; false; end | |
888 |
|
905 | |||
889 | assert user.change_password_allowed? |
|
906 | assert user.change_password_allowed? | |
890 |
|
907 | |||
891 | user.auth_source = allowed_auth_source |
|
908 | user.auth_source = allowed_auth_source | |
892 | assert user.change_password_allowed?, "User not allowed to change password, though auth source does" |
|
909 | assert user.change_password_allowed?, "User not allowed to change password, though auth source does" | |
893 |
|
910 | |||
894 | user.auth_source = denied_auth_source |
|
911 | user.auth_source = denied_auth_source | |
895 | assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" |
|
912 | assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" | |
896 | end |
|
913 | end | |
897 |
|
914 | |||
898 | def test_own_account_deletable_should_be_true_with_unsubscrive_enabled |
|
915 | def test_own_account_deletable_should_be_true_with_unsubscrive_enabled | |
899 | with_settings :unsubscribe => '1' do |
|
916 | with_settings :unsubscribe => '1' do | |
900 | assert_equal true, User.find(2).own_account_deletable? |
|
917 | assert_equal true, User.find(2).own_account_deletable? | |
901 | end |
|
918 | end | |
902 | end |
|
919 | end | |
903 |
|
920 | |||
904 | def test_own_account_deletable_should_be_false_with_unsubscrive_disabled |
|
921 | def test_own_account_deletable_should_be_false_with_unsubscrive_disabled | |
905 | with_settings :unsubscribe => '0' do |
|
922 | with_settings :unsubscribe => '0' do | |
906 | assert_equal false, User.find(2).own_account_deletable? |
|
923 | assert_equal false, User.find(2).own_account_deletable? | |
907 | end |
|
924 | end | |
908 | end |
|
925 | end | |
909 |
|
926 | |||
910 | def test_own_account_deletable_should_be_false_for_a_single_admin |
|
927 | def test_own_account_deletable_should_be_false_for_a_single_admin | |
911 | User.delete_all(["admin = ? AND id <> ?", true, 1]) |
|
928 | User.delete_all(["admin = ? AND id <> ?", true, 1]) | |
912 |
|
929 | |||
913 | with_settings :unsubscribe => '1' do |
|
930 | with_settings :unsubscribe => '1' do | |
914 | assert_equal false, User.find(1).own_account_deletable? |
|
931 | assert_equal false, User.find(1).own_account_deletable? | |
915 | end |
|
932 | end | |
916 | end |
|
933 | end | |
917 |
|
934 | |||
918 | def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists |
|
935 | def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists | |
919 | User.generate! do |user| |
|
936 | User.generate! do |user| | |
920 | user.admin = true |
|
937 | user.admin = true | |
921 | end |
|
938 | end | |
922 |
|
939 | |||
923 | with_settings :unsubscribe => '1' do |
|
940 | with_settings :unsubscribe => '1' do | |
924 | assert_equal true, User.find(1).own_account_deletable? |
|
941 | assert_equal true, User.find(1).own_account_deletable? | |
925 | end |
|
942 | end | |
926 | end |
|
943 | end | |
927 |
|
944 | |||
928 | context "#allowed_to?" do |
|
945 | context "#allowed_to?" do | |
929 | context "with a unique project" do |
|
946 | context "with a unique project" do | |
930 | should "return false if project is archived" do |
|
947 | should "return false if project is archived" do | |
931 | project = Project.find(1) |
|
948 | project = Project.find(1) | |
932 | Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED) |
|
949 | Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED) | |
933 | assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1)) |
|
950 | assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1)) | |
934 | end |
|
951 | end | |
935 |
|
952 | |||
936 | should "return false for write action if project is closed" do |
|
953 | should "return false for write action if project is closed" do | |
937 | project = Project.find(1) |
|
954 | project = Project.find(1) | |
938 | Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED) |
|
955 | Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED) | |
939 | assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1)) |
|
956 | assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1)) | |
940 | end |
|
957 | end | |
941 |
|
958 | |||
942 | should "return true for read action if project is closed" do |
|
959 | should "return true for read action if project is closed" do | |
943 | project = Project.find(1) |
|
960 | project = Project.find(1) | |
944 | Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED) |
|
961 | Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED) | |
945 | assert_equal true, @admin.allowed_to?(:view_project, Project.find(1)) |
|
962 | assert_equal true, @admin.allowed_to?(:view_project, Project.find(1)) | |
946 | end |
|
963 | end | |
947 |
|
964 | |||
948 | should "return false if related module is disabled" do |
|
965 | should "return false if related module is disabled" do | |
949 | project = Project.find(1) |
|
966 | project = Project.find(1) | |
950 | project.enabled_module_names = ["issue_tracking"] |
|
967 | project.enabled_module_names = ["issue_tracking"] | |
951 | assert_equal true, @admin.allowed_to?(:add_issues, project) |
|
968 | assert_equal true, @admin.allowed_to?(:add_issues, project) | |
952 | assert_equal false, @admin.allowed_to?(:view_wiki_pages, project) |
|
969 | assert_equal false, @admin.allowed_to?(:view_wiki_pages, project) | |
953 | end |
|
970 | end | |
954 |
|
971 | |||
955 | should "authorize nearly everything for admin users" do |
|
972 | should "authorize nearly everything for admin users" do | |
956 | project = Project.find(1) |
|
973 | project = Project.find(1) | |
957 | assert ! @admin.member_of?(project) |
|
974 | assert ! @admin.member_of?(project) | |
958 | %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p| |
|
975 | %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p| | |
959 | assert_equal true, @admin.allowed_to?(p.to_sym, project) |
|
976 | assert_equal true, @admin.allowed_to?(p.to_sym, project) | |
960 | end |
|
977 | end | |
961 | end |
|
978 | end | |
962 |
|
979 | |||
963 | should "authorize normal users depending on their roles" do |
|
980 | should "authorize normal users depending on their roles" do | |
964 | project = Project.find(1) |
|
981 | project = Project.find(1) | |
965 | assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager |
|
982 | assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager | |
966 | assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper |
|
983 | assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper | |
967 | end |
|
984 | end | |
968 | end |
|
985 | end | |
969 |
|
986 | |||
970 | context "with multiple projects" do |
|
987 | context "with multiple projects" do | |
971 | should "return false if array is empty" do |
|
988 | should "return false if array is empty" do | |
972 | assert_equal false, @admin.allowed_to?(:view_project, []) |
|
989 | assert_equal false, @admin.allowed_to?(:view_project, []) | |
973 | end |
|
990 | end | |
974 |
|
991 | |||
975 | should "return true only if user has permission on all these projects" do |
|
992 | should "return true only if user has permission on all these projects" do | |
976 | assert_equal true, @admin.allowed_to?(:view_project, Project.all) |
|
993 | assert_equal true, @admin.allowed_to?(:view_project, Project.all) | |
977 | assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2) |
|
994 | assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2) | |
978 | assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere |
|
995 | assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere | |
979 | assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers |
|
996 | assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers | |
980 | end |
|
997 | end | |
981 |
|
998 | |||
982 | should "behave correctly with arrays of 1 project" do |
|
999 | should "behave correctly with arrays of 1 project" do | |
983 | assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first]) |
|
1000 | assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first]) | |
984 | end |
|
1001 | end | |
985 | end |
|
1002 | end | |
986 |
|
1003 | |||
987 | context "with options[:global]" do |
|
1004 | context "with options[:global]" do | |
988 | should "authorize if user has at least one role that has this permission" do |
|
1005 | should "authorize if user has at least one role that has this permission" do | |
989 | @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere |
|
1006 | @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere | |
990 | @anonymous = User.find(6) |
|
1007 | @anonymous = User.find(6) | |
991 | assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true) |
|
1008 | assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true) | |
992 | assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true) |
|
1009 | assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true) | |
993 | assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true) |
|
1010 | assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true) | |
994 | assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true) |
|
1011 | assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true) | |
995 | assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true) |
|
1012 | assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true) | |
996 | end |
|
1013 | end | |
997 | end |
|
1014 | end | |
998 | end |
|
1015 | end | |
999 |
|
1016 | |||
1000 | context "User#notify_about?" do |
|
1017 | context "User#notify_about?" do | |
1001 | context "Issues" do |
|
1018 | context "Issues" do | |
1002 | setup do |
|
1019 | setup do | |
1003 | @project = Project.find(1) |
|
1020 | @project = Project.find(1) | |
1004 | @author = User.generate! |
|
1021 | @author = User.generate! | |
1005 | @assignee = User.generate! |
|
1022 | @assignee = User.generate! | |
1006 | @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author) |
|
1023 | @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author) | |
1007 | end |
|
1024 | end | |
1008 |
|
1025 | |||
1009 | should "be true for a user with :all" do |
|
1026 | should "be true for a user with :all" do | |
1010 | @author.update_attribute(:mail_notification, 'all') |
|
1027 | @author.update_attribute(:mail_notification, 'all') | |
1011 | assert @author.notify_about?(@issue) |
|
1028 | assert @author.notify_about?(@issue) | |
1012 | end |
|
1029 | end | |
1013 |
|
1030 | |||
1014 | should "be false for a user with :none" do |
|
1031 | should "be false for a user with :none" do | |
1015 | @author.update_attribute(:mail_notification, 'none') |
|
1032 | @author.update_attribute(:mail_notification, 'none') | |
1016 | assert ! @author.notify_about?(@issue) |
|
1033 | assert ! @author.notify_about?(@issue) | |
1017 | end |
|
1034 | end | |
1018 |
|
1035 | |||
1019 | should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do |
|
1036 | should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do | |
1020 | @user = User.generate!(:mail_notification => 'only_my_events') |
|
1037 | @user = User.generate!(:mail_notification => 'only_my_events') | |
1021 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) |
|
1038 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) | |
1022 | assert ! @user.notify_about?(@issue) |
|
1039 | assert ! @user.notify_about?(@issue) | |
1023 | end |
|
1040 | end | |
1024 |
|
1041 | |||
1025 | should "be true for a user with :only_my_events and is the author" do |
|
1042 | should "be true for a user with :only_my_events and is the author" do | |
1026 | @author.update_attribute(:mail_notification, 'only_my_events') |
|
1043 | @author.update_attribute(:mail_notification, 'only_my_events') | |
1027 | assert @author.notify_about?(@issue) |
|
1044 | assert @author.notify_about?(@issue) | |
1028 | end |
|
1045 | end | |
1029 |
|
1046 | |||
1030 | should "be true for a user with :only_my_events and is the assignee" do |
|
1047 | should "be true for a user with :only_my_events and is the assignee" do | |
1031 | @assignee.update_attribute(:mail_notification, 'only_my_events') |
|
1048 | @assignee.update_attribute(:mail_notification, 'only_my_events') | |
1032 | assert @assignee.notify_about?(@issue) |
|
1049 | assert @assignee.notify_about?(@issue) | |
1033 | end |
|
1050 | end | |
1034 |
|
1051 | |||
1035 | should "be true for a user with :only_assigned and is the assignee" do |
|
1052 | should "be true for a user with :only_assigned and is the assignee" do | |
1036 | @assignee.update_attribute(:mail_notification, 'only_assigned') |
|
1053 | @assignee.update_attribute(:mail_notification, 'only_assigned') | |
1037 | assert @assignee.notify_about?(@issue) |
|
1054 | assert @assignee.notify_about?(@issue) | |
1038 | end |
|
1055 | end | |
1039 |
|
1056 | |||
1040 | should "be false for a user with :only_assigned and is not the assignee" do |
|
1057 | should "be false for a user with :only_assigned and is not the assignee" do | |
1041 | @author.update_attribute(:mail_notification, 'only_assigned') |
|
1058 | @author.update_attribute(:mail_notification, 'only_assigned') | |
1042 | assert ! @author.notify_about?(@issue) |
|
1059 | assert ! @author.notify_about?(@issue) | |
1043 | end |
|
1060 | end | |
1044 |
|
1061 | |||
1045 | should "be true for a user with :only_owner and is the author" do |
|
1062 | should "be true for a user with :only_owner and is the author" do | |
1046 | @author.update_attribute(:mail_notification, 'only_owner') |
|
1063 | @author.update_attribute(:mail_notification, 'only_owner') | |
1047 | assert @author.notify_about?(@issue) |
|
1064 | assert @author.notify_about?(@issue) | |
1048 | end |
|
1065 | end | |
1049 |
|
1066 | |||
1050 | should "be false for a user with :only_owner and is not the author" do |
|
1067 | should "be false for a user with :only_owner and is not the author" do | |
1051 | @assignee.update_attribute(:mail_notification, 'only_owner') |
|
1068 | @assignee.update_attribute(:mail_notification, 'only_owner') | |
1052 | assert ! @assignee.notify_about?(@issue) |
|
1069 | assert ! @assignee.notify_about?(@issue) | |
1053 | end |
|
1070 | end | |
1054 |
|
1071 | |||
1055 | should "be true for a user with :selected and is the author" do |
|
1072 | should "be true for a user with :selected and is the author" do | |
1056 | @author.update_attribute(:mail_notification, 'selected') |
|
1073 | @author.update_attribute(:mail_notification, 'selected') | |
1057 | assert @author.notify_about?(@issue) |
|
1074 | assert @author.notify_about?(@issue) | |
1058 | end |
|
1075 | end | |
1059 |
|
1076 | |||
1060 | should "be true for a user with :selected and is the assignee" do |
|
1077 | should "be true for a user with :selected and is the assignee" do | |
1061 | @assignee.update_attribute(:mail_notification, 'selected') |
|
1078 | @assignee.update_attribute(:mail_notification, 'selected') | |
1062 | assert @assignee.notify_about?(@issue) |
|
1079 | assert @assignee.notify_about?(@issue) | |
1063 | end |
|
1080 | end | |
1064 |
|
1081 | |||
1065 | should "be false for a user with :selected and is not the author or assignee" do |
|
1082 | should "be false for a user with :selected and is not the author or assignee" do | |
1066 | @user = User.generate!(:mail_notification => 'selected') |
|
1083 | @user = User.generate!(:mail_notification => 'selected') | |
1067 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) |
|
1084 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) | |
1068 | assert ! @user.notify_about?(@issue) |
|
1085 | assert ! @user.notify_about?(@issue) | |
1069 | end |
|
1086 | end | |
1070 | end |
|
1087 | end | |
1071 | end |
|
1088 | end | |
1072 |
|
1089 | |||
1073 | def test_notify_about_news |
|
1090 | def test_notify_about_news | |
1074 | user = User.generate! |
|
1091 | user = User.generate! | |
1075 | news = News.new |
|
1092 | news = News.new | |
1076 |
|
1093 | |||
1077 | User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option| |
|
1094 | User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option| | |
1078 | user.mail_notification = option |
|
1095 | user.mail_notification = option | |
1079 | assert_equal (option != 'none'), user.notify_about?(news) |
|
1096 | assert_equal (option != 'none'), user.notify_about?(news) | |
1080 | end |
|
1097 | end | |
1081 | end |
|
1098 | end | |
1082 |
|
1099 | |||
1083 | def test_salt_unsalted_passwords |
|
1100 | def test_salt_unsalted_passwords | |
1084 | # Restore a user with an unsalted password |
|
1101 | # Restore a user with an unsalted password | |
1085 | user = User.find(1) |
|
1102 | user = User.find(1) | |
1086 | user.salt = nil |
|
1103 | user.salt = nil | |
1087 | user.hashed_password = User.hash_password("unsalted") |
|
1104 | user.hashed_password = User.hash_password("unsalted") | |
1088 | user.save! |
|
1105 | user.save! | |
1089 |
|
1106 | |||
1090 | User.salt_unsalted_passwords! |
|
1107 | User.salt_unsalted_passwords! | |
1091 |
|
1108 | |||
1092 | user.reload |
|
1109 | user.reload | |
1093 | # Salt added |
|
1110 | # Salt added | |
1094 | assert !user.salt.blank? |
|
1111 | assert !user.salt.blank? | |
1095 | # Password still valid |
|
1112 | # Password still valid | |
1096 | assert user.check_password?("unsalted") |
|
1113 | assert user.check_password?("unsalted") | |
1097 | assert_equal user, User.try_to_login(user.login, "unsalted") |
|
1114 | assert_equal user, User.try_to_login(user.login, "unsalted") | |
1098 | end |
|
1115 | end | |
1099 |
|
1116 | |||
1100 | if Object.const_defined?(:OpenID) |
|
1117 | if Object.const_defined?(:OpenID) | |
1101 |
|
1118 | |||
1102 | def test_setting_identity_url |
|
1119 | def test_setting_identity_url | |
1103 | normalized_open_id_url = 'http://example.com/' |
|
1120 | normalized_open_id_url = 'http://example.com/' | |
1104 | u = User.new( :identity_url => 'http://example.com/' ) |
|
1121 | u = User.new( :identity_url => 'http://example.com/' ) | |
1105 | assert_equal normalized_open_id_url, u.identity_url |
|
1122 | assert_equal normalized_open_id_url, u.identity_url | |
1106 | end |
|
1123 | end | |
1107 |
|
1124 | |||
1108 | def test_setting_identity_url_without_trailing_slash |
|
1125 | def test_setting_identity_url_without_trailing_slash | |
1109 | normalized_open_id_url = 'http://example.com/' |
|
1126 | normalized_open_id_url = 'http://example.com/' | |
1110 | u = User.new( :identity_url => 'http://example.com' ) |
|
1127 | u = User.new( :identity_url => 'http://example.com' ) | |
1111 | assert_equal normalized_open_id_url, u.identity_url |
|
1128 | assert_equal normalized_open_id_url, u.identity_url | |
1112 | end |
|
1129 | end | |
1113 |
|
1130 | |||
1114 | def test_setting_identity_url_without_protocol |
|
1131 | def test_setting_identity_url_without_protocol | |
1115 | normalized_open_id_url = 'http://example.com/' |
|
1132 | normalized_open_id_url = 'http://example.com/' | |
1116 | u = User.new( :identity_url => 'example.com' ) |
|
1133 | u = User.new( :identity_url => 'example.com' ) | |
1117 | assert_equal normalized_open_id_url, u.identity_url |
|
1134 | assert_equal normalized_open_id_url, u.identity_url | |
1118 | end |
|
1135 | end | |
1119 |
|
1136 | |||
1120 | def test_setting_blank_identity_url |
|
1137 | def test_setting_blank_identity_url | |
1121 | u = User.new( :identity_url => 'example.com' ) |
|
1138 | u = User.new( :identity_url => 'example.com' ) | |
1122 | u.identity_url = '' |
|
1139 | u.identity_url = '' | |
1123 | assert u.identity_url.blank? |
|
1140 | assert u.identity_url.blank? | |
1124 | end |
|
1141 | end | |
1125 |
|
1142 | |||
1126 | def test_setting_invalid_identity_url |
|
1143 | def test_setting_invalid_identity_url | |
1127 | u = User.new( :identity_url => 'this is not an openid url' ) |
|
1144 | u = User.new( :identity_url => 'this is not an openid url' ) | |
1128 | assert u.identity_url.blank? |
|
1145 | assert u.identity_url.blank? | |
1129 | end |
|
1146 | end | |
1130 |
|
1147 | |||
1131 | else |
|
1148 | else | |
1132 | puts "Skipping openid tests." |
|
1149 | puts "Skipping openid tests." | |
1133 | end |
|
1150 | end | |
1134 |
|
1151 | |||
1135 | end |
|
1152 | end |
General Comments 0
You need to be logged in to leave comments.
Login now