annotate test/unit/attachment_test.rb @ 1082:997f6d7738f7 bug_531

In repo controller entry action, show the page for the file even if it's binary (so user still has access to history etc links). This makes it possible to use the entry action as the default when a file is clicked on
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 22 Nov 2012 18:04:17 +0000
parents 5e80956cc792
children bb32da3bea34
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@909 23 fixtures :users, :projects, :roles, :members, :member_roles,
Chris@909 24 :enabled_modules, :issues, :trackers, :attachments
Chris@909 25
Chris@909 26 class MockFile
Chris@909 27 attr_reader :original_filename, :content_type, :content, :size
Chris@909 28
Chris@909 29 def initialize(attributes)
Chris@909 30 @original_filename = attributes[:original_filename]
Chris@909 31 @content_type = attributes[:content_type]
Chris@909 32 @content = attributes[:content] || "Content"
Chris@909 33 @size = content.size
Chris@909 34 end
Chris@909 35 end
Chris@441 36
Chris@0 37 def setup
Chris@909 38 set_tmp_attachments_directory
Chris@0 39 end
Chris@0 40
Chris@0 41 def test_create
Chris@0 42 a = Attachment.new(:container => Issue.find(1),
Chris@0 43 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@0 44 :author => User.find(1))
Chris@0 45 assert a.save
Chris@0 46 assert_equal 'testfile.txt', a.filename
Chris@0 47 assert_equal 59, a.filesize
Chris@0 48 assert_equal 'text/plain', a.content_type
Chris@0 49 assert_equal 0, a.downloads
Chris@119 50 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
Chris@0 51 assert File.exist?(a.diskfile)
Chris@909 52 assert_equal 59, File.size(a.diskfile)
Chris@909 53 end
Chris@909 54
Chris@909 55 def test_destroy
Chris@909 56 a = Attachment.new(:container => Issue.find(1),
Chris@909 57 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@909 58 :author => User.find(1))
Chris@909 59 assert a.save
Chris@909 60 assert_equal 'testfile.txt', a.filename
Chris@909 61 assert_equal 59, a.filesize
Chris@909 62 assert_equal 'text/plain', a.content_type
Chris@909 63 assert_equal 0, a.downloads
Chris@909 64 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
Chris@909 65 diskfile = a.diskfile
Chris@909 66 assert File.exist?(diskfile)
Chris@909 67 assert_equal 59, File.size(a.diskfile)
Chris@909 68 assert a.destroy
Chris@909 69 assert !File.exist?(diskfile)
Chris@0 70 end
Chris@441 71
Chris@0 72 def test_create_should_auto_assign_content_type
Chris@0 73 a = Attachment.new(:container => Issue.find(1),
Chris@0 74 :file => uploaded_test_file("testfile.txt", ""),
Chris@0 75 :author => User.find(1))
Chris@0 76 assert a.save
Chris@0 77 assert_equal 'text/plain', a.content_type
Chris@0 78 end
Chris@441 79
Chris@0 80 def test_identical_attachments_at_the_same_time_should_not_overwrite
Chris@0 81 a1 = Attachment.create!(:container => Issue.find(1),
Chris@0 82 :file => uploaded_test_file("testfile.txt", ""),
Chris@0 83 :author => User.find(1))
Chris@0 84 a2 = Attachment.create!(:container => Issue.find(1),
Chris@0 85 :file => uploaded_test_file("testfile.txt", ""),
Chris@0 86 :author => User.find(1))
Chris@0 87 assert a1.disk_filename != a2.disk_filename
Chris@0 88 end
Chris@909 89
Chris@909 90 def test_filename_should_be_basenamed
Chris@909 91 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
Chris@909 92 assert_equal 'file', a.filename
Chris@909 93 end
Chris@909 94
Chris@909 95 def test_filename_should_be_sanitized
Chris@909 96 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
Chris@909 97 assert_equal 'valid_[] invalid_chars', a.filename
Chris@909 98 end
Chris@441 99
Chris@0 100 def test_diskfilename
Chris@0 101 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
Chris@0 102 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
Chris@0 103 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
Chris@0 104 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
Chris@0 105 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
Chris@0 106 end
Chris@0 107
Chris@909 108 context "Attachmnet.attach_files" do
Chris@909 109 should "attach the file" do
Chris@909 110 issue = Issue.first
Chris@909 111 assert_difference 'Attachment.count' do
Chris@909 112 Attachment.attach_files(issue,
Chris@909 113 '1' => {
Chris@909 114 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
Chris@909 115 'description' => 'test'
Chris@909 116 })
Chris@909 117 end
Chris@909 118
Chris@909 119 attachment = Attachment.first(:order => 'id DESC')
Chris@909 120 assert_equal issue, attachment.container
Chris@909 121 assert_equal 'testfile.txt', attachment.filename
Chris@909 122 assert_equal 59, attachment.filesize
Chris@909 123 assert_equal 'test', attachment.description
Chris@909 124 assert_equal 'text/plain', attachment.content_type
Chris@909 125 assert File.exists?(attachment.diskfile)
Chris@909 126 assert_equal 59, File.size(attachment.diskfile)
Chris@909 127 end
Chris@909 128
Chris@0 129 should "add unsaved files to the object as unsaved attachments" do
Chris@0 130 # Max size of 0 to force Attachment creation failures
Chris@0 131 with_settings(:attachment_max_size => 0) do
Chris@0 132 @project = Project.generate!
Chris@0 133 response = Attachment.attach_files(@project, {
Chris@0 134 '1' => {'file' => mock_file, 'description' => 'test'},
Chris@0 135 '2' => {'file' => mock_file, 'description' => 'test'}
Chris@0 136 })
Chris@0 137
Chris@0 138 assert response[:unsaved].present?
Chris@0 139 assert_equal 2, response[:unsaved].length
Chris@0 140 assert response[:unsaved].first.new_record?
Chris@0 141 assert response[:unsaved].second.new_record?
Chris@0 142 assert_equal response[:unsaved], @project.unsaved_attachments
Chris@0 143 end
Chris@0 144 end
Chris@0 145 end
Chris@909 146
Chris@909 147 def test_latest_attach
Chris@909 148 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
Chris@909 149 a1 = Attachment.find(16)
Chris@909 150 assert_equal "testfile.png", a1.filename
Chris@909 151 assert a1.readable?
Chris@909 152 assert (! a1.visible?(User.anonymous))
Chris@909 153 assert a1.visible?(User.find(2))
Chris@909 154 a2 = Attachment.find(17)
Chris@909 155 assert_equal "testfile.PNG", a2.filename
Chris@909 156 assert a2.readable?
Chris@909 157 assert (! a2.visible?(User.anonymous))
Chris@909 158 assert a2.visible?(User.find(2))
Chris@909 159 assert a1.created_on < a2.created_on
Chris@909 160
Chris@909 161 la1 = Attachment.latest_attach([a1, a2], "testfile.png")
Chris@909 162 assert_equal 17, la1.id
Chris@909 163 la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
Chris@909 164 assert_equal 17, la2.id
Chris@909 165
Chris@909 166 set_tmp_attachments_directory
Chris@909 167 end
Chris@0 168 end