Revision 1297:0a574315af3e .svn/pristine/76
| .svn/pristine/76/7627b3e96063b3b2d9ab37b36a7671517dc19460.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
|
| 3 |
# |
|
| 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 |
require File.expand_path('../../test_helper', __FILE__)
|
|
| 19 |
require 'issue_categories_controller' |
|
| 20 |
|
|
| 21 |
# Re-raise errors caught by the controller. |
|
| 22 |
class IssueCategoriesController; def rescue_action(e) raise e end; end |
|
| 23 |
|
|
| 24 |
class IssueCategoriesControllerTest < ActionController::TestCase |
|
| 25 |
fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules, :issue_categories, |
|
| 26 |
:issues |
|
| 27 |
|
|
| 28 |
def setup |
|
| 29 |
@controller = IssueCategoriesController.new |
|
| 30 |
@request = ActionController::TestRequest.new |
|
| 31 |
@response = ActionController::TestResponse.new |
|
| 32 |
User.current = nil |
|
| 33 |
@request.session[:user_id] = 2 |
|
| 34 |
end |
|
| 35 |
|
|
| 36 |
def test_new |
|
| 37 |
@request.session[:user_id] = 2 # manager |
|
| 38 |
get :new, :project_id => '1' |
|
| 39 |
assert_response :success |
|
| 40 |
assert_template 'new' |
|
| 41 |
assert_select 'input[name=?]', 'issue_category[name]' |
|
| 42 |
end |
|
| 43 |
|
|
| 44 |
def test_new_from_issue_form |
|
| 45 |
@request.session[:user_id] = 2 # manager |
|
| 46 |
xhr :get, :new, :project_id => '1' |
|
| 47 |
|
|
| 48 |
assert_response :success |
|
| 49 |
assert_template 'new' |
|
| 50 |
assert_equal 'text/javascript', response.content_type |
|
| 51 |
end |
|
| 52 |
|
|
| 53 |
def test_create |
|
| 54 |
@request.session[:user_id] = 2 # manager |
|
| 55 |
assert_difference 'IssueCategory.count' do |
|
| 56 |
post :create, :project_id => '1', :issue_category => {:name => 'New category'}
|
|
| 57 |
end |
|
| 58 |
assert_redirected_to '/projects/ecookbook/settings/categories' |
|
| 59 |
category = IssueCategory.find_by_name('New category')
|
|
| 60 |
assert_not_nil category |
|
| 61 |
assert_equal 1, category.project_id |
|
| 62 |
end |
|
| 63 |
|
|
| 64 |
def test_create_failure |
|
| 65 |
@request.session[:user_id] = 2 |
|
| 66 |
post :create, :project_id => '1', :issue_category => {:name => ''}
|
|
| 67 |
assert_response :success |
|
| 68 |
assert_template 'new' |
|
| 69 |
end |
|
| 70 |
|
|
| 71 |
def test_create_from_issue_form |
|
| 72 |
@request.session[:user_id] = 2 # manager |
|
| 73 |
assert_difference 'IssueCategory.count' do |
|
| 74 |
xhr :post, :create, :project_id => '1', :issue_category => {:name => 'New category'}
|
|
| 75 |
end |
|
| 76 |
category = IssueCategory.first(:order => 'id DESC') |
|
| 77 |
assert_equal 'New category', category.name |
|
| 78 |
|
|
| 79 |
assert_response :success |
|
| 80 |
assert_template 'create' |
|
| 81 |
assert_equal 'text/javascript', response.content_type |
|
| 82 |
end |
|
| 83 |
|
|
| 84 |
def test_create_from_issue_form_with_failure |
|
| 85 |
@request.session[:user_id] = 2 # manager |
|
| 86 |
assert_no_difference 'IssueCategory.count' do |
|
| 87 |
xhr :post, :create, :project_id => '1', :issue_category => {:name => ''}
|
|
| 88 |
end |
|
| 89 |
|
|
| 90 |
assert_response :success |
|
| 91 |
assert_template 'new' |
|
| 92 |
assert_equal 'text/javascript', response.content_type |
|
| 93 |
end |
|
| 94 |
|
|
| 95 |
def test_edit |
|
| 96 |
@request.session[:user_id] = 2 |
|
| 97 |
get :edit, :id => 2 |
|
| 98 |
assert_response :success |
|
| 99 |
assert_template 'edit' |
|
| 100 |
assert_select 'input[name=?][value=?]', 'issue_category[name]', 'Recipes' |
|
| 101 |
end |
|
| 102 |
|
|
| 103 |
def test_update |
|
| 104 |
assert_no_difference 'IssueCategory.count' do |
|
| 105 |
put :update, :id => 2, :issue_category => { :name => 'Testing' }
|
|
| 106 |
end |
|
| 107 |
assert_redirected_to '/projects/ecookbook/settings/categories' |
|
| 108 |
assert_equal 'Testing', IssueCategory.find(2).name |
|
| 109 |
end |
|
| 110 |
|
|
| 111 |
def test_update_failure |
|
| 112 |
put :update, :id => 2, :issue_category => { :name => '' }
|
|
| 113 |
assert_response :success |
|
| 114 |
assert_template 'edit' |
|
| 115 |
end |
|
| 116 |
|
|
| 117 |
def test_update_not_found |
|
| 118 |
put :update, :id => 97, :issue_category => { :name => 'Testing' }
|
|
| 119 |
assert_response 404 |
|
| 120 |
end |
|
| 121 |
|
|
| 122 |
def test_destroy_category_not_in_use |
|
| 123 |
delete :destroy, :id => 2 |
|
| 124 |
assert_redirected_to '/projects/ecookbook/settings/categories' |
|
| 125 |
assert_nil IssueCategory.find_by_id(2) |
|
| 126 |
end |
|
| 127 |
|
|
| 128 |
def test_destroy_category_in_use |
|
| 129 |
delete :destroy, :id => 1 |
|
| 130 |
assert_response :success |
|
| 131 |
assert_template 'destroy' |
|
| 132 |
assert_not_nil IssueCategory.find_by_id(1) |
|
| 133 |
end |
|
| 134 |
|
|
| 135 |
def test_destroy_category_in_use_with_reassignment |
|
| 136 |
issue = Issue.find(:first, :conditions => {:category_id => 1})
|
|
| 137 |
delete :destroy, :id => 1, :todo => 'reassign', :reassign_to_id => 2 |
|
| 138 |
assert_redirected_to '/projects/ecookbook/settings/categories' |
|
| 139 |
assert_nil IssueCategory.find_by_id(1) |
|
| 140 |
# check that the issue was reassign |
|
| 141 |
assert_equal 2, issue.reload.category_id |
|
| 142 |
end |
|
| 143 |
|
|
| 144 |
def test_destroy_category_in_use_without_reassignment |
|
| 145 |
issue = Issue.find(:first, :conditions => {:category_id => 1})
|
|
| 146 |
delete :destroy, :id => 1, :todo => 'nullify' |
|
| 147 |
assert_redirected_to '/projects/ecookbook/settings/categories' |
|
| 148 |
assert_nil IssueCategory.find_by_id(1) |
|
| 149 |
# check that the issue category was nullified |
|
| 150 |
assert_nil issue.reload.category_id |
|
| 151 |
end |
|
| 152 |
end |
|
| .svn/pristine/76/7677229bdec49f4afccd35e0e3322d16769ce80a.svn-base | ||
|---|---|---|
| 1 |
# encoding: utf-8 |
|
| 2 |
# |
|
| 3 |
# Redmine - project management software |
|
| 4 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
|
| 5 |
# |
|
| 6 |
# This program is free software; you can redistribute it and/or |
|
| 7 |
# modify it under the terms of the GNU General Public License |
|
| 8 |
# as published by the Free Software Foundation; either version 2 |
|
| 9 |
# of the License, or (at your option) any later version. |
|
| 10 |
# |
|
| 11 |
# This program is distributed in the hope that it will be useful, |
|
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
# GNU General Public License for more details. |
|
| 15 |
# |
|
| 16 |
# You should have received a copy of the GNU General Public License |
|
| 17 |
# along with this program; if not, write to the Free Software |
|
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 19 |
|
|
| 20 |
module WorkflowsHelper |
|
| 21 |
def field_required?(field) |
|
| 22 |
field.is_a?(CustomField) ? field.is_required? : %w(project_id tracker_id subject priority_id is_private).include?(field) |
|
| 23 |
end |
|
| 24 |
|
|
| 25 |
def field_permission_tag(permissions, status, field) |
|
| 26 |
name = field.is_a?(CustomField) ? field.id.to_s : field |
|
| 27 |
options = [["", ""], [l(:label_readonly), "readonly"]] |
|
| 28 |
options << [l(:label_required), "required"] unless field_required?(field) |
|
| 29 |
|
|
| 30 |
select_tag("permissions[#{name}][#{status.id}]", options_for_select(options, permissions[status.id][name]))
|
|
| 31 |
end |
|
| 32 |
end |
|
| .svn/pristine/76/76c9364a72c309fc8295666ab0923fa136a43fd5.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
|
| 3 |
# |
|
| 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 |
module Redmine |
|
| 19 |
module AccessControl |
|
| 20 |
|
|
| 21 |
class << self |
|
| 22 |
def map |
|
| 23 |
mapper = Mapper.new |
|
| 24 |
yield mapper |
|
| 25 |
@permissions ||= [] |
|
| 26 |
@permissions += mapper.mapped_permissions |
|
| 27 |
end |
|
| 28 |
|
|
| 29 |
def permissions |
|
| 30 |
@permissions |
|
| 31 |
end |
|
| 32 |
|
|
| 33 |
# Returns the permission of given name or nil if it wasn't found |
|
| 34 |
# Argument should be a symbol |
|
| 35 |
def permission(name) |
|
| 36 |
permissions.detect {|p| p.name == name}
|
|
| 37 |
end |
|
| 38 |
|
|
| 39 |
# Returns the actions that are allowed by the permission of given name |
|
| 40 |
def allowed_actions(permission_name) |
|
| 41 |
perm = permission(permission_name) |
|
| 42 |
perm ? perm.actions : [] |
|
| 43 |
end |
|
| 44 |
|
|
| 45 |
def public_permissions |
|
| 46 |
@public_permissions ||= @permissions.select {|p| p.public?}
|
|
| 47 |
end |
|
| 48 |
|
|
| 49 |
def members_only_permissions |
|
| 50 |
@members_only_permissions ||= @permissions.select {|p| p.require_member?}
|
|
| 51 |
end |
|
| 52 |
|
|
| 53 |
def loggedin_only_permissions |
|
| 54 |
@loggedin_only_permissions ||= @permissions.select {|p| p.require_loggedin?}
|
|
| 55 |
end |
|
| 56 |
|
|
| 57 |
def read_action?(action) |
|
| 58 |
if action.is_a?(Symbol) |
|
| 59 |
perm = permission(action) |
|
| 60 |
!perm.nil? && perm.read? |
|
| 61 |
else |
|
| 62 |
s = "#{action[:controller]}/#{action[:action]}"
|
|
| 63 |
permissions.detect {|p| p.actions.include?(s) && !p.read?}.nil?
|
|
| 64 |
end |
|
| 65 |
end |
|
| 66 |
|
|
| 67 |
def available_project_modules |
|
| 68 |
@available_project_modules ||= @permissions.collect(&:project_module).uniq.compact |
|
| 69 |
end |
|
| 70 |
|
|
| 71 |
def modules_permissions(modules) |
|
| 72 |
@permissions.select {|p| p.project_module.nil? || modules.include?(p.project_module.to_s)}
|
|
| 73 |
end |
|
| 74 |
end |
|
| 75 |
|
|
| 76 |
class Mapper |
|
| 77 |
def initialize |
|
| 78 |
@project_module = nil |
|
| 79 |
end |
|
| 80 |
|
|
| 81 |
def permission(name, hash, options={})
|
|
| 82 |
@permissions ||= [] |
|
| 83 |
options.merge!(:project_module => @project_module) |
|
| 84 |
@permissions << Permission.new(name, hash, options) |
|
| 85 |
end |
|
| 86 |
|
|
| 87 |
def project_module(name, options={})
|
|
| 88 |
@project_module = name |
|
| 89 |
yield self |
|
| 90 |
@project_module = nil |
|
| 91 |
end |
|
| 92 |
|
|
| 93 |
def mapped_permissions |
|
| 94 |
@permissions |
|
| 95 |
end |
|
| 96 |
end |
|
| 97 |
|
|
| 98 |
class Permission |
|
| 99 |
attr_reader :name, :actions, :project_module |
|
| 100 |
|
|
| 101 |
def initialize(name, hash, options) |
|
| 102 |
@name = name |
|
| 103 |
@actions = [] |
|
| 104 |
@public = options[:public] || false |
|
| 105 |
@require = options[:require] |
|
| 106 |
@read = options[:read] || false |
|
| 107 |
@project_module = options[:project_module] |
|
| 108 |
hash.each do |controller, actions| |
|
| 109 |
if actions.is_a? Array |
|
| 110 |
@actions << actions.collect {|action| "#{controller}/#{action}"}
|
|
| 111 |
else |
|
| 112 |
@actions << "#{controller}/#{actions}"
|
|
| 113 |
end |
|
| 114 |
end |
|
| 115 |
@actions.flatten! |
|
| 116 |
end |
|
| 117 |
|
|
| 118 |
def public? |
|
| 119 |
@public |
|
| 120 |
end |
|
| 121 |
|
|
| 122 |
def require_member? |
|
| 123 |
@require && @require == :member |
|
| 124 |
end |
|
| 125 |
|
|
| 126 |
def require_loggedin? |
|
| 127 |
@require && (@require == :member || @require == :loggedin) |
|
| 128 |
end |
|
| 129 |
|
|
| 130 |
def read? |
|
| 131 |
@read |
|
| 132 |
end |
|
| 133 |
end |
|
| 134 |
end |
|
| 135 |
end |
|
Also available in: Unified diff