From ba6313f38e6bb014f639aa2872aeb0a96dca3911 Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Fri, 17 Feb 2017 18:00:27 +0800 Subject: [PATCH] fixes uploading of empty files - prevents creation of attachment records without existing diskfile and empty digest - adds test case to check file upload API response --- app/models/attachment.rb | 4 +--- test/integration/api_test/attachments_test.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index d038437..52c7825 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -85,7 +85,6 @@ class Attachment < ActiveRecord::Base def file=(incoming_file) unless incoming_file.nil? @temp_file = incoming_file - if @temp_file.size > 0 if @temp_file.respond_to?(:original_filename) self.filename = @temp_file.original_filename self.filename.force_encoding("UTF-8") @@ -94,7 +93,6 @@ class Attachment < ActiveRecord::Base self.content_type = @temp_file.content_type.to_s.chomp end self.filesize = @temp_file.size - end end end @@ -110,7 +108,7 @@ class Attachment < ActiveRecord::Base # Copies the temporary file to its final location # and computes its MD5 hash def files_to_final_location - if @temp_file && (@temp_file.size > 0) + if @temp_file self.disk_directory = target_directory self.disk_filename = Attachment.disk_filename(filename, disk_directory) logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") if logger diff --git a/test/integration/api_test/attachments_test.rb b/test/integration/api_test/attachments_test.rb index 4188d71..641dcca 100644 --- a/test/integration/api_test/attachments_test.rb +++ b/test/integration/api_test/attachments_test.rb @@ -197,4 +197,23 @@ class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base end end end + + test "POST /uploads.json should create an empty file and return a valid token" do + set_tmp_attachments_directory + assert_difference 'Attachment.count' do + post '/uploads.json', '', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) + assert_response :created + + end + + json = ActiveSupport::JSON.decode(response.body) + assert_kind_of Hash, json['upload'] + token = json['upload']['token'] + assert token.present? + + assert attachment = Attachment.find_by_token(token) + assert_equal 0, attachment.filesize + assert attachment.digest.present? + assert File.exist? attachment.diskfile + end end -- 2.1.4