forked from thoughtbot/paperclip
/
uploaded_file_adapter_test.rb
98 lines (81 loc) · 2.7 KB
/
uploaded_file_adapter_test.rb
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
require './test/helper'
class UploadedFileAdapterTest < Test::Unit::TestCase
context "a new instance" do
context "with UploadedFile responding to #tempfile" do
setup do
class UploadedFile < OpenStruct; end
tempfile = File.new(fixture_file("5k.png"))
tempfile.binmode
@file = UploadedFile.new(
:original_filename => "5k.png",
:content_type => "image/png",
:head => "",
:tempfile => tempfile
)
@subject = Paperclip.io_adapters.for(@file)
end
should "get the right filename" do
assert_equal "5k.png", @subject.original_filename
end
should "force binmode on tempfile" do
assert @subject.instance_variable_get("@tempfile").binmode?
end
should "get the content type" do
assert_equal "image/png", @subject.content_type
end
should "get the file's size" do
assert_equal 4456, @subject.size
end
should "return false for a call to nil?" do
assert ! @subject.nil?
end
should "generate a MD5 hash of the contents" do
expected = Digest::MD5.file(@file.tempfile.path).to_s
assert_equal expected, @subject.fingerprint
end
should "read the contents of the file" do
expected = @file.tempfile.read
assert expected.length > 0
assert_equal expected, @subject.read
end
end
context "with UploadFile responding to #path" do
setup do
class UploadedFile < OpenStruct; end
@file = UploadedFile.new(
:original_filename => "5k.png",
:content_type => "image/png",
:head => "",
:path => fixture_file("5k.png")
)
@subject = Paperclip.io_adapters.for(@file)
end
should "get the right filename" do
assert_equal "5k.png", @subject.original_filename
end
should "force binmode on tempfile" do
assert @subject.instance_variable_get("@tempfile").binmode?
end
should "get the content type" do
assert_equal "image/png", @subject.content_type
end
should "get the file's size" do
assert_equal 4456, @subject.size
end
should "return false for a call to nil?" do
assert ! @subject.nil?
end
should "generate a MD5 hash of the contents" do
expected = Digest::MD5.file(@file.path).to_s
assert_equal expected, @subject.fingerprint
end
should "read the contents of the file" do
expected_file = File.new(@file.path)
expected_file.binmode
expected = expected_file.read
assert expected.length > 0
assert_equal expected, @subject.read
end
end
end
end