annotate .svn/pristine/3d/3d5312afd2b7a1501b0ec17ef249ac149a4a5a3f.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
rev   line source
Chris@1295 1 # encoding: utf-8
Chris@1295 2 #
Chris@1295 3 # Redmine - project management software
Chris@1295 4 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 5 #
Chris@1295 6 # This program is free software; you can redistribute it and/or
Chris@1295 7 # modify it under the terms of the GNU General Public License
Chris@1295 8 # as published by the Free Software Foundation; either version 2
Chris@1295 9 # of the License, or (at your option) any later version.
Chris@1295 10 #
Chris@1295 11 # This program is distributed in the hope that it will be useful,
Chris@1295 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 14 # GNU General Public License for more details.
Chris@1295 15 #
Chris@1295 16 # You should have received a copy of the GNU General Public License
Chris@1295 17 # along with this program; if not, write to the Free Software
Chris@1295 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 19
Chris@1295 20 require File.expand_path('../../test_helper', __FILE__)
Chris@1295 21 require 'attachments_controller'
Chris@1295 22
Chris@1295 23 # Re-raise errors caught by the controller.
Chris@1295 24 class AttachmentsController; def rescue_action(e) raise e end; end
Chris@1295 25
Chris@1295 26 class AttachmentsControllerTest < ActionController::TestCase
Chris@1295 27 fixtures :users, :projects, :roles, :members, :member_roles,
Chris@1295 28 :enabled_modules, :issues, :trackers, :attachments,
Chris@1295 29 :versions, :wiki_pages, :wikis, :documents
Chris@1295 30
Chris@1295 31 def setup
Chris@1295 32 @controller = AttachmentsController.new
Chris@1295 33 @request = ActionController::TestRequest.new
Chris@1295 34 @response = ActionController::TestResponse.new
Chris@1295 35 User.current = nil
Chris@1295 36 set_fixtures_attachments_directory
Chris@1295 37 end
Chris@1295 38
Chris@1295 39 def teardown
Chris@1295 40 set_tmp_attachments_directory
Chris@1295 41 end
Chris@1295 42
Chris@1295 43 def test_show_diff
Chris@1295 44 ['inline', 'sbs'].each do |dt|
Chris@1295 45 # 060719210727_changeset_utf8.diff
Chris@1295 46 get :show, :id => 14, :type => dt
Chris@1295 47 assert_response :success
Chris@1295 48 assert_template 'diff'
Chris@1295 49 assert_equal 'text/html', @response.content_type
Chris@1295 50 assert_tag 'th',
Chris@1295 51 :attributes => {:class => /filename/},
Chris@1295 52 :content => /issues_controller.rb\t\(révision 1484\)/
Chris@1295 53 assert_tag 'td',
Chris@1295 54 :attributes => {:class => /line-code/},
Chris@1295 55 :content => /Demande créée avec succès/
Chris@1295 56 end
Chris@1295 57 set_tmp_attachments_directory
Chris@1295 58 end
Chris@1295 59
Chris@1295 60 def test_show_diff_replcace_cannot_convert_content
Chris@1295 61 with_settings :repositories_encodings => 'UTF-8' do
Chris@1295 62 ['inline', 'sbs'].each do |dt|
Chris@1295 63 # 060719210727_changeset_iso8859-1.diff
Chris@1295 64 get :show, :id => 5, :type => dt
Chris@1295 65 assert_response :success
Chris@1295 66 assert_template 'diff'
Chris@1295 67 assert_equal 'text/html', @response.content_type
Chris@1295 68 assert_tag 'th',
Chris@1295 69 :attributes => {:class => "filename"},
Chris@1295 70 :content => /issues_controller.rb\t\(r\?vision 1484\)/
Chris@1295 71 assert_tag 'td',
Chris@1295 72 :attributes => {:class => /line-code/},
Chris@1295 73 :content => /Demande cr\?\?e avec succ\?s/
Chris@1295 74 end
Chris@1295 75 end
Chris@1295 76 set_tmp_attachments_directory
Chris@1295 77 end
Chris@1295 78
Chris@1295 79 def test_show_diff_latin_1
Chris@1295 80 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
Chris@1295 81 ['inline', 'sbs'].each do |dt|
Chris@1295 82 # 060719210727_changeset_iso8859-1.diff
Chris@1295 83 get :show, :id => 5, :type => dt
Chris@1295 84 assert_response :success
Chris@1295 85 assert_template 'diff'
Chris@1295 86 assert_equal 'text/html', @response.content_type
Chris@1295 87 assert_tag 'th',
Chris@1295 88 :attributes => {:class => "filename"},
Chris@1295 89 :content => /issues_controller.rb\t\(révision 1484\)/
Chris@1295 90 assert_tag 'td',
Chris@1295 91 :attributes => {:class => /line-code/},
Chris@1295 92 :content => /Demande créée avec succès/
Chris@1295 93 end
Chris@1295 94 end
Chris@1295 95 set_tmp_attachments_directory
Chris@1295 96 end
Chris@1295 97
Chris@1295 98 def test_save_diff_type
Chris@1295 99 user1 = User.find(1)
Chris@1295 100 user1.pref[:diff_type] = nil
Chris@1295 101 user1.preference.save
Chris@1295 102 user = User.find(1)
Chris@1295 103 assert_nil user.pref[:diff_type]
Chris@1295 104
Chris@1295 105 @request.session[:user_id] = 1 # admin
Chris@1295 106 get :show, :id => 5
Chris@1295 107 assert_response :success
Chris@1295 108 assert_template 'diff'
Chris@1295 109 user.reload
Chris@1295 110 assert_equal "inline", user.pref[:diff_type]
Chris@1295 111 get :show, :id => 5, :type => 'sbs'
Chris@1295 112 assert_response :success
Chris@1295 113 assert_template 'diff'
Chris@1295 114 user.reload
Chris@1295 115 assert_equal "sbs", user.pref[:diff_type]
Chris@1295 116 end
Chris@1295 117
Chris@1295 118 def test_diff_show_filename_in_mercurial_export
Chris@1295 119 set_tmp_attachments_directory
Chris@1295 120 a = Attachment.new(:container => Issue.find(1),
Chris@1295 121 :file => uploaded_test_file("hg-export.diff", "text/plain"),
Chris@1295 122 :author => User.find(1))
Chris@1295 123 assert a.save
Chris@1295 124 assert_equal 'hg-export.diff', a.filename
Chris@1295 125
Chris@1295 126 get :show, :id => a.id, :type => 'inline'
Chris@1295 127 assert_response :success
Chris@1295 128 assert_template 'diff'
Chris@1295 129 assert_equal 'text/html', @response.content_type
Chris@1295 130 assert_select 'th.filename', :text => 'test1.txt'
Chris@1295 131 end
Chris@1295 132
Chris@1295 133 def test_show_text_file
Chris@1295 134 get :show, :id => 4
Chris@1295 135 assert_response :success
Chris@1295 136 assert_template 'file'
Chris@1295 137 assert_equal 'text/html', @response.content_type
Chris@1295 138 set_tmp_attachments_directory
Chris@1295 139 end
Chris@1295 140
Chris@1295 141 def test_show_text_file_utf_8
Chris@1295 142 set_tmp_attachments_directory
Chris@1295 143 a = Attachment.new(:container => Issue.find(1),
Chris@1295 144 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
Chris@1295 145 :author => User.find(1))
Chris@1295 146 assert a.save
Chris@1295 147 assert_equal 'japanese-utf-8.txt', a.filename
Chris@1295 148
Chris@1295 149 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
Chris@1295 150 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
Chris@1295 151
Chris@1295 152 get :show, :id => a.id
Chris@1295 153 assert_response :success
Chris@1295 154 assert_template 'file'
Chris@1295 155 assert_equal 'text/html', @response.content_type
Chris@1295 156 assert_tag :tag => 'th',
Chris@1295 157 :content => '1',
Chris@1295 158 :attributes => { :class => 'line-num' },
Chris@1295 159 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
Chris@1295 160 end
Chris@1295 161
Chris@1295 162 def test_show_text_file_replcace_cannot_convert_content
Chris@1295 163 set_tmp_attachments_directory
Chris@1295 164 with_settings :repositories_encodings => 'UTF-8' do
Chris@1295 165 a = Attachment.new(:container => Issue.find(1),
Chris@1295 166 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
Chris@1295 167 :author => User.find(1))
Chris@1295 168 assert a.save
Chris@1295 169 assert_equal 'iso8859-1.txt', a.filename
Chris@1295 170
Chris@1295 171 get :show, :id => a.id
Chris@1295 172 assert_response :success
Chris@1295 173 assert_template 'file'
Chris@1295 174 assert_equal 'text/html', @response.content_type
Chris@1295 175 assert_tag :tag => 'th',
Chris@1295 176 :content => '7',
Chris@1295 177 :attributes => { :class => 'line-num' },
Chris@1295 178 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
Chris@1295 179 end
Chris@1295 180 end
Chris@1295 181
Chris@1295 182 def test_show_text_file_latin_1
Chris@1295 183 set_tmp_attachments_directory
Chris@1295 184 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
Chris@1295 185 a = Attachment.new(:container => Issue.find(1),
Chris@1295 186 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
Chris@1295 187 :author => User.find(1))
Chris@1295 188 assert a.save
Chris@1295 189 assert_equal 'iso8859-1.txt', a.filename
Chris@1295 190
Chris@1295 191 get :show, :id => a.id
Chris@1295 192 assert_response :success
Chris@1295 193 assert_template 'file'
Chris@1295 194 assert_equal 'text/html', @response.content_type
Chris@1295 195 assert_tag :tag => 'th',
Chris@1295 196 :content => '7',
Chris@1295 197 :attributes => { :class => 'line-num' },
Chris@1295 198 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
Chris@1295 199 end
Chris@1295 200 end
Chris@1295 201
Chris@1295 202 def test_show_text_file_should_send_if_too_big
Chris@1295 203 Setting.file_max_size_displayed = 512
Chris@1295 204 Attachment.find(4).update_attribute :filesize, 754.kilobyte
Chris@1295 205
Chris@1295 206 get :show, :id => 4
Chris@1295 207 assert_response :success
Chris@1295 208 assert_equal 'application/x-ruby', @response.content_type
Chris@1295 209 set_tmp_attachments_directory
Chris@1295 210 end
Chris@1295 211
Chris@1295 212 def test_show_other
Chris@1295 213 get :show, :id => 6
Chris@1295 214 assert_response :success
Chris@1295 215 assert_equal 'application/octet-stream', @response.content_type
Chris@1295 216 set_tmp_attachments_directory
Chris@1295 217 end
Chris@1295 218
Chris@1295 219 def test_show_file_from_private_issue_without_permission
Chris@1295 220 get :show, :id => 15
Chris@1295 221 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
Chris@1295 222 set_tmp_attachments_directory
Chris@1295 223 end
Chris@1295 224
Chris@1295 225 def test_show_file_from_private_issue_with_permission
Chris@1295 226 @request.session[:user_id] = 2
Chris@1295 227 get :show, :id => 15
Chris@1295 228 assert_response :success
Chris@1295 229 assert_tag 'h2', :content => /private.diff/
Chris@1295 230 set_tmp_attachments_directory
Chris@1295 231 end
Chris@1295 232
Chris@1295 233 def test_show_file_without_container_should_be_denied
Chris@1295 234 set_tmp_attachments_directory
Chris@1295 235 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
Chris@1295 236
Chris@1295 237 @request.session[:user_id] = 2
Chris@1295 238 get :show, :id => attachment.id
Chris@1295 239 assert_response 403
Chris@1295 240 end
Chris@1295 241
Chris@1295 242 def test_show_invalid_should_respond_with_404
Chris@1295 243 get :show, :id => 999
Chris@1295 244 assert_response 404
Chris@1295 245 end
Chris@1295 246
Chris@1295 247 def test_download_text_file
Chris@1295 248 get :download, :id => 4
Chris@1295 249 assert_response :success
Chris@1295 250 assert_equal 'application/x-ruby', @response.content_type
Chris@1295 251 set_tmp_attachments_directory
Chris@1295 252 end
Chris@1295 253
Chris@1295 254 def test_download_version_file_with_issue_tracking_disabled
Chris@1295 255 Project.find(1).disable_module! :issue_tracking
Chris@1295 256 get :download, :id => 9
Chris@1295 257 assert_response :success
Chris@1295 258 end
Chris@1295 259
Chris@1295 260 def test_download_should_assign_content_type_if_blank
Chris@1295 261 Attachment.find(4).update_attribute(:content_type, '')
Chris@1295 262
Chris@1295 263 get :download, :id => 4
Chris@1295 264 assert_response :success
Chris@1295 265 assert_equal 'text/x-ruby', @response.content_type
Chris@1295 266 set_tmp_attachments_directory
Chris@1295 267 end
Chris@1295 268
Chris@1295 269 def test_download_missing_file
Chris@1295 270 get :download, :id => 2
Chris@1295 271 assert_response 404
Chris@1295 272 set_tmp_attachments_directory
Chris@1295 273 end
Chris@1295 274
Chris@1295 275 def test_download_should_be_denied_without_permission
Chris@1295 276 get :download, :id => 7
Chris@1295 277 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
Chris@1295 278 set_tmp_attachments_directory
Chris@1295 279 end
Chris@1295 280
Chris@1295 281 if convert_installed?
Chris@1295 282 def test_thumbnail
Chris@1295 283 Attachment.clear_thumbnails
Chris@1295 284 @request.session[:user_id] = 2
Chris@1295 285
Chris@1295 286 get :thumbnail, :id => 16
Chris@1295 287 assert_response :success
Chris@1295 288 assert_equal 'image/png', response.content_type
Chris@1295 289 end
Chris@1295 290
Chris@1295 291 def test_thumbnail_should_not_exceed_maximum_size
Chris@1295 292 Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 800}
Chris@1295 293
Chris@1295 294 @request.session[:user_id] = 2
Chris@1295 295 get :thumbnail, :id => 16, :size => 2000
Chris@1295 296 end
Chris@1295 297
Chris@1295 298 def test_thumbnail_should_round_size
Chris@1295 299 Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 250}
Chris@1295 300
Chris@1295 301 @request.session[:user_id] = 2
Chris@1295 302 get :thumbnail, :id => 16, :size => 260
Chris@1295 303 end
Chris@1295 304
Chris@1295 305 def test_thumbnail_should_return_404_for_non_image_attachment
Chris@1295 306 @request.session[:user_id] = 2
Chris@1295 307
Chris@1295 308 get :thumbnail, :id => 15
Chris@1295 309 assert_response 404
Chris@1295 310 end
Chris@1295 311
Chris@1295 312 def test_thumbnail_should_return_404_if_thumbnail_generation_failed
Chris@1295 313 Attachment.any_instance.stubs(:thumbnail).returns(nil)
Chris@1295 314 @request.session[:user_id] = 2
Chris@1295 315
Chris@1295 316 get :thumbnail, :id => 16
Chris@1295 317 assert_response 404
Chris@1295 318 end
Chris@1295 319
Chris@1295 320 def test_thumbnail_should_be_denied_without_permission
Chris@1295 321 get :thumbnail, :id => 16
Chris@1295 322 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fthumbnail%2F16'
Chris@1295 323 end
Chris@1295 324 else
Chris@1295 325 puts '(ImageMagick convert not available)'
Chris@1295 326 end
Chris@1295 327
Chris@1295 328 def test_destroy_issue_attachment
Chris@1295 329 set_tmp_attachments_directory
Chris@1295 330 issue = Issue.find(3)
Chris@1295 331 @request.session[:user_id] = 2
Chris@1295 332
Chris@1295 333 assert_difference 'issue.attachments.count', -1 do
Chris@1295 334 assert_difference 'Journal.count' do
Chris@1295 335 delete :destroy, :id => 1
Chris@1295 336 assert_redirected_to '/projects/ecookbook'
Chris@1295 337 end
Chris@1295 338 end
Chris@1295 339 assert_nil Attachment.find_by_id(1)
Chris@1295 340 j = Journal.first(:order => 'id DESC')
Chris@1295 341 assert_equal issue, j.journalized
Chris@1295 342 assert_equal 'attachment', j.details.first.property
Chris@1295 343 assert_equal '1', j.details.first.prop_key
Chris@1295 344 assert_equal 'error281.txt', j.details.first.old_value
Chris@1295 345 assert_equal User.find(2), j.user
Chris@1295 346 end
Chris@1295 347
Chris@1295 348 def test_destroy_wiki_page_attachment
Chris@1295 349 set_tmp_attachments_directory
Chris@1295 350 @request.session[:user_id] = 2
Chris@1295 351 assert_difference 'Attachment.count', -1 do
Chris@1295 352 delete :destroy, :id => 3
Chris@1295 353 assert_response 302
Chris@1295 354 end
Chris@1295 355 end
Chris@1295 356
Chris@1295 357 def test_destroy_project_attachment
Chris@1295 358 set_tmp_attachments_directory
Chris@1295 359 @request.session[:user_id] = 2
Chris@1295 360 assert_difference 'Attachment.count', -1 do
Chris@1295 361 delete :destroy, :id => 8
Chris@1295 362 assert_response 302
Chris@1295 363 end
Chris@1295 364 end
Chris@1295 365
Chris@1295 366 def test_destroy_version_attachment
Chris@1295 367 set_tmp_attachments_directory
Chris@1295 368 @request.session[:user_id] = 2
Chris@1295 369 assert_difference 'Attachment.count', -1 do
Chris@1295 370 delete :destroy, :id => 9
Chris@1295 371 assert_response 302
Chris@1295 372 end
Chris@1295 373 end
Chris@1295 374
Chris@1295 375 def test_destroy_without_permission
Chris@1295 376 set_tmp_attachments_directory
Chris@1295 377 assert_no_difference 'Attachment.count' do
Chris@1295 378 delete :destroy, :id => 3
Chris@1295 379 end
Chris@1295 380 assert_response 302
Chris@1295 381 assert Attachment.find_by_id(3)
Chris@1295 382 end
Chris@1295 383 end