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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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 < ActionController::IntegrationTest
  19. fixtures :projects, :enabled_modules,
  20. :users, :roles, :members, :member_roles,
  21. :trackers, :projects_trackers,
  22. :issue_statuses, :enumerations
  23. def test_upload_as_js_and_attach_to_an_issue
  24. log_user('jsmith', 'jsmith')
  25. token = ajax_upload('myupload.txt', 'File content')
  26. assert_difference 'Issue.count' do
  27. post '/projects/ecookbook/issues', {
  28. :issue => {:tracker_id => 1, :subject => 'Issue with upload'},
  29. :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
  30. }
  31. assert_response 302
  32. end
  33. issue = Issue.order('id DESC').first
  34. assert_equal 'Issue with upload', issue.subject
  35. assert_equal 1, issue.attachments.count
  36. attachment = issue.attachments.first
  37. assert_equal 'myupload.txt', attachment.filename
  38. assert_equal 'My uploaded file', attachment.description
  39. assert_equal 'File content'.length, attachment.filesize
  40. end
  41. def test_upload_as_js_and_preview_as_inline_attachment
  42. log_user('jsmith', 'jsmith')
  43. token = ajax_upload('myupload.jpg', 'JPEG content')
  44. post '/issues/preview/new/ecookbook', {
  45. :issue => {:tracker_id => 1, :description => 'Inline upload: !myupload.jpg!'},
  46. :attachments => {'1' => {:filename => 'myupload.jpg', :description => 'My uploaded file', :token => token}}
  47. }
  48. assert_response :success
  49. attachment_path = response.body.match(%r{<img src="(/attachments/download/\d+/myupload.jpg)"})[1]
  50. assert_not_nil token, "No attachment path found in response:\n#{response.body}"
  51. get attachment_path
  52. assert_response :success
  53. assert_equal 'JPEG content', response.body
  54. end
  55. def test_upload_and_resubmit_after_validation_failure
  56. log_user('jsmith', 'jsmith')
  57. token = ajax_upload('myupload.txt', 'File content')
  58. assert_no_difference 'Issue.count' do
  59. post '/projects/ecookbook/issues', {
  60. :issue => {:tracker_id => 1, :subject => ''},
  61. :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
  62. }
  63. assert_response :success
  64. end
  65. assert_select 'input[type=hidden][name=?][value=?]', 'attachments[p0][token]', token
  66. assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'myupload.txt'
  67. assert_select 'input[name=?][value=?]', 'attachments[p0][description]', 'My uploaded file'
  68. assert_difference 'Issue.count' do
  69. post '/projects/ecookbook/issues', {
  70. :issue => {:tracker_id => 1, :subject => 'Issue with upload'},
  71. :attachments => {'p0' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
  72. }
  73. assert_response 302
  74. end
  75. issue = Issue.order('id DESC').first
  76. assert_equal 'Issue with upload', issue.subject
  77. assert_equal 1, issue.attachments.count
  78. attachment = issue.attachments.first
  79. assert_equal 'myupload.txt', attachment.filename
  80. assert_equal 'My uploaded file', attachment.description
  81. assert_equal 'File content'.length, attachment.filesize
  82. end
  83. def test_upload_as_js_and_destroy
  84. log_user('jsmith', 'jsmith')
  85. token = ajax_upload('myupload.txt', 'File content')
  86. attachment = Attachment.order('id DESC').first
  87. attachment_path = "/attachments/#{attachment.id}.js?attachment_id=1"
  88. assert_include "href: '#{attachment_path}'", response.body, "Path to attachment: #{attachment_path} not found in response:\n#{response.body}"
  89. assert_difference 'Attachment.count', -1 do
  90. delete attachment_path
  91. assert_response :success
  92. end
  93. assert_include "$('#attachments_1').remove();", response.body
  94. end
  95. private
  96. def ajax_upload(filename, content, attachment_id=1)
  97. assert_difference 'Attachment.count' do
  98. post "/uploads.js?attachment_id=#{attachment_id}&filename=#{filename}", content, {"CONTENT_TYPE" => 'application/octet-stream'}
  99. assert_response :success
  100. assert_equal 'text/javascript', response.content_type
  101. end
  102. token = response.body.match(/\.val\('(\d+\.[0-9a-f]+)'\)/)[1]
  103. assert_not_nil token, "No upload token found in response:\n#{response.body}"
  104. token
  105. end
  106. end