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