annotate test/integration/attachments_test.rb @ 1298:4f746d8966dd redmine_2.3_integration

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