annotate test/functional/attachments_controller_test.rb @ 1465:ab8bd24eeb65 bug_635

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