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