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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2013 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, :projects, :roles, :members, :member_roles,
  22. :enabled_modules, :issues, :trackers, :attachments
  23. class MockFile
  24. attr_reader :original_filename, :content_type, :content, :size
  25. def initialize(attributes)
  26. @original_filename = attributes[:original_filename]
  27. @content_type = attributes[:content_type]
  28. @content = attributes[:content] || "Content"
  29. @size = content.size
  30. end
  31. end
  32. def setup
  33. set_tmp_attachments_directory
  34. end
  35. def test_container_for_new_attachment_should_be_nil
  36. assert_nil Attachment.new.container
  37. end
  38. def test_create
  39. a = Attachment.new(:container => Issue.find(1),
  40. :file => uploaded_test_file("testfile.txt", "text/plain"),
  41. :author => User.find(1))
  42. assert a.save
  43. assert_equal 'testfile.txt', a.filename
  44. assert_equal 59, a.filesize
  45. assert_equal 'text/plain', a.content_type
  46. assert_equal 0, a.downloads
  47. assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
  48. assert a.disk_directory
  49. assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory
  50. assert File.exist?(a.diskfile)
  51. assert_equal 59, File.size(a.diskfile)
  52. end
  53. def test_copy_should_preserve_attributes
  54. a = Attachment.find(1)
  55. copy = a.copy
  56. assert_save copy
  57. copy = Attachment.order('id DESC').first
  58. %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute|
  59. assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different"
  60. end
  61. end
  62. def test_size_should_be_validated_for_new_file
  63. with_settings :attachment_max_size => 0 do
  64. a = Attachment.new(:container => Issue.find(1),
  65. :file => uploaded_test_file("testfile.txt", "text/plain"),
  66. :author => User.find(1))
  67. assert !a.save
  68. end
  69. end
  70. def test_size_should_not_be_validated_when_copying
  71. a = Attachment.create!(:container => Issue.find(1),
  72. :file => uploaded_test_file("testfile.txt", "text/plain"),
  73. :author => User.find(1))
  74. with_settings :attachment_max_size => 0 do
  75. copy = a.copy
  76. assert copy.save
  77. end
  78. end
  79. def test_description_length_should_be_validated
  80. a = Attachment.new(:description => 'a' * 300)
  81. assert !a.save
  82. assert_not_equal [], a.errors[:description]
  83. end
  84. def test_destroy
  85. a = Attachment.new(:container => Issue.find(1),
  86. :file => uploaded_test_file("testfile.txt", "text/plain"),
  87. :author => User.find(1))
  88. assert a.save
  89. assert_equal 'testfile.txt', a.filename
  90. assert_equal 59, a.filesize
  91. assert_equal 'text/plain', a.content_type
  92. assert_equal 0, a.downloads
  93. assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
  94. diskfile = a.diskfile
  95. assert File.exist?(diskfile)
  96. assert_equal 59, File.size(a.diskfile)
  97. assert a.destroy
  98. assert !File.exist?(diskfile)
  99. end
  100. def test_destroy_should_not_delete_file_referenced_by_other_attachment
  101. a = Attachment.create!(:container => Issue.find(1),
  102. :file => uploaded_test_file("testfile.txt", "text/plain"),
  103. :author => User.find(1))
  104. diskfile = a.diskfile
  105. copy = a.copy
  106. copy.save!
  107. assert File.exists?(diskfile)
  108. a.destroy
  109. assert File.exists?(diskfile)
  110. copy.destroy
  111. assert !File.exists?(diskfile)
  112. end
  113. def test_create_should_auto_assign_content_type
  114. a = Attachment.new(:container => Issue.find(1),
  115. :file => uploaded_test_file("testfile.txt", ""),
  116. :author => User.find(1))
  117. assert a.save
  118. assert_equal 'text/plain', a.content_type
  119. end
  120. def test_identical_attachments_at_the_same_time_should_not_overwrite
  121. a1 = Attachment.create!(:container => Issue.find(1),
  122. :file => uploaded_test_file("testfile.txt", ""),
  123. :author => User.find(1))
  124. a2 = Attachment.create!(:container => Issue.find(1),
  125. :file => uploaded_test_file("testfile.txt", ""),
  126. :author => User.find(1))
  127. assert a1.disk_filename != a2.disk_filename
  128. end
  129. def test_filename_should_be_basenamed
  130. a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
  131. assert_equal 'file', a.filename
  132. end
  133. def test_filename_should_be_sanitized
  134. a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
  135. assert_equal 'valid_[] invalid_chars', a.filename
  136. end
  137. def test_diskfilename
  138. assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
  139. assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
  140. assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
  141. assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
  142. assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
  143. end
  144. def test_title
  145. a = Attachment.new(:filename => "test.png")
  146. assert_equal "test.png", a.title
  147. a = Attachment.new(:filename => "test.png", :description => "Cool image")
  148. assert_equal "test.png (Cool image)", a.title
  149. end
  150. def test_prune_should_destroy_old_unattached_attachments
  151. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  152. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  153. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  154. assert_difference 'Attachment.count', -2 do
  155. Attachment.prune
  156. end
  157. end
  158. def test_move_from_root_to_target_directory_should_move_root_files
  159. a = Attachment.find(20)
  160. assert a.disk_directory.blank?
  161. # Create a real file for this fixture
  162. File.open(a.diskfile, "w") do |f|
  163. f.write "test file at the root of files directory"
  164. end
  165. assert a.readable?
  166. Attachment.move_from_root_to_target_directory
  167. a.reload
  168. assert_equal '2012/05', a.disk_directory
  169. assert a.readable?
  170. end
  171. test "Attachmnet.attach_files should attach the file" do
  172. issue = Issue.first
  173. assert_difference 'Attachment.count' do
  174. Attachment.attach_files(issue,
  175. '1' => {
  176. 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
  177. 'description' => 'test'
  178. })
  179. end
  180. attachment = Attachment.first(:order => 'id DESC')
  181. assert_equal issue, attachment.container
  182. assert_equal 'testfile.txt', attachment.filename
  183. assert_equal 59, attachment.filesize
  184. assert_equal 'test', attachment.description
  185. assert_equal 'text/plain', attachment.content_type
  186. assert File.exists?(attachment.diskfile)
  187. assert_equal 59, File.size(attachment.diskfile)
  188. end
  189. test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do
  190. # Max size of 0 to force Attachment creation failures
  191. with_settings(:attachment_max_size => 0) do
  192. @project = Project.find(1)
  193. response = Attachment.attach_files(@project, {
  194. '1' => {'file' => mock_file, 'description' => 'test'},
  195. '2' => {'file' => mock_file, 'description' => 'test'}
  196. })
  197. assert response[:unsaved].present?
  198. assert_equal 2, response[:unsaved].length
  199. assert response[:unsaved].first.new_record?
  200. assert response[:unsaved].second.new_record?
  201. assert_equal response[:unsaved], @project.unsaved_attachments
  202. end
  203. end
  204. def test_latest_attach
  205. set_fixtures_attachments_directory
  206. a1 = Attachment.find(16)
  207. assert_equal "testfile.png", a1.filename
  208. assert a1.readable?
  209. assert (! a1.visible?(User.anonymous))
  210. assert a1.visible?(User.find(2))
  211. a2 = Attachment.find(17)
  212. assert_equal "testfile.PNG", a2.filename
  213. assert a2.readable?
  214. assert (! a2.visible?(User.anonymous))
  215. assert a2.visible?(User.find(2))
  216. assert a1.created_on < a2.created_on
  217. la1 = Attachment.latest_attach([a1, a2], "testfile.png")
  218. assert_equal 17, la1.id
  219. la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
  220. assert_equal 17, la2.id
  221. set_tmp_attachments_directory
  222. end
  223. def test_thumbnailable_should_be_true_for_images
  224. assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
  225. end
  226. def test_thumbnailable_should_be_true_for_non_images
  227. assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
  228. end
  229. if convert_installed?
  230. def test_thumbnail_should_generate_the_thumbnail
  231. set_fixtures_attachments_directory
  232. attachment = Attachment.find(16)
  233. Attachment.clear_thumbnails
  234. assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
  235. thumbnail = attachment.thumbnail
  236. assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
  237. assert File.exists?(thumbnail)
  238. end
  239. end
  240. else
  241. puts '(ImageMagick convert not available)'
  242. end
  243. end