Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: require File.expand_path('../../test_helper', __FILE__) Chris@1295: Chris@1295: class AttachmentsTest < ActionController::IntegrationTest Chris@1295: fixtures :projects, :enabled_modules, Chris@1295: :users, :roles, :members, :member_roles, Chris@1295: :trackers, :projects_trackers, Chris@1295: :issue_statuses, :enumerations Chris@1295: Chris@1295: def test_upload_as_js_and_attach_to_an_issue Chris@1295: log_user('jsmith', 'jsmith') Chris@1295: Chris@1295: token = ajax_upload('myupload.txt', 'File content') Chris@1295: Chris@1295: assert_difference 'Issue.count' do Chris@1295: post '/projects/ecookbook/issues', { Chris@1295: :issue => {:tracker_id => 1, :subject => 'Issue with upload'}, Chris@1295: :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}} Chris@1295: } Chris@1295: assert_response 302 Chris@1295: end Chris@1295: Chris@1295: issue = Issue.order('id DESC').first Chris@1295: assert_equal 'Issue with upload', issue.subject Chris@1295: assert_equal 1, issue.attachments.count Chris@1295: Chris@1295: attachment = issue.attachments.first Chris@1295: assert_equal 'myupload.txt', attachment.filename Chris@1295: assert_equal 'My uploaded file', attachment.description Chris@1295: assert_equal 'File content'.length, attachment.filesize Chris@1295: end Chris@1295: Chris@1295: def test_upload_as_js_and_preview_as_inline_attachment Chris@1295: log_user('jsmith', 'jsmith') Chris@1295: Chris@1295: token = ajax_upload('myupload.jpg', 'JPEG content') Chris@1295: Chris@1295: post '/issues/preview/new/ecookbook', { Chris@1295: :issue => {:tracker_id => 1, :description => 'Inline upload: !myupload.jpg!'}, Chris@1295: :attachments => {'1' => {:filename => 'myupload.jpg', :description => 'My uploaded file', :token => token}} Chris@1295: } Chris@1295: assert_response :success Chris@1295: Chris@1295: attachment_path = response.body.match(%r{ {:tracker_id => 1, :subject => ''}, Chris@1295: :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}} Chris@1295: } Chris@1295: assert_response :success Chris@1295: end Chris@1295: assert_select 'input[type=hidden][name=?][value=?]', 'attachments[p0][token]', token Chris@1295: assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'myupload.txt' Chris@1295: assert_select 'input[name=?][value=?]', 'attachments[p0][description]', 'My uploaded file' Chris@1295: Chris@1295: assert_difference 'Issue.count' do Chris@1295: post '/projects/ecookbook/issues', { Chris@1295: :issue => {:tracker_id => 1, :subject => 'Issue with upload'}, Chris@1295: :attachments => {'p0' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}} Chris@1295: } Chris@1295: assert_response 302 Chris@1295: end Chris@1295: Chris@1295: issue = Issue.order('id DESC').first Chris@1295: assert_equal 'Issue with upload', issue.subject Chris@1295: assert_equal 1, issue.attachments.count Chris@1295: Chris@1295: attachment = issue.attachments.first Chris@1295: assert_equal 'myupload.txt', attachment.filename Chris@1295: assert_equal 'My uploaded file', attachment.description Chris@1295: assert_equal 'File content'.length, attachment.filesize Chris@1295: end Chris@1295: Chris@1295: def test_upload_as_js_and_destroy Chris@1295: log_user('jsmith', 'jsmith') Chris@1295: Chris@1295: token = ajax_upload('myupload.txt', 'File content') Chris@1295: Chris@1295: attachment = Attachment.order('id DESC').first Chris@1295: attachment_path = "/attachments/#{attachment.id}.js?attachment_id=1" Chris@1295: assert_include "href: '#{attachment_path}'", response.body, "Path to attachment: #{attachment_path} not found in response:\n#{response.body}" Chris@1295: Chris@1295: assert_difference 'Attachment.count', -1 do Chris@1295: delete attachment_path Chris@1295: assert_response :success Chris@1295: end Chris@1295: Chris@1295: assert_include "$('#attachments_1').remove();", response.body Chris@1295: end Chris@1295: Chris@1295: private Chris@1295: Chris@1295: def ajax_upload(filename, content, attachment_id=1) Chris@1295: assert_difference 'Attachment.count' do Chris@1295: post "/uploads.js?attachment_id=#{attachment_id}&filename=#{filename}", content, {"CONTENT_TYPE" => 'application/octet-stream'} Chris@1295: assert_response :success Chris@1295: assert_equal 'text/javascript', response.content_type Chris@1295: end Chris@1295: Chris@1295: token = response.body.match(/\.val\('(\d+\.[0-9a-f]+)'\)/)[1] Chris@1295: assert_not_nil token, "No upload token found in response:\n#{response.body}" Chris@1295: token Chris@1295: end Chris@1295: end