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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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_diskfilename
  240. assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
  241. assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
  242. assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
  243. assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
  244. assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
  245. end
  246. def test_title
  247. a = Attachment.new(:filename => "test.png")
  248. assert_equal "test.png", a.title
  249. a = Attachment.new(:filename => "test.png", :description => "Cool image")
  250. assert_equal "test.png (Cool image)", a.title
  251. assert_equal "test.png", a.filename
  252. end
  253. def test_new_attachment_should_be_editable_by_author
  254. user = User.find(1)
  255. a = Attachment.new(:author => user)
  256. assert_equal true, a.editable?(user)
  257. end
  258. def test_prune_should_destroy_old_unattached_attachments
  259. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  260. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  261. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  262. assert_difference 'Attachment.count', -2 do
  263. Attachment.prune
  264. end
  265. end
  266. def test_archive_attachments
  267. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  268. zip_data = Attachment.archive_attachments([attachment])
  269. file_names = []
  270. Zip::InputStream.open(StringIO.new(zip_data)) do |io|
  271. while (entry = io.get_next_entry)
  272. file_names << entry.name
  273. end
  274. end
  275. assert_equal ['testfile.txt'], file_names
  276. end
  277. def test_archive_attachments_without_attachments
  278. zip_data = Attachment.archive_attachments([])
  279. assert_nil zip_data
  280. end
  281. def test_archive_attachments_should_rename_duplicate_file_names
  282. attachment1 = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  283. attachment2 = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  284. zip_data = Attachment.archive_attachments([attachment1, attachment2])
  285. file_names = []
  286. Zip::InputStream.open(StringIO.new(zip_data)) do |io|
  287. while (entry = io.get_next_entry)
  288. file_names << entry.name
  289. end
  290. end
  291. assert_equal ['testfile.txt', 'testfile(1).txt'], file_names
  292. end
  293. def test_move_from_root_to_target_directory_should_move_root_files
  294. a = Attachment.find(20)
  295. assert a.disk_directory.blank?
  296. # Create a real file for this fixture
  297. File.open(a.diskfile, "w") do |f|
  298. f.write "test file at the root of files directory"
  299. end
  300. assert a.readable?
  301. Attachment.move_from_root_to_target_directory
  302. a.reload
  303. assert_equal '2012/05', a.disk_directory
  304. assert a.readable?
  305. end
  306. test "Attachmnet.attach_files should attach the file" do
  307. issue = Issue.first
  308. assert_difference 'Attachment.count' do
  309. Attachment.attach_files(
  310. issue,
  311. '1' => {
  312. 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
  313. 'description' => 'test'
  314. })
  315. end
  316. attachment = Attachment.order('id DESC').first
  317. assert_equal issue, attachment.container
  318. assert_equal 'testfile.txt', attachment.filename
  319. assert_equal 59, attachment.filesize
  320. assert_equal 'test', attachment.description
  321. assert_equal 'text/plain', attachment.content_type
  322. assert File.exists?(attachment.diskfile)
  323. assert_equal 59, File.size(attachment.diskfile)
  324. end
  325. test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do
  326. # Max size of 0 to force Attachment creation failures
  327. with_settings(:attachment_max_size => 0) do
  328. @project = Project.find(1)
  329. response = Attachment.attach_files(@project, {
  330. '1' => {'file' => mock_file, 'description' => 'test'},
  331. '2' => {'file' => mock_file, 'description' => 'test'}
  332. })
  333. assert response[:unsaved].present?
  334. assert_equal 2, response[:unsaved].length
  335. assert response[:unsaved].first.new_record?
  336. assert response[:unsaved].second.new_record?
  337. assert_equal response[:unsaved], @project.unsaved_attachments
  338. end
  339. end
  340. test "Attachment.attach_files should preserve the content_type of attachments added by token" do
  341. @project = Project.find(1)
  342. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  343. assert_equal 'text/plain', attachment.content_type
  344. Attachment.attach_files(@project, {'1' => {'token' => attachment.token}})
  345. attachment.reload
  346. assert_equal 'text/plain', attachment.content_type
  347. end
  348. def test_update_digest_to_sha256_should_update_digest
  349. set_fixtures_attachments_directory
  350. attachment = Attachment.find 6
  351. assert attachment.readable?
  352. attachment.update_digest_to_sha256!
  353. assert_equal 'ac5c6e99a21ae74b2e3f5b8e5b568be1b9107cd7153d139e822b9fe5caf50938', attachment.digest
  354. ensure
  355. set_tmp_attachments_directory
  356. end
  357. def test_update_attachments
  358. attachments = Attachment.where(:id => [2, 3]).to_a
  359. assert(
  360. Attachment.update_attachments(
  361. attachments,
  362. {
  363. '2' => {:filename => 'newname.txt', :description => 'New description'},
  364. 3 => {:filename => 'othername.txt'}
  365. }
  366. )
  367. )
  368. attachment = Attachment.find(2)
  369. assert_equal 'newname.txt', attachment.filename
  370. assert_equal 'New description', attachment.description
  371. attachment = Attachment.find(3)
  372. assert_equal 'othername.txt', attachment.filename
  373. end
  374. def test_update_attachments_with_failure
  375. attachments = Attachment.where(:id => [2, 3]).to_a
  376. assert(
  377. !Attachment.update_attachments(
  378. attachments,
  379. {
  380. '2' => {
  381. :filename => '', :description => 'New description'
  382. },
  383. 3 => {:filename => 'othername.txt'}
  384. }
  385. )
  386. )
  387. attachment = Attachment.find(3)
  388. assert_equal 'logo.gif', attachment.filename
  389. end
  390. def test_update_attachments_should_sanitize_filename
  391. attachments = Attachment.where(:id => 2).to_a
  392. assert(
  393. Attachment.update_attachments(
  394. attachments,
  395. {2 => {:filename => 'newname?.txt'},}
  396. )
  397. )
  398. attachment = Attachment.find(2)
  399. assert_equal 'newname_.txt', attachment.filename
  400. end
  401. def test_latest_attach
  402. set_fixtures_attachments_directory
  403. a1 = Attachment.find(16)
  404. assert_equal "testfile.png", a1.filename
  405. assert a1.readable?
  406. assert (! a1.visible?(User.anonymous))
  407. assert a1.visible?(User.find(2))
  408. a2 = Attachment.find(17)
  409. assert_equal "testfile.PNG", a2.filename
  410. assert a2.readable?
  411. assert (! a2.visible?(User.anonymous))
  412. assert a2.visible?(User.find(2))
  413. assert a1.created_on < a2.created_on
  414. la1 = Attachment.latest_attach([a1, a2], "testfile.png")
  415. assert_equal 17, la1.id
  416. la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
  417. assert_equal 17, la2.id
  418. ensure
  419. set_tmp_attachments_directory
  420. end
  421. def test_latest_attach_should_not_error_with_string_with_invalid_encoding
  422. string = "width:50\xFE-Image.jpg"
  423. assert_equal false, string.valid_encoding?
  424. Attachment.latest_attach(Attachment.limit(2).to_a, string)
  425. end
  426. def test_latest_attach_should_support_unicode_case_folding
  427. a_capital = Attachment.create!(
  428. :author => User.find(1),
  429. :file => mock_file(:filename => 'Ā.TXT')
  430. )
  431. a_small = Attachment.create!(
  432. :author => User.find(1),
  433. :file => mock_file(:filename => 'ā.txt')
  434. )
  435. assert_equal(a_small, Attachment.latest_attach([a_capital, a_small], 'Ā.TXT'))
  436. end
  437. def test_thumbnailable_should_be_true_for_images
  438. skip unless convert_installed?
  439. assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
  440. end
  441. def test_thumbnailable_should_be_false_for_images_if_convert_is_unavailable
  442. Redmine::Thumbnail.stubs(:convert_available?).returns(false)
  443. assert_equal false, Attachment.new(:filename => 'test.jpg').thumbnailable?
  444. end
  445. def test_thumbnailable_should_be_false_for_non_images
  446. assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
  447. end
  448. if convert_installed?
  449. def test_thumbnail_should_generate_the_thumbnail
  450. set_fixtures_attachments_directory
  451. Attachment.clear_thumbnails
  452. to_test = []
  453. # image/png
  454. to_test << Attachment.find(16)
  455. # application/pdf
  456. if Redmine::Thumbnail.gs_available?
  457. to_test << Attachment.find(23)
  458. else
  459. puts '(Ghostscript not available)'
  460. end
  461. assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size", to_test.size do
  462. to_test.each do |attachment|
  463. thumbnail = attachment.thumbnail
  464. thumbnail_name = "#{attachment.digest}_#{attachment.filesize}_#{Setting.thumbnails_size}.thumb"
  465. assert_equal thumbnail_name, File.basename(thumbnail)
  466. assert File.exist?(thumbnail)
  467. end
  468. end
  469. ensure
  470. set_tmp_attachments_directory
  471. end
  472. def test_should_reuse_thumbnail
  473. Attachment.clear_thumbnails
  474. a = Attachment.create!(
  475. :container => Issue.find(1),
  476. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  477. :author => User.find(1)
  478. )
  479. a_thumb = b_thumb = nil
  480. assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
  481. a_thumb = a.thumbnail
  482. end
  483. b = Attachment.create!(
  484. :container => Issue.find(2),
  485. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  486. :author => User.find(1)
  487. )
  488. assert_no_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
  489. b_thumb = b.thumbnail
  490. end
  491. assert_equal a_thumb, b_thumb
  492. end
  493. def test_destroy_should_destroy_thumbnails
  494. a = Attachment.create!(
  495. :container => Issue.find(1),
  496. :file => uploaded_test_file("2010/11/101123161450_testfile_1.png", "image/png"),
  497. :author => User.find(1)
  498. )
  499. diskfile = a.diskfile
  500. thumbnail = a.thumbnail
  501. assert File.exist?(diskfile)
  502. assert File.exist?(thumbnail)
  503. assert a.destroy
  504. refute File.exist?(diskfile)
  505. refute File.exist?(thumbnail)
  506. end
  507. def test_thumbnail_should_return_nil_if_generation_fails
  508. Redmine::Thumbnail.expects(:generate).raises(SystemCallError, 'Something went wrong')
  509. set_fixtures_attachments_directory
  510. attachment = Attachment.find(16)
  511. assert_nil attachment.thumbnail
  512. ensure
  513. set_tmp_attachments_directory
  514. end
  515. def test_thumbnail_should_be_at_least_of_requested_size
  516. set_fixtures_attachments_directory
  517. attachment = Attachment.find(16)
  518. Attachment.clear_thumbnails
  519. [
  520. [0, 100],
  521. [49, 50],
  522. [50, 50],
  523. [51, 100],
  524. [100, 100],
  525. [101, 150],
  526. ].each do |size, generated_size|
  527. thumbnail = attachment.thumbnail(size: size)
  528. assert_equal(
  529. "8e0294de2441577c529f170b6fb8f638_2654_#{generated_size}.thumb",
  530. File.basename(thumbnail))
  531. end
  532. ensure
  533. set_tmp_attachments_directory
  534. end
  535. else
  536. puts '(ImageMagick convert not available)'
  537. end
  538. def test_is_text
  539. js_attachment = Attachment.new(
  540. :container => Issue.find(1),
  541. :file => uploaded_test_file('hello.js', 'application/javascript'),
  542. :author => User.find(1))
  543. to_test = {
  544. js_attachment => true, # hello.js (application/javascript)
  545. attachments(:attachments_003) => false, # logo.gif (image/gif)
  546. attachments(:attachments_004) => true, # source.rb (application/x-ruby)
  547. attachments(:attachments_015) => true, # private.diff (text/x-diff)
  548. attachments(:attachments_016) => false, # testfile.png (image/png)
  549. }
  550. to_test.each do |attachment, expected|
  551. assert_equal expected, attachment.is_text?, attachment.inspect
  552. end
  553. end
  554. end