annotate test/functional/attachments_controller_test.rb @ 1082:997f6d7738f7 bug_531

In repo controller entry action, show the page for the file even if it's binary (so user still has access to history etc links). This makes it possible to use the entry action as the default when a file is clicked on
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 22 Nov 2012 18:04:17 +0000
parents 5e80956cc792
children bb32da3bea34
rev   line source
Chris@119 1 # encoding: utf-8
Chris@119 2 #
Chris@119 3 # Redmine - project management software
Chris@119 4 # Copyright (C) 2006-2011 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@909 35 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
Chris@0 36 User.current = nil
Chris@0 37 end
Chris@441 38
Chris@0 39 def test_show_diff
Chris@909 40 ['inline', 'sbs'].each do |dt|
Chris@909 41 # 060719210727_changeset_utf8.diff
Chris@909 42 get :show, :id => 14, :type => dt
Chris@909 43 assert_response :success
Chris@909 44 assert_template 'diff'
Chris@909 45 assert_equal 'text/html', @response.content_type
Chris@909 46 assert_tag 'th',
Chris@909 47 :attributes => {:class => /filename/},
Chris@909 48 :content => /issues_controller.rb\t\(révision 1484\)/
Chris@909 49 assert_tag 'td',
Chris@909 50 :attributes => {:class => /line-code/},
Chris@909 51 :content => /Demande créée avec succès/
Chris@909 52 end
Chris@909 53 set_tmp_attachments_directory
Chris@909 54 end
Chris@909 55
Chris@909 56 def test_show_diff_replcace_cannot_convert_content
Chris@909 57 with_settings :repositories_encodings => 'UTF-8' do
Chris@909 58 ['inline', 'sbs'].each do |dt|
Chris@909 59 # 060719210727_changeset_iso8859-1.diff
Chris@909 60 get :show, :id => 5, :type => dt
Chris@909 61 assert_response :success
Chris@909 62 assert_template 'diff'
Chris@909 63 assert_equal 'text/html', @response.content_type
Chris@909 64 assert_tag 'th',
Chris@909 65 :attributes => {:class => "filename"},
Chris@909 66 :content => /issues_controller.rb\t\(r\?vision 1484\)/
Chris@909 67 assert_tag 'td',
Chris@909 68 :attributes => {:class => /line-code/},
Chris@909 69 :content => /Demande cr\?\?e avec succ\?s/
Chris@909 70 end
Chris@909 71 end
Chris@909 72 set_tmp_attachments_directory
Chris@909 73 end
Chris@909 74
Chris@909 75 def test_show_diff_latin_1
Chris@909 76 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
Chris@909 77 ['inline', 'sbs'].each do |dt|
Chris@909 78 # 060719210727_changeset_iso8859-1.diff
Chris@909 79 get :show, :id => 5, :type => dt
Chris@909 80 assert_response :success
Chris@909 81 assert_template 'diff'
Chris@909 82 assert_equal 'text/html', @response.content_type
Chris@909 83 assert_tag 'th',
Chris@909 84 :attributes => {:class => "filename"},
Chris@909 85 :content => /issues_controller.rb\t\(révision 1484\)/
Chris@909 86 assert_tag 'td',
Chris@909 87 :attributes => {:class => /line-code/},
Chris@909 88 :content => /Demande créée avec succès/
Chris@909 89 end
Chris@909 90 end
Chris@909 91 set_tmp_attachments_directory
Chris@909 92 end
Chris@909 93
Chris@909 94 def test_save_diff_type
Chris@909 95 @request.session[:user_id] = 1 # admin
Chris@909 96 user = User.find(1)
Chris@909 97 get :show, :id => 5
Chris@0 98 assert_response :success
Chris@0 99 assert_template 'diff'
Chris@909 100 user.reload
Chris@909 101 assert_equal "inline", user.pref[:diff_type]
Chris@909 102 get :show, :id => 5, :type => 'sbs'
Chris@119 103 assert_response :success
Chris@119 104 assert_template 'diff'
Chris@909 105 user.reload
Chris@909 106 assert_equal "sbs", user.pref[:diff_type]
Chris@0 107 end
Chris@441 108
Chris@0 109 def test_show_text_file
Chris@0 110 get :show, :id => 4
Chris@0 111 assert_response :success
Chris@0 112 assert_template 'file'
Chris@0 113 assert_equal 'text/html', @response.content_type
Chris@909 114 set_tmp_attachments_directory
Chris@909 115 end
Chris@909 116
Chris@909 117 def test_show_text_file_utf_8
Chris@909 118 set_tmp_attachments_directory
Chris@909 119 a = Attachment.new(:container => Issue.find(1),
Chris@909 120 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
Chris@909 121 :author => User.find(1))
Chris@909 122 assert a.save
Chris@909 123 assert_equal 'japanese-utf-8.txt', a.filename
Chris@909 124
Chris@909 125 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
Chris@909 126 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
Chris@909 127
Chris@909 128 get :show, :id => a.id
Chris@909 129 assert_response :success
Chris@909 130 assert_template 'file'
Chris@909 131 assert_equal 'text/html', @response.content_type
Chris@909 132 assert_tag :tag => 'th',
Chris@909 133 :content => '1',
Chris@909 134 :attributes => { :class => 'line-num' },
Chris@909 135 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
Chris@909 136 end
Chris@909 137
Chris@909 138 def test_show_text_file_replcace_cannot_convert_content
Chris@909 139 set_tmp_attachments_directory
Chris@909 140 with_settings :repositories_encodings => 'UTF-8' do
Chris@909 141 a = Attachment.new(:container => Issue.find(1),
Chris@909 142 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
Chris@909 143 :author => User.find(1))
Chris@909 144 assert a.save
Chris@909 145 assert_equal 'iso8859-1.txt', a.filename
Chris@909 146
Chris@909 147 get :show, :id => a.id
Chris@909 148 assert_response :success
Chris@909 149 assert_template 'file'
Chris@909 150 assert_equal 'text/html', @response.content_type
Chris@909 151 assert_tag :tag => 'th',
Chris@909 152 :content => '7',
Chris@909 153 :attributes => { :class => 'line-num' },
Chris@909 154 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
Chris@909 155 end
Chris@909 156 end
Chris@909 157
Chris@909 158 def test_show_text_file_latin_1
Chris@909 159 set_tmp_attachments_directory
Chris@909 160 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
Chris@909 161 a = Attachment.new(:container => Issue.find(1),
Chris@909 162 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
Chris@909 163 :author => User.find(1))
Chris@909 164 assert a.save
Chris@909 165 assert_equal 'iso8859-1.txt', a.filename
Chris@909 166
Chris@909 167 get :show, :id => a.id
Chris@909 168 assert_response :success
Chris@909 169 assert_template 'file'
Chris@909 170 assert_equal 'text/html', @response.content_type
Chris@909 171 assert_tag :tag => 'th',
Chris@909 172 :content => '7',
Chris@909 173 :attributes => { :class => 'line-num' },
Chris@909 174 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
Chris@909 175 end
Chris@0 176 end
Chris@441 177
Chris@0 178 def test_show_text_file_should_send_if_too_big
Chris@0 179 Setting.file_max_size_displayed = 512
Chris@0 180 Attachment.find(4).update_attribute :filesize, 754.kilobyte
Chris@441 181
Chris@0 182 get :show, :id => 4
Chris@0 183 assert_response :success
Chris@0 184 assert_equal 'application/x-ruby', @response.content_type
Chris@909 185 set_tmp_attachments_directory
Chris@0 186 end
Chris@441 187
Chris@0 188 def test_show_other
Chris@0 189 get :show, :id => 6
Chris@0 190 assert_response :success
Chris@0 191 assert_equal 'application/octet-stream', @response.content_type
Chris@909 192 set_tmp_attachments_directory
Chris@0 193 end
Chris@441 194
Chris@441 195 def test_show_file_from_private_issue_without_permission
Chris@441 196 get :show, :id => 15
Chris@441 197 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
Chris@909 198 set_tmp_attachments_directory
Chris@441 199 end
Chris@441 200
Chris@441 201 def test_show_file_from_private_issue_with_permission
Chris@441 202 @request.session[:user_id] = 2
Chris@441 203 get :show, :id => 15
Chris@441 204 assert_response :success
Chris@441 205 assert_tag 'h2', :content => /private.diff/
Chris@909 206 set_tmp_attachments_directory
Chris@441 207 end
Chris@441 208
Chris@0 209 def test_download_text_file
Chris@0 210 get :download, :id => 4
Chris@0 211 assert_response :success
Chris@0 212 assert_equal 'application/x-ruby', @response.content_type
Chris@909 213 set_tmp_attachments_directory
Chris@909 214 end
Chris@909 215
Chris@909 216 def test_download_version_file_with_issue_tracking_disabled
Chris@909 217 Project.find(1).disable_module! :issue_tracking
Chris@909 218 get :download, :id => 9
Chris@909 219 assert_response :success
Chris@0 220 end
Chris@441 221
Chris@0 222 def test_download_should_assign_content_type_if_blank
Chris@0 223 Attachment.find(4).update_attribute(:content_type, '')
Chris@441 224
Chris@0 225 get :download, :id => 4
Chris@0 226 assert_response :success
Chris@0 227 assert_equal 'text/x-ruby', @response.content_type
Chris@909 228 set_tmp_attachments_directory
Chris@0 229 end
Chris@441 230
Chris@0 231 def test_download_missing_file
Chris@0 232 get :download, :id => 2
Chris@0 233 assert_response 404
Chris@909 234 set_tmp_attachments_directory
Chris@0 235 end
Chris@441 236
Chris@0 237 def test_anonymous_on_private_private
Chris@0 238 get :download, :id => 7
Chris@0 239 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
Chris@909 240 set_tmp_attachments_directory
Chris@0 241 end
Chris@441 242
Chris@0 243 def test_destroy_issue_attachment
Chris@909 244 set_tmp_attachments_directory
Chris@0 245 issue = Issue.find(3)
Chris@0 246 @request.session[:user_id] = 2
Chris@441 247
Chris@0 248 assert_difference 'issue.attachments.count', -1 do
Chris@909 249 delete :destroy, :id => 1
Chris@0 250 end
Chris@0 251 # no referrer
chris@37 252 assert_redirected_to '/projects/ecookbook'
Chris@0 253 assert_nil Attachment.find_by_id(1)
Chris@0 254 j = issue.journals.find(:first, :order => 'created_on DESC')
Chris@0 255 assert_equal 'attachment', j.details.first.property
Chris@0 256 assert_equal '1', j.details.first.prop_key
Chris@0 257 assert_equal 'error281.txt', j.details.first.old_value
Chris@0 258 end
Chris@441 259
Chris@0 260 def test_destroy_wiki_page_attachment
Chris@909 261 set_tmp_attachments_directory
Chris@0 262 @request.session[:user_id] = 2
Chris@0 263 assert_difference 'Attachment.count', -1 do
Chris@909 264 delete :destroy, :id => 3
Chris@0 265 assert_response 302
Chris@0 266 end
Chris@0 267 end
Chris@441 268
Chris@0 269 def test_destroy_project_attachment
Chris@909 270 set_tmp_attachments_directory
Chris@0 271 @request.session[:user_id] = 2
Chris@0 272 assert_difference 'Attachment.count', -1 do
Chris@909 273 delete :destroy, :id => 8
Chris@0 274 assert_response 302
Chris@0 275 end
Chris@0 276 end
Chris@441 277
Chris@0 278 def test_destroy_version_attachment
Chris@909 279 set_tmp_attachments_directory
Chris@0 280 @request.session[:user_id] = 2
Chris@0 281 assert_difference 'Attachment.count', -1 do
Chris@909 282 delete :destroy, :id => 9
Chris@0 283 assert_response 302
Chris@0 284 end
Chris@0 285 end
Chris@441 286
Chris@0 287 def test_destroy_without_permission
Chris@909 288 set_tmp_attachments_directory
Chris@909 289 assert_no_difference 'Attachment.count' do
Chris@909 290 delete :destroy, :id => 3
Chris@909 291 end
Chris@909 292 assert_response 302
Chris@0 293 assert Attachment.find_by_id(3)
Chris@0 294 end
Chris@0 295 end