annotate .svn/pristine/94/94850f049cba8fd1d7b631af60e061c441dc4401.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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