annotate .svn/pristine/dc/dc488722255cd0f93614e33a6e9db17ff574d2f6.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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