Mercurial > hg > soundsoftware-site
comparison .svn/pristine/0f/0f41eec6f8bdcfb1c9cd2c332bcf208f96828f37.svn-base @ 1517:dffacf8a6908 redmine-2.5
Update to Redmine SVN revision 13367 on 2.5-stable branch
author | Chris Cannam |
---|---|
date | Tue, 09 Sep 2014 09:29:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1516:b450a9d58aed | 1517:dffacf8a6908 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 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 | |
20 class IssuesCustomFieldsVisibilityTest < ActionController::TestCase | |
21 tests IssuesController | |
22 fixtures :projects, | |
23 :users, | |
24 :roles, | |
25 :members, | |
26 :member_roles, | |
27 :issue_statuses, | |
28 :trackers, | |
29 :projects_trackers, | |
30 :enabled_modules, | |
31 :enumerations, | |
32 :workflows | |
33 | |
34 def setup | |
35 CustomField.delete_all | |
36 Issue.delete_all | |
37 field_attributes = {:field_format => 'string', :is_for_all => true, :is_filter => true, :trackers => Tracker.all} | |
38 @fields = [] | |
39 @fields << (@field1 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 1', :visible => true))) | |
40 @fields << (@field2 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 2', :visible => false, :role_ids => [1, 2]))) | |
41 @fields << (@field3 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 3', :visible => false, :role_ids => [1, 3]))) | |
42 @issue = Issue.generate!( | |
43 :author_id => 1, | |
44 :project_id => 1, | |
45 :tracker_id => 1, | |
46 :custom_field_values => {@field1.id => 'Value0', @field2.id => 'Value1', @field3.id => 'Value2'} | |
47 ) | |
48 | |
49 @user_with_role_on_other_project = User.generate! | |
50 User.add_to_project(@user_with_role_on_other_project, Project.find(2), Role.find(3)) | |
51 | |
52 @users_to_test = { | |
53 User.find(1) => [@field1, @field2, @field3], | |
54 User.find(3) => [@field1, @field2], | |
55 @user_with_role_on_other_project => [@field1], # should see field1 only on Project 1 | |
56 User.generate! => [@field1], | |
57 User.anonymous => [@field1] | |
58 } | |
59 | |
60 Member.where(:project_id => 1).each do |member| | |
61 member.destroy unless @users_to_test.keys.include?(member.principal) | |
62 end | |
63 end | |
64 | |
65 def test_show_should_show_visible_custom_fields_only | |
66 @users_to_test.each do |user, fields| | |
67 @request.session[:user_id] = user.id | |
68 get :show, :id => @issue.id | |
69 @fields.each_with_index do |field, i| | |
70 if fields.include?(field) | |
71 assert_select 'td', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name}" | |
72 else | |
73 assert_select 'td', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name}" | |
74 end | |
75 end | |
76 end | |
77 end | |
78 | |
79 def test_show_should_show_visible_custom_fields_only_in_api | |
80 @users_to_test.each do |user, fields| | |
81 with_settings :rest_api_enabled => '1' do | |
82 get :show, :id => @issue.id, :format => 'xml', :include => 'custom_fields', :key => user.api_key | |
83 end | |
84 @fields.each_with_index do |field, i| | |
85 if fields.include?(field) | |
86 assert_select "custom_field[id=#{field.id}] value", {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name} in API" | |
87 else | |
88 assert_select "custom_field[id=#{field.id}] value", {:text => "Value#{i}", :count => 0}, "User #{user.id} was not able to view #{field.name} in API" | |
89 end | |
90 end | |
91 end | |
92 end | |
93 | |
94 def test_show_should_show_visible_custom_fields_only_in_history | |
95 @issue.init_journal(User.find(1)) | |
96 @issue.custom_field_values = {@field1.id => 'NewValue0', @field2.id => 'NewValue1', @field3.id => 'NewValue2'} | |
97 @issue.save! | |
98 | |
99 @users_to_test.each do |user, fields| | |
100 @request.session[:user_id] = user.id | |
101 get :show, :id => @issue.id | |
102 @fields.each_with_index do |field, i| | |
103 if fields.include?(field) | |
104 assert_select 'ul.details i', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name} change" | |
105 else | |
106 assert_select 'ul.details i', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name} change" | |
107 end | |
108 end | |
109 end | |
110 end | |
111 | |
112 def test_show_should_show_visible_custom_fields_only_in_history_api | |
113 @issue.init_journal(User.find(1)) | |
114 @issue.custom_field_values = {@field1.id => 'NewValue0', @field2.id => 'NewValue1', @field3.id => 'NewValue2'} | |
115 @issue.save! | |
116 | |
117 @users_to_test.each do |user, fields| | |
118 with_settings :rest_api_enabled => '1' do | |
119 get :show, :id => @issue.id, :format => 'xml', :include => 'journals', :key => user.api_key | |
120 end | |
121 @fields.each_with_index do |field, i| | |
122 if fields.include?(field) | |
123 assert_select 'details old_value', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name} change in API" | |
124 else | |
125 assert_select 'details old_value', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name} change in API" | |
126 end | |
127 end | |
128 end | |
129 end | |
130 | |
131 def test_edit_should_show_visible_custom_fields_only | |
132 Role.anonymous.add_permission! :edit_issues | |
133 | |
134 @users_to_test.each do |user, fields| | |
135 @request.session[:user_id] = user.id | |
136 get :edit, :id => @issue.id | |
137 @fields.each_with_index do |field, i| | |
138 if fields.include?(field) | |
139 assert_select 'input[value=?]', "Value#{i}", 1, "User #{user.id} was not able to edit #{field.name}" | |
140 else | |
141 assert_select 'input[value=?]', "Value#{i}", 0, "User #{user.id} was able to edit #{field.name}" | |
142 end | |
143 end | |
144 end | |
145 end | |
146 | |
147 def test_update_should_update_visible_custom_fields_only | |
148 Role.anonymous.add_permission! :edit_issues | |
149 | |
150 @users_to_test.each do |user, fields| | |
151 @request.session[:user_id] = user.id | |
152 put :update, :id => @issue.id, | |
153 :issue => {:custom_field_values => { | |
154 @field1.id.to_s => "User#{user.id}Value0", | |
155 @field2.id.to_s => "User#{user.id}Value1", | |
156 @field3.id.to_s => "User#{user.id}Value2", | |
157 }} | |
158 @issue.reload | |
159 @fields.each_with_index do |field, i| | |
160 if fields.include?(field) | |
161 assert_equal "User#{user.id}Value#{i}", @issue.custom_field_value(field), "User #{user.id} was not able to update #{field.name}" | |
162 else | |
163 assert_not_equal "User#{user.id}Value#{i}", @issue.custom_field_value(field), "User #{user.id} was able to update #{field.name}" | |
164 end | |
165 end | |
166 end | |
167 end | |
168 | |
169 def test_index_should_show_visible_custom_fields_only | |
170 @users_to_test.each do |user, fields| | |
171 @request.session[:user_id] = user.id | |
172 get :index, :c => (["subject"] + @fields.map{|f| "cf_#{f.id}"}) | |
173 @fields.each_with_index do |field, i| | |
174 if fields.include?(field) | |
175 assert_select 'td', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name}" | |
176 else | |
177 assert_select 'td', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name}" | |
178 end | |
179 end | |
180 end | |
181 end | |
182 | |
183 def test_index_as_csv_should_show_visible_custom_fields_only | |
184 @users_to_test.each do |user, fields| | |
185 @request.session[:user_id] = user.id | |
186 get :index, :c => (["subject"] + @fields.map{|f| "cf_#{f.id}"}), :format => 'csv' | |
187 @fields.each_with_index do |field, i| | |
188 if fields.include?(field) | |
189 assert_include "Value#{i}", response.body, "User #{user.id} was not able to view #{field.name} in CSV" | |
190 else | |
191 assert_not_include "Value#{i}", response.body, "User #{user.id} was able to view #{field.name} in CSV" | |
192 end | |
193 end | |
194 end | |
195 end | |
196 | |
197 def test_index_with_partial_custom_field_visibility | |
198 Issue.delete_all | |
199 p1 = Project.generate! | |
200 p2 = Project.generate! | |
201 user = User.generate! | |
202 User.add_to_project(user, p1, Role.where(:id => [1, 3]).all) | |
203 User.add_to_project(user, p2, Role.where(:id => 3).all) | |
204 Issue.generate!(:project => p1, :tracker_id => 1, :custom_field_values => {@field2.id => 'ValueA'}) | |
205 Issue.generate!(:project => p2, :tracker_id => 1, :custom_field_values => {@field2.id => 'ValueB'}) | |
206 Issue.generate!(:project => p1, :tracker_id => 1, :custom_field_values => {@field2.id => 'ValueC'}) | |
207 | |
208 @request.session[:user_id] = user.id | |
209 get :index, :c => ["subject", "cf_#{@field2.id}"] | |
210 assert_select 'td', :text => 'ValueA' | |
211 assert_select 'td', :text => 'ValueB', :count => 0 | |
212 assert_select 'td', :text => 'ValueC' | |
213 | |
214 get :index, :sort => "cf_#{@field2.id}" | |
215 # ValueB is not visible to user and ignored while sorting | |
216 assert_equal %w(ValueB ValueA ValueC), assigns(:issues).map{|i| i.custom_field_value(@field2)} | |
217 | |
218 get :index, :set_filter => '1', "cf_#{@field2.id}" => '*' | |
219 assert_equal %w(ValueA ValueC), assigns(:issues).map{|i| i.custom_field_value(@field2)} | |
220 | |
221 CustomField.update_all(:field_format => 'list') | |
222 get :index, :group => "cf_#{@field2.id}" | |
223 assert_equal %w(ValueA ValueC), assigns(:issues).map{|i| i.custom_field_value(@field2)} | |
224 end | |
225 | |
226 def test_create_should_send_notifications_according_custom_fields_visibility | |
227 # anonymous user is never notified | |
228 users_to_test = @users_to_test.reject {|k,v| k.anonymous?} | |
229 | |
230 ActionMailer::Base.deliveries.clear | |
231 @request.session[:user_id] = 1 | |
232 with_settings :bcc_recipients => '1' do | |
233 assert_difference 'Issue.count' do | |
234 post :create, | |
235 :project_id => 1, | |
236 :issue => { | |
237 :tracker_id => 1, | |
238 :status_id => 1, | |
239 :subject => 'New issue', | |
240 :priority_id => 5, | |
241 :custom_field_values => {@field1.id.to_s => 'Value0', @field2.id.to_s => 'Value1', @field3.id.to_s => 'Value2'}, | |
242 :watcher_user_ids => users_to_test.keys.map(&:id) | |
243 } | |
244 assert_response 302 | |
245 end | |
246 end | |
247 assert_equal users_to_test.values.uniq.size, ActionMailer::Base.deliveries.size | |
248 # tests that each user receives 1 email with the custom fields he is allowed to see only | |
249 users_to_test.each do |user, fields| | |
250 mails = ActionMailer::Base.deliveries.select {|m| m.bcc.include? user.mail} | |
251 assert_equal 1, mails.size | |
252 mail = mails.first | |
253 @fields.each_with_index do |field, i| | |
254 if fields.include?(field) | |
255 assert_mail_body_match "Value#{i}", mail, "User #{user.id} was not able to view #{field.name} in notification" | |
256 else | |
257 assert_mail_body_no_match "Value#{i}", mail, "User #{user.id} was able to view #{field.name} in notification" | |
258 end | |
259 end | |
260 end | |
261 end | |
262 | |
263 def test_update_should_send_notifications_according_custom_fields_visibility | |
264 # anonymous user is never notified | |
265 users_to_test = @users_to_test.reject {|k,v| k.anonymous?} | |
266 | |
267 users_to_test.keys.each do |user| | |
268 Watcher.create!(:user => user, :watchable => @issue) | |
269 end | |
270 ActionMailer::Base.deliveries.clear | |
271 @request.session[:user_id] = 1 | |
272 with_settings :bcc_recipients => '1' do | |
273 put :update, | |
274 :id => @issue.id, | |
275 :issue => { | |
276 :custom_field_values => {@field1.id.to_s => 'NewValue0', @field2.id.to_s => 'NewValue1', @field3.id.to_s => 'NewValue2'} | |
277 } | |
278 assert_response 302 | |
279 end | |
280 assert_equal users_to_test.values.uniq.size, ActionMailer::Base.deliveries.size | |
281 # tests that each user receives 1 email with the custom fields he is allowed to see only | |
282 users_to_test.each do |user, fields| | |
283 mails = ActionMailer::Base.deliveries.select {|m| m.bcc.include? user.mail} | |
284 assert_equal 1, mails.size | |
285 mail = mails.first | |
286 @fields.each_with_index do |field, i| | |
287 if fields.include?(field) | |
288 assert_mail_body_match "Value#{i}", mail, "User #{user.id} was not able to view #{field.name} in notification" | |
289 else | |
290 assert_mail_body_no_match "Value#{i}", mail, "User #{user.id} was able to view #{field.name} in notification" | |
291 end | |
292 end | |
293 end | |
294 end | |
295 | |
296 def test_updating_hidden_custom_fields_only_should_not_notifiy_user | |
297 # anonymous user is never notified | |
298 users_to_test = @users_to_test.reject {|k,v| k.anonymous?} | |
299 | |
300 users_to_test.keys.each do |user| | |
301 Watcher.create!(:user => user, :watchable => @issue) | |
302 end | |
303 ActionMailer::Base.deliveries.clear | |
304 @request.session[:user_id] = 1 | |
305 with_settings :bcc_recipients => '1' do | |
306 put :update, | |
307 :id => @issue.id, | |
308 :issue => { | |
309 :custom_field_values => {@field2.id.to_s => 'NewValue1', @field3.id.to_s => 'NewValue2'} | |
310 } | |
311 assert_response 302 | |
312 end | |
313 users_to_test.each do |user, fields| | |
314 mails = ActionMailer::Base.deliveries.select {|m| m.bcc.include? user.mail} | |
315 if (fields & [@field2, @field3]).any? | |
316 assert_equal 1, mails.size, "User #{user.id} was not notified" | |
317 else | |
318 assert_equal 0, mails.size, "User #{user.id} was notified" | |
319 end | |
320 end | |
321 end | |
322 end |