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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 File.expand_path('../../test_helper', __FILE__)
  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.exists?(diskfile)
  181. a.destroy
  182. assert File.exists?(diskfile)
  183. copy.destroy
  184. assert !File.exists?(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.open(a.diskfile, "w") do |f|
  315. f.write "test file at the root of files directory"
  316. end
  317. assert a.readable?
  318. Attachment.move_from_root_to_target_directory
  319. a.reload
  320. assert_equal '2012/05', a.disk_directory
  321. assert a.readable?
  322. end
  323. test "Attachmnet.attach_files should attach the file" do
  324. issue = Issue.first
  325. assert_difference 'Attachment.count' do
  326. Attachment.attach_files(
  327. issue,
  328. '1' => {
  329. 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
  330. 'description' => 'test'
  331. })
  332. end
  333. attachment = Attachment.order('id DESC').first
  334. assert_equal issue, attachment.container
  335. assert_equal 'testfile.txt', attachment.filename
  336. assert_equal 59, attachment.filesize
  337. assert_equal 'test', attachment.description
  338. assert_equal 'text/plain', attachment.content_type
  339. assert File.exists?(attachment.diskfile)
  340. assert_equal 59, File.size(attachment.diskfile)
  341. end
  342. test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do
  343. # Max size of 0 to force Attachment creation failures
  344. with_settings(:attachment_max_size => 0) do
  345. @project = Project.find(1)
  346. response = Attachment.attach_files(@project, {
  347. '1' => {'file' => mock_file, 'description' => 'test'},
  348. '2' => {'file' => mock_file, 'description' => 'test'}
  349. })
  350. assert response[:unsaved].present?
  351. assert_equal 2, response[:unsaved].length
  352. assert response[:unsaved].first.new_record?
  353. assert response[:unsaved].second.new_record?
  354. assert_equal response[:unsaved], @project.unsaved_attachments
  355. end
  356. end
  357. test "Attachment.attach_files should preserve the content_type of attachments added by token" do
  358. @project = Project.find(1)
  359. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  360. assert_equal 'text/plain', attachment.content_type
  361. Attachment.attach_files(@project, {'1' => {'token' => attachment.token}})
  362. attachment.reload
  363. assert_equal 'text/plain', attachment.content_type
  364. end
  365. def test_update_digest_to_sha256_should_update_digest
  366. set_fixtures_attachments_directory
  367. attachment = Attachment.find 6
  368. assert attachment.readable?
  369. attachment.update_digest_to_sha256!
  370. assert_equal 'ac5c6e99a21ae74b2e3f5b8e5b568be1b9107cd7153d139e822b9fe5caf50938', attachment.digest
  371. ensure
  372. set_tmp_attachments_directory
  373. end
  374. def test_update_attachments
  375. attachments = Attachment.where(:id => [2, 3]).to_a
  376. assert(
  377. Attachment.update_attachments(
  378. attachments,
  379. {
  380. '2' => {:filename => 'newname.txt', :description => 'New description'},
  381. 3 => {:filename => 'othername.txt'}
  382. }
  383. )
  384. )
  385. attachment = Attachment.find(2)
  386. assert_equal 'newname.txt', attachment.filename
  387. assert_equal 'New description', attachment.description
  388. attachment = Attachment.find(3)
  389. assert_equal 'othername.txt', attachment.filename
  390. end
  391. def test_update_attachments_with_failure
  392. attachments = Attachment.where(:id => [2, 3]).to_a
  393. assert(
  394. !Attachment.update_attachments(
  395. attachments,
  396. {
  397. '2' => {
  398. :filename => '', :description => 'New description'
  399. },
  400. 3 => {:filename => 'othername.txt'}
  401. }
  402. )
  403. )
  404. attachment = Attachment.find(3)
  405. assert_equal 'logo.gif', attachment.filename
  406. end
  407. def test_update_attachments_should_sanitize_filename
  408. attachments = Attachment.where(:id => 2).to_a
  409. assert(
  410. Attachment.update_attachments(
  411. attachments,
  412. {2 => {:filename => 'newname?.txt'},}
  413. )
  414. )
  415. attachment = Attachment.find(2)
  416. assert_equal 'newname_.txt', attachment.filename
  417. end
  418. def test_latest_attach
  419. set_fixtures_attachments_directory
  420. a1 = Attachment.find(16)
  421. assert_equal "testfile.png", a1.filename
  422. assert a1.readable?
  423. assert (! a1.visible?(User.anonymous))
  424. assert a1.visible?(User.find(2))
  425. a2 = Attachment.find(17)
  426. assert_equal "testfile.PNG", a2.filename
  427. assert a2.readable?
  428. assert (! a2.visible?(User.anonymous))
  429. assert a2.visible?(User.find(2))
  430. assert a1.created_on < a2.created_on
  431. la1 = Attachment.latest_attach([a1, a2], "testfile.png")
  432. assert_equal 17, la1.id
  433. la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
  434. assert_equal 17, la2.id
  435. ensure
  436. set_tmp_attachments_directory
  437. end
  438. def test_latest_attach_should_not_error_with_string_with_invalid_encoding
  439. string = "width:50\xFE-Image.jpg"
  440. assert_equal false, string.valid_encoding?
  441. Attachment.latest_attach(Attachment.limit(2).to_a, string)
  442. end
  443. def test_latest_attach_should_support_unicode_case_folding
  444. a_capital = Attachment.create!(
  445. :author => User.find(1),
  446. :file => mock_file(:filename => 'Ā.TXT')
  447. )
  448. a_small = Attachment.create!(
  449. :author => User.find(1),
  450. :file => mock_file(:filename => 'ā.txt')
  451. )
  452. assert_equal(a_small, Attachment.latest_attach([a_capital, a_small], 'Ā.TXT'))
  453. end
  454. def test_thumbnailable_should_be_true_for_images
  455. skip unless convert_installed?
  456. assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
  457. end
  458. def test_thumbnailable_should_be_false_for_images_if_convert_is_unavailable
  459. Redmine::Thumbnail.stubs(:convert_available?).returns(false)
  460. assert_equal false, Attachment.new(:filename => 'test.jpg').thumbnailable?
  461. end
  462. def test_thumbnailable_should_be_false_for_non_images
  463. assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
  464. end
  465. if convert_installed?
  466. def test_thumbnail_should_generate_the_thumbnail
  467. set_fixtures_attachments_directory
  468. Attachment.clear_thumbnails
  469. to_test = []
  470. # image/png
  471. to_test << Attachment.find(16)
  472. # application/pdf
  473. if Redmine::Thumbnail.gs_available?
  474. to_test << Attachment.find(23)
  475. else
  476. puts '(Ghostscript not available)'
  477. end
  478. assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size", to_test.size do
  479. to_test.each do |attachment|
  480. thumbnail = attachment.thumbnail
  481. thumbnail_name = "#{attachment.digest}_#{attachment.filesize}_#{Setting.thumbnails_size}.thumb"
  482. assert_equal thumbnail_name, File.basename(thumbnail)
  483. assert File.exist?(thumbnail)
  484. end
  485. end
  486. ensure
  487. set_tmp_attachments_directory
  488. end
  489. def test_should_reuse_thumbnail
  490. Attachment.clear_thumbnails
  491. a = Attachment.create!(
  492. :container => Issue.find(1),
  493. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  494. :author => User.find(1)
  495. )
  496. a_thumb = b_thumb = nil
  497. assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
  498. a_thumb = a.thumbnail
  499. end
  500. b = Attachment.create!(
  501. :container => Issue.find(2),
  502. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  503. :author => User.find(1)
  504. )
  505. assert_no_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
  506. b_thumb = b.thumbnail
  507. end
  508. assert_equal a_thumb, b_thumb
  509. end
  510. def test_destroy_should_destroy_thumbnails
  511. a = Attachment.create!(
  512. :container => Issue.find(1),
  513. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  514. :author => User.find(1)
  515. )
  516. diskfile = a.diskfile
  517. thumbnail = a.thumbnail
  518. assert File.exist?(diskfile)
  519. assert File.exist?(thumbnail)
  520. assert a.destroy
  521. refute File.exist?(diskfile)
  522. refute File.exist?(thumbnail)
  523. end
  524. def test_thumbnail_should_return_nil_if_generation_fails
  525. Redmine::Thumbnail.expects(:generate).raises(SystemCallError, 'Something went wrong')
  526. set_fixtures_attachments_directory
  527. attachment = Attachment.find(16)
  528. assert_nil attachment.thumbnail
  529. ensure
  530. set_tmp_attachments_directory
  531. end
  532. def test_thumbnail_should_be_at_least_of_requested_size
  533. set_fixtures_attachments_directory
  534. attachment = Attachment.find(16)
  535. Attachment.clear_thumbnails
  536. [
  537. [0, 100],
  538. [49, 50],
  539. [50, 50],
  540. [51, 100],
  541. [100, 100],
  542. [101, 150],
  543. ].each do |size, generated_size|
  544. thumbnail = attachment.thumbnail(size: size)
  545. assert_equal(
  546. "8e0294de2441577c529f170b6fb8f638_2654_#{generated_size}.thumb",
  547. File.basename(thumbnail))
  548. end
  549. ensure
  550. set_tmp_attachments_directory
  551. end
  552. else
  553. puts '(ImageMagick convert not available)'
  554. end
  555. def test_is_text
  556. js_attachment = Attachment.new(
  557. :container => Issue.find(1),
  558. :file => uploaded_test_file('hello.js', 'application/javascript'),
  559. :author => User.find(1))
  560. to_test = {
  561. js_attachment => true, # hello.js (application/javascript)
  562. attachments(:attachments_003) => false, # logo.gif (image/gif)
  563. attachments(:attachments_004) => true, # source.rb (application/x-ruby)
  564. attachments(:attachments_015) => true, # private.diff (text/x-diff)
  565. attachments(:attachments_016) => false, # testfile.png (image/png)
  566. }
  567. to_test.each do |attachment, expected|
  568. assert_equal expected, attachment.is_text?, attachment.inspect
  569. end
  570. end
  571. end