1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006-2022 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../test_helper', __FILE__)
class AttachmentsTest < Redmine::IntegrationTest
fixtures :projects, :enabled_modules,
:users, :email_addresses,
:roles, :members, :member_roles,
:trackers, :projects_trackers,
:issues, :issue_statuses, :enumerations,
:attachments,
:wiki_content_versions, :wiki_contents, :wiki_pages,
:journals, :journal_details
def test_upload_should_set_default_content_type
log_user('jsmith', 'jsmith')
assert_difference 'Attachment.count' do
post(
"/uploads.js?attachment_id=1&filename=foo.txt",
:params => "File content",
:headers => {"CONTENT_TYPE" => 'application/octet-stream'})
assert_response :success
end
attachment = Attachment.order(:id => :desc).first
assert_equal 'text/plain', attachment.content_type
end
def test_upload_should_accept_content_type_param
log_user('jsmith', 'jsmith')
assert_difference 'Attachment.count' do
post(
"/uploads.js?attachment_id=1&filename=foo&content_type=image/jpeg",
:params => "File content",
:headers => {"CONTENT_TYPE" => 'application/octet-stream'})
assert_response :success
end
attachment = Attachment.order(:id => :desc).first
assert_equal 'image/jpeg', attachment.content_type
end
def test_upload_as_js_and_attach_to_an_issue
log_user('jsmith', 'jsmith')
token = ajax_upload('myupload.txt', 'File content')
assert_difference 'Issue.count' do
post(
'/projects/ecookbook/issues',
:params => {
:issue => {:tracker_id => 1, :subject => 'Issue with upload'},
:attachments => {
'1' => {
:filename => 'myupload.txt',
:description => 'My uploaded file',
:token => token
}
}
}
)
assert_response 302
end
issue = Issue.order('id DESC').first
assert_equal 'Issue with upload', issue.subject
assert_equal 1, issue.attachments.count
attachment = issue.attachments.first
assert_equal 'myupload.txt', attachment.filename
assert_equal 'My uploaded file', attachment.description
assert_equal 'File content'.length, attachment.filesize
end
def test_upload_as_js_and_preview_as_inline_attachment
log_user('jsmith', 'jsmith')
token = ajax_upload('myupload.jpg', 'JPEG content')
with_settings :text_formatting => 'textile' do
post(
'/issues/preview',
:params => {
:issue => {:tracker_id => 1, :project_id => 'ecookbook'},
:text => 'Inline upload: !myupload.jpg!',
:attachments => {
'1' => {
:filename => 'myupload.jpg',
:description => 'My uploaded file',
:token => token
}
}
}
)
assert_response :success
attachment_path = response.body.match(%r{<img src="(/attachments/download/\d+/myupload.jpg)"})[1]
assert_not_nil token, "No attachment path found in response:\n#{response.body}"
get attachment_path
assert_response :success
assert_equal 'JPEG content', response.body
end
end
def test_upload_and_resubmit_after_validation_failure
log_user('jsmith', 'jsmith')
token = ajax_upload('myupload.txt', 'File content')
assert_no_difference 'Issue.count' do
post(
'/projects/ecookbook/issues',
:params => {
:issue => {:tracker_id => 1, :subject => ''},
:attachments => {
'1' => {
:filename => 'myupload.txt',
:description => 'My uploaded file',
:token => token
}
}
}
)
assert_response :success
end
assert_select 'input[type=hidden][name=?][value=?]', 'attachments[p0][token]', token
assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'myupload.txt'
assert_select 'input[name=?][value=?]', 'attachments[p0][description]', 'My uploaded file'
assert_difference 'Issue.count' do
post(
'/projects/ecookbook/issues',
:params => {
:issue => {:tracker_id => 1, :subject => 'Issue with upload'},
:attachments => {
'p0' => {
:filename => 'myupload.txt',
:description => 'My uploaded file',
:token => token
}
}
}
)
assert_response 302
end
issue = Issue.order('id DESC').first
assert_equal 'Issue with upload', issue.subject
assert_equal 1, issue.attachments.count
attachment = issue.attachments.first
assert_equal 'myupload.txt', attachment.filename
assert_equal 'My uploaded file', attachment.description
assert_equal 'File content'.length, attachment.filesize
end
def test_upload_filename_with_plus
log_user('jsmith', 'jsmith')
filename = 'a+b.txt'
token = ajax_upload(filename, 'File content')
assert_difference 'Issue.count' do
post(
'/projects/ecookbook/issues',
:params => {
:issue => {:tracker_id => 1, :subject => 'Issue with upload'},
:attachments => {'p0' => {:filename => filename, :token => token}}
}
)
assert_response 302
end
issue = Issue.order('id DESC').first
assert_equal 'Issue with upload', issue.subject
assert_equal 1, issue.attachments.count
attachment = issue.attachments.first
assert_equal filename, attachment.filename
assert_equal '', attachment.description
assert_equal 'File content'.length, attachment.filesize
end
def test_upload_as_js_and_destroy
log_user('jsmith', 'jsmith')
token = ajax_upload('myupload.txt', 'File content')
attachment = Attachment.order('id DESC').first
attachment_path = "/attachments/#{attachment.id}.js?attachment_id=1"
assert_include(
"href: '#{attachment_path}'",
response.body,
"Path to attachment: #{attachment_path} not found in response:\n#{response.body}"
)
assert_difference 'Attachment.count', -1 do
delete attachment_path
assert_response :success
end
assert_include "$('#attachments_1').remove();", response.body
end
def test_download_should_set_sendfile_header
set_fixtures_attachments_directory
Rack::Sendfile.any_instance.stubs(:variation).returns("X-Sendfile")
get "/attachments/download/4"
assert_response :success
assert_not_nil response.headers["X-Sendfile"]
ensure
set_tmp_attachments_directory
end
def test_download_all_with_wrong_container_type
set_tmp_attachments_directory
# make the attachment readable
assert a = Attachment.find(3)
FileUtils.mkdir_p File.dirname(a.diskfile)
(File.open(a.diskfile, 'wb') << 'test').close
# there is no 'download all' for WikiContentVersions
with_settings :login_required => '0' do
get "/attachments/wiki_content_versions/7/download"
assert_response :not_found
end
with_settings :login_required => '1' do
get "/attachments/wiki_content_versions/7/download"
assert_response :not_found
end
end
def test_download_all_for_journal_should_check_visibility
set_tmp_attachments_directory
Project.find(1).update_column :is_public, false
# make the attachment readable
assert a = Attachment.find(4)
FileUtils.mkdir_p File.dirname(a.diskfile)
(File.open(a.diskfile, 'wb') << 'test').close
with_settings :login_required => '0' do
get "/attachments/journals/3/download"
assert_response 403
end
with_settings :login_required => '1' do
get "/attachments/journals/3/download"
assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fattachments%2Fjournals%2F3%2Fdownload"
end
Project.find(1).update_column :is_public, true
with_settings :login_required => '0' do
get "/attachments/journals/3/download"
assert_response :success
end
with_settings :login_required => '1' do
get "/attachments/journals/3/download"
assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fattachments%2Fjournals%2F3%2Fdownload"
end
end
private
def ajax_upload(filename, content, attachment_id=1)
assert_difference 'Attachment.count' do
post(
"/uploads.js?attachment_id=#{attachment_id}&filename=#{filename}",
:params => content,
:headers => {"CONTENT_TYPE" => 'application/octet-stream'})
assert_response :success
assert_equal 'text/javascript', response.media_type
end
token = response.body.match(/\.val\('(\d+\.[0-9a-f]+)'\)/)[1]
assert_not_nil token, "No upload token found in response:\n#{response.body}"
token
end
end
|