uploaded_file_test.rb
4.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
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', f.icon_name
end
should 'return mime-type icon for non-image files' do
f= UploadedFile.new
f.expects(:image?).returns(false)
f.expects(:content_type).returns('application/pdf')
assert_equal 'application-pdf', f.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
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 = Folder.create!(:name => 'test_folder', :profile => p)
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent_id => f.id, :profile => p)
assert File.exists?(file.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)
assert File.exists?(file.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)
expects(:link_to).with(file.name, file.url, :class => file.css_class_name)
instance_eval(&file.to_html)
end
end