uploaded_file_test.rb
14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
require File.dirname(__FILE__) + '/../test_helper'
class UploadedFileTest < ActiveSupport::TestCase
def setup
@profile = create_user('testinguser').person
end
attr_reader :profile
should 'return a default icon for uploaded files' do
assert_equal 'upload-file', UploadedFile.icon_name
end
should 'use attachment_fu content_type method to return mime_type' do
f = UploadedFile.new
f.expects(:content_type).returns('application/pdf')
assert_equal 'application/pdf', f.mime_type
end
should 'provide proper description' do
assert_kind_of String, UploadedFile.description
end
should 'provide proper short description' do
assert_kind_of String, UploadedFile.short_description
end
should 'set name from uploaded filename' do
file = UploadedFile.new
file.filename = 'test.txt'
assert_equal 'test.txt', file.name
end
should 'not set filename on name if name is already set' do
file = UploadedFile.new
file.name = "Some name"
file.filename = 'test.txt'
assert_equal 'Some name', file.name
end
should 'provide file content as data' do
file = UploadedFile.new
file.expects(:full_filename).returns('myfilename')
File.expects(:read).with('myfilename').returns('my data')
assert_equal 'my data', file.data
end
should 'not allow child articles' do
assert_equal false, UploadedFile.new.allow_children?
end
should 'properly save images' do
file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
file.profile = profile
assert file.save
assert file.is_image
end
should 'has attachment_fu validation options' do
file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
assert_respond_to file, :attachment_validation_options
end
should 'has attachment_fu validation option for size' do
file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
assert_includes file.attachment_validation_options, :size
end
should 'can display hits' do
file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
assert_equal false, file.can_display_hits?
end
should 'not upload files bigger than max_size' do
f = UploadedFile.new(:profile => @profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
f.expects(:size).returns(UploadedFile.attachment_options[:max_size] + 1024)
assert !f.valid?
end
should 'upload files smaller than max_size' do
f = UploadedFile.new(:profile => @profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
f.expects(:size).returns(UploadedFile.attachment_options[:max_size] - 1024)
assert f.valid?
end
should 'create icon when created in folder' do
p = create_user('test_user').person
f = fast_create(Folder, :name => 'test_folder', :profile_id => p.id)
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent_id => f.id, :profile => p)
process_delayed_job_queue
assert File.exists?(UploadedFile.find(file.id).public_filename(:icon))
file.destroy
end
should 'create icon when not created in folder' do
p = create_user('test_user').person
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => p)
process_delayed_job_queue
assert File.exists?(UploadedFile.find(file.id).public_filename(:icon))
file.destroy
end
should 'match max_size in validates message of size field' do
up = UploadedFile.new(:filename => 'fake_filename.png')
up.valid?
assert_match /#{UploadedFile.max_size.to_humanreadable}/, up.errors[:size]
end
should 'display link to download of non-image files' do
p = create_user('test_user').person
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => p)
ENV.stubs('[]').with('RAILS_ENV').returns('other')
Rails.logger.expects(:warn) # warn about deprecatede usage of UploadedFile#to_html
stubs(:puts)
stubs(:content_tag).returns('link')
expects(:link_to).with(file.name, file.url)
instance_eval(&file.to_html)
end
should 'have title' do
assert_equal 'my title', UploadedFile.new(:title => 'my title').title
end
should 'always provide a display title' do
upload = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'))
assert_equal 'test.txt', upload.title
upload.title = 'My text file'
assert_equal 'My text file', upload.title
upload.title = ''
assert_equal 'test.txt', upload.title
end
should 'use name as title by default' do
upload = UploadedFile.new
upload.stubs(:name).returns('test.txt')
assert_equal 'test.txt', upload.title
end
should 'use name as title by default but cut down the title' do
upload = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/AGENDA_CULTURA_-_FESTA_DE_VAQUEIROS_PONTO_DE_SERRA_PRETA_BAIXA.txt'))
upload.valid?
assert_nil upload.errors[:title]
end
should 'create thumbnails after processing jobs' do
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile)
process_delayed_job_queue
UploadedFile.attachment_options[:thumbnails].each do |suffix, size|
assert File.exists?(UploadedFile.find(file.id).public_filename(suffix))
end
file.destroy
end
should 'set thumbnails_processed to true after creating thumbnails' do
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile)
process_delayed_job_queue
assert UploadedFile.find(file.id).thumbnails_processed
file.destroy
end
should 'have thumbnails_processed attribute' do
assert UploadedFile.new.respond_to?(:thumbnails_processed)
end
should 'return false by default in thumbnails_processed' do
assert !UploadedFile.new.thumbnails_processed
end
should 'set thumbnails_processed to true' do
file = UploadedFile.new
file.thumbnails_processed = true
assert file.thumbnails_processed
end
should 'have a default image if thumbnails were not processed' do
file = UploadedFile.new
file.expects(:thumbnailable?).returns(true)
assert_equal '/images/icons-app/image-loading-thumb.png', file.public_filename
end
should 'return image thumbnail if thumbnails were processed' do
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile)
process_delayed_job_queue
assert_match(/rails_thumb.png/, UploadedFile.find(file.id).public_filename(:thumb))
file.destroy
end
should 'store width and height after processing' do
file = UploadedFile.create!(:profile => @profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
file.create_thumbnails
file = UploadedFile.find(file.id)
assert_equal [50, 64], [file.width, file.height]
end
should 'have a loading image to each size of thumbnails' do
UploadedFile.attachment_options[:thumbnails].each do |suffix, size|
image = RAILS_ROOT + '/public/images/icons-app/image-loading-%s.png' % suffix
assert File.exists?(image)
end
end
should 'return a thumbnail for images' do
f = UploadedFile.new
f.expects(:image?).returns(true)
f.expects(:full_filename).with(:display).returns(File.join(RAILS_ROOT, 'public', 'images', '0000', '0005', 'x.png'))
assert_equal '/images/0000/0005/x.png', f.thumbnail_path
f = UploadedFile.new
f.stubs(:full_filename).with(:display).returns(File.join(RAILS_ROOT, 'public', 'images', '0000', '0005', 'x.png'))
f.expects(:image?).returns(false)
assert_nil f.thumbnail_path
end
should 'track action when a published image is uploaded in a gallery' do
p = fast_create(Gallery, :profile_id => @profile.id)
f = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => p, :profile => @profile)
ta = ActionTracker::Record.last(:conditions => { :verb => "upload_image" })
assert_kind_of String, ta.get_thumbnail_path[0]
assert_equal [f.reload.view_url], ta.get_view_url
assert_equal [p.reload.url], ta.get_parent_url
assert_equal [p.name], ta.get_parent_name
end
should 'not track action when is not image' do
ActionTracker::Record.delete_all
p = fast_create(Gallery, :profile_id => @profile.id)
f = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :parent => p, :profile => @profile)
assert_nil ActionTracker::Record.last(:conditions => { :verb => "upload_image" })
end
should 'not track action when has no parent' do
f = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => nil, :profile => @profile)
assert_nil ActionTracker::Record.last(:conditions => { :verb => "upload_image" })
end
should 'not track action when is not published' do
ActionTracker::Record.delete_all
p = fast_create(Gallery, :profile_id => @profile.id)
f = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => p, :profile => @profile, :published => false)
assert_nil ActionTracker::Record.last(:conditions => { :verb => "upload_image" })
end
should 'not track action when parent is not gallery' do
ActionTracker::Record.delete_all
p = fast_create(Folder, :profile_id => @profile.id)
f = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => p, :profile => @profile)
assert_nil ActionTracker::Record.last(:conditions => { :verb => "upload_image" })
end
should 'not crash if first paragraph called' do
f = fast_create(UploadedFile)
assert_nothing_raised do
f.first_paragraph
end
end
should 'return empty string to lead if no abstract given' do
f = fast_create(UploadedFile, :abstract => nil)
assert_equal '', f.lead
end
should 'upload to a folder with same name as the schema if database is postgresql' do
uses_postgresql 'image_schema_one'
file1 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => @profile)
process_delayed_job_queue
assert_match(/image_schema_one\/\d{4}\/\d{4}\/rails.png/, UploadedFile.find(file1.id).public_filename)
uses_postgresql 'image_schema_two'
file2 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => @profile)
assert_match(/image_schema_two\/\d{4}\/\d{4}\/test.txt/, UploadedFile.find(file2.id).public_filename)
file1.destroy
file2.destroy
uses_sqlite
end
should 'return extension' do
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => @profile)
assert_equal 'png', file.extension
end
should 'upload to path prefix folder if database is not postgresql' do
uses_sqlite
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => @profile)
assert_match(/\/\d{4}\/\d{4}\/test.txt/, UploadedFile.find(file.id).public_filename)
assert_no_match(/test_schema\/\d{4}\/\d{4}\/test.txt/, UploadedFile.find(file.id).public_filename)
file.destroy
end
should 'upload thumbnails to a folder with same name as the schema if database is postgresql' do
uses_postgresql
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => @profile)
process_delayed_job_queue
UploadedFile.attachment_options[:thumbnails].each do |suffix, size|
assert_match(/test_schema\/\d{4}\/\d{4}\/rails_#{suffix}.png/, UploadedFile.find(file.id).public_filename(suffix))
end
file.destroy
uses_sqlite
end
should 'not allow script files to be uploaded without append .txt in the end' do
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('files/hello_world.php', 'application/x-php'), :profile => @profile)
assert_equal 'hello_world.php.txt', file.filename
end
should 'use gallery as target for action tracker' do
gallery = fast_create(Gallery, :profile_id => profile.id)
image = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => gallery, :profile => profile)
activity = ActionTracker::Record.find_last_by_verb 'upload_image'
assert_equal gallery, activity.target
end
should 'group trackers activity of image\'s upload' do
ActionTracker::Record.delete_all
ActionTracker::Record.stubs(:current_user_from_model).returns(profile)
gallery = fast_create(Gallery, :profile_id => profile.id)
image1 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => gallery, :profile => profile)
assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count
image2 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'), :parent => gallery, :profile => profile)
assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count
end
{
nil => 5.megabytes, # default
'1KB' => 1.kilobytes,
'2MB' => 2.megabyte,
'3GB' => 3.gigabytes,
'4TB' => 4.terabytes,
'6 MB' => 6.megabytes, # allow whitespace between number and unit
'0.5 GB' => 512.megabytes, # allow floating point numbers
'2' => 2.megabytes, # assume MB as unit by default
'INVALID' => 5.megabytes, # use default for invalid input
'1ZYX' => 5.megabytes, # use default for invalid input
}.each do |input,output|
test 'maximum upload size: convert %s into %s' % [input, output] do
NOOSFERO_CONF.expects(:[]).with('max_upload_size').returns(input)
assert_equal output, UploadedFile.max_size
end
end
test 'max_size should always return an integer' do
NOOSFERO_CONF.expects(:[]).with('max_upload_size').returns("0.5 GB")
assert_instance_of Fixnum, UploadedFile.max_size
end
end