##// END OF EJS Templates
Rails3: test: replace deprecated Errors#on to Errors#[] and join with to_s at test/unit/user_test.rb...
Toshi MARUYAMA -
r8017:8e5242ef6351
parent child
Show More
@@ -1,879 +1,882
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 test 'object_daddy creation' do
37 test 'object_daddy creation' do
38 User.generate_with_protected!(:firstname => 'Testing connection')
38 User.generate_with_protected!(:firstname => 'Testing connection')
39 User.generate_with_protected!(:firstname => 'Testing connection')
39 User.generate_with_protected!(: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_equal I18n.translate('activerecord.errors.messages.blank'), u.errors.on(:mail)
57 assert_equal I18n.translate('activerecord.errors.messages.blank'),
58 u.errors[:mail].to_s
58 end
59 end
59
60
60 def test_create
61 def test_create
61 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
62 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
62
63
63 user.login = "jsmith"
64 user.login = "jsmith"
64 user.password, user.password_confirmation = "password", "password"
65 user.password, user.password_confirmation = "password", "password"
65 # login uniqueness
66 # login uniqueness
66 assert !user.save
67 assert !user.save
67 assert_equal 1, user.errors.count
68 assert_equal 1, user.errors.count
68
69
69 user.login = "newuser"
70 user.login = "newuser"
70 user.password, user.password_confirmation = "passwd", "password"
71 user.password, user.password_confirmation = "passwd", "password"
71 # password confirmation
72 # password confirmation
72 assert !user.save
73 assert !user.save
73 assert_equal 1, user.errors.count
74 assert_equal 1, user.errors.count
74
75
75 user.password, user.password_confirmation = "password", "password"
76 user.password, user.password_confirmation = "password", "password"
76 assert user.save
77 assert user.save
77 end
78 end
78
79
79 context "User#before_create" do
80 context "User#before_create" do
80 should "set the mail_notification to the default Setting" do
81 should "set the mail_notification to the default Setting" do
81 @user1 = User.generate_with_protected!
82 @user1 = User.generate_with_protected!
82 assert_equal 'only_my_events', @user1.mail_notification
83 assert_equal 'only_my_events', @user1.mail_notification
83
84
84 with_settings :default_notification_option => 'all' do
85 with_settings :default_notification_option => 'all' do
85 @user2 = User.generate_with_protected!
86 @user2 = User.generate_with_protected!
86 assert_equal 'all', @user2.mail_notification
87 assert_equal 'all', @user2.mail_notification
87 end
88 end
88 end
89 end
89 end
90 end
90
91
91 context "User.login" do
92 context "User.login" do
92 should "be case-insensitive." do
93 should "be case-insensitive." do
93 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
94 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
94 u.login = 'newuser'
95 u.login = 'newuser'
95 u.password, u.password_confirmation = "password", "password"
96 u.password, u.password_confirmation = "password", "password"
96 assert u.save
97 assert u.save
97
98
98 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
99 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
99 u.login = 'NewUser'
100 u.login = 'NewUser'
100 u.password, u.password_confirmation = "password", "password"
101 u.password, u.password_confirmation = "password", "password"
101 assert !u.save
102 assert !u.save
102 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:login)
103 assert_equal I18n.translate('activerecord.errors.messages.taken'),
104 u.errors[:login].to_s
103 end
105 end
104 end
106 end
105
107
106 def test_mail_uniqueness_should_not_be_case_sensitive
108 def test_mail_uniqueness_should_not_be_case_sensitive
107 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
109 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
108 u.login = 'newuser1'
110 u.login = 'newuser1'
109 u.password, u.password_confirmation = "password", "password"
111 u.password, u.password_confirmation = "password", "password"
110 assert u.save
112 assert u.save
111
113
112 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
114 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
113 u.login = 'newuser2'
115 u.login = 'newuser2'
114 u.password, u.password_confirmation = "password", "password"
116 u.password, u.password_confirmation = "password", "password"
115 assert !u.save
117 assert !u.save
116 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail)
118 assert_equal I18n.translate('activerecord.errors.messages.taken'),
119 u.errors[:mail].to_s
117 end
120 end
118
121
119 def test_update
122 def test_update
120 assert_equal "admin", @admin.login
123 assert_equal "admin", @admin.login
121 @admin.login = "john"
124 @admin.login = "john"
122 assert @admin.save, @admin.errors.full_messages.join("; ")
125 assert @admin.save, @admin.errors.full_messages.join("; ")
123 @admin.reload
126 @admin.reload
124 assert_equal "john", @admin.login
127 assert_equal "john", @admin.login
125 end
128 end
126
129
127 def test_destroy_should_delete_members_and_roles
130 def test_destroy_should_delete_members_and_roles
128 members = Member.find_all_by_user_id(2)
131 members = Member.find_all_by_user_id(2)
129 ms = members.size
132 ms = members.size
130 rs = members.collect(&:roles).flatten.size
133 rs = members.collect(&:roles).flatten.size
131
134
132 assert_difference 'Member.count', - ms do
135 assert_difference 'Member.count', - ms do
133 assert_difference 'MemberRole.count', - rs do
136 assert_difference 'MemberRole.count', - rs do
134 User.find(2).destroy
137 User.find(2).destroy
135 end
138 end
136 end
139 end
137
140
138 assert_nil User.find_by_id(2)
141 assert_nil User.find_by_id(2)
139 assert Member.find_all_by_user_id(2).empty?
142 assert Member.find_all_by_user_id(2).empty?
140 end
143 end
141
144
142 def test_destroy_should_update_attachments
145 def test_destroy_should_update_attachments
143 attachment = Attachment.create!(:container => Project.find(1),
146 attachment = Attachment.create!(:container => Project.find(1),
144 :file => uploaded_test_file("testfile.txt", "text/plain"),
147 :file => uploaded_test_file("testfile.txt", "text/plain"),
145 :author_id => 2)
148 :author_id => 2)
146
149
147 User.find(2).destroy
150 User.find(2).destroy
148 assert_nil User.find_by_id(2)
151 assert_nil User.find_by_id(2)
149 assert_equal User.anonymous, attachment.reload.author
152 assert_equal User.anonymous, attachment.reload.author
150 end
153 end
151
154
152 def test_destroy_should_update_comments
155 def test_destroy_should_update_comments
153 comment = Comment.create!(
156 comment = Comment.create!(
154 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
157 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
155 :author => User.find(2),
158 :author => User.find(2),
156 :comments => 'foo'
159 :comments => 'foo'
157 )
160 )
158
161
159 User.find(2).destroy
162 User.find(2).destroy
160 assert_nil User.find_by_id(2)
163 assert_nil User.find_by_id(2)
161 assert_equal User.anonymous, comment.reload.author
164 assert_equal User.anonymous, comment.reload.author
162 end
165 end
163
166
164 def test_destroy_should_update_issues
167 def test_destroy_should_update_issues
165 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
168 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
166
169
167 User.find(2).destroy
170 User.find(2).destroy
168 assert_nil User.find_by_id(2)
171 assert_nil User.find_by_id(2)
169 assert_equal User.anonymous, issue.reload.author
172 assert_equal User.anonymous, issue.reload.author
170 end
173 end
171
174
172 def test_destroy_should_unassign_issues
175 def test_destroy_should_unassign_issues
173 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
176 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
174
177
175 User.find(2).destroy
178 User.find(2).destroy
176 assert_nil User.find_by_id(2)
179 assert_nil User.find_by_id(2)
177 assert_nil issue.reload.assigned_to
180 assert_nil issue.reload.assigned_to
178 end
181 end
179
182
180 def test_destroy_should_update_journals
183 def test_destroy_should_update_journals
181 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
184 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
182 issue.init_journal(User.find(2), "update")
185 issue.init_journal(User.find(2), "update")
183 issue.save!
186 issue.save!
184
187
185 User.find(2).destroy
188 User.find(2).destroy
186 assert_nil User.find_by_id(2)
189 assert_nil User.find_by_id(2)
187 assert_equal User.anonymous, issue.journals.first.reload.user
190 assert_equal User.anonymous, issue.journals.first.reload.user
188 end
191 end
189
192
190 def test_destroy_should_update_journal_details_old_value
193 def test_destroy_should_update_journal_details_old_value
191 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
194 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
192 issue.init_journal(User.find(1), "update")
195 issue.init_journal(User.find(1), "update")
193 issue.assigned_to_id = nil
196 issue.assigned_to_id = nil
194 assert_difference 'JournalDetail.count' do
197 assert_difference 'JournalDetail.count' do
195 issue.save!
198 issue.save!
196 end
199 end
197 journal_detail = JournalDetail.first(:order => 'id DESC')
200 journal_detail = JournalDetail.first(:order => 'id DESC')
198 assert_equal '2', journal_detail.old_value
201 assert_equal '2', journal_detail.old_value
199
202
200 User.find(2).destroy
203 User.find(2).destroy
201 assert_nil User.find_by_id(2)
204 assert_nil User.find_by_id(2)
202 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
205 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
203 end
206 end
204
207
205 def test_destroy_should_update_journal_details_value
208 def test_destroy_should_update_journal_details_value
206 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
209 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
207 issue.init_journal(User.find(1), "update")
210 issue.init_journal(User.find(1), "update")
208 issue.assigned_to_id = 2
211 issue.assigned_to_id = 2
209 assert_difference 'JournalDetail.count' do
212 assert_difference 'JournalDetail.count' do
210 issue.save!
213 issue.save!
211 end
214 end
212 journal_detail = JournalDetail.first(:order => 'id DESC')
215 journal_detail = JournalDetail.first(:order => 'id DESC')
213 assert_equal '2', journal_detail.value
216 assert_equal '2', journal_detail.value
214
217
215 User.find(2).destroy
218 User.find(2).destroy
216 assert_nil User.find_by_id(2)
219 assert_nil User.find_by_id(2)
217 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
220 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
218 end
221 end
219
222
220 def test_destroy_should_update_messages
223 def test_destroy_should_update_messages
221 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
224 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
222 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
225 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
223
226
224 User.find(2).destroy
227 User.find(2).destroy
225 assert_nil User.find_by_id(2)
228 assert_nil User.find_by_id(2)
226 assert_equal User.anonymous, message.reload.author
229 assert_equal User.anonymous, message.reload.author
227 end
230 end
228
231
229 def test_destroy_should_update_news
232 def test_destroy_should_update_news
230 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
233 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
231
234
232 User.find(2).destroy
235 User.find(2).destroy
233 assert_nil User.find_by_id(2)
236 assert_nil User.find_by_id(2)
234 assert_equal User.anonymous, news.reload.author
237 assert_equal User.anonymous, news.reload.author
235 end
238 end
236
239
237 def test_destroy_should_delete_private_queries
240 def test_destroy_should_delete_private_queries
238 query = Query.new(:name => 'foo', :is_public => false)
241 query = Query.new(:name => 'foo', :is_public => false)
239 query.project_id = 1
242 query.project_id = 1
240 query.user_id = 2
243 query.user_id = 2
241 query.save!
244 query.save!
242
245
243 User.find(2).destroy
246 User.find(2).destroy
244 assert_nil User.find_by_id(2)
247 assert_nil User.find_by_id(2)
245 assert_nil Query.find_by_id(query.id)
248 assert_nil Query.find_by_id(query.id)
246 end
249 end
247
250
248 def test_destroy_should_update_public_queries
251 def test_destroy_should_update_public_queries
249 query = Query.new(:name => 'foo', :is_public => true)
252 query = Query.new(:name => 'foo', :is_public => true)
250 query.project_id = 1
253 query.project_id = 1
251 query.user_id = 2
254 query.user_id = 2
252 query.save!
255 query.save!
253
256
254 User.find(2).destroy
257 User.find(2).destroy
255 assert_nil User.find_by_id(2)
258 assert_nil User.find_by_id(2)
256 assert_equal User.anonymous, query.reload.user
259 assert_equal User.anonymous, query.reload.user
257 end
260 end
258
261
259 def test_destroy_should_update_time_entries
262 def test_destroy_should_update_time_entries
260 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
263 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
261 entry.project_id = 1
264 entry.project_id = 1
262 entry.user_id = 2
265 entry.user_id = 2
263 entry.save!
266 entry.save!
264
267
265 User.find(2).destroy
268 User.find(2).destroy
266 assert_nil User.find_by_id(2)
269 assert_nil User.find_by_id(2)
267 assert_equal User.anonymous, entry.reload.user
270 assert_equal User.anonymous, entry.reload.user
268 end
271 end
269
272
270 def test_destroy_should_delete_tokens
273 def test_destroy_should_delete_tokens
271 token = Token.create!(:user_id => 2, :value => 'foo')
274 token = Token.create!(:user_id => 2, :value => 'foo')
272
275
273 User.find(2).destroy
276 User.find(2).destroy
274 assert_nil User.find_by_id(2)
277 assert_nil User.find_by_id(2)
275 assert_nil Token.find_by_id(token.id)
278 assert_nil Token.find_by_id(token.id)
276 end
279 end
277
280
278 def test_destroy_should_delete_watchers
281 def test_destroy_should_delete_watchers
279 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
282 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
280 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
283 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
281
284
282 User.find(2).destroy
285 User.find(2).destroy
283 assert_nil User.find_by_id(2)
286 assert_nil User.find_by_id(2)
284 assert_nil Watcher.find_by_id(watcher.id)
287 assert_nil Watcher.find_by_id(watcher.id)
285 end
288 end
286
289
287 def test_destroy_should_update_wiki_contents
290 def test_destroy_should_update_wiki_contents
288 wiki_content = WikiContent.create!(
291 wiki_content = WikiContent.create!(
289 :text => 'foo',
292 :text => 'foo',
290 :author_id => 2,
293 :author_id => 2,
291 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
294 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
292 )
295 )
293 wiki_content.text = 'bar'
296 wiki_content.text = 'bar'
294 assert_difference 'WikiContent::Version.count' do
297 assert_difference 'WikiContent::Version.count' do
295 wiki_content.save!
298 wiki_content.save!
296 end
299 end
297
300
298 User.find(2).destroy
301 User.find(2).destroy
299 assert_nil User.find_by_id(2)
302 assert_nil User.find_by_id(2)
300 assert_equal User.anonymous, wiki_content.reload.author
303 assert_equal User.anonymous, wiki_content.reload.author
301 wiki_content.versions.each do |version|
304 wiki_content.versions.each do |version|
302 assert_equal User.anonymous, version.reload.author
305 assert_equal User.anonymous, version.reload.author
303 end
306 end
304 end
307 end
305
308
306 def test_destroy_should_nullify_issue_categories
309 def test_destroy_should_nullify_issue_categories
307 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
310 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
308
311
309 User.find(2).destroy
312 User.find(2).destroy
310 assert_nil User.find_by_id(2)
313 assert_nil User.find_by_id(2)
311 assert_nil category.reload.assigned_to_id
314 assert_nil category.reload.assigned_to_id
312 end
315 end
313
316
314 def test_destroy_should_nullify_changesets
317 def test_destroy_should_nullify_changesets
315 changeset = Changeset.create!(
318 changeset = Changeset.create!(
316 :repository => Repository::Subversion.create!(
319 :repository => Repository::Subversion.create!(
317 :project_id => 1,
320 :project_id => 1,
318 :url => 'file:///var/svn'
321 :url => 'file:///var/svn'
319 ),
322 ),
320 :revision => '12',
323 :revision => '12',
321 :committed_on => Time.now,
324 :committed_on => Time.now,
322 :committer => 'jsmith'
325 :committer => 'jsmith'
323 )
326 )
324 assert_equal 2, changeset.user_id
327 assert_equal 2, changeset.user_id
325
328
326 User.find(2).destroy
329 User.find(2).destroy
327 assert_nil User.find_by_id(2)
330 assert_nil User.find_by_id(2)
328 assert_nil changeset.reload.user_id
331 assert_nil changeset.reload.user_id
329 end
332 end
330
333
331 def test_anonymous_user_should_not_be_destroyable
334 def test_anonymous_user_should_not_be_destroyable
332 assert_no_difference 'User.count' do
335 assert_no_difference 'User.count' do
333 assert_equal false, User.anonymous.destroy
336 assert_equal false, User.anonymous.destroy
334 end
337 end
335 end
338 end
336
339
337 def test_validate_login_presence
340 def test_validate_login_presence
338 @admin.login = ""
341 @admin.login = ""
339 assert !@admin.save
342 assert !@admin.save
340 assert_equal 1, @admin.errors.count
343 assert_equal 1, @admin.errors.count
341 end
344 end
342
345
343 def test_validate_mail_notification_inclusion
346 def test_validate_mail_notification_inclusion
344 u = User.new
347 u = User.new
345 u.mail_notification = 'foo'
348 u.mail_notification = 'foo'
346 u.save
349 u.save
347 assert_not_nil u.errors[:mail_notification]
350 assert_not_nil u.errors[:mail_notification]
348 end
351 end
349
352
350 context "User#try_to_login" do
353 context "User#try_to_login" do
351 should "fall-back to case-insensitive if user login is not found as-typed." do
354 should "fall-back to case-insensitive if user login is not found as-typed." do
352 user = User.try_to_login("AdMin", "admin")
355 user = User.try_to_login("AdMin", "admin")
353 assert_kind_of User, user
356 assert_kind_of User, user
354 assert_equal "admin", user.login
357 assert_equal "admin", user.login
355 end
358 end
356
359
357 should "select the exact matching user first" do
360 should "select the exact matching user first" do
358 case_sensitive_user = User.generate_with_protected!(
361 case_sensitive_user = User.generate_with_protected!(
359 :login => 'changed', :password => 'admin',
362 :login => 'changed', :password => 'admin',
360 :password_confirmation => 'admin')
363 :password_confirmation => 'admin')
361 # bypass validations to make it appear like existing data
364 # bypass validations to make it appear like existing data
362 case_sensitive_user.update_attribute(:login, 'ADMIN')
365 case_sensitive_user.update_attribute(:login, 'ADMIN')
363
366
364 user = User.try_to_login("ADMIN", "admin")
367 user = User.try_to_login("ADMIN", "admin")
365 assert_kind_of User, user
368 assert_kind_of User, user
366 assert_equal "ADMIN", user.login
369 assert_equal "ADMIN", user.login
367
370
368 end
371 end
369 end
372 end
370
373
371 def test_password
374 def test_password
372 user = User.try_to_login("admin", "admin")
375 user = User.try_to_login("admin", "admin")
373 assert_kind_of User, user
376 assert_kind_of User, user
374 assert_equal "admin", user.login
377 assert_equal "admin", user.login
375 user.password = "hello"
378 user.password = "hello"
376 assert user.save
379 assert user.save
377
380
378 user = User.try_to_login("admin", "hello")
381 user = User.try_to_login("admin", "hello")
379 assert_kind_of User, user
382 assert_kind_of User, user
380 assert_equal "admin", user.login
383 assert_equal "admin", user.login
381 end
384 end
382
385
383 def test_validate_password_length
386 def test_validate_password_length
384 with_settings :password_min_length => '100' do
387 with_settings :password_min_length => '100' do
385 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
388 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
386 user.login = "newuser100"
389 user.login = "newuser100"
387 user.password, user.password_confirmation = "password100", "password100"
390 user.password, user.password_confirmation = "password100", "password100"
388 assert !user.save
391 assert !user.save
389 assert_equal 1, user.errors.count
392 assert_equal 1, user.errors.count
390 end
393 end
391 end
394 end
392
395
393 def test_name_format
396 def test_name_format
394 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
397 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
395 with_settings :user_format => :firstname_lastname do
398 with_settings :user_format => :firstname_lastname do
396 assert_equal 'John Smith', @jsmith.reload.name
399 assert_equal 'John Smith', @jsmith.reload.name
397 end
400 end
398 with_settings :user_format => :username do
401 with_settings :user_format => :username do
399 assert_equal 'jsmith', @jsmith.reload.name
402 assert_equal 'jsmith', @jsmith.reload.name
400 end
403 end
401 end
404 end
402
405
403 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
406 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
404 with_settings :user_format => 'lastname_coma_firstname' do
407 with_settings :user_format => 'lastname_coma_firstname' do
405 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
408 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
406 end
409 end
407 end
410 end
408
411
409 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
412 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
410 with_settings :user_format => 'lastname_firstname' do
413 with_settings :user_format => 'lastname_firstname' do
411 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
414 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
412 end
415 end
413 end
416 end
414
417
415 def test_fields_for_order_statement_with_blank_format_should_return_default
418 def test_fields_for_order_statement_with_blank_format_should_return_default
416 with_settings :user_format => '' do
419 with_settings :user_format => '' do
417 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
420 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
418 end
421 end
419 end
422 end
420
423
421 def test_fields_for_order_statement_with_invalid_format_should_return_default
424 def test_fields_for_order_statement_with_invalid_format_should_return_default
422 with_settings :user_format => 'foo' do
425 with_settings :user_format => 'foo' do
423 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
426 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
424 end
427 end
425 end
428 end
426
429
427 def test_lock
430 def test_lock
428 user = User.try_to_login("jsmith", "jsmith")
431 user = User.try_to_login("jsmith", "jsmith")
429 assert_equal @jsmith, user
432 assert_equal @jsmith, user
430
433
431 @jsmith.status = User::STATUS_LOCKED
434 @jsmith.status = User::STATUS_LOCKED
432 assert @jsmith.save
435 assert @jsmith.save
433
436
434 user = User.try_to_login("jsmith", "jsmith")
437 user = User.try_to_login("jsmith", "jsmith")
435 assert_equal nil, user
438 assert_equal nil, user
436 end
439 end
437
440
438 context ".try_to_login" do
441 context ".try_to_login" do
439 context "with good credentials" do
442 context "with good credentials" do
440 should "return the user" do
443 should "return the user" do
441 user = User.try_to_login("admin", "admin")
444 user = User.try_to_login("admin", "admin")
442 assert_kind_of User, user
445 assert_kind_of User, user
443 assert_equal "admin", user.login
446 assert_equal "admin", user.login
444 end
447 end
445 end
448 end
446
449
447 context "with wrong credentials" do
450 context "with wrong credentials" do
448 should "return nil" do
451 should "return nil" do
449 assert_nil User.try_to_login("admin", "foo")
452 assert_nil User.try_to_login("admin", "foo")
450 end
453 end
451 end
454 end
452 end
455 end
453
456
454 if ldap_configured?
457 if ldap_configured?
455 context "#try_to_login using LDAP" do
458 context "#try_to_login using LDAP" do
456 context "with failed connection to the LDAP server" do
459 context "with failed connection to the LDAP server" do
457 should "return nil" do
460 should "return nil" do
458 @auth_source = AuthSourceLdap.find(1)
461 @auth_source = AuthSourceLdap.find(1)
459 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
462 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
460
463
461 assert_equal nil, User.try_to_login('edavis', 'wrong')
464 assert_equal nil, User.try_to_login('edavis', 'wrong')
462 end
465 end
463 end
466 end
464
467
465 context "with an unsuccessful authentication" do
468 context "with an unsuccessful authentication" do
466 should "return nil" do
469 should "return nil" do
467 assert_equal nil, User.try_to_login('edavis', 'wrong')
470 assert_equal nil, User.try_to_login('edavis', 'wrong')
468 end
471 end
469 end
472 end
470
473
471 context "on the fly registration" do
474 context "on the fly registration" do
472 setup do
475 setup do
473 @auth_source = AuthSourceLdap.find(1)
476 @auth_source = AuthSourceLdap.find(1)
474 end
477 end
475
478
476 context "with a successful authentication" do
479 context "with a successful authentication" do
477 should "create a new user account if it doesn't exist" do
480 should "create a new user account if it doesn't exist" do
478 assert_difference('User.count') do
481 assert_difference('User.count') do
479 user = User.try_to_login('edavis', '123456')
482 user = User.try_to_login('edavis', '123456')
480 assert !user.admin?
483 assert !user.admin?
481 end
484 end
482 end
485 end
483
486
484 should "retrieve existing user" do
487 should "retrieve existing user" do
485 user = User.try_to_login('edavis', '123456')
488 user = User.try_to_login('edavis', '123456')
486 user.admin = true
489 user.admin = true
487 user.save!
490 user.save!
488
491
489 assert_no_difference('User.count') do
492 assert_no_difference('User.count') do
490 user = User.try_to_login('edavis', '123456')
493 user = User.try_to_login('edavis', '123456')
491 assert user.admin?
494 assert user.admin?
492 end
495 end
493 end
496 end
494 end
497 end
495 end
498 end
496 end
499 end
497
500
498 else
501 else
499 puts "Skipping LDAP tests."
502 puts "Skipping LDAP tests."
500 end
503 end
501
504
502 def test_create_anonymous
505 def test_create_anonymous
503 AnonymousUser.delete_all
506 AnonymousUser.delete_all
504 anon = User.anonymous
507 anon = User.anonymous
505 assert !anon.new_record?
508 assert !anon.new_record?
506 assert_kind_of AnonymousUser, anon
509 assert_kind_of AnonymousUser, anon
507 end
510 end
508
511
509 def test_ensure_single_anonymous_user
512 def test_ensure_single_anonymous_user
510 AnonymousUser.delete_all
513 AnonymousUser.delete_all
511 anon1 = User.anonymous
514 anon1 = User.anonymous
512 assert !anon1.new_record?
515 assert !anon1.new_record?
513 assert_kind_of AnonymousUser, anon1
516 assert_kind_of AnonymousUser, anon1
514 anon2 = AnonymousUser.create(
517 anon2 = AnonymousUser.create(
515 :lastname => 'Anonymous', :firstname => '',
518 :lastname => 'Anonymous', :firstname => '',
516 :mail => '', :login => '', :status => 0)
519 :mail => '', :login => '', :status => 0)
517 assert_equal 1, anon2.errors.count
520 assert_equal 1, anon2.errors.count
518 end
521 end
519
522
520 should_have_one :rss_token
523 should_have_one :rss_token
521
524
522 def test_rss_key
525 def test_rss_key
523 assert_nil @jsmith.rss_token
526 assert_nil @jsmith.rss_token
524 key = @jsmith.rss_key
527 key = @jsmith.rss_key
525 assert_equal 40, key.length
528 assert_equal 40, key.length
526
529
527 @jsmith.reload
530 @jsmith.reload
528 assert_equal key, @jsmith.rss_key
531 assert_equal key, @jsmith.rss_key
529 end
532 end
530
533
531
534
532 should_have_one :api_token
535 should_have_one :api_token
533
536
534 context "User#api_key" do
537 context "User#api_key" do
535 should "generate a new one if the user doesn't have one" do
538 should "generate a new one if the user doesn't have one" do
536 user = User.generate_with_protected!(:api_token => nil)
539 user = User.generate_with_protected!(:api_token => nil)
537 assert_nil user.api_token
540 assert_nil user.api_token
538
541
539 key = user.api_key
542 key = user.api_key
540 assert_equal 40, key.length
543 assert_equal 40, key.length
541 user.reload
544 user.reload
542 assert_equal key, user.api_key
545 assert_equal key, user.api_key
543 end
546 end
544
547
545 should "return the existing api token value" do
548 should "return the existing api token value" do
546 user = User.generate_with_protected!
549 user = User.generate_with_protected!
547 token = Token.generate!(:action => 'api')
550 token = Token.generate!(:action => 'api')
548 user.api_token = token
551 user.api_token = token
549 assert user.save
552 assert user.save
550
553
551 assert_equal token.value, user.api_key
554 assert_equal token.value, user.api_key
552 end
555 end
553 end
556 end
554
557
555 context "User#find_by_api_key" do
558 context "User#find_by_api_key" do
556 should "return nil if no matching key is found" do
559 should "return nil if no matching key is found" do
557 assert_nil User.find_by_api_key('zzzzzzzzz')
560 assert_nil User.find_by_api_key('zzzzzzzzz')
558 end
561 end
559
562
560 should "return nil if the key is found for an inactive user" do
563 should "return nil if the key is found for an inactive user" do
561 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
564 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
562 token = Token.generate!(:action => 'api')
565 token = Token.generate!(:action => 'api')
563 user.api_token = token
566 user.api_token = token
564 user.save
567 user.save
565
568
566 assert_nil User.find_by_api_key(token.value)
569 assert_nil User.find_by_api_key(token.value)
567 end
570 end
568
571
569 should "return the user if the key is found for an active user" do
572 should "return the user if the key is found for an active user" do
570 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
573 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
571 token = Token.generate!(:action => 'api')
574 token = Token.generate!(:action => 'api')
572 user.api_token = token
575 user.api_token = token
573 user.save
576 user.save
574
577
575 assert_equal user, User.find_by_api_key(token.value)
578 assert_equal user, User.find_by_api_key(token.value)
576 end
579 end
577 end
580 end
578
581
579 def test_roles_for_project
582 def test_roles_for_project
580 # user with a role
583 # user with a role
581 roles = @jsmith.roles_for_project(Project.find(1))
584 roles = @jsmith.roles_for_project(Project.find(1))
582 assert_kind_of Role, roles.first
585 assert_kind_of Role, roles.first
583 assert_equal "Manager", roles.first.name
586 assert_equal "Manager", roles.first.name
584
587
585 # user with no role
588 # user with no role
586 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
589 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
587 end
590 end
588
591
589 def test_projects_by_role_for_user_with_role
592 def test_projects_by_role_for_user_with_role
590 user = User.find(2)
593 user = User.find(2)
591 assert_kind_of Hash, user.projects_by_role
594 assert_kind_of Hash, user.projects_by_role
592 assert_equal 2, user.projects_by_role.size
595 assert_equal 2, user.projects_by_role.size
593 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
596 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
594 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
597 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
595 end
598 end
596
599
597 def test_projects_by_role_for_user_with_no_role
600 def test_projects_by_role_for_user_with_no_role
598 user = User.generate!
601 user = User.generate!
599 assert_equal({}, user.projects_by_role)
602 assert_equal({}, user.projects_by_role)
600 end
603 end
601
604
602 def test_projects_by_role_for_anonymous
605 def test_projects_by_role_for_anonymous
603 assert_equal({}, User.anonymous.projects_by_role)
606 assert_equal({}, User.anonymous.projects_by_role)
604 end
607 end
605
608
606 def test_valid_notification_options
609 def test_valid_notification_options
607 # without memberships
610 # without memberships
608 assert_equal 5, User.find(7).valid_notification_options.size
611 assert_equal 5, User.find(7).valid_notification_options.size
609 # with memberships
612 # with memberships
610 assert_equal 6, User.find(2).valid_notification_options.size
613 assert_equal 6, User.find(2).valid_notification_options.size
611 end
614 end
612
615
613 def test_valid_notification_options_class_method
616 def test_valid_notification_options_class_method
614 assert_equal 5, User.valid_notification_options.size
617 assert_equal 5, User.valid_notification_options.size
615 assert_equal 5, User.valid_notification_options(User.find(7)).size
618 assert_equal 5, User.valid_notification_options(User.find(7)).size
616 assert_equal 6, User.valid_notification_options(User.find(2)).size
619 assert_equal 6, User.valid_notification_options(User.find(2)).size
617 end
620 end
618
621
619 def test_mail_notification_all
622 def test_mail_notification_all
620 @jsmith.mail_notification = 'all'
623 @jsmith.mail_notification = 'all'
621 @jsmith.notified_project_ids = []
624 @jsmith.notified_project_ids = []
622 @jsmith.save
625 @jsmith.save
623 @jsmith.reload
626 @jsmith.reload
624 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
627 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
625 end
628 end
626
629
627 def test_mail_notification_selected
630 def test_mail_notification_selected
628 @jsmith.mail_notification = 'selected'
631 @jsmith.mail_notification = 'selected'
629 @jsmith.notified_project_ids = [1]
632 @jsmith.notified_project_ids = [1]
630 @jsmith.save
633 @jsmith.save
631 @jsmith.reload
634 @jsmith.reload
632 assert Project.find(1).recipients.include?(@jsmith.mail)
635 assert Project.find(1).recipients.include?(@jsmith.mail)
633 end
636 end
634
637
635 def test_mail_notification_only_my_events
638 def test_mail_notification_only_my_events
636 @jsmith.mail_notification = 'only_my_events'
639 @jsmith.mail_notification = 'only_my_events'
637 @jsmith.notified_project_ids = []
640 @jsmith.notified_project_ids = []
638 @jsmith.save
641 @jsmith.save
639 @jsmith.reload
642 @jsmith.reload
640 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
643 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
641 end
644 end
642
645
643 def test_comments_sorting_preference
646 def test_comments_sorting_preference
644 assert !@jsmith.wants_comments_in_reverse_order?
647 assert !@jsmith.wants_comments_in_reverse_order?
645 @jsmith.pref.comments_sorting = 'asc'
648 @jsmith.pref.comments_sorting = 'asc'
646 assert !@jsmith.wants_comments_in_reverse_order?
649 assert !@jsmith.wants_comments_in_reverse_order?
647 @jsmith.pref.comments_sorting = 'desc'
650 @jsmith.pref.comments_sorting = 'desc'
648 assert @jsmith.wants_comments_in_reverse_order?
651 assert @jsmith.wants_comments_in_reverse_order?
649 end
652 end
650
653
651 def test_find_by_mail_should_be_case_insensitive
654 def test_find_by_mail_should_be_case_insensitive
652 u = User.find_by_mail('JSmith@somenet.foo')
655 u = User.find_by_mail('JSmith@somenet.foo')
653 assert_not_nil u
656 assert_not_nil u
654 assert_equal 'jsmith@somenet.foo', u.mail
657 assert_equal 'jsmith@somenet.foo', u.mail
655 end
658 end
656
659
657 def test_random_password
660 def test_random_password
658 u = User.new
661 u = User.new
659 u.random_password
662 u.random_password
660 assert !u.password.blank?
663 assert !u.password.blank?
661 assert !u.password_confirmation.blank?
664 assert !u.password_confirmation.blank?
662 end
665 end
663
666
664 context "#change_password_allowed?" do
667 context "#change_password_allowed?" do
665 should "be allowed if no auth source is set" do
668 should "be allowed if no auth source is set" do
666 user = User.generate_with_protected!
669 user = User.generate_with_protected!
667 assert user.change_password_allowed?
670 assert user.change_password_allowed?
668 end
671 end
669
672
670 should "delegate to the auth source" do
673 should "delegate to the auth source" do
671 user = User.generate_with_protected!
674 user = User.generate_with_protected!
672
675
673 allowed_auth_source = AuthSource.generate!
676 allowed_auth_source = AuthSource.generate!
674 def allowed_auth_source.allow_password_changes?; true; end
677 def allowed_auth_source.allow_password_changes?; true; end
675
678
676 denied_auth_source = AuthSource.generate!
679 denied_auth_source = AuthSource.generate!
677 def denied_auth_source.allow_password_changes?; false; end
680 def denied_auth_source.allow_password_changes?; false; end
678
681
679 assert user.change_password_allowed?
682 assert user.change_password_allowed?
680
683
681 user.auth_source = allowed_auth_source
684 user.auth_source = allowed_auth_source
682 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
685 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
683
686
684 user.auth_source = denied_auth_source
687 user.auth_source = denied_auth_source
685 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
688 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
686 end
689 end
687
690
688 end
691 end
689
692
690 context "#allowed_to?" do
693 context "#allowed_to?" do
691 context "with a unique project" do
694 context "with a unique project" do
692 should "return false if project is archived" do
695 should "return false if project is archived" do
693 project = Project.find(1)
696 project = Project.find(1)
694 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
697 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
695 assert ! @admin.allowed_to?(:view_issues, Project.find(1))
698 assert ! @admin.allowed_to?(:view_issues, Project.find(1))
696 end
699 end
697
700
698 should "return false if related module is disabled" do
701 should "return false if related module is disabled" do
699 project = Project.find(1)
702 project = Project.find(1)
700 project.enabled_module_names = ["issue_tracking"]
703 project.enabled_module_names = ["issue_tracking"]
701 assert @admin.allowed_to?(:add_issues, project)
704 assert @admin.allowed_to?(:add_issues, project)
702 assert ! @admin.allowed_to?(:view_wiki_pages, project)
705 assert ! @admin.allowed_to?(:view_wiki_pages, project)
703 end
706 end
704
707
705 should "authorize nearly everything for admin users" do
708 should "authorize nearly everything for admin users" do
706 project = Project.find(1)
709 project = Project.find(1)
707 assert ! @admin.member_of?(project)
710 assert ! @admin.member_of?(project)
708 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
711 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
709 assert @admin.allowed_to?(p.to_sym, project)
712 assert @admin.allowed_to?(p.to_sym, project)
710 end
713 end
711 end
714 end
712
715
713 should "authorize normal users depending on their roles" do
716 should "authorize normal users depending on their roles" do
714 project = Project.find(1)
717 project = Project.find(1)
715 assert @jsmith.allowed_to?(:delete_messages, project) #Manager
718 assert @jsmith.allowed_to?(:delete_messages, project) #Manager
716 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
719 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
717 end
720 end
718 end
721 end
719
722
720 context "with multiple projects" do
723 context "with multiple projects" do
721 should "return false if array is empty" do
724 should "return false if array is empty" do
722 assert ! @admin.allowed_to?(:view_project, [])
725 assert ! @admin.allowed_to?(:view_project, [])
723 end
726 end
724
727
725 should "return true only if user has permission on all these projects" do
728 should "return true only if user has permission on all these projects" do
726 assert @admin.allowed_to?(:view_project, Project.all)
729 assert @admin.allowed_to?(:view_project, Project.all)
727 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
730 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
728 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
731 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
729 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
732 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
730 end
733 end
731
734
732 should "behave correctly with arrays of 1 project" do
735 should "behave correctly with arrays of 1 project" do
733 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
736 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
734 end
737 end
735 end
738 end
736
739
737 context "with options[:global]" do
740 context "with options[:global]" do
738 should "authorize if user has at least one role that has this permission" do
741 should "authorize if user has at least one role that has this permission" do
739 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
742 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
740 @anonymous = User.find(6)
743 @anonymous = User.find(6)
741 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
744 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
742 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
745 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
743 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
746 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
744 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
747 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
745 assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
748 assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
746 end
749 end
747 end
750 end
748 end
751 end
749
752
750 context "User#notify_about?" do
753 context "User#notify_about?" do
751 context "Issues" do
754 context "Issues" do
752 setup do
755 setup do
753 @project = Project.find(1)
756 @project = Project.find(1)
754 @author = User.generate_with_protected!
757 @author = User.generate_with_protected!
755 @assignee = User.generate_with_protected!
758 @assignee = User.generate_with_protected!
756 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
759 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
757 end
760 end
758
761
759 should "be true for a user with :all" do
762 should "be true for a user with :all" do
760 @author.update_attribute(:mail_notification, 'all')
763 @author.update_attribute(:mail_notification, 'all')
761 assert @author.notify_about?(@issue)
764 assert @author.notify_about?(@issue)
762 end
765 end
763
766
764 should "be false for a user with :none" do
767 should "be false for a user with :none" do
765 @author.update_attribute(:mail_notification, 'none')
768 @author.update_attribute(:mail_notification, 'none')
766 assert ! @author.notify_about?(@issue)
769 assert ! @author.notify_about?(@issue)
767 end
770 end
768
771
769 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
772 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
770 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
773 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
771 Member.create!(:user => @user, :project => @project, :role_ids => [1])
774 Member.create!(:user => @user, :project => @project, :role_ids => [1])
772 assert ! @user.notify_about?(@issue)
775 assert ! @user.notify_about?(@issue)
773 end
776 end
774
777
775 should "be true for a user with :only_my_events and is the author" do
778 should "be true for a user with :only_my_events and is the author" do
776 @author.update_attribute(:mail_notification, 'only_my_events')
779 @author.update_attribute(:mail_notification, 'only_my_events')
777 assert @author.notify_about?(@issue)
780 assert @author.notify_about?(@issue)
778 end
781 end
779
782
780 should "be true for a user with :only_my_events and is the assignee" do
783 should "be true for a user with :only_my_events and is the assignee" do
781 @assignee.update_attribute(:mail_notification, 'only_my_events')
784 @assignee.update_attribute(:mail_notification, 'only_my_events')
782 assert @assignee.notify_about?(@issue)
785 assert @assignee.notify_about?(@issue)
783 end
786 end
784
787
785 should "be true for a user with :only_assigned and is the assignee" do
788 should "be true for a user with :only_assigned and is the assignee" do
786 @assignee.update_attribute(:mail_notification, 'only_assigned')
789 @assignee.update_attribute(:mail_notification, 'only_assigned')
787 assert @assignee.notify_about?(@issue)
790 assert @assignee.notify_about?(@issue)
788 end
791 end
789
792
790 should "be false for a user with :only_assigned and is not the assignee" do
793 should "be false for a user with :only_assigned and is not the assignee" do
791 @author.update_attribute(:mail_notification, 'only_assigned')
794 @author.update_attribute(:mail_notification, 'only_assigned')
792 assert ! @author.notify_about?(@issue)
795 assert ! @author.notify_about?(@issue)
793 end
796 end
794
797
795 should "be true for a user with :only_owner and is the author" do
798 should "be true for a user with :only_owner and is the author" do
796 @author.update_attribute(:mail_notification, 'only_owner')
799 @author.update_attribute(:mail_notification, 'only_owner')
797 assert @author.notify_about?(@issue)
800 assert @author.notify_about?(@issue)
798 end
801 end
799
802
800 should "be false for a user with :only_owner and is not the author" do
803 should "be false for a user with :only_owner and is not the author" do
801 @assignee.update_attribute(:mail_notification, 'only_owner')
804 @assignee.update_attribute(:mail_notification, 'only_owner')
802 assert ! @assignee.notify_about?(@issue)
805 assert ! @assignee.notify_about?(@issue)
803 end
806 end
804
807
805 should "be true for a user with :selected and is the author" do
808 should "be true for a user with :selected and is the author" do
806 @author.update_attribute(:mail_notification, 'selected')
809 @author.update_attribute(:mail_notification, 'selected')
807 assert @author.notify_about?(@issue)
810 assert @author.notify_about?(@issue)
808 end
811 end
809
812
810 should "be true for a user with :selected and is the assignee" do
813 should "be true for a user with :selected and is the assignee" do
811 @assignee.update_attribute(:mail_notification, 'selected')
814 @assignee.update_attribute(:mail_notification, 'selected')
812 assert @assignee.notify_about?(@issue)
815 assert @assignee.notify_about?(@issue)
813 end
816 end
814
817
815 should "be false for a user with :selected and is not the author or assignee" do
818 should "be false for a user with :selected and is not the author or assignee" do
816 @user = User.generate_with_protected!(:mail_notification => 'selected')
819 @user = User.generate_with_protected!(:mail_notification => 'selected')
817 Member.create!(:user => @user, :project => @project, :role_ids => [1])
820 Member.create!(:user => @user, :project => @project, :role_ids => [1])
818 assert ! @user.notify_about?(@issue)
821 assert ! @user.notify_about?(@issue)
819 end
822 end
820 end
823 end
821
824
822 context "other events" do
825 context "other events" do
823 should 'be added and tested'
826 should 'be added and tested'
824 end
827 end
825 end
828 end
826
829
827 def test_salt_unsalted_passwords
830 def test_salt_unsalted_passwords
828 # Restore a user with an unsalted password
831 # Restore a user with an unsalted password
829 user = User.find(1)
832 user = User.find(1)
830 user.salt = nil
833 user.salt = nil
831 user.hashed_password = User.hash_password("unsalted")
834 user.hashed_password = User.hash_password("unsalted")
832 user.save!
835 user.save!
833
836
834 User.salt_unsalted_passwords!
837 User.salt_unsalted_passwords!
835
838
836 user.reload
839 user.reload
837 # Salt added
840 # Salt added
838 assert !user.salt.blank?
841 assert !user.salt.blank?
839 # Password still valid
842 # Password still valid
840 assert user.check_password?("unsalted")
843 assert user.check_password?("unsalted")
841 assert_equal user, User.try_to_login(user.login, "unsalted")
844 assert_equal user, User.try_to_login(user.login, "unsalted")
842 end
845 end
843
846
844 if Object.const_defined?(:OpenID)
847 if Object.const_defined?(:OpenID)
845
848
846 def test_setting_identity_url
849 def test_setting_identity_url
847 normalized_open_id_url = 'http://example.com/'
850 normalized_open_id_url = 'http://example.com/'
848 u = User.new( :identity_url => 'http://example.com/' )
851 u = User.new( :identity_url => 'http://example.com/' )
849 assert_equal normalized_open_id_url, u.identity_url
852 assert_equal normalized_open_id_url, u.identity_url
850 end
853 end
851
854
852 def test_setting_identity_url_without_trailing_slash
855 def test_setting_identity_url_without_trailing_slash
853 normalized_open_id_url = 'http://example.com/'
856 normalized_open_id_url = 'http://example.com/'
854 u = User.new( :identity_url => 'http://example.com' )
857 u = User.new( :identity_url => 'http://example.com' )
855 assert_equal normalized_open_id_url, u.identity_url
858 assert_equal normalized_open_id_url, u.identity_url
856 end
859 end
857
860
858 def test_setting_identity_url_without_protocol
861 def test_setting_identity_url_without_protocol
859 normalized_open_id_url = 'http://example.com/'
862 normalized_open_id_url = 'http://example.com/'
860 u = User.new( :identity_url => 'example.com' )
863 u = User.new( :identity_url => 'example.com' )
861 assert_equal normalized_open_id_url, u.identity_url
864 assert_equal normalized_open_id_url, u.identity_url
862 end
865 end
863
866
864 def test_setting_blank_identity_url
867 def test_setting_blank_identity_url
865 u = User.new( :identity_url => 'example.com' )
868 u = User.new( :identity_url => 'example.com' )
866 u.identity_url = ''
869 u.identity_url = ''
867 assert u.identity_url.blank?
870 assert u.identity_url.blank?
868 end
871 end
869
872
870 def test_setting_invalid_identity_url
873 def test_setting_invalid_identity_url
871 u = User.new( :identity_url => 'this is not an openid url' )
874 u = User.new( :identity_url => 'this is not an openid url' )
872 assert u.identity_url.blank?
875 assert u.identity_url.blank?
873 end
876 end
874
877
875 else
878 else
876 puts "Skipping openid tests."
879 puts "Skipping openid tests."
877 end
880 end
878
881
879 end
882 end
General Comments 0
You need to be logged in to leave comments. Login now