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