@@ -1,822 +1,832 | |||||
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_validate_password_length | |||
|
374 | with_settings :password_min_length => '100' do | |||
|
375 | user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo") | |||
|
376 | user.login = "newuser100" | |||
|
377 | user.password, user.password_confirmation = "password100", "password100" | |||
|
378 | assert !user.save | |||
|
379 | assert_equal 1, user.errors.count | |||
|
380 | end | |||
|
381 | end | |||
|
382 | ||||
373 | def test_name_format |
|
383 | def test_name_format | |
374 | assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname) |
|
384 | assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname) | |
375 | Setting.user_format = :firstname_lastname |
|
385 | Setting.user_format = :firstname_lastname | |
376 | assert_equal 'John Smith', @jsmith.reload.name |
|
386 | assert_equal 'John Smith', @jsmith.reload.name | |
377 | Setting.user_format = :username |
|
387 | Setting.user_format = :username | |
378 | assert_equal 'jsmith', @jsmith.reload.name |
|
388 | assert_equal 'jsmith', @jsmith.reload.name | |
379 | end |
|
389 | end | |
380 |
|
390 | |||
381 | def test_lock |
|
391 | def test_lock | |
382 | user = User.try_to_login("jsmith", "jsmith") |
|
392 | user = User.try_to_login("jsmith", "jsmith") | |
383 | assert_equal @jsmith, user |
|
393 | assert_equal @jsmith, user | |
384 |
|
394 | |||
385 | @jsmith.status = User::STATUS_LOCKED |
|
395 | @jsmith.status = User::STATUS_LOCKED | |
386 | assert @jsmith.save |
|
396 | assert @jsmith.save | |
387 |
|
397 | |||
388 | user = User.try_to_login("jsmith", "jsmith") |
|
398 | user = User.try_to_login("jsmith", "jsmith") | |
389 | assert_equal nil, user |
|
399 | assert_equal nil, user | |
390 | end |
|
400 | end | |
391 |
|
401 | |||
392 | context ".try_to_login" do |
|
402 | context ".try_to_login" do | |
393 | context "with good credentials" do |
|
403 | context "with good credentials" do | |
394 | should "return the user" do |
|
404 | should "return the user" do | |
395 | user = User.try_to_login("admin", "admin") |
|
405 | user = User.try_to_login("admin", "admin") | |
396 | assert_kind_of User, user |
|
406 | assert_kind_of User, user | |
397 | assert_equal "admin", user.login |
|
407 | assert_equal "admin", user.login | |
398 | end |
|
408 | end | |
399 | end |
|
409 | end | |
400 |
|
410 | |||
401 | context "with wrong credentials" do |
|
411 | context "with wrong credentials" do | |
402 | should "return nil" do |
|
412 | should "return nil" do | |
403 | assert_nil User.try_to_login("admin", "foo") |
|
413 | assert_nil User.try_to_login("admin", "foo") | |
404 | end |
|
414 | end | |
405 | end |
|
415 | end | |
406 | end |
|
416 | end | |
407 |
|
417 | |||
408 | if ldap_configured? |
|
418 | if ldap_configured? | |
409 | context "#try_to_login using LDAP" do |
|
419 | context "#try_to_login using LDAP" do | |
410 | context "with failed connection to the LDAP server" do |
|
420 | context "with failed connection to the LDAP server" do | |
411 | should "return nil" do |
|
421 | should "return nil" do | |
412 | @auth_source = AuthSourceLdap.find(1) |
|
422 | @auth_source = AuthSourceLdap.find(1) | |
413 | AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect') |
|
423 | AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect') | |
414 |
|
424 | |||
415 | assert_equal nil, User.try_to_login('edavis', 'wrong') |
|
425 | assert_equal nil, User.try_to_login('edavis', 'wrong') | |
416 | end |
|
426 | end | |
417 | end |
|
427 | end | |
418 |
|
428 | |||
419 | context "with an unsuccessful authentication" do |
|
429 | context "with an unsuccessful authentication" do | |
420 | should "return nil" do |
|
430 | should "return nil" do | |
421 | assert_equal nil, User.try_to_login('edavis', 'wrong') |
|
431 | assert_equal nil, User.try_to_login('edavis', 'wrong') | |
422 | end |
|
432 | end | |
423 | end |
|
433 | end | |
424 |
|
434 | |||
425 | context "on the fly registration" do |
|
435 | context "on the fly registration" do | |
426 | setup do |
|
436 | setup do | |
427 | @auth_source = AuthSourceLdap.find(1) |
|
437 | @auth_source = AuthSourceLdap.find(1) | |
428 | end |
|
438 | end | |
429 |
|
439 | |||
430 | context "with a successful authentication" do |
|
440 | context "with a successful authentication" do | |
431 | should "create a new user account if it doesn't exist" do |
|
441 | should "create a new user account if it doesn't exist" do | |
432 | assert_difference('User.count') do |
|
442 | assert_difference('User.count') do | |
433 | user = User.try_to_login('edavis', '123456') |
|
443 | user = User.try_to_login('edavis', '123456') | |
434 | assert !user.admin? |
|
444 | assert !user.admin? | |
435 | end |
|
445 | end | |
436 | end |
|
446 | end | |
437 |
|
447 | |||
438 | should "retrieve existing user" do |
|
448 | should "retrieve existing user" do | |
439 | user = User.try_to_login('edavis', '123456') |
|
449 | user = User.try_to_login('edavis', '123456') | |
440 | user.admin = true |
|
450 | user.admin = true | |
441 | user.save! |
|
451 | user.save! | |
442 |
|
452 | |||
443 | assert_no_difference('User.count') do |
|
453 | assert_no_difference('User.count') do | |
444 | user = User.try_to_login('edavis', '123456') |
|
454 | user = User.try_to_login('edavis', '123456') | |
445 | assert user.admin? |
|
455 | assert user.admin? | |
446 | end |
|
456 | end | |
447 | end |
|
457 | end | |
448 | end |
|
458 | end | |
449 | end |
|
459 | end | |
450 | end |
|
460 | end | |
451 |
|
461 | |||
452 | else |
|
462 | else | |
453 | puts "Skipping LDAP tests." |
|
463 | puts "Skipping LDAP tests." | |
454 | end |
|
464 | end | |
455 |
|
465 | |||
456 | def test_create_anonymous |
|
466 | def test_create_anonymous | |
457 | AnonymousUser.delete_all |
|
467 | AnonymousUser.delete_all | |
458 | anon = User.anonymous |
|
468 | anon = User.anonymous | |
459 | assert !anon.new_record? |
|
469 | assert !anon.new_record? | |
460 | assert_kind_of AnonymousUser, anon |
|
470 | assert_kind_of AnonymousUser, anon | |
461 | end |
|
471 | end | |
462 |
|
472 | |||
463 | should_have_one :rss_token |
|
473 | should_have_one :rss_token | |
464 |
|
474 | |||
465 | def test_rss_key |
|
475 | def test_rss_key | |
466 | assert_nil @jsmith.rss_token |
|
476 | assert_nil @jsmith.rss_token | |
467 | key = @jsmith.rss_key |
|
477 | key = @jsmith.rss_key | |
468 | assert_equal 40, key.length |
|
478 | assert_equal 40, key.length | |
469 |
|
479 | |||
470 | @jsmith.reload |
|
480 | @jsmith.reload | |
471 | assert_equal key, @jsmith.rss_key |
|
481 | assert_equal key, @jsmith.rss_key | |
472 | end |
|
482 | end | |
473 |
|
483 | |||
474 |
|
484 | |||
475 | should_have_one :api_token |
|
485 | should_have_one :api_token | |
476 |
|
486 | |||
477 | context "User#api_key" do |
|
487 | context "User#api_key" do | |
478 | should "generate a new one if the user doesn't have one" do |
|
488 | should "generate a new one if the user doesn't have one" do | |
479 | user = User.generate_with_protected!(:api_token => nil) |
|
489 | user = User.generate_with_protected!(:api_token => nil) | |
480 | assert_nil user.api_token |
|
490 | assert_nil user.api_token | |
481 |
|
491 | |||
482 | key = user.api_key |
|
492 | key = user.api_key | |
483 | assert_equal 40, key.length |
|
493 | assert_equal 40, key.length | |
484 | user.reload |
|
494 | user.reload | |
485 | assert_equal key, user.api_key |
|
495 | assert_equal key, user.api_key | |
486 | end |
|
496 | end | |
487 |
|
497 | |||
488 | should "return the existing api token value" do |
|
498 | should "return the existing api token value" do | |
489 | user = User.generate_with_protected! |
|
499 | user = User.generate_with_protected! | |
490 | token = Token.generate!(:action => 'api') |
|
500 | token = Token.generate!(:action => 'api') | |
491 | user.api_token = token |
|
501 | user.api_token = token | |
492 | assert user.save |
|
502 | assert user.save | |
493 |
|
503 | |||
494 | assert_equal token.value, user.api_key |
|
504 | assert_equal token.value, user.api_key | |
495 | end |
|
505 | end | |
496 | end |
|
506 | end | |
497 |
|
507 | |||
498 | context "User#find_by_api_key" do |
|
508 | context "User#find_by_api_key" do | |
499 | should "return nil if no matching key is found" do |
|
509 | should "return nil if no matching key is found" do | |
500 | assert_nil User.find_by_api_key('zzzzzzzzz') |
|
510 | assert_nil User.find_by_api_key('zzzzzzzzz') | |
501 | end |
|
511 | end | |
502 |
|
512 | |||
503 | should "return nil if the key is found for an inactive user" do |
|
513 | should "return nil if the key is found for an inactive user" do | |
504 | user = User.generate_with_protected!(:status => User::STATUS_LOCKED) |
|
514 | user = User.generate_with_protected!(:status => User::STATUS_LOCKED) | |
505 | token = Token.generate!(:action => 'api') |
|
515 | token = Token.generate!(:action => 'api') | |
506 | user.api_token = token |
|
516 | user.api_token = token | |
507 | user.save |
|
517 | user.save | |
508 |
|
518 | |||
509 | assert_nil User.find_by_api_key(token.value) |
|
519 | assert_nil User.find_by_api_key(token.value) | |
510 | end |
|
520 | end | |
511 |
|
521 | |||
512 | should "return the user if the key is found for an active user" do |
|
522 | should "return the user if the key is found for an active user" do | |
513 | user = User.generate_with_protected!(:status => User::STATUS_ACTIVE) |
|
523 | user = User.generate_with_protected!(:status => User::STATUS_ACTIVE) | |
514 | token = Token.generate!(:action => 'api') |
|
524 | token = Token.generate!(:action => 'api') | |
515 | user.api_token = token |
|
525 | user.api_token = token | |
516 | user.save |
|
526 | user.save | |
517 |
|
527 | |||
518 | assert_equal user, User.find_by_api_key(token.value) |
|
528 | assert_equal user, User.find_by_api_key(token.value) | |
519 | end |
|
529 | end | |
520 | end |
|
530 | end | |
521 |
|
531 | |||
522 | def test_roles_for_project |
|
532 | def test_roles_for_project | |
523 | # user with a role |
|
533 | # user with a role | |
524 | roles = @jsmith.roles_for_project(Project.find(1)) |
|
534 | roles = @jsmith.roles_for_project(Project.find(1)) | |
525 | assert_kind_of Role, roles.first |
|
535 | assert_kind_of Role, roles.first | |
526 | assert_equal "Manager", roles.first.name |
|
536 | assert_equal "Manager", roles.first.name | |
527 |
|
537 | |||
528 | # user with no role |
|
538 | # user with no role | |
529 | assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?} |
|
539 | assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?} | |
530 | end |
|
540 | end | |
531 |
|
541 | |||
532 | def test_projects_by_role_for_user_with_role |
|
542 | def test_projects_by_role_for_user_with_role | |
533 | user = User.find(2) |
|
543 | user = User.find(2) | |
534 | assert_kind_of Hash, user.projects_by_role |
|
544 | assert_kind_of Hash, user.projects_by_role | |
535 | assert_equal 2, user.projects_by_role.size |
|
545 | assert_equal 2, user.projects_by_role.size | |
536 | assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort |
|
546 | 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 |
|
547 | assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort | |
538 | end |
|
548 | end | |
539 |
|
549 | |||
540 | def test_projects_by_role_for_user_with_no_role |
|
550 | def test_projects_by_role_for_user_with_no_role | |
541 | user = User.generate! |
|
551 | user = User.generate! | |
542 | assert_equal({}, user.projects_by_role) |
|
552 | assert_equal({}, user.projects_by_role) | |
543 | end |
|
553 | end | |
544 |
|
554 | |||
545 | def test_projects_by_role_for_anonymous |
|
555 | def test_projects_by_role_for_anonymous | |
546 | assert_equal({}, User.anonymous.projects_by_role) |
|
556 | assert_equal({}, User.anonymous.projects_by_role) | |
547 | end |
|
557 | end | |
548 |
|
558 | |||
549 | def test_valid_notification_options |
|
559 | def test_valid_notification_options | |
550 | # without memberships |
|
560 | # without memberships | |
551 | assert_equal 5, User.find(7).valid_notification_options.size |
|
561 | assert_equal 5, User.find(7).valid_notification_options.size | |
552 | # with memberships |
|
562 | # with memberships | |
553 | assert_equal 6, User.find(2).valid_notification_options.size |
|
563 | assert_equal 6, User.find(2).valid_notification_options.size | |
554 | end |
|
564 | end | |
555 |
|
565 | |||
556 | def test_valid_notification_options_class_method |
|
566 | def test_valid_notification_options_class_method | |
557 | assert_equal 5, User.valid_notification_options.size |
|
567 | assert_equal 5, User.valid_notification_options.size | |
558 | assert_equal 5, User.valid_notification_options(User.find(7)).size |
|
568 | assert_equal 5, User.valid_notification_options(User.find(7)).size | |
559 | assert_equal 6, User.valid_notification_options(User.find(2)).size |
|
569 | assert_equal 6, User.valid_notification_options(User.find(2)).size | |
560 | end |
|
570 | end | |
561 |
|
571 | |||
562 | def test_mail_notification_all |
|
572 | def test_mail_notification_all | |
563 | @jsmith.mail_notification = 'all' |
|
573 | @jsmith.mail_notification = 'all' | |
564 | @jsmith.notified_project_ids = [] |
|
574 | @jsmith.notified_project_ids = [] | |
565 | @jsmith.save |
|
575 | @jsmith.save | |
566 | @jsmith.reload |
|
576 | @jsmith.reload | |
567 | assert @jsmith.projects.first.recipients.include?(@jsmith.mail) |
|
577 | assert @jsmith.projects.first.recipients.include?(@jsmith.mail) | |
568 | end |
|
578 | end | |
569 |
|
579 | |||
570 | def test_mail_notification_selected |
|
580 | def test_mail_notification_selected | |
571 | @jsmith.mail_notification = 'selected' |
|
581 | @jsmith.mail_notification = 'selected' | |
572 | @jsmith.notified_project_ids = [1] |
|
582 | @jsmith.notified_project_ids = [1] | |
573 | @jsmith.save |
|
583 | @jsmith.save | |
574 | @jsmith.reload |
|
584 | @jsmith.reload | |
575 | assert Project.find(1).recipients.include?(@jsmith.mail) |
|
585 | assert Project.find(1).recipients.include?(@jsmith.mail) | |
576 | end |
|
586 | end | |
577 |
|
587 | |||
578 | def test_mail_notification_only_my_events |
|
588 | def test_mail_notification_only_my_events | |
579 | @jsmith.mail_notification = 'only_my_events' |
|
589 | @jsmith.mail_notification = 'only_my_events' | |
580 | @jsmith.notified_project_ids = [] |
|
590 | @jsmith.notified_project_ids = [] | |
581 | @jsmith.save |
|
591 | @jsmith.save | |
582 | @jsmith.reload |
|
592 | @jsmith.reload | |
583 | assert !@jsmith.projects.first.recipients.include?(@jsmith.mail) |
|
593 | assert !@jsmith.projects.first.recipients.include?(@jsmith.mail) | |
584 | end |
|
594 | end | |
585 |
|
595 | |||
586 | def test_comments_sorting_preference |
|
596 | def test_comments_sorting_preference | |
587 | assert !@jsmith.wants_comments_in_reverse_order? |
|
597 | assert !@jsmith.wants_comments_in_reverse_order? | |
588 | @jsmith.pref.comments_sorting = 'asc' |
|
598 | @jsmith.pref.comments_sorting = 'asc' | |
589 | assert !@jsmith.wants_comments_in_reverse_order? |
|
599 | assert !@jsmith.wants_comments_in_reverse_order? | |
590 | @jsmith.pref.comments_sorting = 'desc' |
|
600 | @jsmith.pref.comments_sorting = 'desc' | |
591 | assert @jsmith.wants_comments_in_reverse_order? |
|
601 | assert @jsmith.wants_comments_in_reverse_order? | |
592 | end |
|
602 | end | |
593 |
|
603 | |||
594 | def test_find_by_mail_should_be_case_insensitive |
|
604 | def test_find_by_mail_should_be_case_insensitive | |
595 | u = User.find_by_mail('JSmith@somenet.foo') |
|
605 | u = User.find_by_mail('JSmith@somenet.foo') | |
596 | assert_not_nil u |
|
606 | assert_not_nil u | |
597 | assert_equal 'jsmith@somenet.foo', u.mail |
|
607 | assert_equal 'jsmith@somenet.foo', u.mail | |
598 | end |
|
608 | end | |
599 |
|
609 | |||
600 | def test_random_password |
|
610 | def test_random_password | |
601 | u = User.new |
|
611 | u = User.new | |
602 | u.random_password |
|
612 | u.random_password | |
603 | assert !u.password.blank? |
|
613 | assert !u.password.blank? | |
604 | assert !u.password_confirmation.blank? |
|
614 | assert !u.password_confirmation.blank? | |
605 | end |
|
615 | end | |
606 |
|
616 | |||
607 | context "#change_password_allowed?" do |
|
617 | context "#change_password_allowed?" do | |
608 | should "be allowed if no auth source is set" do |
|
618 | should "be allowed if no auth source is set" do | |
609 | user = User.generate_with_protected! |
|
619 | user = User.generate_with_protected! | |
610 | assert user.change_password_allowed? |
|
620 | assert user.change_password_allowed? | |
611 | end |
|
621 | end | |
612 |
|
622 | |||
613 | should "delegate to the auth source" do |
|
623 | should "delegate to the auth source" do | |
614 | user = User.generate_with_protected! |
|
624 | user = User.generate_with_protected! | |
615 |
|
625 | |||
616 | allowed_auth_source = AuthSource.generate! |
|
626 | allowed_auth_source = AuthSource.generate! | |
617 | def allowed_auth_source.allow_password_changes?; true; end |
|
627 | def allowed_auth_source.allow_password_changes?; true; end | |
618 |
|
628 | |||
619 | denied_auth_source = AuthSource.generate! |
|
629 | denied_auth_source = AuthSource.generate! | |
620 | def denied_auth_source.allow_password_changes?; false; end |
|
630 | def denied_auth_source.allow_password_changes?; false; end | |
621 |
|
631 | |||
622 | assert user.change_password_allowed? |
|
632 | assert user.change_password_allowed? | |
623 |
|
633 | |||
624 | user.auth_source = allowed_auth_source |
|
634 | user.auth_source = allowed_auth_source | |
625 | assert user.change_password_allowed?, "User not allowed to change password, though auth source does" |
|
635 | assert user.change_password_allowed?, "User not allowed to change password, though auth source does" | |
626 |
|
636 | |||
627 | user.auth_source = denied_auth_source |
|
637 | user.auth_source = denied_auth_source | |
628 | assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" |
|
638 | assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" | |
629 | end |
|
639 | end | |
630 |
|
640 | |||
631 | end |
|
641 | end | |
632 |
|
642 | |||
633 | context "#allowed_to?" do |
|
643 | context "#allowed_to?" do | |
634 | context "with a unique project" do |
|
644 | context "with a unique project" do | |
635 | should "return false if project is archived" do |
|
645 | should "return false if project is archived" do | |
636 | project = Project.find(1) |
|
646 | project = Project.find(1) | |
637 | Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED) |
|
647 | Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED) | |
638 | assert ! @admin.allowed_to?(:view_issues, Project.find(1)) |
|
648 | assert ! @admin.allowed_to?(:view_issues, Project.find(1)) | |
639 | end |
|
649 | end | |
640 |
|
650 | |||
641 | should "return false if related module is disabled" do |
|
651 | should "return false if related module is disabled" do | |
642 | project = Project.find(1) |
|
652 | project = Project.find(1) | |
643 | project.enabled_module_names = ["issue_tracking"] |
|
653 | project.enabled_module_names = ["issue_tracking"] | |
644 | assert @admin.allowed_to?(:add_issues, project) |
|
654 | assert @admin.allowed_to?(:add_issues, project) | |
645 | assert ! @admin.allowed_to?(:view_wiki_pages, project) |
|
655 | assert ! @admin.allowed_to?(:view_wiki_pages, project) | |
646 | end |
|
656 | end | |
647 |
|
657 | |||
648 | should "authorize nearly everything for admin users" do |
|
658 | should "authorize nearly everything for admin users" do | |
649 | project = Project.find(1) |
|
659 | project = Project.find(1) | |
650 | assert ! @admin.member_of?(project) |
|
660 | assert ! @admin.member_of?(project) | |
651 | %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p| |
|
661 | %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p| | |
652 | assert @admin.allowed_to?(p.to_sym, project) |
|
662 | assert @admin.allowed_to?(p.to_sym, project) | |
653 | end |
|
663 | end | |
654 | end |
|
664 | end | |
655 |
|
665 | |||
656 | should "authorize normal users depending on their roles" do |
|
666 | should "authorize normal users depending on their roles" do | |
657 | project = Project.find(1) |
|
667 | project = Project.find(1) | |
658 | assert @jsmith.allowed_to?(:delete_messages, project) #Manager |
|
668 | assert @jsmith.allowed_to?(:delete_messages, project) #Manager | |
659 | assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper |
|
669 | assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper | |
660 | end |
|
670 | end | |
661 | end |
|
671 | end | |
662 |
|
672 | |||
663 | context "with multiple projects" do |
|
673 | context "with multiple projects" do | |
664 | should "return false if array is empty" do |
|
674 | should "return false if array is empty" do | |
665 | assert ! @admin.allowed_to?(:view_project, []) |
|
675 | assert ! @admin.allowed_to?(:view_project, []) | |
666 | end |
|
676 | end | |
667 |
|
677 | |||
668 | should "return true only if user has permission on all these projects" do |
|
678 | should "return true only if user has permission on all these projects" do | |
669 | assert @admin.allowed_to?(:view_project, Project.all) |
|
679 | assert @admin.allowed_to?(:view_project, Project.all) | |
670 | assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2) |
|
680 | 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 |
|
681 | 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 |
|
682 | assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers | |
673 | end |
|
683 | end | |
674 |
|
684 | |||
675 | should "behave correctly with arrays of 1 project" do |
|
685 | should "behave correctly with arrays of 1 project" do | |
676 | assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first]) |
|
686 | assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first]) | |
677 | end |
|
687 | end | |
678 | end |
|
688 | end | |
679 |
|
689 | |||
680 | context "with options[:global]" do |
|
690 | context "with options[:global]" do | |
681 | should "authorize if user has at least one role that has this permission" do |
|
691 | 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 |
|
692 | @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere | |
683 | @anonymous = User.find(6) |
|
693 | @anonymous = User.find(6) | |
684 | assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true) |
|
694 | assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true) | |
685 | assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true) |
|
695 | assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true) | |
686 | assert @dlopper2.allowed_to?(:add_issues, nil, :global => true) |
|
696 | assert @dlopper2.allowed_to?(:add_issues, nil, :global => true) | |
687 | assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true) |
|
697 | assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true) | |
688 | assert @anonymous.allowed_to?(:view_issues, nil, :global => true) |
|
698 | assert @anonymous.allowed_to?(:view_issues, nil, :global => true) | |
689 | end |
|
699 | end | |
690 | end |
|
700 | end | |
691 | end |
|
701 | end | |
692 |
|
702 | |||
693 | context "User#notify_about?" do |
|
703 | context "User#notify_about?" do | |
694 | context "Issues" do |
|
704 | context "Issues" do | |
695 | setup do |
|
705 | setup do | |
696 | @project = Project.find(1) |
|
706 | @project = Project.find(1) | |
697 | @author = User.generate_with_protected! |
|
707 | @author = User.generate_with_protected! | |
698 | @assignee = User.generate_with_protected! |
|
708 | @assignee = User.generate_with_protected! | |
699 | @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author) |
|
709 | @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author) | |
700 | end |
|
710 | end | |
701 |
|
711 | |||
702 | should "be true for a user with :all" do |
|
712 | should "be true for a user with :all" do | |
703 | @author.update_attribute(:mail_notification, 'all') |
|
713 | @author.update_attribute(:mail_notification, 'all') | |
704 | assert @author.notify_about?(@issue) |
|
714 | assert @author.notify_about?(@issue) | |
705 | end |
|
715 | end | |
706 |
|
716 | |||
707 | should "be false for a user with :none" do |
|
717 | should "be false for a user with :none" do | |
708 | @author.update_attribute(:mail_notification, 'none') |
|
718 | @author.update_attribute(:mail_notification, 'none') | |
709 | assert ! @author.notify_about?(@issue) |
|
719 | assert ! @author.notify_about?(@issue) | |
710 | end |
|
720 | end | |
711 |
|
721 | |||
712 | should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do |
|
722 | 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') |
|
723 | @user = User.generate_with_protected!(:mail_notification => 'only_my_events') | |
714 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) |
|
724 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) | |
715 | assert ! @user.notify_about?(@issue) |
|
725 | assert ! @user.notify_about?(@issue) | |
716 | end |
|
726 | end | |
717 |
|
727 | |||
718 | should "be true for a user with :only_my_events and is the author" do |
|
728 | should "be true for a user with :only_my_events and is the author" do | |
719 | @author.update_attribute(:mail_notification, 'only_my_events') |
|
729 | @author.update_attribute(:mail_notification, 'only_my_events') | |
720 | assert @author.notify_about?(@issue) |
|
730 | assert @author.notify_about?(@issue) | |
721 | end |
|
731 | end | |
722 |
|
732 | |||
723 | should "be true for a user with :only_my_events and is the assignee" do |
|
733 | should "be true for a user with :only_my_events and is the assignee" do | |
724 | @assignee.update_attribute(:mail_notification, 'only_my_events') |
|
734 | @assignee.update_attribute(:mail_notification, 'only_my_events') | |
725 | assert @assignee.notify_about?(@issue) |
|
735 | assert @assignee.notify_about?(@issue) | |
726 | end |
|
736 | end | |
727 |
|
737 | |||
728 | should "be true for a user with :only_assigned and is the assignee" do |
|
738 | should "be true for a user with :only_assigned and is the assignee" do | |
729 | @assignee.update_attribute(:mail_notification, 'only_assigned') |
|
739 | @assignee.update_attribute(:mail_notification, 'only_assigned') | |
730 | assert @assignee.notify_about?(@issue) |
|
740 | assert @assignee.notify_about?(@issue) | |
731 | end |
|
741 | end | |
732 |
|
742 | |||
733 | should "be false for a user with :only_assigned and is not the assignee" do |
|
743 | should "be false for a user with :only_assigned and is not the assignee" do | |
734 | @author.update_attribute(:mail_notification, 'only_assigned') |
|
744 | @author.update_attribute(:mail_notification, 'only_assigned') | |
735 | assert ! @author.notify_about?(@issue) |
|
745 | assert ! @author.notify_about?(@issue) | |
736 | end |
|
746 | end | |
737 |
|
747 | |||
738 | should "be true for a user with :only_owner and is the author" do |
|
748 | should "be true for a user with :only_owner and is the author" do | |
739 | @author.update_attribute(:mail_notification, 'only_owner') |
|
749 | @author.update_attribute(:mail_notification, 'only_owner') | |
740 | assert @author.notify_about?(@issue) |
|
750 | assert @author.notify_about?(@issue) | |
741 | end |
|
751 | end | |
742 |
|
752 | |||
743 | should "be false for a user with :only_owner and is not the author" do |
|
753 | should "be false for a user with :only_owner and is not the author" do | |
744 | @assignee.update_attribute(:mail_notification, 'only_owner') |
|
754 | @assignee.update_attribute(:mail_notification, 'only_owner') | |
745 | assert ! @assignee.notify_about?(@issue) |
|
755 | assert ! @assignee.notify_about?(@issue) | |
746 | end |
|
756 | end | |
747 |
|
757 | |||
748 | should "be true for a user with :selected and is the author" do |
|
758 | should "be true for a user with :selected and is the author" do | |
749 | @author.update_attribute(:mail_notification, 'selected') |
|
759 | @author.update_attribute(:mail_notification, 'selected') | |
750 | assert @author.notify_about?(@issue) |
|
760 | assert @author.notify_about?(@issue) | |
751 | end |
|
761 | end | |
752 |
|
762 | |||
753 | should "be true for a user with :selected and is the assignee" do |
|
763 | should "be true for a user with :selected and is the assignee" do | |
754 | @assignee.update_attribute(:mail_notification, 'selected') |
|
764 | @assignee.update_attribute(:mail_notification, 'selected') | |
755 | assert @assignee.notify_about?(@issue) |
|
765 | assert @assignee.notify_about?(@issue) | |
756 | end |
|
766 | end | |
757 |
|
767 | |||
758 | should "be false for a user with :selected and is not the author or assignee" do |
|
768 | 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') |
|
769 | @user = User.generate_with_protected!(:mail_notification => 'selected') | |
760 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) |
|
770 | Member.create!(:user => @user, :project => @project, :role_ids => [1]) | |
761 | assert ! @user.notify_about?(@issue) |
|
771 | assert ! @user.notify_about?(@issue) | |
762 | end |
|
772 | end | |
763 | end |
|
773 | end | |
764 |
|
774 | |||
765 | context "other events" do |
|
775 | context "other events" do | |
766 | should 'be added and tested' |
|
776 | should 'be added and tested' | |
767 | end |
|
777 | end | |
768 | end |
|
778 | end | |
769 |
|
779 | |||
770 | def test_salt_unsalted_passwords |
|
780 | def test_salt_unsalted_passwords | |
771 | # Restore a user with an unsalted password |
|
781 | # Restore a user with an unsalted password | |
772 | user = User.find(1) |
|
782 | user = User.find(1) | |
773 | user.salt = nil |
|
783 | user.salt = nil | |
774 | user.hashed_password = User.hash_password("unsalted") |
|
784 | user.hashed_password = User.hash_password("unsalted") | |
775 | user.save! |
|
785 | user.save! | |
776 |
|
786 | |||
777 | User.salt_unsalted_passwords! |
|
787 | User.salt_unsalted_passwords! | |
778 |
|
788 | |||
779 | user.reload |
|
789 | user.reload | |
780 | # Salt added |
|
790 | # Salt added | |
781 | assert !user.salt.blank? |
|
791 | assert !user.salt.blank? | |
782 | # Password still valid |
|
792 | # Password still valid | |
783 | assert user.check_password?("unsalted") |
|
793 | assert user.check_password?("unsalted") | |
784 | assert_equal user, User.try_to_login(user.login, "unsalted") |
|
794 | assert_equal user, User.try_to_login(user.login, "unsalted") | |
785 | end |
|
795 | end | |
786 |
|
796 | |||
787 | if Object.const_defined?(:OpenID) |
|
797 | if Object.const_defined?(:OpenID) | |
788 |
|
798 | |||
789 | def test_setting_identity_url |
|
799 | def test_setting_identity_url | |
790 | normalized_open_id_url = 'http://example.com/' |
|
800 | normalized_open_id_url = 'http://example.com/' | |
791 | u = User.new( :identity_url => 'http://example.com/' ) |
|
801 | u = User.new( :identity_url => 'http://example.com/' ) | |
792 | assert_equal normalized_open_id_url, u.identity_url |
|
802 | assert_equal normalized_open_id_url, u.identity_url | |
793 | end |
|
803 | end | |
794 |
|
804 | |||
795 | def test_setting_identity_url_without_trailing_slash |
|
805 | def test_setting_identity_url_without_trailing_slash | |
796 | normalized_open_id_url = 'http://example.com/' |
|
806 | normalized_open_id_url = 'http://example.com/' | |
797 | u = User.new( :identity_url => 'http://example.com' ) |
|
807 | u = User.new( :identity_url => 'http://example.com' ) | |
798 | assert_equal normalized_open_id_url, u.identity_url |
|
808 | assert_equal normalized_open_id_url, u.identity_url | |
799 | end |
|
809 | end | |
800 |
|
810 | |||
801 | def test_setting_identity_url_without_protocol |
|
811 | def test_setting_identity_url_without_protocol | |
802 | normalized_open_id_url = 'http://example.com/' |
|
812 | normalized_open_id_url = 'http://example.com/' | |
803 | u = User.new( :identity_url => 'example.com' ) |
|
813 | u = User.new( :identity_url => 'example.com' ) | |
804 | assert_equal normalized_open_id_url, u.identity_url |
|
814 | assert_equal normalized_open_id_url, u.identity_url | |
805 | end |
|
815 | end | |
806 |
|
816 | |||
807 | def test_setting_blank_identity_url |
|
817 | def test_setting_blank_identity_url | |
808 | u = User.new( :identity_url => 'example.com' ) |
|
818 | u = User.new( :identity_url => 'example.com' ) | |
809 | u.identity_url = '' |
|
819 | u.identity_url = '' | |
810 | assert u.identity_url.blank? |
|
820 | assert u.identity_url.blank? | |
811 | end |
|
821 | end | |
812 |
|
822 | |||
813 | def test_setting_invalid_identity_url |
|
823 | def test_setting_invalid_identity_url | |
814 | u = User.new( :identity_url => 'this is not an openid url' ) |
|
824 | u = User.new( :identity_url => 'this is not an openid url' ) | |
815 | assert u.identity_url.blank? |
|
825 | assert u.identity_url.blank? | |
816 | end |
|
826 | end | |
817 |
|
827 | |||
818 | else |
|
828 | else | |
819 | puts "Skipping openid tests." |
|
829 | puts "Skipping openid tests." | |
820 | end |
|
830 | end | |
821 |
|
831 | |||
822 | end |
|
832 | end |
General Comments 0
You need to be logged in to leave comments.
Login now