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