You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachments_test.rb 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class AttachmentsTest < Redmine::IntegrationTest
  19. fixtures :projects, :enabled_modules,
  20. :users, :roles, :members, :member_roles,
  21. :trackers, :projects_trackers,
  22. :issue_statuses, :enumerations
  23. def test_upload_should_set_default_content_type
  24. log_user('jsmith', 'jsmith')
  25. assert_difference 'Attachment.count' do
  26. post "/uploads.js?attachment_id=1&filename=foo.txt", "File content", {"CONTENT_TYPE" => 'application/octet-stream'}
  27. assert_response :success
  28. end
  29. attachment = Attachment.order(:id => :desc).first
  30. assert_equal 'text/plain', attachment.content_type
  31. end
  32. def test_upload_should_accept_content_type_param
  33. log_user('jsmith', 'jsmith')
  34. assert_difference 'Attachment.count' do
  35. post "/uploads.js?attachment_id=1&filename=foo&content_type=image/jpeg", "File content", {"CONTENT_TYPE" => 'application/octet-stream'}
  36. assert_response :success
  37. end
  38. attachment = Attachment.order(:id => :desc).first
  39. assert_equal 'image/jpeg', attachment.content_type
  40. end
  41. def test_upload_as_js_and_attach_to_an_issue
  42. log_user('jsmith', 'jsmith')
  43. token = ajax_upload('myupload.txt', 'File content')
  44. assert_difference 'Issue.count' do
  45. post '/projects/ecookbook/issues', {
  46. :issue => {:tracker_id => 1, :subject => 'Issue with upload'},
  47. :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
  48. }
  49. assert_response 302
  50. end
  51. issue = Issue.order('id DESC').first
  52. assert_equal 'Issue with upload', issue.subject
  53. assert_equal 1, issue.attachments.count
  54. attachment = issue.attachments.first
  55. assert_equal 'myupload.txt', attachment.filename
  56. assert_equal 'My uploaded file', attachment.description
  57. assert_equal 'File content'.length, attachment.filesize
  58. end
  59. def test_upload_as_js_and_preview_as_inline_attachment
  60. log_user('jsmith', 'jsmith')
  61. token = ajax_upload('myupload.jpg', 'JPEG content')
  62. post '/issues/preview/new/ecookbook', {
  63. :issue => {:tracker_id => 1, :description => 'Inline upload: !myupload.jpg!'},
  64. :attachments => {'1' => {:filename => 'myupload.jpg', :description => 'My uploaded file', :token => token}}
  65. }
  66. assert_response :success
  67. attachment_path = response.body.match(%r{<img src="(/attachments/download/\d+/myupload.jpg)"})[1]
  68. assert_not_nil token, "No attachment path found in response:\n#{response.body}"
  69. get attachment_path
  70. assert_response :success
  71. assert_equal 'JPEG content', response.body
  72. end
  73. def test_upload_and_resubmit_after_validation_failure
  74. log_user('jsmith', 'jsmith')
  75. token = ajax_upload('myupload.txt', 'File content')
  76. assert_no_difference 'Issue.count' do
  77. post '/projects/ecookbook/issues', {
  78. :issue => {:tracker_id => 1, :subject => ''},
  79. :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
  80. }
  81. assert_response :success
  82. end
  83. assert_select 'input[type=hidden][name=?][value=?]', 'attachments[p0][token]', token
  84. assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'myupload.txt'
  85. assert_select 'input[name=?][value=?]', 'attachments[p0][description]', 'My uploaded file'
  86. assert_difference 'Issue.count' do
  87. post '/projects/ecookbook/issues', {
  88. :issue => {:tracker_id => 1, :subject => 'Issue with upload'},
  89. :attachments => {'p0' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
  90. }
  91. assert_response 302
  92. end
  93. issue = Issue.order('id DESC').first
  94. assert_equal 'Issue with upload', issue.subject
  95. assert_equal 1, issue.attachments.count
  96. attachment = issue.attachments.first
  97. assert_equal 'myupload.txt', attachment.filename
  98. assert_equal 'My uploaded file', attachment.description
  99. assert_equal 'File content'.length, attachment.filesize
  100. end
  101. def test_upload_as_js_and_destroy
  102. log_user('jsmith', 'jsmith')
  103. token = ajax_upload('myupload.txt', 'File content')
  104. attachment = Attachment.order('id DESC').first
  105. attachment_path = "/attachments/#{attachment.id}.js?attachment_id=1"
  106. assert_include "href: '#{attachment_path}'", response.body, "Path to attachment: #{attachment_path} not found in response:\n#{response.body}"
  107. assert_difference 'Attachment.count', -1 do
  108. delete attachment_path
  109. assert_response :success
  110. end
  111. assert_include "$('#attachments_1').remove();", response.body
  112. end
  113. private
  114. def ajax_upload(filename, content, attachment_id=1)
  115. assert_difference 'Attachment.count' do
  116. post "/uploads.js?attachment_id=#{attachment_id}&filename=#{filename}", content, {"CONTENT_TYPE" => 'application/octet-stream'}
  117. assert_response :success
  118. assert_equal 'text/javascript', response.content_type
  119. end
  120. token = response.body.match(/\.val\('(\d+\.[0-9a-f]+)'\)/)[1]
  121. assert_not_nil token, "No upload token found in response:\n#{response.body}"
  122. token
  123. end
  124. end