annotate .svn/pristine/82/82a76dad4aa1fe1fa139ffda525754e5f82b97fb.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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