uploaded_file_test.rb
12.6 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
require File.dirname(__FILE__) + '/../test_helper'
class UploadedFileTest < Test::Unit::TestCase
  def setup
    @profile = create_user('testinguser').person
  end
  attr_reader :profile
  should 'return a thumbnail as icon for images ' do
    f = UploadedFile.new
    f.expects(:image?).returns(true)
    f.expects(:public_filename).with(:icon).returns('/path/to/file.xyz')
    assert_equal '/path/to/file.xyz', UploadedFile.icon_name(f)
  end
  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 '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)
    stubs(:content_tag).returns('link')
    expects(:link_to).with(file.name, file.url, :class => file.css_class_name)
    instance_eval(&file.to_html)
  end
  should 'have title' do
    assert_equal 'my title', UploadedFile.new(:title => 'my title').title
  end
  should 'limit title to 140 characters' do
    upload = UploadedFile.new
    upload.title = '+' * 61; upload.valid?
    assert upload.errors[:title]
    upload.title = '+' * 60; upload.valid?
    assert !upload.errors[: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.display_title
    upload.title = 'My text file'
    assert_equal 'My text file', upload.display_title
    upload.title = ''
    assert_equal 'test.txt', upload.display_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 'return the default thumbnail image as icon for images ' do
    f = UploadedFile.new
    f.expects(:image?).returns(true)
    f.expects(:public_filename).with(:icon).returns('/path/to/file.xyz')
    assert_equal '/path/to/file.xyz', UploadedFile.icon_name(f)
  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(:thumb).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(:thumb).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 'survive when try to get icon_name from a file with mime_type nil' do
    f = UploadedFile.new
    f.expects(:mime_type).returns(nil)
    assert_equal 'upload-file', UploadedFile.icon_name(f)
  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 '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
end