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