paperclip 2.1.2 → 2.1.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of paperclip might be problematic. Click here for more details.

@@ -1,230 +0,0 @@
1
- require 'test/helper'
2
-
3
- class Dummy
4
- # This is a dummy class
5
- end
6
-
7
- class AttachmentTest < Test::Unit::TestCase
8
- context "Attachment default_options" do
9
- setup do
10
- rebuild_model
11
- @old_default_options = Paperclip::Attachment.default_options.dup
12
- @new_default_options = @old_default_options.merge({
13
- :path => "argle/bargle",
14
- :url => "fooferon",
15
- :default_url => "not here.png"
16
- })
17
- end
18
-
19
- teardown do
20
- Paperclip::Attachment.default_options.merge! @old_default_options
21
- end
22
-
23
- should "be overrideable" do
24
- Paperclip::Attachment.default_options.merge!(@new_default_options)
25
- @new_default_options.keys.each do |key|
26
- assert_equal @new_default_options[key],
27
- Paperclip::Attachment.default_options[key]
28
- end
29
- end
30
-
31
- context "without an Attachment" do
32
- setup do
33
- @dummy = Dummy.new
34
- end
35
-
36
- should "return false when asked exists?" do
37
- assert !@dummy.avatar.exists?
38
- end
39
- end
40
-
41
- context "on an Attachment" do
42
- setup do
43
- @dummy = Dummy.new
44
- @attachment = @dummy.avatar
45
- end
46
-
47
- Paperclip::Attachment.default_options.keys.each do |key|
48
- should "be the default_options for #{key}" do
49
- assert_equal @old_default_options[key],
50
- @attachment.instance_variable_get("@#{key}"),
51
- key
52
- end
53
- end
54
-
55
- context "when redefined" do
56
- setup do
57
- Paperclip::Attachment.default_options.merge!(@new_default_options)
58
- @dummy = Dummy.new
59
- @attachment = @dummy.avatar
60
- end
61
-
62
- Paperclip::Attachment.default_options.keys.each do |key|
63
- should "be the new default_options for #{key}" do
64
- assert_equal @new_default_options[key],
65
- @attachment.instance_variable_get("@#{key}"),
66
- key
67
- end
68
- end
69
- end
70
- end
71
- end
72
-
73
- context "An attachment with similarly named interpolations" do
74
- setup do
75
- rebuild_model :path => ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
76
- @dummy = Dummy.new
77
- @dummy.stubs(:id).returns(1024)
78
- @file = File.new(File.join(File.dirname(__FILE__),
79
- "fixtures",
80
- "5k.png"))
81
- @dummy.avatar = @file
82
- end
83
-
84
- should "make sure that they are interpolated correctly" do
85
- assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
86
- end
87
- end
88
-
89
- context "An attachment" do
90
- setup do
91
- Paperclip::Attachment.default_options.merge!({
92
- :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
93
- })
94
- FileUtils.rm_rf("tmp")
95
- @instance = stub
96
- @instance.stubs(:id).returns(41)
97
- @instance.stubs(:class).returns(Dummy)
98
- @instance.stubs(:[]).with(:test_file_name).returns(nil)
99
- @instance.stubs(:[]).with(:test_content_type).returns(nil)
100
- @instance.stubs(:[]).with(:test_file_size).returns(nil)
101
- @attachment = Paperclip::Attachment.new(:test,
102
- @instance)
103
- @file = File.new(File.join(File.dirname(__FILE__),
104
- "fixtures",
105
- "5k.png"))
106
- end
107
-
108
- should "return its default_url when no file assigned" do
109
- assert @attachment.to_file.nil?
110
- assert_equal "/tests/original/missing.png", @attachment.url
111
- assert_equal "/tests/blah/missing.png", @attachment.url(:blah)
112
- end
113
-
114
- context "with a file assigned in the database" do
115
- setup do
116
- @instance.stubs(:[]).with(:test_file_name).returns("5k.png")
117
- @instance.stubs(:[]).with(:test_content_type).returns("image/png")
118
- @instance.stubs(:[]).with(:test_file_size).returns(12345)
119
- end
120
-
121
- should "return a correct url even if the file does not exist" do
122
- assert_nil @attachment.to_file
123
- assert_equal "/tests/41/blah/5k.png", @attachment.url(:blah)
124
- end
125
-
126
- should "return the proper path when filename has a single .'s" do
127
- assert_equal "./test/../tmp/tests/dummies/original/41/5k.png", @attachment.path
128
- end
129
-
130
- should "return the proper path when filename has multiple .'s" do
131
- @instance.stubs(:[]).with(:test_file_name).returns("5k.old.png")
132
- assert_equal "./test/../tmp/tests/dummies/original/41/5k.old.png", @attachment.path
133
- end
134
-
135
- context "when expecting three styles" do
136
- setup do
137
- styles = {:styles => { :large => ["400x400", :png],
138
- :medium => ["100x100", :gif],
139
- :small => ["32x32#", :jpg]}}
140
- @attachment = Paperclip::Attachment.new(:test,
141
- @instance,
142
- styles)
143
- end
144
-
145
- context "and assigned a file" do
146
- setup do
147
- @instance.expects(:[]=).with(:test_file_name,
148
- File.basename(@file.path))
149
- @instance.expects(:[]=).with(:test_content_type, "image/png")
150
- @instance.expects(:[]=).with(:test_file_size, @file.size)
151
- @instance.expects(:[]=).with(:test_file_name, nil)
152
- @instance.expects(:[]=).with(:test_content_type, nil)
153
- @instance.expects(:[]=).with(:test_file_size, nil)
154
- @attachment.assign(@file)
155
- end
156
-
157
- should "be dirty" do
158
- assert @attachment.dirty?
159
- end
160
-
161
- context "and saved" do
162
- setup do
163
- @attachment.save
164
- end
165
-
166
- should "return the real url" do
167
- assert @attachment.to_file
168
- assert_equal "/tests/41/original/5k.png", @attachment.url
169
- assert_equal "/tests/41/small/5k.jpg", @attachment.url(:small)
170
- end
171
-
172
- should "commit the files to disk" do
173
- [:large, :medium, :small].each do |style|
174
- io = @attachment.to_io(style)
175
- assert File.exists?(io)
176
- assert ! io.is_a?(::Tempfile)
177
- end
178
- end
179
-
180
- should "save the files as the right formats and sizes" do
181
- [[:large, 400, 61, "PNG"],
182
- [:medium, 100, 15, "GIF"],
183
- [:small, 32, 32, "JPEG"]].each do |style|
184
- cmd = "identify -format '%w %h %b %m' " +
185
- "#{@attachment.to_io(style.first).path}"
186
- out = `#{cmd}`
187
- width, height, size, format = out.split(" ")
188
- assert_equal style[1].to_s, width.to_s
189
- assert_equal style[2].to_s, height.to_s
190
- assert_equal style[3].to_s, format.to_s
191
- end
192
- end
193
-
194
- should "still have its #file attribute not be nil" do
195
- assert ! @attachment.to_file.nil?
196
- end
197
-
198
- context "and deleted" do
199
- setup do
200
- @existing_names = @attachment.styles.keys.collect do |style|
201
- @attachment.path(style)
202
- end
203
- @instance.expects(:[]=).with(:test_file_name, nil)
204
- @instance.expects(:[]=).with(:test_content_type, nil)
205
- @instance.expects(:[]=).with(:test_file_size, nil)
206
- @attachment.assign nil
207
- @attachment.save
208
- end
209
-
210
- should "delete the files" do
211
- @existing_names.each{|f| assert ! File.exists?(f) }
212
- end
213
- end
214
- end
215
- end
216
- end
217
-
218
- end
219
-
220
- context "when trying a nonexistant storage type" do
221
- setup do
222
- rebuild_model :storage => :not_here
223
- end
224
-
225
- should "not be able to find the module" do
226
- assert_raise(NameError){ Dummy.new.avatar }
227
- end
228
- end
229
- end
230
- end
@@ -1,60 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'stringio'
4
- require 'tempfile'
5
- require 'shoulda'
6
-
7
- require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'iostream.rb')
8
-
9
- class IOStreamTest < Test::Unit::TestCase
10
- context "IOStream" do
11
- should "be included in IO, File, Tempfile, and StringIO" do
12
- [IO, File, Tempfile, StringIO].each do |klass|
13
- assert klass.included_modules.include?(IOStream), "Not in #{klass}"
14
- end
15
- end
16
- end
17
-
18
- context "A file" do
19
- setup do
20
- @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
21
- end
22
-
23
- context "that is sent #stream_to" do
24
-
25
- [["/tmp/iostream.string.test", File],
26
- [Tempfile.new('iostream.test'), Tempfile]].each do |args|
27
-
28
- context "and given a #{args[0].class.to_s}" do
29
- setup do
30
- assert @result = @file.stream_to(args[0])
31
- end
32
-
33
- should "return a #{args[1].to_s}" do
34
- assert @result.is_a?(args[1])
35
- end
36
-
37
- should "contain the same data as the original file" do
38
- @file.rewind; @result.rewind
39
- assert_equal @file.read, @result.read
40
- end
41
- end
42
- end
43
- end
44
-
45
- context "that is sent #to_tempfile" do
46
- setup do
47
- assert @tempfile = @file.to_tempfile
48
- end
49
-
50
- should "convert it to a Tempfile" do
51
- assert @tempfile.is_a?(Tempfile)
52
- end
53
-
54
- should "have the Tempfile contain the same data as the file" do
55
- @file.rewind; @tempfile.rewind
56
- assert_equal @file.read, @tempfile.read
57
- end
58
- end
59
- end
60
- end
@@ -1,123 +0,0 @@
1
- require 'test/helper.rb'
2
-
3
- class PaperclipTest < Test::Unit::TestCase
4
- context "An ActiveRecord model with an 'avatar' attachment" do
5
- setup do
6
- rebuild_model :path => "tmp/:class/omg/:style.:extension"
7
- @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
8
- end
9
-
10
- context "that is attr_protected" do
11
- setup do
12
- Dummy.class_eval do
13
- attr_protected :avatar
14
- end
15
- @dummy = Dummy.new
16
- end
17
-
18
- should "not assign the avatar on mass-set" do
19
- @dummy.logger.expects(:debug)
20
-
21
- @dummy.attributes = { :other => "I'm set!",
22
- :avatar => @file }
23
-
24
- assert_equal "I'm set!", @dummy.other
25
- assert ! @dummy.avatar?
26
- end
27
-
28
- should "still allow assigment on normal set" do
29
- @dummy.logger.expects(:debug).times(0)
30
-
31
- @dummy.other = "I'm set!"
32
- @dummy.avatar = @file
33
-
34
- assert_equal "I'm set!", @dummy.other
35
- assert @dummy.avatar?
36
- end
37
- end
38
-
39
- context "with a subclass" do
40
- setup do
41
- class ::SubDummy < Dummy; end
42
- end
43
-
44
- should "be able to use the attachment from the subclass" do
45
- assert_nothing_raised do
46
- @subdummy = SubDummy.create(:avatar => @file)
47
- end
48
- end
49
-
50
- should "be able to see the attachment definition from the subclass's class" do
51
- assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
52
- end
53
-
54
- teardown do
55
- Object.send(:remove_const, "SubDummy") rescue nil
56
- end
57
- end
58
-
59
- should "have an #avatar method" do
60
- assert Dummy.new.respond_to?(:avatar)
61
- end
62
-
63
- should "have an #avatar= method" do
64
- assert Dummy.new.respond_to?(:avatar=)
65
- end
66
-
67
- [[:presence, nil, "5k.png", nil],
68
- [:size, {:in => 1..10240}, "5k.png", "12k.png"],
69
- [:size2, {:in => 1..10240}, nil, "12k.png"],
70
- [:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
71
- [:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
72
- [:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
73
- [:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
74
- context "with #{args[0]} validations" do
75
- setup do
76
- Dummy.class_eval do
77
- send(*[:"validates_attachment_#{args[0].to_s[/[a-z_]*/]}", :avatar, args[1]].compact)
78
- end
79
- @dummy = Dummy.new
80
- end
81
-
82
- context "and a valid file" do
83
- setup do
84
- @file = args[2] && File.new(File.join(FIXTURES_DIR, args[2]))
85
- end
86
-
87
- should "not have any errors" do
88
- @dummy.avatar = @file
89
- assert @dummy.avatar.valid?
90
- assert_equal 0, @dummy.avatar.errors.length
91
- end
92
- end
93
-
94
- context "and an invalid file" do
95
- setup do
96
- @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
97
- end
98
-
99
- should "have errors" do
100
- @dummy.avatar = @file
101
- assert ! @dummy.avatar.valid?
102
- assert_equal 1, @dummy.avatar.errors.length
103
- end
104
- end
105
-
106
- # context "and an invalid file with :message" do
107
- # setup do
108
- # @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
109
- # end
110
- #
111
- # should "have errors" do
112
- # if args[1] && args[1][:message] && args[4]
113
- # @dummy.avatar = @file
114
- # assert ! @dummy.avatar.valid?
115
- # assert_equal 1, @dummy.avatar.errors.length
116
- # assert_equal args[4], @dummy.avatar.errors[0]
117
- # end
118
- # end
119
- # end
120
- end
121
- end
122
- end
123
- end