Chris@0: # encoding: utf-8 Chris@0: # Chris@441: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../test_helper', __FILE__) Chris@0: Chris@0: class AttachmentTest < ActiveSupport::TestCase Chris@909: fixtures :users, :projects, :roles, :members, :member_roles, Chris@909: :enabled_modules, :issues, :trackers, :attachments Chris@909: Chris@909: class MockFile Chris@909: attr_reader :original_filename, :content_type, :content, :size Chris@909: Chris@909: def initialize(attributes) Chris@909: @original_filename = attributes[:original_filename] Chris@909: @content_type = attributes[:content_type] Chris@909: @content = attributes[:content] || "Content" Chris@909: @size = content.size Chris@909: end Chris@909: end Chris@441: Chris@0: def setup Chris@909: set_tmp_attachments_directory Chris@0: end Chris@0: Chris@1115: def test_container_for_new_attachment_should_be_nil Chris@1115: assert_nil Attachment.new.container Chris@1115: end Chris@1115: Chris@0: def test_create Chris@0: a = Attachment.new(:container => Issue.find(1), Chris@0: :file => uploaded_test_file("testfile.txt", "text/plain"), Chris@0: :author => User.find(1)) Chris@0: assert a.save Chris@0: assert_equal 'testfile.txt', a.filename Chris@0: assert_equal 59, a.filesize Chris@0: assert_equal 'text/plain', a.content_type Chris@0: assert_equal 0, a.downloads Chris@119: assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest Chris@1295: Chris@1295: assert a.disk_directory Chris@1295: assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory Chris@1295: Chris@0: assert File.exist?(a.diskfile) Chris@909: assert_equal 59, File.size(a.diskfile) Chris@909: end Chris@909: Chris@1295: def test_copy_should_preserve_attributes Chris@1295: a = Attachment.find(1) Chris@1295: copy = a.copy Chris@1295: Chris@1295: assert_save copy Chris@1295: copy = Attachment.order('id DESC').first Chris@1295: %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute| Chris@1295: assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different" Chris@1295: end Chris@1295: end Chris@1295: Chris@1115: def test_size_should_be_validated_for_new_file Chris@1115: with_settings :attachment_max_size => 0 do Chris@1115: a = Attachment.new(:container => Issue.find(1), Chris@1115: :file => uploaded_test_file("testfile.txt", "text/plain"), Chris@1115: :author => User.find(1)) Chris@1115: assert !a.save Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: def test_size_should_not_be_validated_when_copying Chris@1115: a = Attachment.create!(:container => Issue.find(1), Chris@1115: :file => uploaded_test_file("testfile.txt", "text/plain"), Chris@1115: :author => User.find(1)) Chris@1115: with_settings :attachment_max_size => 0 do Chris@1115: copy = a.copy Chris@1115: assert copy.save Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: def test_description_length_should_be_validated Chris@1115: a = Attachment.new(:description => 'a' * 300) Chris@1115: assert !a.save Chris@1115: assert_not_nil a.errors[:description] Chris@1115: end Chris@1115: Chris@909: def test_destroy Chris@909: a = Attachment.new(:container => Issue.find(1), Chris@909: :file => uploaded_test_file("testfile.txt", "text/plain"), Chris@909: :author => User.find(1)) Chris@909: assert a.save Chris@909: assert_equal 'testfile.txt', a.filename Chris@909: assert_equal 59, a.filesize Chris@909: assert_equal 'text/plain', a.content_type Chris@909: assert_equal 0, a.downloads Chris@909: assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest Chris@909: diskfile = a.diskfile Chris@909: assert File.exist?(diskfile) Chris@909: assert_equal 59, File.size(a.diskfile) Chris@909: assert a.destroy Chris@909: assert !File.exist?(diskfile) Chris@0: end Chris@441: Chris@1115: def test_destroy_should_not_delete_file_referenced_by_other_attachment Chris@1115: a = Attachment.create!(:container => Issue.find(1), Chris@1115: :file => uploaded_test_file("testfile.txt", "text/plain"), Chris@1115: :author => User.find(1)) Chris@1115: diskfile = a.diskfile Chris@1115: Chris@1115: copy = a.copy Chris@1115: copy.save! Chris@1115: Chris@1115: assert File.exists?(diskfile) Chris@1115: a.destroy Chris@1115: assert File.exists?(diskfile) Chris@1115: copy.destroy Chris@1115: assert !File.exists?(diskfile) Chris@1115: end Chris@1115: Chris@0: def test_create_should_auto_assign_content_type Chris@0: a = Attachment.new(:container => Issue.find(1), Chris@0: :file => uploaded_test_file("testfile.txt", ""), Chris@0: :author => User.find(1)) Chris@0: assert a.save Chris@0: assert_equal 'text/plain', a.content_type Chris@0: end Chris@441: Chris@0: def test_identical_attachments_at_the_same_time_should_not_overwrite Chris@0: a1 = Attachment.create!(:container => Issue.find(1), Chris@0: :file => uploaded_test_file("testfile.txt", ""), Chris@0: :author => User.find(1)) Chris@0: a2 = Attachment.create!(:container => Issue.find(1), Chris@0: :file => uploaded_test_file("testfile.txt", ""), Chris@0: :author => User.find(1)) Chris@0: assert a1.disk_filename != a2.disk_filename Chris@0: end Chris@909: Chris@909: def test_filename_should_be_basenamed Chris@909: a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file")) Chris@909: assert_equal 'file', a.filename Chris@909: end Chris@909: Chris@909: def test_filename_should_be_sanitized Chris@909: a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars")) Chris@909: assert_equal 'valid_[] invalid_chars', a.filename Chris@909: end Chris@441: Chris@0: def test_diskfilename Chris@0: assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ Chris@0: assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] Chris@0: assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1] Chris@0: assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1] Chris@0: assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1] Chris@0: end Chris@0: Chris@1115: def test_title Chris@1115: a = Attachment.new(:filename => "test.png") Chris@1115: assert_equal "test.png", a.title Chris@1115: Chris@1115: a = Attachment.new(:filename => "test.png", :description => "Cool image") Chris@1115: assert_equal "test.png (Cool image)", a.title Chris@1115: end Chris@1115: Chris@1115: def test_prune_should_destroy_old_unattached_attachments Chris@1115: Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago) Chris@1115: Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago) Chris@1115: Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1) Chris@1115: Chris@1115: assert_difference 'Attachment.count', -2 do Chris@1115: Attachment.prune Chris@1115: end Chris@1115: end Chris@1115: Chris@1295: def test_move_from_root_to_target_directory_should_move_root_files Chris@1295: a = Attachment.find(20) Chris@1295: assert a.disk_directory.blank? Chris@1295: # Create a real file for this fixture Chris@1295: File.open(a.diskfile, "w") do |f| Chris@1295: f.write "test file at the root of files directory" Chris@1295: end Chris@1295: assert a.readable? Chris@1295: Attachment.move_from_root_to_target_directory Chris@909: Chris@1295: a.reload Chris@1295: assert_equal '2012/05', a.disk_directory Chris@1295: assert a.readable? Chris@1295: end Chris@1295: Chris@1295: test "Attachmnet.attach_files should attach the file" do Chris@1295: issue = Issue.first Chris@1295: assert_difference 'Attachment.count' do Chris@1295: Attachment.attach_files(issue, Chris@1295: '1' => { Chris@1295: 'file' => uploaded_test_file('testfile.txt', 'text/plain'), Chris@1295: 'description' => 'test' Chris@1295: }) Chris@909: end Chris@909: Chris@1295: attachment = Attachment.first(:order => 'id DESC') Chris@1295: assert_equal issue, attachment.container Chris@1295: assert_equal 'testfile.txt', attachment.filename Chris@1295: assert_equal 59, attachment.filesize Chris@1295: assert_equal 'test', attachment.description Chris@1295: assert_equal 'text/plain', attachment.content_type Chris@1295: assert File.exists?(attachment.diskfile) Chris@1295: assert_equal 59, File.size(attachment.diskfile) Chris@1295: end Chris@0: Chris@1295: test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do Chris@1295: # Max size of 0 to force Attachment creation failures Chris@1295: with_settings(:attachment_max_size => 0) do Chris@1295: @project = Project.find(1) Chris@1295: response = Attachment.attach_files(@project, { Chris@1295: '1' => {'file' => mock_file, 'description' => 'test'}, Chris@1295: '2' => {'file' => mock_file, 'description' => 'test'} Chris@1295: }) Chris@1295: Chris@1295: assert response[:unsaved].present? Chris@1295: assert_equal 2, response[:unsaved].length Chris@1295: assert response[:unsaved].first.new_record? Chris@1295: assert response[:unsaved].second.new_record? Chris@1295: assert_equal response[:unsaved], @project.unsaved_attachments Chris@0: end Chris@0: end Chris@909: Chris@909: def test_latest_attach Chris@1115: set_fixtures_attachments_directory Chris@909: a1 = Attachment.find(16) Chris@909: assert_equal "testfile.png", a1.filename Chris@909: assert a1.readable? Chris@909: assert (! a1.visible?(User.anonymous)) Chris@909: assert a1.visible?(User.find(2)) Chris@909: a2 = Attachment.find(17) Chris@909: assert_equal "testfile.PNG", a2.filename Chris@909: assert a2.readable? Chris@909: assert (! a2.visible?(User.anonymous)) Chris@909: assert a2.visible?(User.find(2)) Chris@909: assert a1.created_on < a2.created_on Chris@909: Chris@909: la1 = Attachment.latest_attach([a1, a2], "testfile.png") Chris@909: assert_equal 17, la1.id Chris@909: la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG") Chris@909: assert_equal 17, la2.id Chris@909: Chris@909: set_tmp_attachments_directory Chris@909: end Chris@1115: Chris@1115: def test_thumbnailable_should_be_true_for_images Chris@1115: assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable? Chris@1115: end Chris@1115: Chris@1115: def test_thumbnailable_should_be_true_for_non_images Chris@1115: assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable? Chris@1115: end Chris@1115: Chris@1115: if convert_installed? Chris@1115: def test_thumbnail_should_generate_the_thumbnail Chris@1115: set_fixtures_attachments_directory Chris@1115: attachment = Attachment.find(16) Chris@1115: Attachment.clear_thumbnails Chris@1115: Chris@1115: assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do Chris@1115: thumbnail = attachment.thumbnail Chris@1115: assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail) Chris@1115: assert File.exists?(thumbnail) Chris@1115: end Chris@1115: end Chris@1115: else Chris@1115: puts '(ImageMagick convert not available)' Chris@1115: end Chris@0: end