2011-08-25 04:37:56 +04:00
|
|
|
# Redmine - project management software
|
2012-05-05 16:56:53 +04:00
|
|
|
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
2008-03-15 11:27:38 +03:00
|
|
|
#
|
|
|
|
# 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.
|
2011-08-25 04:37:56 +04:00
|
|
|
#
|
2008-03-15 11:27:38 +03:00
|
|
|
# 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.
|
2011-08-25 04:37:56 +04:00
|
|
|
#
|
2008-03-15 11:27:38 +03:00
|
|
|
# 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.
|
|
|
|
|
2010-12-13 02:24:34 +03:00
|
|
|
require File.expand_path('../../test_helper', __FILE__)
|
2008-03-15 11:27:38 +03:00
|
|
|
|
2009-09-13 21:14:35 +04:00
|
|
|
class RolesControllerTest < ActionController::TestCase
|
2011-05-02 03:14:28 +04:00
|
|
|
fixtures :roles, :users, :members, :member_roles, :workflows, :trackers
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-15 11:27:38 +03:00
|
|
|
def setup
|
2012-03-04 16:44:58 +04:00
|
|
|
@controller = RolesController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2008-03-15 11:27:38 +03:00
|
|
|
User.current = nil
|
|
|
|
@request.session[:user_id] = 1 # admin
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_index
|
2008-03-17 23:02:51 +03:00
|
|
|
get :index
|
|
|
|
assert_response :success
|
2010-02-15 19:41:16 +03:00
|
|
|
assert_template 'index'
|
2008-03-17 23:02:51 +03:00
|
|
|
|
|
|
|
assert_not_nil assigns(:roles)
|
|
|
|
assert_equal Role.find(:all, :order => 'builtin, position'), assigns(:roles)
|
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
assert_tag :tag => 'a', :attributes => { :href => '/roles/1/edit' },
|
2008-03-17 23:02:51 +03:00
|
|
|
:content => 'Manager'
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_new
|
2008-03-15 11:27:38 +03:00
|
|
|
get :new
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'new'
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_create_with_validaton_failure
|
|
|
|
post :create, :role => {:name => '',
|
2008-03-15 11:27:38 +03:00
|
|
|
:permissions => ['add_issues', 'edit_issues', 'log_time', ''],
|
|
|
|
:assignable => '0'}
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-15 11:27:38 +03:00
|
|
|
assert_response :success
|
|
|
|
assert_template 'new'
|
|
|
|
assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' }
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_create_without_workflow_copy
|
|
|
|
post :create, :role => {:name => 'RoleWithoutWorkflowCopy',
|
2008-03-15 11:27:38 +03:00
|
|
|
:permissions => ['add_issues', 'edit_issues', 'log_time', ''],
|
|
|
|
:assignable => '0'}
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-15 11:27:38 +03:00
|
|
|
role = Role.find_by_name('RoleWithoutWorkflowCopy')
|
|
|
|
assert_not_nil role
|
|
|
|
assert_equal [:add_issues, :edit_issues, :log_time], role.permissions
|
|
|
|
assert !role.assignable?
|
|
|
|
end
|
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_create_with_workflow_copy
|
|
|
|
post :create, :role => {:name => 'RoleWithWorkflowCopy',
|
2008-03-15 11:27:38 +03:00
|
|
|
:permissions => ['add_issues', 'edit_issues', 'log_time', ''],
|
|
|
|
:assignable => '0'},
|
|
|
|
:copy_workflow_from => '1'
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-15 11:27:38 +03:00
|
|
|
role = Role.find_by_name('RoleWithWorkflowCopy')
|
|
|
|
assert_not_nil role
|
2012-07-15 18:12:17 +04:00
|
|
|
assert_equal Role.find(1).workflow_rules.size, role.workflow_rules.size
|
2008-03-15 11:27:38 +03:00
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_edit
|
2008-03-15 11:27:38 +03:00
|
|
|
get :edit, :id => 1
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'edit'
|
|
|
|
assert_equal Role.find(1), assigns(:role)
|
|
|
|
end
|
|
|
|
|
2012-02-23 18:26:30 +04:00
|
|
|
def test_edit_invalid_should_respond_with_404
|
|
|
|
get :edit, :id => 999
|
|
|
|
assert_response 404
|
|
|
|
end
|
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_update
|
|
|
|
put :update, :id => 1,
|
2008-03-15 11:27:38 +03:00
|
|
|
:role => {:name => 'Manager',
|
|
|
|
:permissions => ['edit_project', ''],
|
|
|
|
:assignable => '0'}
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-15 11:27:38 +03:00
|
|
|
role = Role.find(1)
|
|
|
|
assert_equal [:edit_project], role.permissions
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_update_with_failure
|
|
|
|
put :update, :id => 1, :role => {:name => ''}
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'edit'
|
|
|
|
end
|
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
def test_destroy
|
2011-12-10 03:29:58 +04:00
|
|
|
r = Role.create!(:name => 'ToBeDestroyed', :permissions => [:view_wiki_pages])
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
delete :destroy, :id => r
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_nil Role.find_by_id(r.id)
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
def test_destroy_role_in_use
|
2011-12-10 03:29:58 +04:00
|
|
|
delete :destroy, :id => 1
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2011-12-10 03:29:58 +04:00
|
|
|
assert_equal 'This role is in use and cannot be deleted.', flash[:error]
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_not_nil Role.find_by_id(1)
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_get_permissions
|
|
|
|
get :permissions
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_response :success
|
2011-12-10 03:29:58 +04:00
|
|
|
assert_template 'permissions'
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_not_nil assigns(:roles)
|
|
|
|
assert_equal Role.find(:all, :order => 'builtin, position'), assigns(:roles)
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
|
|
|
|
:name => 'permissions[3][]',
|
|
|
|
:value => 'add_issues',
|
|
|
|
:checked => 'checked' }
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
|
|
|
|
:name => 'permissions[3][]',
|
|
|
|
:value => 'delete_issues',
|
|
|
|
:checked => nil }
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2011-12-10 03:29:58 +04:00
|
|
|
def test_post_permissions
|
|
|
|
post :permissions, :permissions => { '0' => '', '1' => ['edit_issues'], '3' => ['add_issues', 'delete_issues']}
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_equal [:edit_issues], Role.find(1).permissions
|
|
|
|
assert_equal [:add_issues, :delete_issues], Role.find(3).permissions
|
|
|
|
assert Role.find(2).permissions.empty?
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
def test_clear_all_permissions
|
2011-12-10 03:29:58 +04:00
|
|
|
post :permissions, :permissions => { '0' => '' }
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-17 23:02:51 +03:00
|
|
|
assert Role.find(1).permissions.empty?
|
|
|
|
end
|
2011-08-25 04:37:56 +04:00
|
|
|
|
2008-03-17 23:02:51 +03:00
|
|
|
def test_move_highest
|
2011-12-10 03:29:58 +04:00
|
|
|
put :update, :id => 3, :role => {:move_to => 'highest'}
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_equal 1, Role.find(3).position
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_move_higher
|
|
|
|
position = Role.find(3).position
|
2011-12-10 03:29:58 +04:00
|
|
|
put :update, :id => 3, :role => {:move_to => 'higher'}
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_equal position - 1, Role.find(3).position
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_move_lower
|
|
|
|
position = Role.find(2).position
|
2011-12-10 03:29:58 +04:00
|
|
|
put :update, :id => 2, :role => {:move_to => 'lower'}
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_equal position + 1, Role.find(2).position
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_move_lowest
|
2011-12-10 03:29:58 +04:00
|
|
|
put :update, :id => 2, :role => {:move_to => 'lowest'}
|
2010-11-14 19:45:32 +03:00
|
|
|
assert_redirected_to '/roles'
|
2008-03-17 23:02:51 +03:00
|
|
|
assert_equal Role.count, Role.find(2).position
|
|
|
|
end
|
2008-03-15 11:27:38 +03:00
|
|
|
end
|