annotate test/functional/attachments_controller_test.rb @ 1628:9c5f8e24dadc live tip

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