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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2011 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 File.exist?(a.diskfile)
  49. assert_equal 59, File.size(a.diskfile)
  50. end
  51. def test_size_should_be_validated_for_new_file
  52. with_settings :attachment_max_size => 0 do
  53. a = Attachment.new(:container => Issue.find(1),
  54. :file => uploaded_test_file("testfile.txt", "text/plain"),
  55. :author => User.find(1))
  56. assert !a.save
  57. end
  58. end
  59. def test_size_should_not_be_validated_when_copying
  60. a = Attachment.create!(:container => Issue.find(1),
  61. :file => uploaded_test_file("testfile.txt", "text/plain"),
  62. :author => User.find(1))
  63. with_settings :attachment_max_size => 0 do
  64. copy = a.copy
  65. assert copy.save
  66. end
  67. end
  68. def test_destroy
  69. a = Attachment.new(:container => Issue.find(1),
  70. :file => uploaded_test_file("testfile.txt", "text/plain"),
  71. :author => User.find(1))
  72. assert a.save
  73. assert_equal 'testfile.txt', a.filename
  74. assert_equal 59, a.filesize
  75. assert_equal 'text/plain', a.content_type
  76. assert_equal 0, a.downloads
  77. assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
  78. diskfile = a.diskfile
  79. assert File.exist?(diskfile)
  80. assert_equal 59, File.size(a.diskfile)
  81. assert a.destroy
  82. assert !File.exist?(diskfile)
  83. end
  84. def test_destroy_should_not_delete_file_referenced_by_other_attachment
  85. a = Attachment.create!(:container => Issue.find(1),
  86. :file => uploaded_test_file("testfile.txt", "text/plain"),
  87. :author => User.find(1))
  88. diskfile = a.diskfile
  89. copy = a.copy
  90. copy.save!
  91. assert File.exists?(diskfile)
  92. a.destroy
  93. assert File.exists?(diskfile)
  94. copy.destroy
  95. assert !File.exists?(diskfile)
  96. end
  97. def test_create_should_auto_assign_content_type
  98. a = Attachment.new(:container => Issue.find(1),
  99. :file => uploaded_test_file("testfile.txt", ""),
  100. :author => User.find(1))
  101. assert a.save
  102. assert_equal 'text/plain', a.content_type
  103. end
  104. def test_identical_attachments_at_the_same_time_should_not_overwrite
  105. a1 = Attachment.create!(:container => Issue.find(1),
  106. :file => uploaded_test_file("testfile.txt", ""),
  107. :author => User.find(1))
  108. a2 = Attachment.create!(:container => Issue.find(1),
  109. :file => uploaded_test_file("testfile.txt", ""),
  110. :author => User.find(1))
  111. assert a1.disk_filename != a2.disk_filename
  112. end
  113. def test_filename_should_be_basenamed
  114. a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
  115. assert_equal 'file', a.filename
  116. end
  117. def test_filename_should_be_sanitized
  118. a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
  119. assert_equal 'valid_[] invalid_chars', a.filename
  120. end
  121. def test_diskfilename
  122. assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
  123. assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
  124. assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
  125. assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
  126. assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
  127. end
  128. def test_prune_should_destroy_old_unattached_attachments
  129. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  130. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
  131. Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
  132. assert_difference 'Attachment.count', -2 do
  133. Attachment.prune
  134. end
  135. end
  136. context "Attachmnet.attach_files" do
  137. should "attach the file" do
  138. issue = Issue.first
  139. assert_difference 'Attachment.count' do
  140. Attachment.attach_files(issue,
  141. '1' => {
  142. 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
  143. 'description' => 'test'
  144. })
  145. end
  146. attachment = Attachment.first(:order => 'id DESC')
  147. assert_equal issue, attachment.container
  148. assert_equal 'testfile.txt', attachment.filename
  149. assert_equal 59, attachment.filesize
  150. assert_equal 'test', attachment.description
  151. assert_equal 'text/plain', attachment.content_type
  152. assert File.exists?(attachment.diskfile)
  153. assert_equal 59, File.size(attachment.diskfile)
  154. end
  155. should "add unsaved files to the object as unsaved attachments" do
  156. # Max size of 0 to force Attachment creation failures
  157. with_settings(:attachment_max_size => 0) do
  158. @project = Project.find(1)
  159. response = Attachment.attach_files(@project, {
  160. '1' => {'file' => mock_file, 'description' => 'test'},
  161. '2' => {'file' => mock_file, 'description' => 'test'}
  162. })
  163. assert response[:unsaved].present?
  164. assert_equal 2, response[:unsaved].length
  165. assert response[:unsaved].first.new_record?
  166. assert response[:unsaved].second.new_record?
  167. assert_equal response[:unsaved], @project.unsaved_attachments
  168. end
  169. end
  170. end
  171. def test_latest_attach
  172. set_fixtures_attachments_directory
  173. a1 = Attachment.find(16)
  174. assert_equal "testfile.png", a1.filename
  175. assert a1.readable?
  176. assert (! a1.visible?(User.anonymous))
  177. assert a1.visible?(User.find(2))
  178. a2 = Attachment.find(17)
  179. assert_equal "testfile.PNG", a2.filename
  180. assert a2.readable?
  181. assert (! a2.visible?(User.anonymous))
  182. assert a2.visible?(User.find(2))
  183. assert a1.created_on < a2.created_on
  184. la1 = Attachment.latest_attach([a1, a2], "testfile.png")
  185. assert_equal 17, la1.id
  186. la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
  187. assert_equal 17, la2.id
  188. set_tmp_attachments_directory
  189. end
  190. end