annotate test/integration/attachments_test.rb @ 1628:9c5f8e24dadc live tip

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