To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / test / functional / users_controller_test.rb @ 441:cbce1fd3b1b7

History | View | Annotate | Download (9.1 KB)

1 441:cbce1fd3b1b7 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 0:513646585e45 Chris
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 119:8661b858af72 Chris
require File.expand_path('../../test_helper', __FILE__)
19 0:513646585e45 Chris
require 'users_controller'
20
21
# Re-raise errors caught by the controller.
22
class UsersController; def rescue_action(e) raise e end; end
23
24
class UsersControllerTest < ActionController::TestCase
25
  include Redmine::I18n
26
27 441:cbce1fd3b1b7 Chris
  fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values, :groups_users
28 0:513646585e45 Chris
29
  def setup
30
    @controller = UsersController.new
31
    @request    = ActionController::TestRequest.new
32
    @response   = ActionController::TestResponse.new
33
    User.current = nil
34
    @request.session[:user_id] = 1 # admin
35
  end
36
37
  def test_index
38
    get :index
39
    assert_response :success
40
    assert_template 'index'
41
  end
42
43
  def test_index
44
    get :index
45
    assert_response :success
46
    assert_template 'index'
47
    assert_not_nil assigns(:users)
48
    # active users only
49
    assert_nil assigns(:users).detect {|u| !u.active?}
50
  end
51
52
  def test_index_with_name_filter
53
    get :index, :name => 'john'
54
    assert_response :success
55
    assert_template 'index'
56
    users = assigns(:users)
57
    assert_not_nil users
58
    assert_equal 1, users.size
59
    assert_equal 'John', users.first.firstname
60
  end
61
62 441:cbce1fd3b1b7 Chris
  def test_index_with_group_filter
63
    get :index, :group_id => '10'
64
    assert_response :success
65
    assert_template 'index'
66
    users = assigns(:users)
67
    assert users.any?
68
    assert_equal([], (users - Group.find(10).users))
69
  end
70
71 0:513646585e45 Chris
  def test_show
72
    @request.session[:user_id] = nil
73
    get :show, :id => 2
74
    assert_response :success
75
    assert_template 'show'
76
    assert_not_nil assigns(:user)
77 37:94944d00e43c chris
78
    assert_tag 'li', :content => /Phone number/
79
  end
80
81
  def test_show_should_not_display_hidden_custom_fields
82
    @request.session[:user_id] = nil
83
    UserCustomField.find_by_name('Phone number').update_attribute :visible, false
84
    get :show, :id => 2
85
    assert_response :success
86
    assert_template 'show'
87
    assert_not_nil assigns(:user)
88
89
    assert_no_tag 'li', :content => /Phone number/
90 0:513646585e45 Chris
  end
91
92
  def test_show_should_not_fail_when_custom_values_are_nil
93
    user = User.find(2)
94
95
    # Create a custom field to illustrate the issue
96
    custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
97
    custom_value = user.custom_values.build(:custom_field => custom_field).save!
98
99
    get :show, :id => 2
100
    assert_response :success
101
  end
102
103
  def test_show_inactive
104
    @request.session[:user_id] = nil
105
    get :show, :id => 5
106
    assert_response 404
107
  end
108
109
  def test_show_should_not_reveal_users_with_no_visible_activity_or_project
110
    @request.session[:user_id] = nil
111
    get :show, :id => 9
112
    assert_response 404
113
  end
114
115
  def test_show_inactive_by_admin
116
    @request.session[:user_id] = 1
117
    get :show, :id => 5
118
    assert_response 200
119
    assert_not_nil assigns(:user)
120
  end
121 14:1d32c0a0efbf Chris
122
  def test_show_displays_memberships_based_on_project_visibility
123
    @request.session[:user_id] = 1
124
    get :show, :id => 2
125
    assert_response :success
126
    memberships = assigns(:memberships)
127
    assert_not_nil memberships
128
    project_ids = memberships.map(&:project_id)
129
    assert project_ids.include?(2) #private project admin can see
130
  end
131 119:8661b858af72 Chris
132
  def test_show_current_should_require_authentication
133
    @request.session[:user_id] = nil
134
    get :show, :id => 'current'
135
    assert_response 302
136
  end
137
138
  def test_show_current
139
    @request.session[:user_id] = 2
140
    get :show, :id => 'current'
141
    assert_response :success
142
    assert_template 'show'
143
    assert_equal User.find(2), assigns(:user)
144
  end
145
146
  def test_new
147
    get :new
148
149
    assert_response :success
150
    assert_template :new
151
    assert assigns(:user)
152
  end
153
154
  def test_create
155
    Setting.bcc_recipients = '1'
156
157
    assert_difference 'User.count' do
158
      assert_difference 'ActionMailer::Base.deliveries.size' do
159
        post :create,
160
          :user => {
161
            :firstname => 'John',
162
            :lastname => 'Doe',
163
            :login => 'jdoe',
164
            :password => 'secret',
165
            :password_confirmation => 'secret',
166
            :mail => 'jdoe@gmail.com',
167
            :mail_notification => 'none'
168
          },
169
          :send_information => '1'
170
      end
171 37:94944d00e43c chris
    end
172 119:8661b858af72 Chris
173
    user = User.first(:order => 'id DESC')
174
    assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
175
176
    assert_equal 'John', user.firstname
