Chris@0: # encoding: utf-8 Chris@0: # Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 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@0: # 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@0: # 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@0: require File.dirname(__FILE__) + '/../test_helper' Chris@0: Chris@0: class AttachmentTest < ActiveSupport::TestCase Chris@0: fixtures :issues, :users Chris@0: Chris@0: def setup Chris@0: end Chris@0: 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@0: assert_equal Digest::MD5.hexdigest(uploaded_test_file("testfile.txt", "text/plain").read), a.digest Chris@0: assert File.exist?(a.diskfile) Chris@0: end Chris@0: 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@0: 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@0: 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@0: context "Attachmnet#attach_files" do Chris@0: should "add unsaved files to the object as unsaved attachments" do Chris@0: # Max size of 0 to force Attachment creation failures Chris@0: with_settings(:attachment_max_size => 0) do Chris@0: @project = Project.generate! Chris@0: response = Attachment.attach_files(@project, { Chris@0: '1' => {'file' => mock_file, 'description' => 'test'}, Chris@0: '2' => {'file' => mock_file, 'description' => 'test'} Chris@0: }) Chris@0: Chris@0: assert response[:unsaved].present? Chris@0: assert_equal 2, response[:unsaved].length Chris@0: assert response[:unsaved].first.new_record? Chris@0: assert response[:unsaved].second.new_record? Chris@0: assert_equal response[:unsaved], @project.unsaved_attachments Chris@0: end Chris@0: end Chris@0: end Chris@0: end