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