You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachment_test.rb 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class AttachmentTest < ActiveSupport::TestCase
  20. fixtures :users, :email_addresses, :projects, :roles, :members, :member_roles,
  21. :enabled_modules, :issues, :trackers, :attachments
  22. def setup
  23. User.current = nil
  24. set_tmp_attachments_directory
  25. end
  26. def test_container_for_new_attachment_should_be_nil
  27. assert_nil Attachment.new.container
  28. end
  29. def test_filename_should_remove_eols
  30. assert_equal "line_feed", Attachment.new(:filename => "line\nfeed").filename
  31. assert_equal "line_feed", Attachment.new(:filename => "some\npath/line\nfeed").filename
  32. assert_equal "carriage_return", Attachment.new(:filename => "carriage\rreturn").filename
  33. assert_equal "carriage_return", Attachment.new(:filename => "some\rpath/carriage\rreturn").filename
  34. end
  35. def test_create
  36. a = Attachment.new(:container => Issue.find(1),
  37. :file => uploaded_test_file("testfile.txt", "text/plain"),
  38. :author => User.find(1))
  39. assert a.save
  40. assert_equal 'testfile.txt', a.filename
  41. assert_equal 59, a.filesize
  42. assert_equal 'text/plain', a.content_type
  43. assert_equal 0, a.downloads
  44. assert_equal '6bc2eb7e87cfbf9145065689aaa8b5f513089ca0af68e2dc41f9cc025473d106', a.digest
  45. assert a.disk_directory
  46. assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory
  47. assert File.exist?(a.diskfile)
  48. assert_equal 59, File.size(a.diskfile)
  49. end
  50. def test_create_should_clear_content_type_if_too_long
  51. a = Attachment.new(:container => Issue.find(1),
  52. :file => uploaded_test_file("testfile.txt", "text/plain"),
  53. :author => User.find(1),
  54. :content_type => 'a'*300)
  55. assert a.save
  56. a.reload
  57. assert_nil a.content_type
  58. end
  59. def test_shorted_filename_if_too_long
  60. file = mock_file_with_options(:original_filename => "#{'a'*251}.txt")
  61. a = Attachment.new(:container => Issue.find(1),
  62. :file => file,
  63. :author => User.find(1))
  64. assert a.save
  65. a.reload
  66. assert_equal 12 + 1 + 32 + 4, a.disk_filename.length
  67. assert_equal 255, a.filename.length
  68. end
  69. def test_copy_should_preserve_attributes
  70. # prevent re-use of data from other attachments with equal contents
  71. Attachment.where('id <> 1').destroy_all
  72. a = Attachment.find(1)
  73. copy = a.copy
  74. assert_save copy
  75. copy = Attachment.order('id DESC').first
  76. %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute|
  77. assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different"
  78. end
  79. end
  80. def test_size_should_be_validated_for_new_file
  81. with_settings :attachment_max_size => 0 do
  82. a = Attachment.new(:container => Issue.find(1),
  83. :file => uploaded_test_file("testfile.txt", "text/plain"),
  84. :author => User.find(1))
  85. assert !a.save
  86. end
  87. end
  88. def test_size_should_not_be_validated_when_copying
  89. a = Attachment.create!(:container => Issue.find(1),
  90. :file => uploaded_test_file("testfile.txt", "text/plain"),
  91. :author => User.find(1))
  92. with_settings :attachment_max_size => 0 do
  93. copy = a.copy
  94. assert copy.save
  95. end
  96. end
  97. def test_filesize_greater_than_2gb_should_be_supported
  98. with_settings :attachment_max_size => (50.gigabyte / 1024) do
  99. a = Attachment.create!(:container => Issue.find(1),
  100. :file => uploaded_test_file("testfile.txt", "text/plain"),
  101. :author => User.find(1))
  102. a.filesize = 20.gigabyte
  103. a.save!
  104. assert_equal 20.gigabyte, a.reload.filesize
  105. end
  106. end
  107. def test_extension_should_be_validated_against_allowed_extensions
  108. with_settings :attachment_extensions_allowed => "txt, png" do
  109. a = Attachment.new(:container => Issue.find(1),
  110. :file => mock_file_with_options(:original_filename => "test.png"),
  111. :author => User.find(1))
  112. assert_save a
  113. a = Attachment.new(:container => Issue.find(1),
  114. :file => mock_file_with_options(:original_filename => "test.jpeg"),
  115. :author => User.find(1))
  116. assert !a.save
  117. end
  118. end
  119. def test_extension_should_be_validated_against_denied_extensions
  120. with_settings :attachment_extensions_denied => "txt, png" do
  121. a = Attachment.new(:container => Issue.find(1),
  122. :file => mock_file_with_options(:original_filename => "test.jpeg"),
  123. :author => User.find(1))
  124. assert_save a
  125. a = Attachment.new(:container => Issue.find(1),
  126. :file => mock_file_with_options(:original_filename => "test.png"),
  127. :author => User.find(1))
  128. assert !a.save
  129. end
  130. end
  131. def test_extension_update_should_be_validated_against_denied_extensions
  132. with_settings :attachment_extensions_denied => "txt, png" do
  133. a = Attachment.new(:container => Issue.find(1),
  134. :file => mock_file_with_options(:original_filename => "test.jpeg"),
  135. :author => User.find(1))
  136. assert_save a
  137. b = Attachment.find(a.id)
  138. b.filename = "test.png"
  139. assert !b.save
  140. end
  141. end
  142. def test_valid_extension_should_be_case_insensitive
  143. with_settings :attachment_extensions_allowed => "txt, Png" do
  144. assert Attachment.valid_extension?(".pnG")
  145. assert !Attachment.valid_extension?(".jpeg")
  146. end
  147. with_settings :attachment_extensions_denied => "txt, Png" do
  148. assert !Attachment.valid_extension?(".pnG")
  149. assert Attachment.valid_extension?(".jpeg")
  150. end
  151. end
  152. def test_description_length_should_be_validated
  153. a = Attachment.new(:description => 'a' * 300)
  154. assert !a.save
  155. assert_not_equal [], a.errors[:description]
  156. end
  157. def test_destroy
  158. a = Attachment.new(:container => Issue.find(1),
  159. :file => uploaded_test_file("testfile.txt", "text/plain"),
  160. :author => User.find(1))
  161. assert a.save
  162. assert_equal 'testfile.txt', a.filename
  163. assert_equal 59, a.filesize
  164. assert_equal 'text/plain', a.content_type
  165. assert_equal 0, a.downloads
  166. assert_equal '6bc2eb7e87cfbf9145065689aaa8b5f513089ca0af68e2dc41f9cc025473d106', a.digest
  167. diskfile = a.diskfile
  168. assert File.exist?(diskfile)
  169. assert_equal 59, File.size(a.diskfile)
  170. assert a.destroy
  171. assert !File.exist?(diskfile)
  172. end
  173. def test_destroy_should_not_delete_file_referenced_by_other_attachment
  174. a = Attachment.create!(:container => Issue.find(1),
  175. :file => uploaded_test_file("testfile.txt", "text/plain"),
  176. :author => User.find(1))
  177. diskfile = a.diskfile
  178. copy = a.copy
  179. copy.save!
  180. assert File.exist?(diskfile)
  181. a.destroy
  182. assert File.exist?(diskfile)
  183. copy.destroy
  184. assert !File.exist?(diskfile)
  185. end
  186. def test_create_should_auto_assign_content_type
  187. a = Attachment.new(:container => Issue.find(1),
  188. :file => uploaded_test_file("testfile.txt", ""),
  189. :author => User.find(1))
  190. assert a.save
  191. assert_equal 'text/plain', a.content_type
  192. end
  193. def test_attachments_with_same_content_should_reuse_same_file
  194. a1 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
  195. :file => mock_file(:filename => 'foo', :content => 'abcd'))
  196. a2 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
  197. :file => mock_file(:filename => 'bar', :content => 'abcd'))
  198. assert_equal a1.diskfile, a2.diskfile
  199. end
  200. def test_attachments_with_same_content_should_not_reuse_same_file_if_deleted
  201. a1 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
  202. :file => mock_file(:filename => 'foo', :content => 'abcd'))
  203. a1.delete_from_disk
  204. a2 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
  205. :file => mock_file(:filename => 'bar', :content => 'abcd'))
  206. assert_not_equal a1.diskfile, a2.diskfile
  207. end
  208. def test_attachments_with_same_filename_at_the_same_time_should_not_overwrite
  209. a1 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
  210. :file => mock_file(:filename => 'foo', :content => 'abcd'))
  211. a2 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
  212. :file => mock_file(:filename => 'foo', :content => 'efgh'))
  213. assert_not_equal a1.diskfile, a2.diskfile
  214. end
  215. def test_identical_attachments_created_in_same_transaction_should_not_end_up_unreadable
  216. attachments = []
  217. Project.transaction do
  218. 3.times do
  219. a = Attachment.create!(
  220. :container => Issue.find(1), :author => User.find(1),
  221. :file => mock_file(:filename => 'foo', :content => 'abcde')
  222. )
  223. attachments << a
  224. end
  225. end
  226. attachments.each do |a|
  227. assert a.readable?
  228. end
  229. assert_equal 1, attachments.map(&:diskfile).uniq.size
  230. end
  231. def test_filename_should_be_basenamed
  232. a = Attachment.new(:file => mock_file(:original_filename => "path/to/the/file"))
  233. assert_equal 'file', a.filename
  234. end
  235. def test_filename_should_be_sanitized
  236. a = Attachment.new(:file => mock_file(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
  237. assert_equal 'valid_[] invalid_chars', a.filename
  238. end
  239. def test_create_diskfile
  240. path = nil
  241. Attachment.create_diskfile("test_file.txt") do |f|
  242. path = f.path
  243. assert_match(/^\d{12}_test_file.txt$/, File.basename(path))
  244. assert_equal 'test_file.txt', File.basename(path)[13..-1]
  245. end
  246. File.unlink path
  247. Attachment.create_diskfile("test_accentué.txt") do |f|
  248. path = f.path
  249. assert_equal '770c509475505f37c2b8fb6030434d6b.txt', File.basename(f.path)[13..-1]
  250. end
  251. File.unlink path
  252. Attachment.create_diskfile("test_accentué") do |f|
  253. path = f.path
  254. assert_equal 'f8139524ebb8f32e51976982cd20a85d', File.basename(f.path)[13..-1]
  255. end
  256. File.unlink path
  257. Attachment.create_diskfile("test_accentué.ça") do |f|
  258. path = f.path
  259. assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', File.basename(f.path)[13..-1]
  260. end
  261. File.unlink path
  262. end
  263. def test_title
  264. a = Attachment.new(:filename => "test.png")
  265. assert_equal "test.png", a.title
  266. a = Attachment.new(:filename => "test.png", :description => "Cool image")
  267. assert_equal "test.png (Cool image)", a.title
  268. assert_equal "test.png", a.filename
  269. end
  270. def test_new_attachment_should_be_editable_by_author
  271. user = User.find(1)
  272. a = Attachment.new(:author => user)
  273. assert_equal true, a.editable?(user)
  274. end
  275. def test_prune_should_destroy_old_unattached_attachments
  276. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  277. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  278. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  279. assert_difference 'Attachment.count', -2 do
  280. Attachment.prune
  281. end
  282. end
  283. def test_archive_attachments
  284. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  285. zip_data = Attachment.archive_attachments([attachment])
  286. file_names = []
  287. Zip::InputStream.open(StringIO.new(zip_data)) do |io|
  288. while (entry = io.get_next_entry)
  289. file_names << entry.name
  290. end
  291. end
  292. assert_equal ['testfile.txt'], file_names
  293. end
  294. def test_archive_attachments_without_attachments
  295. zip_data = Attachment.archive_attachments([])
  296. assert_nil zip_data
  297. end
  298. def test_archive_attachments_should_rename_duplicate_file_names
  299. attachment1 = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  300. attachment2 = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  301. zip_data = Attachment.archive_attachments([attachment1, attachment2])
  302. file_names = []
  303. Zip::InputStream.open(StringIO.new(zip_data)) do |io|
  304. while (entry = io.get_next_entry)
  305. file_names << entry.name
  306. end
  307. end
  308. assert_equal ['testfile.txt', 'testfile(1).txt'], file_names
  309. end
  310. def test_move_from_root_to_target_directory_should_move_root_files
  311. a = Attachment.find(20)
  312. assert a.disk_directory.blank?
  313. # Create a real file for this fixture
  314. File.write(a.diskfile, 'test file at the root of files directory')
  315. assert a.readable?
  316. Attachment.move_from_root_to_target_directory
  317. a.reload
  318. assert_equal '2012/05', a.disk_directory
  319. assert a.readable?
  320. end
  321. test "Attachmnet.attach_files should attach the file" do
  322. issue = Issue.first
  323. assert_difference 'Attachment.count' do
  324. Attachment.attach_files(
  325. issue,
  326. '1' => {
  327. 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
  328. 'description' => 'test'
  329. })
  330. end
  331. attachment = Attachment.order('id DESC').first
  332. assert_equal issue, attachment.container
  333. assert_equal 'testfile.txt', attachment.filename
  334. assert_equal 59, attachment.filesize
  335. assert_equal 'test', attachment.description
  336. assert_equal 'text/plain', attachment.content_type
  337. assert File.exist?(attachment.diskfile)
  338. assert_equal 59, File.size(attachment.diskfile)
  339. end
  340. test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do
  341. # Max size of 0 to force Attachment creation failures
  342. with_settings(:attachment_max_size => 0) do
  343. @project = Project.find(1)
  344. response = Attachment.attach_files(@project, {
  345. '1' => {'file' => mock_file, 'description' => 'test'},
  346. '2' => {'file' => mock_file, 'description' => 'test'}
  347. })
  348. assert response[:unsaved].present?
  349. assert_equal 2, response[:unsaved].length
  350. assert response[:unsaved].first.new_record?
  351. assert response[:unsaved].second.new_record?
  352. assert_equal response[:unsaved], @project.unsaved_attachments
  353. end
  354. end
  355. test "Attachment.attach_files should preserve the content_type of attachments added by token" do
  356. @project = Project.find(1)
  357. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  358. assert_equal 'text/plain', attachment.content_type
  359. Attachment.attach_files(@project, {'1' => {'token' => attachment.token}})
  360. attachment.reload
  361. assert_equal 'text/plain', attachment.content_type
  362. end
  363. def test_update_digest_to_sha256_should_update_digest
  364. set_fixtures_attachments_directory
  365. attachment = Attachment.find 6
  366. assert attachment.readable?
  367. attachment.update_digest_to_sha256!
  368. assert_equal 'ac5c6e99a21ae74b2e3f5b8e5b568be1b9107cd7153d139e822b9fe5caf50938', attachment.digest
  369. ensure
  370. set_tmp_attachments_directory
  371. end
  372. def test_update_attachments
  373. attachments = Attachment.where(:id => [2, 3]).to_a
  374. assert(
  375. Attachment.update_attachments(
  376. attachments,
  377. {
  378. '2' => {:filename => 'newname.txt', :description => 'New description'},
  379. 3 => {:filename => 'othername.txt'}
  380. }
  381. )
  382. )
  383. attachment = Attachment.find(2)
  384. assert_equal 'newname.txt', attachment.filename
  385. assert_equal 'New description', attachment.description
  386. attachment = Attachment.find(3)
  387. assert_equal 'othername.txt', attachment.filename
  388. end
  389. def test_update_attachments_with_failure
  390. attachments = Attachment.where(:id => [2, 3]).to_a
  391. assert(
  392. !Attachment.update_attachments(
  393. attachments,
  394. {
  395. '2' => {
  396. :filename => '', :description => 'New description'
  397. },
  398. 3 => {:filename => 'othername.txt'}
  399. }
  400. )
  401. )
  402. attachment = Attachment.find(3)
  403. assert_equal 'logo.gif', attachment.filename
  404. end
  405. def test_update_attachments_should_sanitize_filename
  406. attachments = Attachment.where(:id => 2).to_a
  407. assert(
  408. Attachment.update_attachments(
  409. attachments,
  410. {2 => {:filename => 'newname?.txt'},}
  411. )
  412. )
  413. attachment = Attachment.find(2)
  414. assert_equal 'newname_.txt', attachment.filename
  415. end
  416. def test_latest_attach
  417. set_fixtures_attachments_directory
  418. a1 = Attachment.find(16)
  419. assert_equal "testfile.png", a1.filename
  420. assert a1.readable?
  421. assert (! a1.visible?(User.anonymous))
  422. assert a1.visible?(User.find(2))
  423. a2 = Attachment.find(17)
  424. assert_equal "testfile.PNG", a2.filename
  425. assert a2.readable?
  426. assert (! a2.visible?(User.anonymous))
  427. assert a2.visible?(User.find(2))
  428. assert a1.created_on < a2.created_on
  429. la1 = Attachment.latest_attach([a1, a2], "testfile.png")
  430. assert_equal 17, la1.id
  431. la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
  432. assert_equal 17, la2.id
  433. ensure
  434. set_tmp_attachments_directory
  435. end
  436. def test_latest_attach_should_not_error_with_string_with_invalid_encoding
  437. string = "width:50\xFE-Image.jpg"
  438. assert_equal false, string.valid_encoding?
  439. Attachment.latest_attach(Attachment.limit(2).to_a, string)
  440. end
  441. def test_latest_attach_should_support_unicode_case_folding
  442. a_capital = Attachment.create!(
  443. :author => User.find(1),
  444. :file => mock_file(:filename => 'Ā.TXT')
  445. )
  446. a_small = Attachment.create!(
  447. :author => User.find(1),
  448. :file => mock_file(:filename => 'ā.txt')
  449. )
  450. assert_equal(a_small, Attachment.latest_attach([a_capital, a_small], 'Ā.TXT'))
  451. end
  452. def test_thumbnailable_should_be_true_for_images
  453. skip unless convert_installed?
  454. assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
  455. assert_equal true, Attachment.new(:filename => 'test.webp').thumbnailable?
  456. end
  457. def test_thumbnailable_should_be_false_for_images_if_convert_is_unavailable
  458. Redmine::Thumbnail.stubs(:convert_available?).returns(false)
  459. assert_equal false, Attachment.new(:filename => 'test.jpg').thumbnailable?
  460. end
  461. def test_thumbnailable_should_be_false_for_non_images
  462. assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
  463. end
  464. if convert_installed?
  465. def test_thumbnail_should_generate_the_thumbnail
  466. set_fixtures_attachments_directory
  467. Attachment.clear_thumbnails
  468. to_test = []
  469. # image/png
  470. to_test << Attachment.find(16)
  471. # application/pdf
  472. if Redmine::Thumbnail.gs_available?
  473. to_test << Attachment.find(23)
  474. else
  475. puts '(Ghostscript not available)'
  476. end
  477. assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size", to_test.size do
  478. to_test.each do |attachment|
  479. thumbnail = attachment.thumbnail
  480. thumbnail_name = "#{attachment.digest}_#{attachment.filesize}_#{Setting.thumbnails_size}.thumb"
  481. assert_equal thumbnail_name, File.basename(thumbnail)
  482. assert File.exist?(thumbnail)
  483. end
  484. end
  485. ensure
  486. set_tmp_attachments_directory
  487. end
  488. def test_should_reuse_thumbnail
  489. Attachment.clear_thumbnails
  490. a = Attachment.create!(
  491. :container => Issue.find(1),
  492. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  493. :author => User.find(1)
  494. )
  495. a_thumb = b_thumb = nil
  496. assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
  497. a_thumb = a.thumbnail
  498. end
  499. b = Attachment.create!(
  500. :container => Issue.find(2),
  501. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  502. :author => User.find(1)
  503. )
  504. assert_no_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
  505. b_thumb = b.thumbnail
  506. end
  507. assert_equal a_thumb, b_thumb
  508. end
  509. def test_destroy_should_destroy_thumbnails
  510. a = Attachment.create!(
  511. :container => Issue.find(1),
  512. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  513. :author => User.find(1)
  514. )
  515. diskfile = a.diskfile
  516. thumbnail = a.thumbnail
  517. assert File.exist?(diskfile)
  518. assert File.exist?(thumbnail)
  519. assert a.destroy
  520. refute File.exist?(diskfile)
  521. refute File.exist?(thumbnail)
  522. end
  523. def test_thumbnail_should_return_nil_if_generation_fails
  524. Redmine::Thumbnail.expects(:generate).raises(SystemCallError, 'Something went wrong')
  525. set_fixtures_attachments_directory
  526. attachment = Attachment.find(16)
  527. assert_nil attachment.thumbnail
  528. ensure
  529. set_tmp_attachments_directory
  530. end
  531. def test_thumbnail_should_be_at_least_of_requested_size
  532. set_fixtures_attachments_directory
  533. attachment = Attachment.find(16)
  534. Attachment.clear_thumbnails
  535. [
  536. [0, 100],
  537. [49, 50],
  538. [50, 50],
  539. [51, 100],
  540. [100, 100],
  541. [101, 150],
  542. ].each do |size, generated_size|
  543. thumbnail = attachment.thumbnail(size: size)
  544. assert_equal(
  545. "8e0294de2441577c529f170b6fb8f638_2654_#{generated_size}.thumb",
  546. File.basename(thumbnail))
  547. end
  548. ensure
  549. set_tmp_attachments_directory
  550. end
  551. else
  552. puts '(ImageMagick convert not available)'
  553. end
  554. def test_is_text
  555. js_attachment = Attachment.new(
  556. :container => Issue.find(1),
  557. :file => uploaded_test_file('hello.js', 'application/javascript'),
  558. :author => User.find(1))
  559. to_test = {
  560. js_attachment => true, # hello.js (application/javascript)
  561. attachments(:attachments_003) => false, # logo.gif (image/gif)
  562. attachments(:attachments_004) => true, # source.rb (application/x-ruby)
  563. attachments(:attachments_015) => true, # private.diff (text/x-diff)
  564. attachments(:attachments_016) => false, # testfile.png (image/png)
  565. }
  566. to_test.each do |attachment, expected|
  567. assert_equal expected, attachment.is_text?, attachment.inspect
  568. end
  569. end
  570. end