annotate test/unit/attachment_test.rb @ 1174:928c0d0f624e bug_365

Close obsolete branch bug_365
author Chris Cannam
date Fri, 03 Feb 2012 14:03:51 +0000
parents 753f1380d6bc
children 5e80956cc792
rev   line source
Chris@0 1 # encoding: utf-8
Chris@0 2 #
Chris@441 3 # Redmine - project management software
Chris@441 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@441 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@441 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@119 20 require File.expand_path('../../test_helper', __FILE__)
Chris@0 21
Chris@0 22 class AttachmentTest < ActiveSupport::TestCase
Chris@0 23 fixtures :issues, :users
Chris@441 24
Chris@0 25 def setup
Chris@0 26 end
Chris@0 27
Chris@0 28 def test_create
Chris@0 29 a = Attachment.new(:container => Issue.find(1),
Chris@0 30 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@0 31 :author => User.find(1))
Chris@0 32 assert a.save
Chris@0 33 assert_equal 'testfile.txt', a.filename
Chris@0 34 assert_equal 59, a.filesize
Chris@0 35 assert_equal 'text/plain', a.content_type
Chris@0 36 assert_equal 0, a.downloads
Chris@119 37 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
Chris@0 38 assert File.exist?(a.diskfile)
Chris@0 39 end
Chris@441 40
Chris@0 41 def test_create_should_auto_assign_content_type
Chris@0 42 a = Attachment.new(:container => Issue.find(1),
Chris@0 43 :file => uploaded_test_file("testfile.txt", ""),
Chris@0 44 :author => User.find(1))
Chris@0 45 assert a.save
Chris@0 46 assert_equal 'text/plain', a.content_type
Chris@0 47 end
Chris@441 48
Chris@0 49 def test_identical_attachments_at_the_same_time_should_not_overwrite
Chris@0 50 a1 = Attachment.create!(:container => Issue.find(1),
Chris@0 51 :file => uploaded_test_file("testfile.txt", ""),
Chris@0 52 :author => User.find(1))
Chris@0 53 a2 = Attachment.create!(:container => Issue.find(1),
Chris@0 54 :file => uploaded_test_file("testfile.txt", ""),
Chris@0 55 :author => User.find(1))
Chris@0 56 assert a1.disk_filename != a2.disk_filename
Chris@0 57 end
Chris@441 58
Chris@0 59 def test_diskfilename
Chris@0 60 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
Chris@0 61 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
Chris@0 62 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
Chris@0 63 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
Chris@0 64 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
Chris@0 65 end
Chris@0 66
Chris@0 67 context "Attachmnet#attach_files" do
Chris@0 68 should "add unsaved files to the object as unsaved attachments" do
Chris@0 69 # Max size of 0 to force Attachment creation failures
Chris@0 70 with_settings(:attachment_max_size => 0) do
Chris@0 71 @project = Project.generate!
Chris@0 72 response = Attachment.attach_files(@project, {
Chris@0 73 '1' => {'file' => mock_file, 'description' => 'test'},
Chris@0 74 '2' => {'file' => mock_file, 'description' => 'test'}
Chris@0 75 })
Chris@0 76
Chris@0 77 assert response[:unsaved].present?
Chris@0 78 assert_equal 2, response[:unsaved].length
Chris@0 79 assert response[:unsaved].first.new_record?
Chris@0 80 assert response[:unsaved].second.new_record?
Chris@0 81 assert_equal response[:unsaved], @project.unsaved_attachments
Chris@0 82 end
Chris@0 83 end
Chris@0 84 end
Chris@0 85 end