##// END OF EJS Templates
Test cleanup....
Jean-Philippe Lang -
r15704:831a2d58aba5
parent child
Show More
@@ -1,434 +1,433
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2016 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.expand_path('../../test_helper', __FILE__)
21 21
22 22 class AttachmentTest < ActiveSupport::TestCase
23 23 fixtures :users, :email_addresses, :projects, :roles, :members, :member_roles,
24 24 :enabled_modules, :issues, :trackers, :attachments
25 25
26 26 # TODO: remove this with Rails 5 that supports after_commit callbacks
27 27 # in transactional fixtures (https://github.com/rails/rails/pull/18458)
28 28 self.use_transactional_fixtures = false
29 29
30 30 class MockFile
31 31 attr_reader :original_filename, :content_type, :content, :size
32 32
33 33 def initialize(attributes)
34 34 @original_filename = attributes[:original_filename]
35 35 @content_type = attributes[:content_type]
36 36 @content = attributes[:content] || "Content"
37 37 @size = content.size
38 38 end
39 39 end
40 40
41 41 def setup
42 42 set_tmp_attachments_directory
43 43 end
44 44
45 45 def test_container_for_new_attachment_should_be_nil
46 46 assert_nil Attachment.new.container
47 47 end
48 48
49 49 def test_filename_should_remove_eols
50 50 assert_equal "line_feed", Attachment.new(:filename => "line\nfeed").filename
51 51 assert_equal "line_feed", Attachment.new(:filename => "some\npath/line\nfeed").filename
52 52 assert_equal "carriage_return", Attachment.new(:filename => "carriage\rreturn").filename
53 53 assert_equal "carriage_return", Attachment.new(:filename => "some\rpath/carriage\rreturn").filename
54 54 end
55 55
56 56 def test_create
57 57 a = Attachment.new(:container => Issue.find(1),
58 58 :file => uploaded_test_file("testfile.txt", "text/plain"),
59 59 :author => User.find(1))
60 60 assert a.save
61 61 assert_equal 'testfile.txt', a.filename
62 62 assert_equal 59, a.filesize
63 63 assert_equal 'text/plain', a.content_type
64 64 assert_equal 0, a.downloads
65 65 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
66 66
67 67 assert a.disk_directory
68 68 assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory
69 69
70 70 assert File.exist?(a.diskfile)
71 71 assert_equal 59, File.size(a.diskfile)
72 72 end
73 73
74 74 def test_create_should_clear_content_type_if_too_long
75 75 a = Attachment.new(:container => Issue.find(1),
76 76 :file => uploaded_test_file("testfile.txt", "text/plain"),
77 77 :author => User.find(1),
78 78 :content_type => 'a'*300)
79 79 assert a.save
80 80 a.reload
81 81 assert_nil a.content_type
82 82 end
83 83
84 84 def test_shorted_filename_if_too_long
85 85 file = mock_file_with_options(:original_filename => "#{'a'*251}.txt")
86 assert_equal 255, file.original_filename.length
87 86
88 87 a = Attachment.new(:container => Issue.find(1),
89 88 :file => file,
90 89 :author => User.find(1))
91 90 assert a.save
92 91 a.reload
93 92 assert_equal 12 + 1 + 32 + 4, a.disk_filename.length
94 93 assert_equal 255, a.filename.length
95 94 end
96 95
97 96 def test_copy_should_preserve_attributes
98 97 a = Attachment.find(1)
99 98 copy = a.copy
100 99
101 100 assert_save copy
102 101 copy = Attachment.order('id DESC').first
103 102 %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute|
104 103 assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different"
105 104 end
106 105 end
107 106
108 107 def test_size_should_be_validated_for_new_file
109 108 with_settings :attachment_max_size => 0 do
110 109 a = Attachment.new(:container => Issue.find(1),
111 110 :file => uploaded_test_file("testfile.txt", "text/plain"),
112 111 :author => User.find(1))
113 112 assert !a.save
114 113 end
115 114 end
116 115
117 116 def test_size_should_not_be_validated_when_copying
118 117 a = Attachment.create!(:container => Issue.find(1),
119 118 :file => uploaded_test_file("testfile.txt", "text/plain"),
120 119 :author => User.find(1))
121 120 with_settings :attachment_max_size => 0 do
122 121 copy = a.copy
123 122 assert copy.save
124 123 end
125 124 end
126 125
127 126 def test_filesize_greater_than_2gb_should_be_supported
128 127 with_settings :attachment_max_size => (50.gigabyte / 1024) do
129 128 a = Attachment.create!(:container => Issue.find(1),
130 129 :file => uploaded_test_file("testfile.txt", "text/plain"),
131 130 :author => User.find(1))
132 131 a.filesize = 20.gigabyte
133 132 a.save!
134 133 assert_equal 20.gigabyte, a.reload.filesize
135 134 end
136 135 end
137 136
138 137 def test_extension_should_be_validated_against_allowed_extensions
139 138 with_settings :attachment_extensions_allowed => "txt, png" do
140 139 a = Attachment.new(:container => Issue.find(1),
141 140 :file => mock_file_with_options(:original_filename => "test.png"),
142 141 :author => User.find(1))
143 142 assert_save a
144 143
145 144 a = Attachment.new(:container => Issue.find(1),
146 145 :file => mock_file_with_options(:original_filename => "test.jpeg"),
147 146 :author => User.find(1))
148 147 assert !a.save
149 148 end
150 149 end
151 150
152 151 def test_extension_should_be_validated_against_denied_extensions
153 152 with_settings :attachment_extensions_denied => "txt, png" do
154 153 a = Attachment.new(:container => Issue.find(1),
155 154 :file => mock_file_with_options(:original_filename => "test.jpeg"),
156 155 :author => User.find(1))
157 156 assert_save a
158 157
159 158 a = Attachment.new(:container => Issue.find(1),
160 159 :file => mock_file_with_options(:original_filename => "test.png"),
161 160 :author => User.find(1))
162 161 assert !a.save
163 162 end
164 163 end
165 164
166 165 def test_valid_extension_should_be_case_insensitive
167 166 with_settings :attachment_extensions_allowed => "txt, Png" do
168 167 assert Attachment.valid_extension?(".pnG")
169 168 assert !Attachment.valid_extension?(".jpeg")
170 169 end
171 170 with_settings :attachment_extensions_denied => "txt, Png" do
172 171 assert !Attachment.valid_extension?(".pnG")
173 172 assert Attachment.valid_extension?(".jpeg")
174 173 end
175 174 end
176 175
177 176 def test_description_length_should_be_validated
178 177 a = Attachment.new(:description => 'a' * 300)
179 178 assert !a.save
180 179 assert_not_equal [], a.errors[:description]
181 180 end
182 181
183 182 def test_destroy
184 183 a = Attachment.new(:container => Issue.find(1),
185 184 :file => uploaded_test_file("testfile.txt", "text/plain"),
186 185 :author => User.find(1))
187 186 assert a.save
188 187 assert_equal 'testfile.txt', a.filename
189 188 assert_equal 59, a.filesize
190 189 assert_equal 'text/plain', a.content_type
191 190 assert_equal 0, a.downloads
192 191 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
193 192 diskfile = a.diskfile
194 193 assert File.exist?(diskfile)
195 194 assert_equal 59, File.size(a.diskfile)
196 195 assert a.destroy
197 196 assert !File.exist?(diskfile)
198 197 end
199 198
200 199 def test_destroy_should_not_delete_file_referenced_by_other_attachment
201 200 a = Attachment.create!(:container => Issue.find(1),
202 201 :file => uploaded_test_file("testfile.txt", "text/plain"),
203 202 :author => User.find(1))
204 203 diskfile = a.diskfile
205 204
206 205 copy = a.copy
207 206 copy.save!
208 207
209 208 assert File.exists?(diskfile)
210 209 a.destroy
211 210 assert File.exists?(diskfile)
212 211 copy.destroy
213 212 assert !File.exists?(diskfile)
214 213 end
215 214
216 215 def test_create_should_auto_assign_content_type
217 216 a = Attachment.new(:container => Issue.find(1),
218 217 :file => uploaded_test_file("testfile.txt", ""),
219 218 :author => User.find(1))
220 219 assert a.save
221 220 assert_equal 'text/plain', a.content_type
222 221 end
223 222
224 223 def test_identical_attachments_at_the_same_time_should_not_overwrite
225 224 a1 = Attachment.create!(:container => Issue.find(1),
226 225 :file => uploaded_test_file("testfile.txt", ""),
227 226 :author => User.find(1))
228 227 a2 = Attachment.create!(:container => Issue.find(1),
229 228 :file => uploaded_test_file("testfile.txt", ""),
230 229 :author => User.find(1))
231 230 assert a1.disk_filename != a2.disk_filename
232 231 end
233 232
234 233 def test_filename_should_be_basenamed
235 234 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
236 235 assert_equal 'file', a.filename
237 236 end
238 237
239 238 def test_filename_should_be_sanitized
240 239 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
241 240 assert_equal 'valid_[] invalid_chars', a.filename
242 241 end
243 242
244 243 def test_diskfilename
245 244 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
246 245 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
247 246 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1]
248 247 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1]
249 248 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1]
250 249 end
251 250
252 251 def test_title
253 252 a = Attachment.new(:filename => "test.png")
254 253 assert_equal "test.png", a.title
255 254
256 255 a = Attachment.new(:filename => "test.png", :description => "Cool image")
257 256 assert_equal "test.png (Cool image)", a.title
258 257 end
259 258
260 259 def test_new_attachment_should_be_editable_by_author
261 260 user = User.find(1)
262 261 a = Attachment.new(:author => user)
263 262 assert_equal true, a.editable?(user)
264 263 end
265 264
266 265 def test_prune_should_destroy_old_unattached_attachments
267 266 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
268 267 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
269 268 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
270 269
271 270 assert_difference 'Attachment.count', -2 do
272 271 Attachment.prune
273 272 end
274 273 end
275 274
276 275 def test_move_from_root_to_target_directory_should_move_root_files
277 276 a = Attachment.find(20)
278 277 assert a.disk_directory.blank?
279 278 # Create a real file for this fixture
280 279 File.open(a.diskfile, "w") do |f|
281 280 f.write "test file at the root of files directory"
282 281 end
283 282 assert a.readable?
284 283 Attachment.move_from_root_to_target_directory
285 284
286 285 a.reload
287 286 assert_equal '2012/05', a.disk_directory
288 287 assert a.readable?
289 288 end
290 289
291 290 test "Attachmnet.attach_files should attach the file" do
292 291 issue = Issue.first
293 292 assert_difference 'Attachment.count' do
294 293 Attachment.attach_files(issue,
295 294 '1' => {
296 295 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
297 296 'description' => 'test'
298 297 })
299 298 end
300 299 attachment = Attachment.order('id DESC').first
301 300 assert_equal issue, attachment.container
302 301 assert_equal 'testfile.txt', attachment.filename
303 302 assert_equal 59, attachment.filesize
304 303 assert_equal 'test', attachment.description
305 304 assert_equal 'text/plain', attachment.content_type
306 305 assert File.exists?(attachment.diskfile)
307 306 assert_equal 59, File.size(attachment.diskfile)
308 307 end
309 308
310 309 test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do
311 310 # Max size of 0 to force Attachment creation failures
312 311 with_settings(:attachment_max_size => 0) do
313 312 @project = Project.find(1)
314 313 response = Attachment.attach_files(@project, {
315 314 '1' => {'file' => mock_file, 'description' => 'test'},
316 315 '2' => {'file' => mock_file, 'description' => 'test'}
317 316 })
318 317
319 318 assert response[:unsaved].present?
320 319 assert_equal 2, response[:unsaved].length
321 320 assert response[:unsaved].first.new_record?
322 321 assert response[:unsaved].second.new_record?
323 322 assert_equal response[:unsaved], @project.unsaved_attachments
324 323 end
325 324 end
326 325
327 326 test "Attachment.attach_files should preserve the content_type of attachments added by token" do
328 327 @project = Project.find(1)
329 328 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
330 329 assert_equal 'text/plain', attachment.content_type
331 330 Attachment.attach_files(@project, { '1' => {'token' => attachment.token } })
332 331 attachment.reload
333 332 assert_equal 'text/plain', attachment.content_type
334 333 end
335 334
336 335 def test_update_attachments
337 336 attachments = Attachment.where(:id => [2, 3]).to_a
338 337
339 338 assert Attachment.update_attachments(attachments, {
340 339 '2' => {:filename => 'newname.txt', :description => 'New description'},
341 340 3 => {:filename => 'othername.txt'}
342 341 })
343 342
344 343 attachment = Attachment.find(2)
345 344 assert_equal 'newname.txt', attachment.filename
346 345 assert_equal 'New description', attachment.description
347 346
348 347 attachment = Attachment.find(3)
349 348 assert_equal 'othername.txt', attachment.filename
350 349 end
351 350
352 351 def test_update_attachments_with_failure
353 352 attachments = Attachment.where(:id => [2, 3]).to_a
354 353
355 354 assert !Attachment.update_attachments(attachments, {
356 355 '2' => {:filename => '', :description => 'New description'},
357 356 3 => {:filename => 'othername.txt'}
358 357 })
359 358
360 359 attachment = Attachment.find(3)
361 360 assert_equal 'logo.gif', attachment.filename
362 361 end
363 362
364 363 def test_update_attachments_should_sanitize_filename
365 364 attachments = Attachment.where(:id => 2).to_a
366 365
367 366 assert Attachment.update_attachments(attachments, {
368 367 2 => {:filename => 'newname?.txt'},
369 368 })
370 369
371 370 attachment = Attachment.find(2)
372 371 assert_equal 'newname_.txt', attachment.filename
373 372 end
374 373
375 374 def test_latest_attach
376 375 set_fixtures_attachments_directory
377 376 a1 = Attachment.find(16)
378 377 assert_equal "testfile.png", a1.filename
379 378 assert a1.readable?
380 379 assert (! a1.visible?(User.anonymous))
381 380 assert a1.visible?(User.find(2))
382 381 a2 = Attachment.find(17)
383 382 assert_equal "testfile.PNG", a2.filename
384 383 assert a2.readable?
385 384 assert (! a2.visible?(User.anonymous))
386 385 assert a2.visible?(User.find(2))
387 386 assert a1.created_on < a2.created_on
388 387
389 388 la1 = Attachment.latest_attach([a1, a2], "testfile.png")
390 389 assert_equal 17, la1.id
391 390 la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
392 391 assert_equal 17, la2.id
393 392
394 393 set_tmp_attachments_directory
395 394 end
396 395
397 396 def test_latest_attach_should_not_error_with_string_with_invalid_encoding
398 397 string = "width:50\xFE-Image.jpg".force_encoding('UTF-8')
399 398 assert_equal false, string.valid_encoding?
400 399
401 400 Attachment.latest_attach(Attachment.limit(2).to_a, string)
402 401 end
403 402
404 403 def test_thumbnailable_should_be_true_for_images
405 404 assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
406 405 end
407 406
408 407 def test_thumbnailable_should_be_true_for_non_images
409 408 assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
410 409 end
411 410
412 411 if convert_installed?
413 412 def test_thumbnail_should_generate_the_thumbnail
414 413 set_fixtures_attachments_directory
415 414 attachment = Attachment.find(16)
416 415 Attachment.clear_thumbnails
417 416
418 417 assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
419 418 thumbnail = attachment.thumbnail
420 419 assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
421 420 assert File.exists?(thumbnail)
422 421 end
423 422 end
424 423
425 424 def test_thumbnail_should_return_nil_if_generation_fails
426 425 Redmine::Thumbnail.expects(:generate).raises(SystemCallError, 'Something went wrong')
427 426 set_fixtures_attachments_directory
428 427 attachment = Attachment.find(16)
429 428 assert_nil attachment.thumbnail
430 429 end
431 430 else
432 431 puts '(ImageMagick convert not available)'
433 432 end
434 433 end
General Comments 0
You need to be logged in to leave comments. Login now