annotate .svn/pristine/9d/9d0bd051174c188e1084c470e35e435886bd6bff.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 cbb26bc654de
children
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@909 3 # Redmine - project management software
Chris@909 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 5 #
Chris@909 6 # This program is free software; you can redistribute it and/or
Chris@909 7 # modify it under the terms of the GNU General Public License
Chris@909 8 # as published by the Free Software Foundation; either version 2
Chris@909 9 # of the License, or (at your option) any later version.
Chris@909 10 #
Chris@909 11 # This program is distributed in the hope that it will be useful,
Chris@909 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 14 # GNU General Public License for more details.
Chris@909 15 #
Chris@909 16 # You should have received a copy of the GNU General Public License
Chris@909 17 # along with this program; if not, write to the Free Software
Chris@909 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 19
Chris@909 20 require File.expand_path('../../test_helper', __FILE__)
Chris@909 21 require 'attachments_controller'
Chris@909 22
Chris@909 23 # Re-raise errors caught by the controller.
Chris@909 24 class AttachmentsController; def rescue_action(e) raise e end; end
Chris@909 25
Chris@909 26 class AttachmentsControllerTest < ActionController::TestCase
Chris@909 27 fixtures :users, :projects, :roles, :members, :member_roles,
Chris@909 28 :enabled_modules, :issues, :trackers, :attachments,
Chris@909 29 :versions, :wiki_pages, :wikis, :documents
Chris@909 30
Chris@909 31 def setup
Chris@909 32 @controller = AttachmentsController.new
Chris@909 33 @request = ActionController::TestRequest.new
Chris@909 34 @response = ActionController::TestResponse.new
Chris@909 35 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
Chris@909 36 User.current = nil
Chris@909 37 end
Chris@909 38
Chris@909 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@909 98 assert_response :success
Chris@909 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@909 103 assert_response :success
Chris@909 104 assert_template 'diff'
Chris@909 105 user.reload
Chris@909 106 assert_equal "sbs", user.pref[:diff_type]
Chris@909 107 end
Chris@909 108
Chris@909 109 def test_show_text_file
Chris@909 110 get :show, :id => 4
Chris@909 111 assert_response :success
Chris@909 112 assert_template 'file'
Chris@909 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@909 176 end
Chris@909 177
Chris@909 178 def test_show_text_file_should_send_if_too_big
Chris@909 179 Setting.file_max_size_displayed = 512
Chris@909 180 Attachment.find(4).update_attribute :filesize, 754.kilobyte
Chris@909 181
Chris@909 182 get :show, :id => 4
Chris@909 183 assert_response :success
Chris@909 184 assert_equal 'application/x-ruby', @response.content_type
Chris@909 185 set_tmp_attachments_directory
Chris@909 186 end
Chris@909 187
Chris@909 188 def test_show_other
Chris@909 189 get :show, :id => 6
Chris@909 190 assert_response :success
Chris@909 191 assert_equal 'application/octet-stream', @response.content_type
Chris@909 192 set_tmp_attachments_directory
Chris@909 193 end
Chris@909 194
Chris@909 195 def test_show_file_from_private_issue_without_permission
Chris@909 196 get :show, :id => 15
Chris@909 197 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
Chris@909 198 set_tmp_attachments_directory
Chris@909 199 end
Chris@909 200
Chris@909 201 def test_show_file_from_private_issue_with_permission
Chris@909 202 @request.session[:user_id] = 2
Chris@909 203 get :show, :id => 15
Chris@909 204 assert_response :success
Chris@909 205 assert_tag 'h2', :content => /private.diff/
Chris@909 206 set_tmp_attachments_directory
Chris@909 207 end
Chris@909 208
Chris@909 209 def test_download_text_file
Chris@909 210 get :download, :id => 4
Chris@909 211 assert_response :success
Chris@909 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@909 220 end
Chris@909 221
Chris@909 222 def test_download_should_assign_content_type_if_blank
Chris@909 223 Attachment.find(4).update_attribute(:content_type, '')
Chris@909 224
Chris@909 225 get :download, :id => 4
Chris@909 226 assert_response :success
Chris@909 227 assert_equal 'text/x-ruby', @response.content_type
Chris@909 228 set_tmp_attachments_directory
Chris@909 229 end
Chris@909 230
Chris@909 231 def test_download_missing_file
Chris@909 232 get :download, :id => 2
Chris@909 233 assert_response 404
Chris@909 234 set_tmp_attachments_directory
Chris@909 235 end
Chris@909 236
Chris@909 237 def test_anonymous_on_private_private
Chris@909 238 get :download, :id => 7
Chris@909 239 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
Chris@909 240 set_tmp_attachments_directory
Chris@909 241 end
Chris@909 242
Chris@909 243 def test_destroy_issue_attachment
Chris@909 244 set_tmp_attachments_directory
Chris@909 245 issue = Issue.find(3)
Chris@909 246 @request.session[:user_id] = 2
Chris@909 247
Chris@909 248 assert_difference 'issue.attachments.count', -1 do
Chris@909 249 delete :destroy, :id => 1
Chris@909 250 end
Chris@909 251 # no referrer
Chris@909 252 assert_redirected_to '/projects/ecookbook'
Chris@909 253 assert_nil Attachment.find_by_id(1)
Chris@909 254 j = issue.journals.find(:first, :order => 'created_on DESC')
Chris@909 255 assert_equal 'attachment', j.details.first.property
Chris@909 256 assert_equal '1', j.details.first.prop_key
Chris@909 257 assert_equal 'error281.txt', j.details.first.old_value
Chris@909 258 end
Chris@909 259
Chris@909 260 def test_destroy_wiki_page_attachment
Chris@909 261 set_tmp_attachments_directory
Chris@909 262 @request.session[:user_id] = 2
Chris@909 263 assert_difference 'Attachment.count', -1 do
Chris@909 264 delete :destroy, :id => 3
Chris@909 265 assert_response 302
Chris@909 266 end
Chris@909 267 end
Chris@909 268
Chris@909 269 def test_destroy_project_attachment
Chris@909 270 set_tmp_attachments_directory
Chris@909 271 @request.session[:user_id] = 2
Chris@909 272 assert_difference 'Attachment.count', -1 do
Chris@909 273 delete :destroy, :id => 8
Chris@909 274 assert_response 302
Chris@909 275 end
Chris@909 276 end
Chris@909 277
Chris@909 278 def test_destroy_version_attachment
Chris@909 279 set_tmp_attachments_directory
Chris@909 280 @request.session[:user_id] = 2
Chris@909 281 assert_difference 'Attachment.count', -1 do
Chris@909 282 delete :destroy, :id => 9
Chris@909 283 assert_response 302
Chris@909 284 end
Chris@909 285 end
Chris@909 286
Chris@909 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@909 293 assert Attachment.find_by_id(3)
Chris@909 294 end
Chris@909 295 end