annotate .svn/pristine/3d/3d5312afd2b7a1501b0ec17ef249ac149a4a5a3f.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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