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