summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/functional/attachments_controller_test.rb58
-rw-r--r--test/integration/routing/attachments_test.rb3
-rw-r--r--test/unit/attachment_test.rb39
3 files changed, 100 insertions, 0 deletions
diff --git a/test/functional/attachments_controller_test.rb b/test/functional/attachments_controller_test.rb
index 19e4d0c09..c1b27c64e 100644
--- a/test/functional/attachments_controller_test.rb
+++ b/test/functional/attachments_controller_test.rb
@@ -327,6 +327,64 @@ class AttachmentsControllerTest < ActionController::TestCase
puts '(ImageMagick convert not available)'
end
+ def test_edit
+ @request.session[:user_id] = 2
+ get :edit, :object_type => 'issues', :object_id => '3'
+ assert_response :success
+ assert_template 'edit'
+
+ container = Issue.find(3)
+ assert_equal container, assigns(:container)
+ assert_equal container.attachments.size, assigns(:attachments).size
+
+ assert_select 'form[action=?]', '/attachments/issues/3' do
+ assert_select 'tr#attachment-4' do
+ assert_select 'input[name=?][value=?]', 'attachments[4][filename]', 'source.rb'
+ assert_select 'input[name=?][value=?]', 'attachments[4][description]', 'This is a Ruby source file'
+ end
+ end
+ end
+
+ def test_edit_invalid_container_class_should_return_404
+ get :edit, :object_type => 'nuggets', :object_id => '3'
+ assert_response 404
+ end
+
+ def test_edit_for_object_that_is_not_visible_should_return_403
+ get :edit, :object_type => 'issues', :object_id => '4'
+ assert_response 403
+ end
+
+ def test_update
+ @request.session[:user_id] = 2
+ patch :update, :object_type => 'issues', :object_id => '3', :attachments => {
+ '1' => {:filename => 'newname.text', :description => ''},
+ '4' => {:filename => 'newname.rb', :description => 'Renamed'},
+ }
+
+ assert_response 302
+ attachment = Attachment.find(4)
+ assert_equal 'newname.rb', attachment.filename
+ assert_equal 'Renamed', attachment.description
+ end
+
+ def test_update_with_failure
+ @request.session[:user_id] = 2
+ patch :update, :object_type => 'issues', :object_id => '3', :attachments => {
+ '1' => {:filename => '', :description => ''},
+ '4' => {:filename => 'newname.rb', :description => 'Renamed'},
+ }
+
+ assert_response :success
+ assert_template 'edit'
+ assert_select_error /file #{ESCAPED_CANT} be blank/i
+
+ # The other attachment should not be updated
+ attachment = Attachment.find(4)
+ assert_equal 'source.rb', attachment.filename
+ assert_equal 'This is a Ruby source file', attachment.description
+ end
+
def test_destroy_issue_attachment
set_tmp_attachments_directory
issue = Issue.find(3)
diff --git a/test/integration/routing/attachments_test.rb b/test/integration/routing/attachments_test.rb
index d6d278001..9727f9ee3 100644
--- a/test/integration/routing/attachments_test.rb
+++ b/test/integration/routing/attachments_test.rb
@@ -29,5 +29,8 @@ class RoutingAttachmentsTest < Redmine::RoutingTest
should_route 'GET /attachments/thumbnail/1/200' => 'attachments#thumbnail', :id => '1', :size => '200'
should_route 'DELETE /attachments/1' => 'attachments#destroy', :id => '1'
+
+ should_route 'GET /attachments/issues/1/edit' => 'attachments#edit', :object_type => 'issues', :object_id => '1'
+ should_route 'PATCH /attachments/issues/1' => 'attachments#update', :object_type => 'issues', :object_id => '1'
end
end
diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb
index 6a077fcc2..29c433f7b 100644
--- a/test/unit/attachment_test.rb
+++ b/test/unit/attachment_test.rb
@@ -250,6 +250,45 @@ class AttachmentTest < ActiveSupport::TestCase
assert_equal 'text/plain', attachment.content_type
end
+ def test_update_attachments
+ attachments = Attachment.where(:id => [2, 3]).to_a
+
+ assert Attachment.update_attachments(attachments, {
+ '2' => {:filename => 'newname.txt', :description => 'New description'},
+ 3 => {:filename => 'othername.txt'}
+ })
+
+ attachment = Attachment.find(2)
+ assert_equal 'newname.txt', attachment.filename
+ assert_equal 'New description', attachment.description
+
+ attachment = Attachment.find(3)
+ assert_equal 'othername.txt', attachment.filename
+ end
+
+ def test_update_attachments_with_failure
+ attachments = Attachment.where(:id => [2, 3]).to_a
+
+ assert !Attachment.update_attachments(attachments, {
+ '2' => {:filename => '', :description => 'New description'},
+ 3 => {:filename => 'othername.txt'}
+ })
+
+ attachment = Attachment.find(3)
+ assert_equal 'logo.gif', attachment.filename
+ end
+
+ def test_update_attachments_should_sanitize_filename
+ attachments = Attachment.where(:id => 2).to_a
+
+ assert Attachment.update_attachments(attachments, {
+ 2 => {:filename => 'newname?.txt'},
+ })
+
+ attachment = Attachment.find(2)
+ assert_equal 'newname_.txt', attachment.filename
+ end
+
def test_latest_attach
set_fixtures_attachments_directory
a1 = Attachment.find(16)