To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 9d / 9d0bd051174c188e1084c470e35e435886bd6bff.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (9.53 KB)
| 1 |
# encoding: utf-8 |
|---|---|
| 2 |
# |
| 3 |
# Redmine - project management software |
| 4 |
# Copyright (C) 2006-2011 Jean-Philippe Lang |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU General Public License |
| 8 |
# as published by the Free Software Foundation; either version 2 |
| 9 |
# of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
|
| 20 |
require File.expand_path('../../test_helper', __FILE__)
|
| 21 |
require 'attachments_controller' |
| 22 |
|
| 23 |
# Re-raise errors caught by the controller. |
| 24 |
class AttachmentsController; def rescue_action(e) raise e end; end |
| 25 |
|
| 26 |
class AttachmentsControllerTest < ActionController::TestCase |
| 27 |
fixtures :users, :projects, :roles, :members, :member_roles, |
| 28 |
:enabled_modules, :issues, :trackers, :attachments, |
| 29 |
:versions, :wiki_pages, :wikis, :documents |
| 30 |
|
| 31 |
def setup |
| 32 |
@controller = AttachmentsController.new |
| 33 |
@request = ActionController::TestRequest.new |
| 34 |
@response = ActionController::TestResponse.new |
| 35 |
Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
|
| 36 |
User.current = nil |
| 37 |
end |
| 38 |
|
| 39 |
def test_show_diff |
| 40 |
['inline', 'sbs'].each do |dt| |
| 41 |
# 060719210727_changeset_utf8.diff |
| 42 |
get :show, :id => 14, :type => dt |
| 43 |
assert_response :success |
| 44 |
assert_template 'diff' |
| 45 |
assert_equal 'text/html', @response.content_type |
| 46 |
assert_tag 'th', |
| 47 |
:attributes => {:class => /filename/},
|
| 48 |
:content => /issues_controller.rb\t\(révision 1484\)/ |
| 49 |
assert_tag 'td', |
| 50 |
:attributes => {:class => /line-code/},
|
| 51 |
:content => /Demande créée avec succès/ |
| 52 |
end |
| 53 |
set_tmp_attachments_directory |
| 54 |
end |
| 55 |
|
| 56 |
def test_show_diff_replcace_cannot_convert_content |
| 57 |
with_settings :repositories_encodings => 'UTF-8' do |
| 58 |
['inline', 'sbs'].each do |dt| |
| 59 |
# 060719210727_changeset_iso8859-1.diff |
| 60 |
get :show, :id => 5, :type => dt |
| 61 |
assert_response :success |
| 62 |
assert_template 'diff' |
| 63 |
assert_equal 'text/html', @response.content_type |
| 64 |
assert_tag 'th', |
| 65 |
:attributes => {:class => "filename"},
|
| 66 |
:content => /issues_controller.rb\t\(r\?vision 1484\)/ |
| 67 |
assert_tag 'td', |
| 68 |
:attributes => {:class => /line-code/},
|
| 69 |
:content => /Demande cr\?\?e avec succ\?s/ |
| 70 |
end |
| 71 |
end |
| 72 |
set_tmp_attachments_directory |
| 73 |
end |
| 74 |
|
| 75 |
def test_show_diff_latin_1 |
| 76 |
with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do |
| 77 |
['inline', 'sbs'].each do |dt| |
| 78 |
# 060719210727_changeset_iso8859-1.diff |
| 79 |
get :show, :id => 5, :type => dt |
| 80 |
assert_response :success |
| 81 |
assert_template 'diff' |
| 82 |
assert_equal 'text/html', @response.content_type |
| 83 |
assert_tag 'th', |
| 84 |
:attributes => {:class => "filename"},
|
| 85 |
:content => /issues_controller.rb\t\(révision 1484\)/ |
| 86 |
assert_tag 'td', |
| 87 |
:attributes => {:class => /line-code/},
|
| 88 |
:content => /Demande créée avec succès/ |
| 89 |
end |
| 90 |
end |
| 91 |
set_tmp_attachments_directory |
| 92 |
end |
| 93 |
|
| 94 |
def test_save_diff_type |
| 95 |
@request.session[:user_id] = 1 # admin |
| 96 |
user = User.find(1) |
| 97 |
get :show, :id => 5 |
| 98 |
assert_response :success |
| 99 |
assert_template 'diff' |
| 100 |
user.reload |
| 101 |
assert_equal "inline", user.pref[:diff_type] |
| 102 |
get :show, :id => 5, :type => 'sbs' |
| 103 |
assert_response :success |
| 104 |
assert_template 'diff' |
| 105 |
user.reload |
| 106 |
assert_equal "sbs", user.pref[:diff_type] |
| 107 |
end |
| 108 |
|
| 109 |
def test_show_text_file |
| 110 |
get :show, :id => 4 |
| 111 |
assert_response :success |
| 112 |
assert_template 'file' |
| 113 |
assert_equal 'text/html', @response.content_type |
| 114 |
set_tmp_attachments_directory |
| 115 |
end |
| 116 |
|
| 117 |
def test_show_text_file_utf_8 |
| 118 |
set_tmp_attachments_directory |
| 119 |
a = Attachment.new(:container => Issue.find(1), |
| 120 |
:file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
|
| 121 |
:author => User.find(1)) |
| 122 |
assert a.save |
| 123 |
assert_equal 'japanese-utf-8.txt', a.filename |
| 124 |
|
| 125 |
str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" |
| 126 |
str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
|
| 127 |
|
| 128 |
get :show, :id => a.id |
| 129 |
assert_response :success |
| 130 |
assert_template 'file' |
| 131 |
assert_equal 'text/html', @response.content_type |
| 132 |
assert_tag :tag => 'th', |
| 133 |
:content => '1', |
| 134 |
:attributes => { :class => 'line-num' },
|
| 135 |
:sibling => { :tag => 'td', :content => /#{str_japanese}/ }
|
| 136 |
end |
| 137 |
|
| 138 |
def test_show_text_file_replcace_cannot_convert_content |
| 139 |
set_tmp_attachments_directory |
| 140 |
with_settings :repositories_encodings => 'UTF-8' do |
| 141 |
a = Attachment.new(:container => Issue.find(1), |
| 142 |
:file => uploaded_test_file("iso8859-1.txt", "text/plain"),
|
| 143 |
:author => User.find(1)) |
| 144 |
assert a.save |
| 145 |
assert_equal 'iso8859-1.txt', a.filename |
| 146 |
|
| 147 |
get :show, :id => a.id |
| 148 |
assert_response :success |
| 149 |
assert_template 'file' |
| 150 |
assert_equal 'text/html', @response.content_type |
| 151 |
assert_tag :tag => 'th', |
| 152 |
:content => '7', |
| 153 |
:attributes => { :class => 'line-num' },
|
| 154 |
:sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
|
| 155 |
end |
| 156 |
end |
| 157 |
|
| 158 |
def test_show_text_file_latin_1 |
| 159 |
set_tmp_attachments_directory |
| 160 |
with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do |
| 161 |
a = Attachment.new(:container => Issue.find(1), |
| 162 |
:file => uploaded_test_file("iso8859-1.txt", "text/plain"),
|
| 163 |
:author => User.find(1)) |
| 164 |
assert a.save |
| 165 |
assert_equal 'iso8859-1.txt', a.filename |
| 166 |
|
| 167 |
get :show, :id => a.id |
| 168 |
assert_response :success |
| 169 |
assert_template 'file' |
| 170 |
assert_equal 'text/html', @response.content_type |
| 171 |
assert_tag :tag => 'th', |
| 172 |
:content => '7', |
| 173 |
:attributes => { :class => 'line-num' },
|
| 174 |
:sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
|
| 175 |
end |
| 176 |
end |
| 177 |
|
| 178 |
def test_show_text_file_should_send_if_too_big |
| 179 |
Setting.file_max_size_displayed = 512 |
| 180 |
Attachment.find(4).update_attribute :filesize, 754.kilobyte |
| 181 |
|
| 182 |
get :show, :id => 4 |
| 183 |
assert_response :success |
| 184 |
assert_equal 'application/x-ruby', @response.content_type |
| 185 |
set_tmp_attachments_directory |
| 186 |
end |
| 187 |
|
| 188 |
def test_show_other |
| 189 |
get :show, :id => 6 |
| 190 |
assert_response :success |
| 191 |
assert_equal 'application/octet-stream', @response.content_type |
| 192 |
set_tmp_attachments_directory |
| 193 |
end |
| 194 |
|
| 195 |
def test_show_file_from_private_issue_without_permission |
| 196 |
get :show, :id => 15 |
| 197 |
assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15' |
| 198 |
set_tmp_attachments_directory |
| 199 |
end |
| 200 |
|
| 201 |
def test_show_file_from_private_issue_with_permission |
| 202 |
@request.session[:user_id] = 2 |
| 203 |
get :show, :id => 15 |
| 204 |
assert_response :success |
| 205 |
assert_tag 'h2', :content => /private.diff/ |
| 206 |
set_tmp_attachments_directory |
| 207 |
end |
| 208 |
|
| 209 |
def test_download_text_file |
| 210 |
get :download, :id => 4 |
| 211 |
assert_response :success |
| 212 |
assert_equal 'application/x-ruby', @response.content_type |
| 213 |
set_tmp_attachments_directory |
| 214 |
end |
| 215 |
|
| 216 |
def test_download_version_file_with_issue_tracking_disabled |
| 217 |
Project.find(1).disable_module! :issue_tracking |
| 218 |
get :download, :id => 9 |
| 219 |
assert_response :success |
| 220 |
end |
| 221 |
|
| 222 |
def test_download_should_assign_content_type_if_blank |
| 223 |
Attachment.find(4).update_attribute(:content_type, '') |
| 224 |
|
| 225 |
get :download, :id => 4 |
| 226 |
assert_response :success |
| 227 |
assert_equal 'text/x-ruby', @response.content_type |
| 228 |
set_tmp_attachments_directory |
| 229 |
end |
| 230 |
|
| 231 |
def test_download_missing_file |
| 232 |
get :download, :id => 2 |
| 233 |
assert_response 404 |
| 234 |
set_tmp_attachments_directory |
| 235 |
end |
| 236 |
|
| 237 |
def test_anonymous_on_private_private |
| 238 |
get :download, :id => 7 |
| 239 |
assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7' |
| 240 |
set_tmp_attachments_directory |
| 241 |
end |
| 242 |
|
| 243 |
def test_destroy_issue_attachment |
| 244 |
set_tmp_attachments_directory |
| 245 |
issue = Issue.find(3) |
| 246 |
@request.session[:user_id] = 2 |
| 247 |
|
| 248 |
assert_difference 'issue.attachments.count', -1 do |
| 249 |
delete :destroy, :id => 1 |
| 250 |
end |
| 251 |
# no referrer |
| 252 |
assert_redirected_to '/projects/ecookbook' |
| 253 |
assert_nil Attachment.find_by_id(1) |
| 254 |
j = issue.journals.find(:first, :order => 'created_on DESC') |
| 255 |
assert_equal 'attachment', j.details.first.property |
| 256 |
assert_equal '1', j.details.first.prop_key |
| 257 |
assert_equal 'error281.txt', j.details.first.old_value |
| 258 |
end |
| 259 |
|
| 260 |
def test_destroy_wiki_page_attachment |
| 261 |
set_tmp_attachments_directory |
| 262 |
@request.session[:user_id] = 2 |
| 263 |
assert_difference 'Attachment.count', -1 do |
| 264 |
delete :destroy, :id => 3 |
| 265 |
assert_response 302 |
| 266 |
end |
| 267 |
end |
| 268 |
|
| 269 |
def test_destroy_project_attachment |
| 270 |
set_tmp_attachments_directory |
| 271 |
@request.session[:user_id] = 2 |
| 272 |
assert_difference 'Attachment.count', -1 do |
| 273 |
delete :destroy, :id => 8 |
| 274 |
assert_response 302 |
| 275 |
end |
| 276 |
end |
| 277 |
|
| 278 |
def test_destroy_version_attachment |
| 279 |
set_tmp_attachments_directory |
| 280 |
@request.session[:user_id] = 2 |
| 281 |
assert_difference 'Attachment.count', -1 do |
| 282 |
delete :destroy, :id => 9 |
| 283 |
assert_response 302 |
| 284 |
end |
| 285 |
end |
| 286 |
|
| 287 |
def test_destroy_without_permission |
| 288 |
set_tmp_attachments_directory |
| 289 |
assert_no_difference 'Attachment.count' do |
| 290 |
delete :destroy, :id => 3 |
| 291 |
end |
| 292 |
assert_response 302 |
| 293 |
assert Attachment.find_by_id(3) |
| 294 |
end |
| 295 |
end |