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

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