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