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