annotate .svn/pristine/df/df052a3f075e19d2912a87bab6c7fefd9c8d4b89.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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