Chris@909
|
1 # Redmine - project management software
|
Chris@1115
|
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@119
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@0
|
19
|
Chris@0
|
20 class RolesControllerTest < ActionController::TestCase
|
Chris@441
|
21 fixtures :roles, :users, :members, :member_roles, :workflows, :trackers
|
Chris@909
|
22
|
Chris@0
|
23 def setup
|
Chris@0
|
24 @controller = RolesController.new
|
Chris@0
|
25 @request = ActionController::TestRequest.new
|
Chris@0
|
26 @response = ActionController::TestResponse.new
|
Chris@0
|
27 User.current = nil
|
Chris@0
|
28 @request.session[:user_id] = 1 # admin
|
Chris@0
|
29 end
|
Chris@909
|
30
|
Chris@1115
|
31 def test_index
|
Chris@0
|
32 get :index
|
Chris@0
|
33 assert_response :success
|
Chris@0
|
34 assert_template 'index'
|
Chris@0
|
35
|
Chris@0
|
36 assert_not_nil assigns(:roles)
|
Chris@0
|
37 assert_equal Role.find(:all, :order => 'builtin, position'), assigns(:roles)
|
Chris@0
|
38
|
Chris@1115
|
39 assert_tag :tag => 'a', :attributes => { :href => '/roles/1/edit' },
|
Chris@0
|
40 :content => 'Manager'
|
Chris@0
|
41 end
|
Chris@909
|
42
|
Chris@1115
|
43 def test_new
|
Chris@0
|
44 get :new
|
Chris@0
|
45 assert_response :success
|
Chris@0
|
46 assert_template 'new'
|
Chris@0
|
47 end
|
Chris@909
|
48
|
Chris@1115
|
49 def test_new_with_copy
|
Chris@1115
|
50 copy_from = Role.find(2)
|
Chris@1115
|
51
|
Chris@1115
|
52 get :new, :copy => copy_from.id.to_s
|
Chris@1115
|
53 assert_response :success
|
Chris@1115
|
54 assert_template 'new'
|
Chris@1115
|
55
|
Chris@1115
|
56 role = assigns(:role)
|
Chris@1115
|
57 assert_equal copy_from.permissions, role.permissions
|
Chris@1115
|
58
|
Chris@1115
|
59 assert_select 'form' do
|
Chris@1115
|
60 # blank name
|
Chris@1115
|
61 assert_select 'input[name=?][value=]', 'role[name]'
|
Chris@1115
|
62 # edit_project permission checked
|
Chris@1115
|
63 assert_select 'input[type=checkbox][name=?][value=edit_project][checked=checked]', 'role[permissions][]'
|
Chris@1115
|
64 # add_project permission not checked
|
Chris@1115
|
65 assert_select 'input[type=checkbox][name=?][value=add_project]', 'role[permissions][]'
|
Chris@1115
|
66 assert_select 'input[type=checkbox][name=?][value=add_project][checked=checked]', 'role[permissions][]', 0
|
Chris@1115
|
67 # workflow copy selected
|
Chris@1115
|
68 assert_select 'select[name=?]', 'copy_workflow_from' do
|
Chris@1115
|
69 assert_select 'option[value=2][selected=selected]'
|
Chris@1115
|
70 end
|
Chris@1115
|
71 end
|
Chris@1115
|
72 end
|
Chris@1115
|
73
|
Chris@1115
|
74 def test_create_with_validaton_failure
|
Chris@1115
|
75 post :create, :role => {:name => '',
|
Chris@0
|
76 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
|
Chris@0
|
77 :assignable => '0'}
|
Chris@909
|
78
|
Chris@0
|
79 assert_response :success
|
Chris@0
|
80 assert_template 'new'
|
Chris@0
|
81 assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' }
|
Chris@0
|
82 end
|
Chris@909
|
83
|
Chris@1115
|
84 def test_create_without_workflow_copy
|
Chris@1115
|
85 post :create, :role => {:name => 'RoleWithoutWorkflowCopy',
|
Chris@0
|
86 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
|
Chris@0
|
87 :assignable => '0'}
|
Chris@909
|
88
|
chris@37
|
89 assert_redirected_to '/roles'
|
Chris@0
|
90 role = Role.find_by_name('RoleWithoutWorkflowCopy')
|
Chris@0
|
91 assert_not_nil role
|
Chris@0
|
92 assert_equal [:add_issues, :edit_issues, :log_time], role.permissions
|
Chris@0
|
93 assert !role.assignable?
|
Chris@0
|
94 end
|
Chris@0
|
95
|
Chris@1115
|
96 def test_create_with_workflow_copy
|
Chris@1115
|
97 post :create, :role => {:name => 'RoleWithWorkflowCopy',
|
Chris@0
|
98 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
|
Chris@0
|
99 :assignable => '0'},
|
Chris@0
|
100 :copy_workflow_from => '1'
|
Chris@909
|
101
|
chris@37
|
102 assert_redirected_to '/roles'
|
Chris@0
|
103 role = Role.find_by_name('RoleWithWorkflowCopy')
|
Chris@0
|
104 assert_not_nil role
|
Chris@1115
|
105 assert_equal Role.find(1).workflow_rules.size, role.workflow_rules.size
|
Chris@0
|
106 end
|
Chris@909
|
107
|
Chris@1115
|
108 def test_edit
|
Chris@0
|
109 get :edit, :id => 1
|
Chris@0
|
110 assert_response :success
|
Chris@0
|
111 assert_template 'edit'
|
Chris@0
|
112 assert_equal Role.find(1), assigns(:role)
|
Chris@1115
|
113 assert_select 'select[name=?]', 'role[issues_visibility]'
|
Chris@0
|
114 end
|
Chris@0
|
115
|
Chris@1115
|
116 def test_edit_anonymous
|
Chris@1115
|
117 get :edit, :id => Role.anonymous.id
|
Chris@1115
|
118 assert_response :success
|
Chris@1115
|
119 assert_template 'edit'
|
Chris@1115
|
120 assert_select 'select[name=?]', 'role[issues_visibility]', 0
|
Chris@1115
|
121 end
|
Chris@1115
|
122
|
Chris@1115
|
123 def test_edit_invalid_should_respond_with_404
|
Chris@1115
|
124 get :edit, :id => 999
|
Chris@1115
|
125 assert_response 404
|
Chris@1115
|
126 end
|
Chris@1115
|
127
|
Chris@1115
|
128 def test_update
|
Chris@1115
|
129 put :update, :id => 1,
|
Chris@0
|
130 :role => {:name => 'Manager',
|
Chris@0
|
131 :permissions => ['edit_project', ''],
|
Chris@0
|
132 :assignable => '0'}
|
Chris@909
|
133
|
chris@37
|
134 assert_redirected_to '/roles'
|
Chris@0
|
135 role = Role.find(1)
|
Chris@0
|
136 assert_equal [:edit_project], role.permissions
|
Chris@0
|
137 end
|
Chris@909
|
138
|
Chris@1115
|
139 def test_update_with_failure
|
Chris@1115
|
140 put :update, :id => 1, :role => {:name => ''}
|
Chris@1115
|
141 assert_response :success
|
Chris@1115
|
142 assert_template 'edit'
|
Chris@1115
|
143 end
|
Chris@1115
|
144
|
Chris@0
|
145 def test_destroy
|
Chris@1115
|
146 r = Role.create!(:name => 'ToBeDestroyed', :permissions => [:view_wiki_pages])
|
Chris@909
|
147
|
Chris@1115
|
148 delete :destroy, :id => r
|
chris@37
|
149 assert_redirected_to '/roles'
|
Chris@0
|
150 assert_nil Role.find_by_id(r.id)
|
Chris@0
|
151 end
|
Chris@909
|
152
|
Chris@0
|
153 def test_destroy_role_in_use
|
Chris@1115
|
154 delete :destroy, :id => 1
|
chris@37
|
155 assert_redirected_to '/roles'
|
Chris@1115
|
156 assert_equal 'This role is in use and cannot be deleted.', flash[:error]
|
Chris@0
|
157 assert_not_nil Role.find_by_id(1)
|
Chris@0
|
158 end
|
Chris@909
|
159
|
Chris@1115
|
160 def test_get_permissions
|
Chris@1115
|
161 get :permissions
|
Chris@0
|
162 assert_response :success
|
Chris@1115
|
163 assert_template 'permissions'
|
Chris@909
|
164
|
Chris@0
|
165 assert_not_nil assigns(:roles)
|
Chris@0
|
166 assert_equal Role.find(:all, :order => 'builtin, position'), assigns(:roles)
|
Chris@909
|
167
|
Chris@0
|
168 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
|
Chris@0
|
169 :name => 'permissions[3][]',
|
Chris@0
|
170 :value => 'add_issues',
|
Chris@0
|
171 :checked => 'checked' }
|
Chris@909
|
172
|
Chris@0
|
173 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
|
Chris@0
|
174 :name => 'permissions[3][]',
|
Chris@0
|
175 :value => 'delete_issues',
|
Chris@0
|
176 :checked => nil }
|
Chris@0
|
177 end
|
Chris@909
|
178
|
Chris@1115
|
179 def test_post_permissions
|
Chris@1115
|
180 post :permissions, :permissions => { '0' => '', '1' => ['edit_issues'], '3' => ['add_issues', 'delete_issues']}
|
chris@37
|
181 assert_redirected_to '/roles'
|
Chris@909
|
182
|
Chris@0
|
183 assert_equal [:edit_issues], Role.find(1).permissions
|
Chris@0
|
184 assert_equal [:add_issues, :delete_issues], Role.find(3).permissions
|
Chris@0
|
185 assert Role.find(2).permissions.empty?
|
Chris@0
|
186 end
|
Chris@909
|
187
|
Chris@0
|
188 def test_clear_all_permissions
|
Chris@1115
|
189 post :permissions, :permissions => { '0' => '' }
|
chris@37
|
190 assert_redirected_to '/roles'
|
Chris@0
|
191 assert Role.find(1).permissions.empty?
|
Chris@0
|
192 end
|
Chris@909
|
193
|
Chris@0
|
194 def test_move_highest
|
Chris@1115
|
195 put :update, :id => 3, :role => {:move_to => 'highest'}
|
chris@37
|
196 assert_redirected_to '/roles'
|
Chris@0
|
197 assert_equal 1, Role.find(3).position
|
Chris@0
|
198 end
|
Chris@0
|
199
|
Chris@0
|
200 def test_move_higher
|
Chris@0
|
201 position = Role.find(3).position
|
Chris@1115
|
202 put :update, :id => 3, :role => {:move_to => 'higher'}
|
chris@37
|
203 assert_redirected_to '/roles'
|
Chris@0
|
204 assert_equal position - 1, Role.find(3).position
|
Chris@0
|
205 end
|
Chris@0
|
206
|
Chris@0
|
207 def test_move_lower
|
Chris@0
|
208 position = Role.find(2).position
|
Chris@1115
|
209 put :update, :id => 2, :role => {:move_to => 'lower'}
|
chris@37
|
210 assert_redirected_to '/roles'
|
Chris@0
|
211 assert_equal position + 1, Role.find(2).position
|
Chris@0
|
212 end
|
Chris@0
|
213
|
Chris@0
|
214 def test_move_lowest
|
Chris@1115
|
215 put :update, :id => 2, :role => {:move_to => 'lowest'}
|
chris@37
|
216 assert_redirected_to '/roles'
|
Chris@0
|
217 assert_equal Role.count, Role.find(2).position
|
Chris@0
|
218 end
|
Chris@0
|
219 end
|