annotate .svn/pristine/e1/e19cb23ac2d90f9735c47ccc904dcda6e52b2377.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # encoding: utf-8
Chris@1295 2 #
Chris@1295 3 # Redmine - project management software
Chris@1295 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 5 #
Chris@1295 6 # This program is free software; you can redistribute it and/or
Chris@1295 7 # modify it under the terms of the GNU General Public License
Chris@1295 8 # as published by the Free Software Foundation; either version 2
Chris@1295 9 # of the License, or (at your option) any later version.
Chris@1295 10 #
Chris@1295 11 # This program is distributed in the hope that it will be useful,
Chris@1295 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 14 # GNU General Public License for more details.
Chris@1295 15 #
Chris@1295 16 # You should have received a copy of the GNU General Public License
Chris@1295 17 # along with this program; if not, write to the Free Software
Chris@1295 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 19
Chris@1295 20 require File.expand_path('../../test_helper', __FILE__)
Chris@1295 21
Chris@1295 22 class AttachmentTest < ActiveSupport::TestCase
Chris@1295 23 fixtures :users, :projects, :roles, :members, :member_roles,
Chris@1295 24 :enabled_modules, :issues, :trackers, :attachments
Chris@1295 25
Chris@1295 26 class MockFile
Chris@1295 27 attr_reader :original_filename, :content_type, :content, :size
Chris@1295 28
Chris@1295 29 def initialize(attributes)
Chris@1295 30 @original_filename = attributes[:original_filename]
Chris@1295 31 @content_type = attributes[:content_type]
Chris@1295 32 @content = attributes[:content] || "Content"
Chris@1295 33 @size = content.size
Chris@1295 34 end
Chris@1295 35 end
Chris@1295 36
Chris@1295 37 def setup
Chris@1295 38 set_tmp_attachments_directory
Chris@1295 39 end
Chris@1295 40
Chris@1295 41 def test_container_for_new_attachment_should_be_nil
Chris@1295 42 assert_nil Attachment.new.container
Chris@1295 43 end
Chris@1295 44
Chris@1295 45 def test_create
Chris@1295 46 a = Attachment.new(:container => Issue.find(1),
Chris@1295 47 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@1295 48 :author => User.find(1))
Chris@1295 49 assert a.save
Chris@1295 50 assert_equal 'testfile.txt', a.filename
Chris@1295 51 assert_equal 59, a.filesize
Chris@1295 52 assert_equal 'text/plain', a.content_type
Chris@1295 53 assert_equal 0, a.downloads
Chris@1295 54 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
Chris@1295 55
Chris@1295 56 assert a.disk_directory
Chris@1295 57 assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory
Chris@1295 58
Chris@1295 59 assert File.exist?(a.diskfile)
Chris@1295 60 assert_equal 59, File.size(a.diskfile)
Chris@1295 61 end
Chris@1295 62
Chris@1295 63 def test_copy_should_preserve_attributes
Chris@1295 64 a = Attachment.find(1)
Chris@1295 65 copy = a.copy
Chris@1295 66
Chris@1295 67 assert_save copy
Chris@1295 68 copy = Attachment.order('id DESC').first
Chris@1295 69 %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute|
Chris@1295 70 assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different"
Chris@1295 71 end
Chris@1295 72 end
Chris@1295 73
Chris@1295 74 def test_size_should_be_validated_for_new_file
Chris@1295 75 with_settings :attachment_max_size => 0 do
Chris@1295 76 a = Attachment.new(:container => Issue.find(1),
Chris@1295 77 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@1295 78 :author => User.find(1))
Chris@1295 79 assert !a.save
Chris@1295 80 end
Chris@1295 81 end
Chris@1295 82
Chris@1295 83 def test_size_should_not_be_validated_when_copying
Chris@1295 84 a = Attachment.create!(:container => Issue.find(1),
Chris@1295 85 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@1295 86 :author => User.find(1))
Chris@1295 87 with_settings :attachment_max_size => 0 do
Chris@1295 88 copy = a.copy
Chris@1295 89 assert copy.save
Chris@1295 90 end
Chris@1295 91 end
Chris@1295 92
Chris@1295 93 def test_description_length_should_be_validated
Chris@1295 94 a = Attachment.new(:description => 'a' * 300)
Chris@1295 95 assert !a.save
Chris@1295 96 assert_not_nil a.errors[:description]
Chris@1295 97 end
Chris@1295 98
Chris@1295 99 def test_destroy
Chris@1295 100 a = Attachment.new(:container => Issue.find(1),
Chris@1295 101 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@1295 102 :author => User.find(1))
Chris@1295 103 assert a.save
Chris@1295 104 assert_equal 'testfile.txt', a.filename
Chris@1295 105 assert_equal 59, a.filesize
Chris@1295 106 assert_equal 'text/plain', a.content_type
Chris@1295 107 assert_equal 0, a.downloads
Chris@1295 108 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
Chris@1295 109 diskfile = a.diskfile
Chris@1295 110 assert File.exist?(diskfile)
Chris@1295 111 assert_equal 59, File.size(a.diskfile)
Chris@1295 112 assert a.destroy
Chris@1295 113 assert !File.exist?(diskfile)
Chris@1295 114 end
Chris@1295 115
Chris@1295 116 def test_destroy_should_not_delete_file_referenced_by_other_attachment
Chris@1295 117 a = Attachment.create!(:container => Issue.find(1),
Chris@1295 118 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@1295 119 :author => User.find(1))
Chris@1295 120 diskfile = a.diskfile
Chris@1295 121
Chris@1295 122 copy = a.copy
Chris@1295 123 copy.save!
Chris@1295 124
Chris@1295 125 assert File.exists?(diskfile)
Chris@1295 126 a.destroy
Chris@1295 127 assert File.exists?(diskfile)
Chris@1295 128 copy.destroy
Chris@1295 129 assert !File.exists?(diskfile)
Chris@1295 130 end
Chris@1295 131
Chris@1295 132 def test_create_should_auto_assign_content_type
Chris@1295 133 a = Attachment.new(:container => Issue.find(1),
Chris@1295 134 :file => uploaded_test_file("testfile.txt", ""),
Chris@1295 135 :author => User.find(1))
Chris@1295 136 assert a.save
Chris@1295 137 assert_equal 'text/plain', a.content_type
Chris@1295 138 end
Chris@1295 139
Chris@1295 140 def test_identical_attachments_at_the_same_time_should_not_overwrite
Chris@1295 141 a1 = Attachment.create!(:container => Issue.find(1),
Chris@1295 142 :file => uploaded_test_file("testfile.txt", ""),
Chris@1295 143 :author => User.find(1))
Chris@1295 144 a2 = Attachment.create!(:container => Issue.find(1),
Chris@1295 145 :file => uploaded_test_file("testfile.txt", ""),
Chris@1295 146 :author => User.find(1))
Chris@1295 147 assert a1.disk_filename != a2.disk_filename
Chris@1295 148 end
Chris@1295 149
Chris@1295 150 def test_filename_should_be_basenamed
Chris@1295 151 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
Chris@1295 152 assert_equal 'file', a.filename
Chris@1295 153 end
Chris@1295 154
Chris@1295 155 def test_filename_should_be_sanitized
Chris@1295 156 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
Chris@1295 157 assert_equal 'valid_[] invalid_chars', a.filename
Chris@1295 158 end
Chris@1295 159
Chris@1295 160 def test_diskfilename
Chris@1295 161 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
Chris@1295 162 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
Chris@1295 163 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
Chris@1295 164 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
Chris@1295 165 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
Chris@1295 166 end
Chris@1295 167
Chris@1295 168 def test_title
Chris@1295 169 a = Attachment.new(:filename => "test.png")
Chris@1295 170 assert_equal "test.png", a.title
Chris@1295 171
Chris@1295 172 a = Attachment.new(:filename => "test.png", :description => "Cool image")
Chris@1295 173 assert_equal "test.png (Cool image)", a.title
Chris@1295 174 end
Chris@1295 175
Chris@1295 176 def test_prune_should_destroy_old_unattached_attachments
Chris@1295 177 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
Chris@1295 178 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
Chris@1295 179 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
Chris@1295 180
Chris@1295 181 assert_difference 'Attachment.count', -2 do
Chris@1295 182 Attachment.prune
Chris@1295 183 end
Chris@1295 184 end
Chris@1295 185
Chris@1295 186 def test_move_from_root_to_target_directory_should_move_root_files
Chris@1295 187 a = Attachment.find(20)
Chris@1295 188 assert a.disk_directory.blank?
Chris@1295 189 # Create a real file for this fixture
Chris@1295 190 File.open(a.diskfile, "w") do |f|
Chris@1295 191 f.write "test file at the root of files directory"
Chris@1295 192 end
Chris@1295 193 assert a.readable?
Chris@1295 194 Attachment.move_from_root_to_target_directory
Chris@1295 195
Chris@1295 196 a.reload
Chris@1295 197 assert_equal '2012/05', a.disk_directory
Chris@1295 198 assert a.readable?
Chris@1295 199 end
Chris@1295 200
Chris@1295 201 test "Attachmnet.attach_files should attach the file" do
Chris@1295 202 issue = Issue.first
Chris@1295 203 assert_difference 'Attachment.count' do
Chris@1295 204 Attachment.attach_files(issue,
Chris@1295 205 '1' => {
Chris@1295 206 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
Chris@1295 207 'description' => 'test'
Chris@1295 208 })
Chris@1295 209 end
Chris@1295 210
Chris@1295 211 attachment = Attachment.first(:order => 'id DESC')
Chris@1295 212 assert_equal issue, attachment.container
Chris@1295 213 assert_equal 'testfile.txt', attachment.filename
Chris@1295 214 assert_equal 59, attachment.filesize
Chris@1295 215 assert_equal 'test', attachment.description
Chris@1295 216 assert_equal 'text/plain', attachment.content_type
Chris@1295 217 assert File.exists?(attachment.diskfile)
Chris@1295 218 assert_equal 59, File.size(attachment.diskfile)
Chris@1295 219 end
Chris@1295 220
Chris@1295 221 test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do
Chris@1295 222 # Max size of 0 to force Attachment creation failures
Chris@1295 223 with_settings(:attachment_max_size => 0) do
Chris@1295 224 @project = Project.find(1)
Chris@1295 225 response = Attachment.attach_files(@project, {
Chris@1295 226 '1' => {'file' => mock_file, 'description' => 'test'},
Chris@1295 227 '2' => {'file' => mock_file, 'description' => 'test'}
Chris@1295 228 })
Chris@1295 229
Chris@1295 230 assert response[:unsaved].present?
Chris@1295 231 assert_equal 2, response[:unsaved].length
Chris@1295 232 assert response[:unsaved].first.new_record?
Chris@1295 233 assert response[:unsaved].second.new_record?
Chris@1295 234 assert_equal response[:unsaved], @project.unsaved_attachments
Chris@1295 235 end
Chris@1295 236 end
Chris@1295 237
Chris@1295 238 def test_latest_attach
Chris@1295 239 set_fixtures_attachments_directory
Chris@1295 240 a1 = Attachment.find(16)
Chris@1295 241 assert_equal "testfile.png", a1.filename
Chris@1295 242 assert a1.readable?
Chris@1295 243 assert (! a1.visible?(User.anonymous))
Chris@1295 244 assert a1.visible?(User.find(2))
Chris@1295 245 a2 = Attachment.find(17)
Chris@1295 246 assert_equal "testfile.PNG", a2.filename
Chris@1295 247 assert a2.readable?
Chris@1295 248 assert (! a2.visible?(User.anonymous))
Chris@1295 249 assert a2.visible?(User.find(2))
Chris@1295 250 assert a1.created_on < a2.created_on
Chris@1295 251
Chris@1295 252 la1 = Attachment.latest_attach([a1, a2], "testfile.png")
Chris@1295 253 assert_equal 17, la1.id
Chris@1295 254 la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
Chris@1295 255 assert_equal 17, la2.id
Chris@1295 256
Chris@1295 257 set_tmp_attachments_directory
Chris@1295 258 end
Chris@1295 259
Chris@1295 260 def test_thumbnailable_should_be_true_for_images
Chris@1295 261 assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
Chris@1295 262 end
Chris@1295 263
Chris@1295 264 def test_thumbnailable_should_be_true_for_non_images
Chris@1295 265 assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
Chris@1295 266 end
Chris@1295 267
Chris@1295 268 if convert_installed?
Chris@1295 269 def test_thumbnail_should_generate_the_thumbnail
Chris@1295 270 set_fixtures_attachments_directory
Chris@1295 271 attachment = Attachment.find(16)
Chris@1295 272 Attachment.clear_thumbnails
Chris@1295 273
Chris@1295 274 assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
Chris@1295 275 thumbnail = attachment.thumbnail
Chris@1295 276 assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
Chris@1295 277 assert File.exists?(thumbnail)
Chris@1295 278 end
Chris@1295 279 end
Chris@1295 280 else
Chris@1295 281 puts '(ImageMagick convert not available)'
Chris@1295 282 end
Chris@1295 283 end