177
    assert_equal 'Doe', user.lastname
178
    assert_equal 'jdoe', user.login
179
    assert_equal 'jdoe@gmail.com', user.mail
180
    assert_equal 'none', user.mail_notification
181
    assert user.check_password?('secret')
182
183
    mail = ActionMailer::Base.deliveries.last
184
    assert_not_nil mail
185
    assert_equal [user.mail], mail.bcc
186
    assert mail.body.include?('secret')
187
  end
188
189
  def test_create_with_failure
190
    assert_no_difference 'User.count' do
191
      post :create, :user => {}
192
    end
193
194
    assert_response :success
195
    assert_template 'new'
196 37:94944d00e43c chris
  end
197
198 119:8661b858af72 Chris
  def test_edit
199
    get :edit, :id => 2
200
201
    assert_response :success
202
    assert_template 'edit'
203
    assert_equal User.find(2), assigns(:user)
204 37:94944d00e43c chris
  end
205
206
  def test_update
207 0:513646585e45 Chris
    ActionMailer::Base.deliveries.clear
208 119:8661b858af72 Chris
    put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
209 37:94944d00e43c chris
210
    user = User.find(2)
211
    assert_equal 'Changed', user.firstname
212 119:8661b858af72 Chris
    assert_equal 'only_assigned', user.mail_notification
213 37:94944d00e43c chris
    assert_equal true, user.pref[:hide_mail]
214
    assert_equal 'desc', user.pref[:comments_sorting]
215 0:513646585e45 Chris
    assert ActionMailer::Base.deliveries.empty?
216
  end
217 119:8661b858af72 Chris
218
  def test_update_with_failure
219
    assert_no_difference 'User.count' do
220
      put :update, :id => 2, :user => {:firstname => ''}
221
    end
222
223
    assert_response :success
224
    assert_template 'edit'
225
  end
226
227
  def test_update_with_group_ids_should_assign_groups
228
    put :update, :id => 2, :user => {:group_ids => ['10']}
229
230
    user = User.find(2)
231
    assert_equal [10], user.group_ids
232
  end
233 0:513646585e45 Chris
234 37:94944d00e43c chris
  def test_update_with_activation_should_send_a_notification
235 0:513646585e45 Chris
    u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
236
    u.login = 'foo'
237
    u.status = User::STATUS_REGISTERED
238
    u.save!
239
    ActionMailer::Base.deliveries.clear
240
    Setting.bcc_recipients = '1'
241
242 37:94944d00e43c chris
    put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
243 0:513646585e45 Chris
    assert u.reload.active?
244
    mail = ActionMailer::Base.deliveries.last
245
    assert_not_nil mail
246
    assert_equal ['foo.bar@somenet.foo'], mail.bcc
247
    assert mail.body.include?(ll('fr', :notice_account_activated))
248
  end
249
250 119:8661b858af72 Chris
  def test_update_with_password_change_should_send_a_notification
251 0:513646585e45 Chris
    ActionMailer::Base.deliveries.clear
252
    Setting.bcc_recipients = '1'
253
254 119:8661b858af72 Chris
    put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
255 0:513646585e45 Chris
    u = User.find(2)
256 119:8661b858af72 Chris
    assert u.check_password?('newpass')
257 0:513646585e45 Chris
258
    mail = ActionMailer::Base.deliveries.last
259
    assert_not_nil mail
260
    assert_equal [u.mail], mail.bcc
261
    assert mail.body.include?('newpass')
262
  end
263 22:40f7cfd4df19 chris
264 37:94944d00e43c chris
  test "put :update with a password change to an AuthSource user switching to Internal authentication" do
265 22:40f7cfd4df19 chris
    # Configure as auth source
266
    u = User.find(2)
267
    u.auth_source = AuthSource.find(1)
268
    u.save!
269
270 119:8661b858af72 Chris
    put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass'
271 22:40f7cfd4df19 chris
272
    assert_equal nil, u.reload.auth_source
273 119:8661b858af72 Chris
    assert u.check_password?('newpass')
274 22:40f7cfd4df19 chris
  end
275 0:513646585e45 Chris
276 128:07fa8a8b56a8 Chris
  def test_destroy
277
    assert_difference 'User.count', -1 do
278
      delete :destroy, :id => 2
279
    end
280
    assert_redirected_to '/users'
281
    assert_nil User.find_by_id(2)
282
  end
283
284
  def test_destroy_should_not_accept_get_requests
285
    assert_no_difference 'User.count' do
286
      get :destroy, :id => 2
287
    end
288
    assert_response 405
289
  end
290
291
  def test_destroy_should_be_denied_for_non_admin_users
292
    @request.session[:user_id] = 3
293
294
    assert_no_difference 'User.count' do
295
      get :destroy, :id => 2
296
    end
297
    assert_response 403
298
  end
299
300 0:513646585e45 Chris
  def test_edit_membership
301
    post :edit_membership, :id => 2, :membership_id => 1,
302
                           :membership => { :role_ids => [2]}
303
    assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
304
    assert_equal [2], Member.find(1).role_ids
305
  end
306
307
  def test_destroy_membership
308
    post :destroy_membership, :id => 2, :membership_id => 1
309
    assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
310
    assert_nil Member.find_by_id(1)
311
  end
312
end