annotate .svn/pristine/0f/0f41eec6f8bdcfb1c9cd2c332bcf208f96828f37.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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