Chris@0
|
1 # encoding: utf-8
|
Chris@0
|
2 #
|
Chris@441
|
3 # Redmine - project management software
|
Chris@1115
|
4 # Copyright (C) 2006-2012 Jean-Philippe Lang
|
Chris@0
|
5 #
|
Chris@0
|
6 # This program is free software; you can redistribute it and/or
|
Chris@0
|
7 # modify it under the terms of the GNU General Public License
|
Chris@0
|
8 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
9 # of the License, or (at your option) any later version.
|
Chris@441
|
10 #
|
Chris@0
|
11 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
14 # GNU General Public License for more details.
|
Chris@441
|
15 #
|
Chris@0
|
16 # You should have received a copy of the GNU General Public License
|
Chris@0
|
17 # along with this program; if not, write to the Free Software
|
Chris@0
|
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
19
|
Chris@119
|
20 require File.expand_path('../../test_helper', __FILE__)
|
Chris@0
|
21
|
Chris@0
|
22 class AttachmentTest < ActiveSupport::TestCase
|
Chris@909
|
23 fixtures :users, :projects, :roles, :members, :member_roles,
|
Chris@909
|
24 :enabled_modules, :issues, :trackers, :attachments
|
Chris@909
|
25
|
Chris@909
|
26 class MockFile
|
Chris@909
|
27 attr_reader :original_filename, :content_type, :content, :size
|
Chris@909
|
28
|
Chris@909
|
29 def initialize(attributes)
|
Chris@909
|
30 @original_filename = attributes[:original_filename]
|
Chris@909
|
31 @content_type = attributes[:content_type]
|
Chris@909
|
32 @content = attributes[:content] || "Content"
|
Chris@909
|
33 @size = content.size
|
Chris@909
|
34 end
|
Chris@909
|
35 end
|
Chris@441
|
36
|
Chris@0
|
37 def setup
|
Chris@909
|
38 set_tmp_attachments_directory
|
Chris@0
|
39 end
|
Chris@0
|
40
|
Chris@1115
|
41 def test_container_for_new_attachment_should_be_nil
|
Chris@1115
|
42 assert_nil Attachment.new.container
|
Chris@1115
|
43 end
|
Chris@1115
|
44
|
Chris@0
|
45 def test_create
|
Chris@0
|
46 a = Attachment.new(:container => Issue.find(1),
|
Chris@0
|
47 :file => uploaded_test_file("testfile.txt", "text/plain"),
|
Chris@0
|
48 :author => User.find(1))
|
Chris@0
|
49 assert a.save
|
Chris@0
|
50 assert_equal 'testfile.txt', a.filename
|
Chris@0
|
51 assert_equal 59, a.filesize
|
Chris@0
|
52 assert_equal 'text/plain', a.content_type
|
Chris@0
|
53 assert_equal 0, a.downloads
|
Chris@119
|
54 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
|
Chris@0
|
55 assert File.exist?(a.diskfile)
|
Chris@909
|
56 assert_equal 59, File.size(a.diskfile)
|
Chris@909
|
57 end
|
Chris@909
|
58
|
Chris@1115
|
59 def test_size_should_be_validated_for_new_file
|
Chris@1115
|
60 with_settings :attachment_max_size => 0 do
|
Chris@1115
|
61 a = Attachment.new(:container => Issue.find(1),
|
Chris@1115
|
62 :file => uploaded_test_file("testfile.txt", "text/plain"),
|
Chris@1115
|
63 :author => User.find(1))
|
Chris@1115
|
64 assert !a.save
|
Chris@1115
|
65 end
|
Chris@1115
|
66 end
|
Chris@1115
|
67
|
Chris@1115
|
68 def test_size_should_not_be_validated_when_copying
|
Chris@1115
|
69 a = Attachment.create!(:container => Issue.find(1),
|
Chris@1115
|
70 :file => uploaded_test_file("testfile.txt", "text/plain"),
|
Chris@1115
|
71 :author => User.find(1))
|
Chris@1115
|
72 with_settings :attachment_max_size => 0 do
|
Chris@1115
|
73 copy = a.copy
|
Chris@1115
|
74 assert copy.save
|
Chris@1115
|
75 end
|
Chris@1115
|
76 end
|
Chris@1115
|
77
|
Chris@1115
|
78 def test_description_length_should_be_validated
|
Chris@1115
|
79 a = Attachment.new(:description => 'a' * 300)
|
Chris@1115
|
80 assert !a.save
|
Chris@1115
|
81 assert_not_nil a.errors[:description]
|
Chris@1115
|
82 end
|
Chris@1115
|
83
|
Chris@909
|
84 def test_destroy
|
Chris@909
|
85 a = Attachment.new(:container => Issue.find(1),
|
Chris@909
|
86 :file => uploaded_test_file("testfile.txt", "text/plain"),
|
Chris@909
|
87 :author => User.find(1))
|
Chris@909
|
88 assert a.save
|
Chris@909
|
89 assert_equal 'testfile.txt', a.filename
|
Chris@909
|
90 assert_equal 59, a.filesize
|
Chris@909
|
91 assert_equal 'text/plain', a.content_type
|
Chris@909
|
92 assert_equal 0, a.downloads
|
Chris@909
|
93 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
|
Chris@909
|
94 diskfile = a.diskfile
|
Chris@909
|
95 assert File.exist?(diskfile)
|
Chris@909
|
96 assert_equal 59, File.size(a.diskfile)
|
Chris@909
|
97 assert a.destroy
|
Chris@909
|
98 assert !File.exist?(diskfile)
|
Chris@0
|
99 end
|
Chris@441
|
100
|
Chris@1115
|
101 def test_destroy_should_not_delete_file_referenced_by_other_attachment
|
Chris@1115
|
102 a = Attachment.create!(:container => Issue.find(1),
|
Chris@1115
|
103 :file => uploaded_test_file("testfile.txt", "text/plain"),
|
Chris@1115
|
104 :author => User.find(1))
|
Chris@1115
|
105 diskfile = a.diskfile
|
Chris@1115
|
106
|
Chris@1115
|
107 copy = a.copy
|
Chris@1115
|
108 copy.save!
|
Chris@1115
|
109
|
Chris@1115
|
110 assert File.exists?(diskfile)
|
Chris@1115
|
111 a.destroy
|
Chris@1115
|
112 assert File.exists?(diskfile)
|
Chris@1115
|
113 copy.destroy
|
Chris@1115
|
114 assert !File.exists?(diskfile)
|
Chris@1115
|
115 end
|
Chris@1115
|
116
|
Chris@0
|
117 def test_create_should_auto_assign_content_type
|
Chris@0
|
118 a = Attachment.new(:container => Issue.find(1),
|
Chris@0
|
119 :file => uploaded_test_file("testfile.txt", ""),
|
Chris@0
|
120 :author => User.find(1))
|
Chris@0
|
121 assert a.save
|
Chris@0
|
122 assert_equal 'text/plain', a.content_type
|
Chris@0
|
123 end
|
Chris@441
|
124
|
Chris@0
|
125 def test_identical_attachments_at_the_same_time_should_not_overwrite
|
Chris@0
|
126 a1 = Attachment.create!(:container => Issue.find(1),
|
Chris@0
|
127 :file => uploaded_test_file("testfile.txt", ""),
|
Chris@0
|
128 :author => User.find(1))
|
Chris@0
|
129 a2 = Attachment.create!(:container => Issue.find(1),
|
Chris@0
|
130 :file => uploaded_test_file("testfile.txt", ""),
|
Chris@0
|
131 :author => User.find(1))
|
Chris@0
|
132 assert a1.disk_filename != a2.disk_filename
|
Chris@0
|
133 end
|
Chris@909
|
134
|
Chris@909
|
135 def test_filename_should_be_basenamed
|
Chris@909
|
136 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
|
Chris@909
|
137 assert_equal 'file', a.filename
|
Chris@909
|
138 end
|
Chris@909
|
139
|
Chris@909
|
140 def test_filename_should_be_sanitized
|
Chris@909
|
141 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
|
Chris@909
|
142 assert_equal 'valid_[] invalid_chars', a.filename
|
Chris@909
|
143 end
|
Chris@441
|
144
|
Chris@0
|
145 def test_diskfilename
|
Chris@0
|
146 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
|
Chris@0
|
147 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
|
Chris@0
|
148 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
|
Chris@0
|
149 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
|
Chris@0
|
150 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
|
Chris@0
|
151 end
|
Chris@0
|
152
|
Chris@1115
|
153 def test_title
|
Chris@1115
|
154 a = Attachment.new(:filename => "test.png")
|
Chris@1115
|
155 assert_equal "test.png", a.title
|
Chris@1115
|
156
|
Chris@1115
|
157 a = Attachment.new(:filename => "test.png", :description => "Cool image")
|
Chris@1115
|
158 assert_equal "test.png (Cool image)", a.title
|
Chris@1115
|
159 end
|
Chris@1115
|
160
|
Chris@1115
|
161 def test_prune_should_destroy_old_unattached_attachments
|
Chris@1115
|
162 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
|
Chris@1115
|
163 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
|
Chris@1115
|
164 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
|
Chris@1115
|
165
|
Chris@1115
|
166 assert_difference 'Attachment.count', -2 do
|
Chris@1115
|
167 Attachment.prune
|
Chris@1115
|
168 end
|
Chris@1115
|
169 end
|
Chris@1115
|
170
|
Chris@909
|
171 context "Attachmnet.attach_files" do
|
Chris@909
|
172 should "attach the file" do
|
Chris@909
|
173 issue = Issue.first
|
Chris@909
|
174 assert_difference 'Attachment.count' do
|
Chris@909
|
175 Attachment.attach_files(issue,
|
Chris@909
|
176 '1' => {
|
Chris@909
|
177 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
|
Chris@909
|
178 'description' => 'test'
|
Chris@909
|
179 })
|
Chris@909
|
180 end
|
Chris@909
|
181
|
Chris@909
|
182 attachment = Attachment.first(:order => 'id DESC')
|
Chris@909
|
183 assert_equal issue, attachment.container
|
Chris@909
|
184 assert_equal 'testfile.txt', attachment.filename
|
Chris@909
|
185 assert_equal 59, attachment.filesize
|
Chris@909
|
186 assert_equal 'test', attachment.description
|
Chris@909
|
187 assert_equal 'text/plain', attachment.content_type
|
Chris@909
|
188 assert File.exists?(attachment.diskfile)
|
Chris@909
|
189 assert_equal 59, File.size(attachment.diskfile)
|
Chris@909
|
190 end
|
Chris@909
|
191
|
Chris@0
|
192 should "add unsaved files to the object as unsaved attachments" do
|
Chris@0
|
193 # Max size of 0 to force Attachment creation failures
|
Chris@0
|
194 with_settings(:attachment_max_size => 0) do
|
Chris@1115
|
195 @project = Project.find(1)
|
Chris@0
|
196 response = Attachment.attach_files(@project, {
|
Chris@0
|
197 '1' => {'file' => mock_file, 'description' => 'test'},
|
Chris@0
|
198 '2' => {'file' => mock_file, 'description' => 'test'}
|
Chris@0
|
199 })
|
Chris@0
|
200
|
Chris@0
|
201 assert response[:unsaved].present?
|
Chris@0
|
202 assert_equal 2, response[:unsaved].length
|
Chris@0
|
203 assert response[:unsaved].first.new_record?
|
Chris@0
|
204 assert response[:unsaved].second.new_record?
|
Chris@0
|
205 assert_equal response[:unsaved], @project.unsaved_attachments
|
Chris@0
|
206 end
|
Chris@0
|
207 end
|
Chris@0
|
208 end
|
Chris@909
|
209
|
Chris@909
|
210 def test_latest_attach
|
Chris@1115
|
211 set_fixtures_attachments_directory
|
Chris@909
|
212 a1 = Attachment.find(16)
|
Chris@909
|
213 assert_equal "testfile.png", a1.filename
|
Chris@909
|
214 assert a1.readable?
|
Chris@909
|
215 assert (! a1.visible?(User.anonymous))
|
Chris@909
|
216 assert a1.visible?(User.find(2))
|
Chris@909
|
217 a2 = Attachment.find(17)
|
Chris@909
|
218 assert_equal "testfile.PNG", a2.filename
|
Chris@909
|
219 assert a2.readable?
|
Chris@909
|
220 assert (! a2.visible?(User.anonymous))
|
Chris@909
|
221 assert a2.visible?(User.find(2))
|
Chris@909
|
222 assert a1.created_on < a2.created_on
|
Chris@909
|
223
|
Chris@909
|
224 la1 = Attachment.latest_attach([a1, a2], "testfile.png")
|
Chris@909
|
225 assert_equal 17, la1.id
|
Chris@909
|
226 la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
|
Chris@909
|
227 assert_equal 17, la2.id
|
Chris@909
|
228
|
Chris@909
|
229 set_tmp_attachments_directory
|
Chris@909
|
230 end
|
Chris@1115
|
231
|
Chris@1115
|
232 def test_thumbnailable_should_be_true_for_images
|
Chris@1115
|
233 assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
|
Chris@1115
|
234 end
|
Chris@1115
|
235
|
Chris@1115
|
236 def test_thumbnailable_should_be_true_for_non_images
|
Chris@1115
|
237 assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
|
Chris@1115
|
238 end
|
Chris@1115
|
239
|
Chris@1115
|
240 if convert_installed?
|
Chris@1115
|
241 def test_thumbnail_should_generate_the_thumbnail
|
Chris@1115
|
242 set_fixtures_attachments_directory
|
Chris@1115
|
243 attachment = Attachment.find(16)
|
Chris@1115
|
244 Attachment.clear_thumbnails
|
Chris@1115
|
245
|
Chris@1115
|
246 assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
|
Chris@1115
|
247 thumbnail = attachment.thumbnail
|
Chris@1115
|
248 assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
|
Chris@1115
|
249 assert File.exists?(thumbnail)
|
Chris@1115
|
250 end
|
Chris@1115
|
251 end
|
Chris@1115
|
252 else
|
Chris@1115
|
253 puts '(ImageMagick convert not available)'
|
Chris@1115
|
254 end
|
Chris@0
|
255 end
|