users_controller_test.rb
635 lines
| 19.1 KiB
| text/x-ruby
|
RubyLexer
|
r5030 | # Redmine - project management software | ||
|
r14856 | # Copyright (C) 2006-2016 Jean-Philippe Lang | ||
|
r974 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6508 | # | ||
|
r974 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6508 | # | ||
|
r974 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r4395 | require File.expand_path('../../test_helper', __FILE__) | ||
|
r974 | |||
|
r15279 | class UsersControllerTest < Redmine::ControllerTest | ||
|
r2431 | include Redmine::I18n | ||
|
r6508 | |||
|
r13504 | fixtures :users, :email_addresses, :projects, :members, :member_roles, :roles, | ||
|
r9547 | :custom_fields, :custom_values, :groups_users, | ||
|
r13501 | :auth_sources, | ||
:enabled_modules, | ||||
:issues, :issue_statuses, | ||||
:trackers | ||||
|
r6508 | |||
|
r974 | def setup | ||
User.current = nil | ||||
@request.session[:user_id] = 1 # admin | ||||
end | ||||
|
r6508 | |||
|
r2877 | def test_index | ||
get :index | ||||
|
r974 | assert_response :success | ||
|
r15342 | assert_select 'table.users' | ||
assert_select 'tr.user.active' | ||||
assert_select 'tr.user.locked', 0 | ||||
|
r974 | end | ||
|
r6508 | |||
|
r7961 | def test_index_with_status_filter | ||
|
r15282 | get :index, :params => {:status => 3} | ||
|
r7961 | assert_response :success | ||
|
r15342 | assert_select 'tr.user.active', 0 | ||
assert_select 'tr.user.locked' | ||||
|
r7961 | end | ||
|
r2877 | def test_index_with_name_filter | ||
|
r15282 | get :index, :params => {:name => 'john'} | ||
|
r1944 | assert_response :success | ||
|
r15342 | assert_select 'tr.user td.username', :text => 'jsmith' | ||
assert_select 'tr.user', 1 | ||||
|
r1944 | end | ||
|
r6508 | |||
|
r5030 | def test_index_with_group_filter | ||
|
r15282 | get :index, :params => {:group_id => '10'} | ||
|
r5030 | assert_response :success | ||
|
r15342 | |||
assert_select 'tr.user', Group.find(10).users.count | ||||
|
r9496 | assert_select 'select[name=group_id]' do | ||
|
r13237 | assert_select 'option[value="10"][selected=selected]' | ||
|
r9496 | end | ||
|
r5030 | end | ||
|
r6508 | |||
|
r2874 | def test_show | ||
@request.session[:user_id] = nil | ||||
|
r15282 | get :show, :params => {:id => 2} | ||
|
r2874 | assert_response :success | ||
|
r15342 | assert_select 'h2', :text => /John Smith/ | ||
end | ||||
def test_show_should_display_visible_custom_fields | ||||
@request.session[:user_id] = nil | ||||
UserCustomField.find_by_name('Phone number').update_attribute :visible, true | ||||
get :show, :params => {:id => 2} | ||||
assert_response :success | ||||
|
r6508 | |||
|
r13242 | assert_select 'li', :text => /Phone number/ | ||
|
r4268 | end | ||
|
r6508 | |||
|
r4268 | def test_show_should_not_display_hidden_custom_fields | ||
@request.session[:user_id] = nil | ||||
UserCustomField.find_by_name('Phone number').update_attribute :visible, false | ||||
|
r15282 | get :show, :params => {:id => 2} | ||
|
r4268 | assert_response :success | ||
|
r6508 | |||
|
r13242 | assert_select 'li', :text => /Phone number/, :count => 0 | ||
|
r2874 | end | ||
def test_show_should_not_fail_when_custom_values_are_nil | ||||
user = User.find(2) | ||||
# Create a custom field to illustrate the issue | ||||
custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text') | ||||
custom_value = user.custom_values.build(:custom_field => custom_field).save! | ||||
|
r15282 | get :show, :params => {:id => 2} | ||
|
r2874 | assert_response :success | ||
end | ||||
def test_show_inactive | ||||
|
r3379 | @request.session[:user_id] = nil | ||
|
r15282 | get :show, :params => {:id => 5} | ||
|
r2874 | assert_response 404 | ||
end | ||||
|
r6508 | |||
|
r3379 | def test_show_inactive_by_admin | ||
@request.session[:user_id] = 1 | ||||
|
r15282 | get :show, :params => {:id => 5} | ||
|
r3379 | assert_response 200 | ||
|
r15342 | assert_select 'h2', :text => /Dave2 Lopper2/ | ||
|
r3379 | end | ||
|
r6508 | |||
|
r13202 | def test_show_user_who_is_not_visible_should_return_404 | ||
Role.anonymous.update! :users_visibility => 'members_of_visible_projects' | ||||
user = User.generate! | ||||
@request.session[:user_id] = nil | ||||
|
r15282 | get :show, :params => {:id => user.id} | ||
|
r13202 | assert_response 404 | ||
end | ||||
|
r3821 | def test_show_displays_memberships_based_on_project_visibility | ||
@request.session[:user_id] = 1 | ||||
|
r15282 | get :show, :params => {:id => 2} | ||
|
r3821 | assert_response :success | ||
|
r15342 | |||
# membership of private project admin can see | ||||
assert_select 'li a', :text => "OnlineStore" | ||||
|
r3821 | end | ||
|
r6508 | |||
|
r4430 | def test_show_current_should_require_authentication | ||
@request.session[:user_id] = nil | ||||
|
r15282 | get :show, :params => {:id => 'current'} | ||
|
r4430 | assert_response 302 | ||
end | ||||
|
r6508 | |||
|
r4430 | def test_show_current | ||
@request.session[:user_id] = 2 | ||||
|
r15282 | get :show, :params => {:id => 'current'} | ||
|
r4430 | assert_response :success | ||
|
r15342 | assert_select 'h2', :text => /John Smith/ | ||
|
r4430 | end | ||
|
r6508 | |||
|
r4387 | def test_new | ||
get :new | ||||
assert_response :success | ||||
|
r15342 | assert_select 'input[name=?]', 'user[login]' | ||
|
r4386 | end | ||
|
r6508 | |||
|
r4386 | def test_create | ||
Setting.bcc_recipients = '1' | ||||
|
r6508 | |||
|
r4386 | assert_difference 'User.count' do | ||
assert_difference 'ActionMailer::Base.deliveries.size' do | ||||
|
r15282 | post :create, :params => { | ||
|
r4386 | :user => { | ||
:firstname => 'John', | ||||
:lastname => 'Doe', | ||||
:login => 'jdoe', | ||||
|
r10659 | :password => 'secret123', | ||
:password_confirmation => 'secret123', | ||||
|
r4386 | :mail => 'jdoe@gmail.com', | ||
:mail_notification => 'none' | ||||
}, | ||||
:send_information => '1' | ||||
|
r15282 | } | ||
|
r4386 | end | ||
end | ||||
|
r6508 | |||
|
r12361 | user = User.order('id DESC').first | ||
|
r4386 | assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id | ||
|
r6508 | |||
|
r4386 | assert_equal 'John', user.firstname | ||
assert_equal 'Doe', user.lastname | ||||
assert_equal 'jdoe', user.login | ||||
assert_equal 'jdoe@gmail.com', user.mail | ||||
assert_equal 'none', user.mail_notification | ||||
|
r10659 | assert user.check_password?('secret123') | ||
|
r6508 | |||
|
r4386 | mail = ActionMailer::Base.deliveries.last | ||
assert_not_nil mail | ||||
assert_equal [user.mail], mail.bcc | ||||
|
r8966 | assert_mail_body_match 'secret', mail | ||
|
r4101 | end | ||
|
r6508 | |||
|
r8947 | def test_create_with_preferences | ||
assert_difference 'User.count' do | ||||
|
r15282 | post :create, :params => { | ||
|
r8947 | :user => { | ||
:firstname => 'John', | ||||
:lastname => 'Doe', | ||||
:login => 'jdoe', | ||||
|
r10659 | :password => 'secret123', | ||
:password_confirmation => 'secret123', | ||||
|
r8947 | :mail => 'jdoe@gmail.com', | ||
:mail_notification => 'none' | ||||
}, | ||||
:pref => { | ||||
'hide_mail' => '1', | ||||
'time_zone' => 'Paris', | ||||
'comments_sorting' => 'desc', | ||||
|
r15371 | 'warn_on_leaving_unsaved' => '0', | ||
'textarea_font' => 'proportional' | ||||
|
r8947 | } | ||
|
r15282 | } | ||
|
r8947 | end | ||
|
r12361 | user = User.order('id DESC').first | ||
|
r8947 | assert_equal 'jdoe', user.login | ||
assert_equal true, user.pref.hide_mail | ||||
assert_equal 'Paris', user.pref.time_zone | ||||
assert_equal 'desc', user.pref[:comments_sorting] | ||||
assert_equal '0', user.pref[:warn_on_leaving_unsaved] | ||||
|
r15371 | assert_equal 'proportional', user.pref[:textarea_font] | ||
|
r8947 | end | ||
|
r11226 | def test_create_with_generate_password_should_email_the_password | ||
assert_difference 'User.count' do | ||||
|
r15282 | post :create, :params => { | ||
:user => { | ||||
:login => 'randompass', | ||||
:firstname => 'Random', | ||||
:lastname => 'Pass', | ||||
:mail => 'randompass@example.net', | ||||
:language => 'en', | ||||
:generate_password => '1', | ||||
:password => '', | ||||
:password_confirmation => '' | ||||
}, | ||||
:send_information => 1 | ||||
} | ||||
|
r11226 | end | ||
user = User.order('id DESC').first | ||||
assert_equal 'randompass', user.login | ||||
mail = ActionMailer::Base.deliveries.last | ||||
assert_not_nil mail | ||||
m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/) | ||||
assert m | ||||
password = m[1] | ||||
assert user.check_password?(password) | ||||
end | ||||
|
r13313 | def test_create_and_continue | ||
|
r15282 | post :create, :params => { | ||
:user => { | ||||
|
r13313 | :login => 'randompass', | ||
:firstname => 'Random', | ||||
:lastname => 'Pass', | ||||
:mail => 'randompass@example.net', | ||||
:generate_password => '1' | ||||
|
r15282 | }, | ||
:continue => '1' | ||||
} | ||||
|
r13313 | assert_redirected_to '/users/new?user%5Bgenerate_password%5D=1' | ||
end | ||||
|
r4387 | def test_create_with_failure | ||
assert_no_difference 'User.count' do | ||||
|
r15282 | post :create, :params => {:user => {}} | ||
|
r4387 | end | ||
assert_response :success | ||||
|
r15342 | assert_select_error /Email cannot be blank/ | ||
|
r4387 | end | ||
|
r12099 | def test_create_with_failure_sould_preserve_preference | ||
assert_no_difference 'User.count' do | ||||
|
r15282 | post :create, :params => { | ||
|
r12099 | :user => {}, | ||
:pref => { | ||||
'no_self_notified' => '1', | ||||
'hide_mail' => '1', | ||||
'time_zone' => 'Paris', | ||||
'comments_sorting' => 'desc', | ||||
'warn_on_leaving_unsaved' => '0' | ||||
} | ||||
|
r15282 | } | ||
|
r12099 | end | ||
assert_response :success | ||||
assert_select 'select#pref_time_zone option[selected=selected]', :text => /Paris/ | ||||
|
r13237 | assert_select 'input#pref_no_self_notified[value="1"][checked=checked]' | ||
|
r12099 | end | ||
|
r14883 | def test_create_admin_should_send_security_notification | ||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | post :create, :params => { | ||
|
r14883 | :user => { | ||
:firstname => 'Edgar', | ||||
:lastname => 'Schmoe', | ||||
:login => 'eschmoe', | ||||
:password => 'secret123', | ||||
:password_confirmation => 'secret123', | ||||
:mail => 'eschmoe@example.foo', | ||||
:admin => '1' | ||||
} | ||||
|
r15282 | } | ||
|
r14883 | |||
assert_not_nil (mail = ActionMailer::Base.deliveries.last) | ||||
assert_mail_body_match '0.0.0.0', mail | ||||
assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: 'eschmoe'), mail | ||||
assert_select_email do | ||||
assert_select 'a[href^=?]', 'http://localhost:3000/users', :text => 'Users' | ||||
end | ||||
# All admins should receive this | ||||
User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin| | ||||
assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) } | ||||
end | ||||
end | ||||
def test_create_non_admin_should_not_send_security_notification | ||||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | post :create, :params => { | ||
|
r14883 | :user => { | ||
:firstname => 'Edgar', | ||||
:lastname => 'Schmoe', | ||||
:login => 'eschmoe', | ||||
:password => 'secret123', | ||||
:password_confirmation => 'secret123', | ||||
:mail => 'eschmoe@example.foo', | ||||
:admin => '0' | ||||
} | ||||
|
r15282 | } | ||
|
r14883 | assert_nil ActionMailer::Base.deliveries.last | ||
end | ||||
|
r4387 | def test_edit | ||
|
r15282 | get :edit, :params => {:id => 2} | ||
|
r4387 | assert_response :success | ||
|
r15342 | assert_select 'input[name=?][value=?]', 'user[login]', 'jsmith' | ||
|
r4387 | end | ||
|
r4101 | |||
|
r13360 | def test_edit_registered_user | ||
assert User.find(2).register! | ||||
|
r15282 | get :edit, :params => {:id => 2} | ||
|
r13360 | assert_response :success | ||
assert_select 'a', :text => 'Activate' | ||||
end | ||||
|
r4116 | def test_update | ||
|
r2708 | ActionMailer::Base.deliveries.clear | ||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, | ||||
:pref => {:hide_mail => '1', :comments_sorting => 'desc'} | ||||
} | ||||
|
r4109 | user = User.find(2) | ||
assert_equal 'Changed', user.firstname | ||||
|
r4382 | assert_equal 'only_assigned', user.mail_notification | ||
|
r4109 | assert_equal true, user.pref[:hide_mail] | ||
assert_equal 'desc', user.pref[:comments_sorting] | ||||
|
r2708 | assert ActionMailer::Base.deliveries.empty? | ||
end | ||||
|
r4387 | |||
def test_update_with_failure | ||||
assert_no_difference 'User.count' do | ||||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => {:firstname => ''} | ||||
} | ||||
|
r4387 | end | ||
assert_response :success | ||||
|
r15342 | assert_select_error /First name cannot be blank/ | ||
|
r4387 | end | ||
|
r6508 | |||
|
r4385 | def test_update_with_group_ids_should_assign_groups | ||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => {:group_ids => ['10']} | ||||
} | ||||
|
r4385 | user = User.find(2) | ||
assert_equal [10], user.group_ids | ||||
end | ||||
|
r6508 | |||
|
r4116 | def test_update_with_activation_should_send_a_notification | ||
|
r2708 | u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr') | ||
u.login = 'foo' | ||||
u.status = User::STATUS_REGISTERED | ||||
u.save! | ||||
ActionMailer::Base.deliveries.clear | ||||
Setting.bcc_recipients = '1' | ||||
|
r6508 | |||
|
r15282 | put :update, :params => { | ||
:id => u.id, | ||||
:user => {:status => User::STATUS_ACTIVE} | ||||
} | ||||
|
r2708 | assert u.reload.active? | ||
mail = ActionMailer::Base.deliveries.last | ||||
assert_not_nil mail | ||||
assert_equal ['foo.bar@somenet.foo'], mail.bcc | ||||
|
r8966 | assert_mail_body_match ll('fr', :notice_account_activated), mail | ||
|
r2708 | end | ||
|
r6508 | |||
|
r4379 | def test_update_with_password_change_should_send_a_notification | ||
|
r2708 | ActionMailer::Base.deliveries.clear | ||
Setting.bcc_recipients = '1' | ||||
|
r6508 | |||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => {:password => 'newpass123', :password_confirmation => 'newpass123'}, | ||||
:send_information => '1' | ||||
} | ||||
|
r2708 | u = User.find(2) | ||
|
r10659 | assert u.check_password?('newpass123') | ||
|
r6508 | |||
|
r2708 | mail = ActionMailer::Base.deliveries.last | ||
assert_not_nil mail | ||||
assert_equal [u.mail], mail.bcc | ||||
|
r10659 | assert_mail_body_match 'newpass123', mail | ||
|
r2708 | end | ||
|
r3952 | |||
|
r11226 | def test_update_with_generate_password_should_email_the_password | ||
ActionMailer::Base.deliveries.clear | ||||
Setting.bcc_recipients = '1' | ||||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => { | ||||
:generate_password => '1', | ||||
:password => '', | ||||
:password_confirmation => '' | ||||
}, | ||||
:send_information => '1' | ||||
} | ||||
|
r11226 | |||
mail = ActionMailer::Base.deliveries.last | ||||
assert_not_nil mail | ||||
m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/) | ||||
assert m | ||||
password = m[1] | ||||
assert User.find(2).check_password?(password) | ||||
end | ||||
def test_update_without_generate_password_should_not_change_password | ||||
|
r15282 | put :update, :params => { | ||
:id => 2, :user => { | ||||
:firstname => 'changed', | ||||
:generate_password => '0', | ||||
:password => '', | ||||
:password_confirmation => '' | ||||
}, | ||||
:send_information => '1' | ||||
} | ||||
|
r11226 | |||
user = User.find(2) | ||||
assert_equal 'changed', user.firstname | ||||
assert user.check_password?('jsmith') | ||||
end | ||||
|
r9111 | def test_update_user_switchin_from_auth_source_to_password_authentication | ||
|
r3952 | # Configure as auth source | ||
u = User.find(2) | ||||
u.auth_source = AuthSource.find(1) | ||||
u.save! | ||||
|
r15282 | put :update, :params => { | ||
:id => u.id, | ||||
:user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'} | ||||
} | ||||
|
r3952 | |||
|
r15678 | assert_nil u.reload.auth_source | ||
|
r10659 | assert u.check_password?('newpass123') | ||
|
r3952 | end | ||
|
r6508 | |||
|
r9551 | def test_update_notified_project | ||
|
r15282 | get :edit, :params => {:id => 2} | ||
|
r9551 | assert_response :success | ||
u = User.find(2) | ||||
assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort | ||||
assert_equal [1, 2, 5], u.notified_projects_ids.sort | ||||
|
r11610 | assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1' | ||
|
r9551 | assert_equal 'all', u.mail_notification | ||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => { | ||||
:mail_notification => 'selected', | ||||
:notified_project_ids => [1, 2] | ||||
} | ||||
} | ||||
|
r9551 | u = User.find(2) | ||
assert_equal 'selected', u.mail_notification | ||||
assert_equal [1, 2], u.notified_projects_ids.sort | ||||
end | ||||
|
r11609 | def test_update_status_should_not_update_attributes | ||
user = User.find(2) | ||||
user.pref[:no_self_notified] = '1' | ||||
user.pref.save | ||||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => {:status => 3} | ||||
} | ||||
|
r11609 | assert_response 302 | ||
user = User.find(2) | ||||
assert_equal 3, user.status | ||||
assert_equal '1', user.pref[:no_self_notified] | ||||
end | ||||
|
r14883 | def test_update_assign_admin_should_send_security_notification | ||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => {:admin => 1} | ||||
|
r14883 | } | ||
assert_not_nil (mail = ActionMailer::Base.deliveries.last) | ||||
assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: User.find(2).login), mail | ||||
# All admins should receive this | ||||
User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin| | ||||
assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) } | ||||
end | ||||
end | ||||
def test_update_unassign_admin_should_send_security_notification | ||||
user = User.find(2) | ||||
user.admin = true | ||||
user.save! | ||||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | put :update, :params => { | ||
:id => user.id, | ||||
:user => {:admin => 0} | ||||
|
r14883 | } | ||
assert_not_nil (mail = ActionMailer::Base.deliveries.last) | ||||
assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: user.login), mail | ||||
# All admins should receive this | ||||
User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin| | ||||
assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) } | ||||
end | ||||
end | ||||
def test_update_lock_admin_should_send_security_notification | ||||
user = User.find(2) | ||||
user.admin = true | ||||
user.save! | ||||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | put :update, :params => { | ||
:id => 2, | ||||
:user => {:status => Principal::STATUS_LOCKED} | ||||
|
r14883 | } | ||
assert_not_nil (mail = ActionMailer::Base.deliveries.last) | ||||
assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: User.find(2).login), mail | ||||
# All admins should receive this | ||||
User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin| | ||||
assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) } | ||||
end | ||||
# if user is already locked, destroying should not send a second mail | ||||
# (for active admins see furtherbelow) | ||||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | delete :destroy, :params => {:id => 1} | ||
|
r14883 | assert_nil ActionMailer::Base.deliveries.last | ||
end | ||||
def test_update_unlock_admin_should_send_security_notification | ||||
user = User.find(5) # already locked | ||||
user.admin = true | ||||
user.save! | ||||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | put :update, :params => { | ||
:id => user.id, | ||||
:user => {:status => Principal::STATUS_ACTIVE} | ||||
|
r14883 | } | ||
assert_not_nil (mail = ActionMailer::Base.deliveries.last) | ||||
assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: user.login), mail | ||||
# All admins should receive this | ||||
User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin| | ||||
assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) } | ||||
end | ||||
end | ||||
def test_update_admin_unrelated_property_should_not_send_security_notification | ||||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | put :update, :params => { | ||
:id => 1, | ||||
:user => {:firstname => 'Jimmy'} | ||||
|
r14883 | } | ||
assert_nil ActionMailer::Base.deliveries.last | ||||
end | ||||
|
r4609 | def test_destroy | ||
assert_difference 'User.count', -1 do | ||||
|
r15282 | delete :destroy, :params => {:id => 2} | ||
|
r4609 | end | ||
assert_redirected_to '/users' | ||||
assert_nil User.find_by_id(2) | ||||
end | ||||
def test_destroy_should_be_denied_for_non_admin_users | ||||
@request.session[:user_id] = 3 | ||||
|
r6508 | |||
|
r4609 | assert_no_difference 'User.count' do | ||
|
r15282 | get :destroy, :params => {:id => 2} | ||
|
r4609 | end | ||
assert_response 403 | ||||
end | ||||
|
r6508 | |||
|
r10057 | def test_destroy_should_redirect_to_back_url_param | ||
assert_difference 'User.count', -1 do | ||||
|
r15282 | delete :destroy, :params => {:id => 2, :back_url => '/users?name=foo'} | ||
|
r10057 | end | ||
assert_redirected_to '/users?name=foo' | ||||
end | ||||
|
r14883 | |||
def test_destroy_active_admin_should_send_security_notification | ||||
user = User.find(2) | ||||
user.admin = true | ||||
user.save! | ||||
ActionMailer::Base.deliveries.clear | ||||
|
r15282 | delete :destroy, :params => {:id => user.id} | ||
|
r14883 | |||
assert_not_nil (mail = ActionMailer::Base.deliveries.last) | ||||
assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: user.login), mail | ||||
# All admins should receive this | ||||
User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin| | ||||
assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) } | ||||
end | ||||
end | ||||
|
r974 | end | ||