thoughtbot-paperclip 2.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+
Binary file
Binary file
Binary file
@@ -0,0 +1 @@
1
+ This is not an image.
File without changes
@@ -0,0 +1,168 @@
1
+ require 'test/helper'
2
+
3
+ class GeometryTest < Test::Unit::TestCase
4
+ context "Paperclip::Geometry" do
5
+ should "correctly report its given dimensions" do
6
+ assert @geo = Paperclip::Geometry.new(1024, 768)
7
+ assert_equal 1024, @geo.width
8
+ assert_equal 768, @geo.height
9
+ end
10
+
11
+ should "set height to 0 if height dimension is missing" do
12
+ assert @geo = Paperclip::Geometry.new(1024)
13
+ assert_equal 1024, @geo.width
14
+ assert_equal 0, @geo.height
15
+ end
16
+
17
+ should "set width to 0 if width dimension is missing" do
18
+ assert @geo = Paperclip::Geometry.new(nil, 768)
19
+ assert_equal 0, @geo.width
20
+ assert_equal 768, @geo.height
21
+ end
22
+
23
+ should "be generated from a WxH-formatted string" do
24
+ assert @geo = Paperclip::Geometry.parse("800x600")
25
+ assert_equal 800, @geo.width
26
+ assert_equal 600, @geo.height
27
+ end
28
+
29
+ should "be generated from a xH-formatted string" do
30
+ assert @geo = Paperclip::Geometry.parse("x600")
31
+ assert_equal 0, @geo.width
32
+ assert_equal 600, @geo.height
33
+ end
34
+
35
+ should "be generated from a Wx-formatted string" do
36
+ assert @geo = Paperclip::Geometry.parse("800x")
37
+ assert_equal 800, @geo.width
38
+ assert_equal 0, @geo.height
39
+ end
40
+
41
+ should "be generated from a W-formatted string" do
42
+ assert @geo = Paperclip::Geometry.parse("800")
43
+ assert_equal 800, @geo.width
44
+ assert_equal 0, @geo.height
45
+ end
46
+
47
+ should "ensure the modifier is nil if not present" do
48
+ assert @geo = Paperclip::Geometry.parse("123x456")
49
+ assert_nil @geo.modifier
50
+ end
51
+
52
+ ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
53
+ should "ensure the modifier #{mod.inspect} is preserved" do
54
+ assert @geo = Paperclip::Geometry.parse("123x456#{mod}")
55
+ assert_equal mod, @geo.modifier
56
+ assert_equal "123x456#{mod}", @geo.to_s
57
+ end
58
+ end
59
+
60
+ ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
61
+ should "ensure the modifier #{mod.inspect} is preserved with no height" do
62
+ assert @geo = Paperclip::Geometry.parse("123x#{mod}")
63
+ assert_equal mod, @geo.modifier
64
+ assert_equal "123#{mod}", @geo.to_s
65
+ end
66
+ end
67
+
68
+ should "make sure the modifier gets passed during transformation_to" do
69
+ assert @src = Paperclip::Geometry.parse("123x456")
70
+ assert @dst = Paperclip::Geometry.parse("123x456>")
71
+ assert_equal "123x456>", @src.transformation_to(@dst).to_s
72
+ end
73
+
74
+ should "generate correct ImageMagick formatting string for W-formatted string" do
75
+ assert @geo = Paperclip::Geometry.parse("800")
76
+ assert_equal "800", @geo.to_s
77
+ end
78
+
79
+ should "generate correct ImageMagick formatting string for Wx-formatted string" do
80
+ assert @geo = Paperclip::Geometry.parse("800x")
81
+ assert_equal "800", @geo.to_s
82
+ end
83
+
84
+ should "generate correct ImageMagick formatting string for xH-formatted string" do
85
+ assert @geo = Paperclip::Geometry.parse("x600")
86
+ assert_equal "x600", @geo.to_s
87
+ end
88
+
89
+ should "generate correct ImageMagick formatting string for WxH-formatted string" do
90
+ assert @geo = Paperclip::Geometry.parse("800x600")
91
+ assert_equal "800x600", @geo.to_s
92
+ end
93
+
94
+ should "be generated from a file" do
95
+ file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
96
+ file = File.new(file, 'rb')
97
+ assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
98
+ assert @geo.height > 0
99
+ assert @geo.width > 0
100
+ end
101
+
102
+ should "be generated from a file path" do
103
+ file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
104
+ assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
105
+ assert @geo.height > 0
106
+ assert @geo.width > 0
107
+ end
108
+
109
+ should "not generate from a bad file" do
110
+ file = "/home/This File Does Not Exist.omg"
111
+ assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
112
+ end
113
+
114
+ [['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
115
+ ['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333],
116
+ ['square', 100, 100, false, false, true, 100, 100, 1]].each do |args|
117
+ context "performing calculations on a #{args[0]} viewport" do
118
+ setup do
119
+ @geo = Paperclip::Geometry.new(args[1], args[2])
120
+ end
121
+
122
+ should "#{args[3] ? "" : "not"} be vertical" do
123
+ assert_equal args[3], @geo.vertical?
124
+ end
125
+
126
+ should "#{args[4] ? "" : "not"} be horizontal" do
127
+ assert_equal args[4], @geo.horizontal?
128
+ end
129
+
130
+ should "#{args[5] ? "" : "not"} be square" do
131
+ assert_equal args[5], @geo.square?
132
+ end
133
+
134
+ should "report that #{args[6]} is the larger dimension" do
135
+ assert_equal args[6], @geo.larger
136
+ end
137
+
138
+ should "report that #{args[7]} is the smaller dimension" do
139
+ assert_equal args[7], @geo.smaller
140
+ end
141
+
142
+ should "have an aspect ratio of #{args[8]}" do
143
+ assert_in_delta args[8], @geo.aspect, 0.0001
144
+ end
145
+ end
146
+ end
147
+
148
+ [[ [1000, 100], [64, 64], "x64", "64x64+288+0" ],
149
+ [ [100, 1000], [50, 950], "x950", "50x950+22+0" ],
150
+ [ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args|
151
+ context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do
152
+ setup do
153
+ @geo = Paperclip::Geometry.new(*args[0])
154
+ @dst = Paperclip::Geometry.new(*args[1])
155
+ @scale, @crop = @geo.transformation_to @dst, true
156
+ end
157
+
158
+ should "be able to return the correct scaling transformation geometry #{args[2]}" do
159
+ assert_equal args[2], @scale
160
+ end
161
+
162
+ should "be able to return the correct crop transformation geometry #{args[3]}" do
163
+ assert_equal args[3], @crop
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'tempfile'
6
+
7
+ require 'active_record'
8
+ require 'active_support'
9
+ begin
10
+ require 'ruby-debug'
11
+ rescue LoadError
12
+ puts "ruby-debug not loaded"
13
+ end
14
+
15
+ ROOT = File.join(File.dirname(__FILE__), '..')
16
+ RAILS_ROOT = ROOT
17
+
18
+ $LOAD_PATH << File.join(ROOT, 'lib')
19
+ $LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
20
+
21
+ require File.join(ROOT, 'lib', 'paperclip.rb')
22
+
23
+ ENV['RAILS_ENV'] ||= 'test'
24
+
25
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
26
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
27
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
28
+ ActiveRecord::Base.establish_connection(config[ENV['RAILS_ENV'] || 'test'])
29
+
30
+ def rebuild_model options = {}
31
+ ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
32
+ table.column :other, :string
33
+ table.column :avatar_file_name, :string
34
+ table.column :avatar_content_type, :string
35
+ table.column :avatar_file_size, :integer
36
+ table.column :avatar_updated_at, :datetime
37
+ end
38
+ rebuild_class options
39
+ end
40
+
41
+ def rebuild_class options = {}
42
+ ActiveRecord::Base.send(:include, Paperclip)
43
+ Object.send(:remove_const, "Dummy") rescue nil
44
+ Object.const_set("Dummy", Class.new(ActiveRecord::Base))
45
+ Dummy.class_eval do
46
+ include Paperclip
47
+ has_attached_file :avatar, options
48
+ end
49
+ end
50
+
51
+ def temporary_rails_env(new_env)
52
+ old_env = defined?(RAILS_ENV) ? RAILS_ENV : nil
53
+ silence_warnings do
54
+ Object.const_set("RAILS_ENV", new_env)
55
+ end
56
+ yield
57
+ silence_warnings do
58
+ Object.const_set("RAILS_ENV", old_env)
59
+ end
60
+ end
@@ -0,0 +1,409 @@
1
+ require 'test/helper'
2
+
3
+ class IntegrationTest < Test::Unit::TestCase
4
+ context "Many models at once" do
5
+ setup do
6
+ rebuild_model
7
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
8
+ 300.times do |i|
9
+ Dummy.create! :avatar => @file
10
+ end
11
+ end
12
+
13
+ should "not exceed the open file limit" do
14
+ assert_nothing_raised do
15
+ dummies = Dummy.find(:all)
16
+ dummies.each { |dummy| dummy.avatar }
17
+ end
18
+ end
19
+ end
20
+
21
+ context "An attachment" do
22
+ setup do
23
+ rebuild_model :styles => { :thumb => "50x50#" }
24
+ @dummy = Dummy.new
25
+ @file = File.new(File.join(File.dirname(__FILE__),
26
+ "fixtures",
27
+ "5k.png"), 'rb')
28
+ @dummy.avatar = @file
29
+ assert @dummy.save
30
+ end
31
+
32
+ should "create its thumbnails properly" do
33
+ assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
34
+ end
35
+
36
+ context "redefining its attachment styles" do
37
+ setup do
38
+ Dummy.class_eval do
39
+ has_attached_file :avatar, :styles => { :thumb => "150x25#" }
40
+ end
41
+ @d2 = Dummy.find(@dummy.id)
42
+ @d2.avatar.reprocess!
43
+ @d2.save
44
+ end
45
+
46
+ should "create its thumbnails properly" do
47
+ assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
48
+ end
49
+ end
50
+ end
51
+
52
+ context "A model with no attachment validation" do
53
+ setup do
54
+ rebuild_model :styles => { :large => "300x300>",
55
+ :medium => "100x100",
56
+ :thumb => ["32x32#", :gif] },
57
+ :default_style => :medium,
58
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
59
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
60
+ @dummy = Dummy.new
61
+ end
62
+
63
+ should "have its definition return false when asked about whiny_thumbnails" do
64
+ assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
65
+ end
66
+
67
+ context "when validates_attachment_thumbnails is called" do
68
+ setup do
69
+ Dummy.validates_attachment_thumbnails :avatar
70
+ end
71
+
72
+ should "have its definition return true when asked about whiny_thumbnails" do
73
+ assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
74
+ end
75
+ end
76
+
77
+ context "redefined to have attachment validations" do
78
+ setup do
79
+ rebuild_model :styles => { :large => "300x300>",
80
+ :medium => "100x100",
81
+ :thumb => ["32x32#", :gif] },
82
+ :whiny_thumbnails => true,
83
+ :default_style => :medium,
84
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
85
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
86
+ end
87
+
88
+ should "have its definition return true when asked about whiny_thumbnails" do
89
+ assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
90
+ end
91
+ end
92
+ end
93
+
94
+ context "A model with no convert_options setting" do
95
+ setup do
96
+ rebuild_model :styles => { :large => "300x300>",
97
+ :medium => "100x100",
98
+ :thumb => ["32x32#", :gif] },
99
+ :default_style => :medium,
100
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
101
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
102
+ @dummy = Dummy.new
103
+ end
104
+
105
+ should "have its definition return nil when asked about convert_options" do
106
+ assert ! Dummy.attachment_definitions[:avatar][:convert_options]
107
+ end
108
+
109
+ context "redefined to have convert_options setting" do
110
+ setup do
111
+ rebuild_model :styles => { :large => "300x300>",
112
+ :medium => "100x100",
113
+ :thumb => ["32x32#", :gif] },
114
+ :convert_options => "-strip -depth 8",
115
+ :default_style => :medium,
116
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
117
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
118
+ end
119
+
120
+ should "have its definition return convert_options value when asked about convert_options" do
121
+ assert_equal "-strip -depth 8", Dummy.attachment_definitions[:avatar][:convert_options]
122
+ end
123
+ end
124
+ end
125
+
126
+ context "A model with a filesystem attachment" do
127
+ setup do
128
+ rebuild_model :styles => { :large => "300x300>",
129
+ :medium => "100x100",
130
+ :thumb => ["32x32#", :gif] },
131
+ :whiny_thumbnails => true,
132
+ :default_style => :medium,
133
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
134
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
135
+ @dummy = Dummy.new
136
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
137
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb')
138
+
139
+ assert @dummy.avatar = @file
140
+ assert @dummy.valid?
141
+ assert @dummy.save
142
+ end
143
+
144
+ should "write and delete its files" do
145
+ [["434x66", :original],
146
+ ["300x46", :large],
147
+ ["100x15", :medium],
148
+ ["32x32", :thumb]].each do |geo, style|
149
+ cmd = %Q[identify -format "%wx%h" "#{@dummy.avatar.path(style)}"]
150
+ assert_equal geo, `#{cmd}`.chomp, cmd
151
+ end
152
+
153
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) }
154
+
155
+ @d2 = Dummy.find(@dummy.id)
156
+ assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path}"`.chomp
157
+ assert_equal "434x66", `identify -format "%wx%h" "#{@d2.avatar.path(:original)}"`.chomp
158
+ assert_equal "300x46", `identify -format "%wx%h" "#{@d2.avatar.path(:large)}"`.chomp
159
+ assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path(:medium)}"`.chomp
160
+ assert_equal "32x32", `identify -format "%wx%h" "#{@d2.avatar.path(:thumb)}"`.chomp
161
+
162
+ @dummy.avatar = "not a valid file but not nil"
163
+ assert_equal File.basename(@file.path), @dummy.avatar_file_name
164
+ assert @dummy.valid?
165
+ assert @dummy.save
166
+
167
+ saved_paths.each do |p|
168
+ assert File.exists?(p)
169
+ end
170
+
171
+ @dummy.avatar = nil
172
+ assert_nil @dummy.avatar_file_name
173
+ assert @dummy.valid?
174
+ assert @dummy.save
175
+
176
+ saved_paths.each do |p|
177
+ assert ! File.exists?(p)
178
+ end
179
+
180
+ @d2 = Dummy.find(@dummy.id)
181
+ assert_nil @d2.avatar_file_name
182
+ end
183
+
184
+ should "work exactly the same when new as when reloaded" do
185
+ @d2 = Dummy.find(@dummy.id)
186
+
187
+ assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
188
+ [:thumb, :medium, :large, :original].each do |style|
189
+ assert_equal @dummy.avatar.path(style), @d2.avatar.path(style)
190
+ end
191
+
192
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) }
193
+
194
+ @d2.avatar = nil
195
+ assert @d2.save
196
+
197
+ saved_paths.each do |p|
198
+ assert ! File.exists?(p)
199
+ end
200
+ end
201
+
202
+ should "know the difference between good files, bad files, not files, and nil" do
203
+ expected = @dummy.avatar.to_file
204
+ @dummy.avatar = "not a file"
205
+ assert @dummy.valid?
206
+ assert_equal expected.path, @dummy.avatar.path
207
+ expected.close
208
+
209
+ @dummy.avatar = @bad_file
210
+ assert ! @dummy.valid?
211
+ @dummy.avatar = nil
212
+ assert @dummy.valid?
213
+ end
214
+
215
+ should "know the difference between good files, bad files, not files, and nil when validating" do
216
+ Dummy.validates_attachment_presence :avatar
217
+ @d2 = Dummy.find(@dummy.id)
218
+ @d2.avatar = @file
219
+ assert @d2.valid?, @d2.errors.full_messages.inspect
220
+ @d2.avatar = @bad_file
221
+ assert ! @d2.valid?
222
+ @d2.avatar = nil
223
+ assert ! @d2.valid?
224
+ end
225
+
226
+ should "be able to reload without saving and not have the file disappear" do
227
+ @dummy.avatar = @file
228
+ assert @dummy.save
229
+ @dummy.avatar = nil
230
+ assert_nil @dummy.avatar_file_name
231
+ @dummy.reload
232
+ assert_equal "5k.png", @dummy.avatar_file_name
233
+ end
234
+
235
+ context "that is assigned its file from another Paperclip attachment" do
236
+ setup do
237
+ @dummy2 = Dummy.new
238
+ @file2 = File.new(File.join(FIXTURES_DIR, "12k.png"), 'rb')
239
+ assert @dummy2.avatar = @file2
240
+ @dummy2.save
241
+ end
242
+
243
+ should "work when assigned a file" do
244
+ assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`,
245
+ `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"`
246
+
247
+ assert @dummy.avatar = @dummy2.avatar
248
+ @dummy.save
249
+ assert_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`,
250
+ `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"`
251
+ end
252
+
253
+ should "work when assigned a nil file" do
254
+ @dummy2.avatar = nil
255
+ @dummy2.save
256
+
257
+ @dummy.avatar = @dummy2.avatar
258
+ @dummy.save
259
+
260
+ assert !@dummy.avatar?
261
+ end
262
+ end
263
+
264
+ end
265
+
266
+ if ENV['S3_TEST_BUCKET']
267
+ def s3_files_for attachment
268
+ [:thumb, :medium, :large, :original].inject({}) do |files, style|
269
+ data = `curl "#{attachment.url(style)}" 2>/dev/null`.chomp
270
+ t = Tempfile.new("paperclip-test")
271
+ t.binmode
272
+ t.write(data)
273
+ t.rewind
274
+ files[style] = t
275
+ files
276
+ end
277
+ end
278
+
279
+ def s3_headers_for attachment, style
280
+ `curl --head "#{attachment.url(style)}" 2>/dev/null`.split("\n").inject({}) do |h,head|
281
+ split_head = head.chomp.split(/\s*:\s*/, 2)
282
+ h[split_head.first.downcase] = split_head.last unless split_head.empty?
283
+ h
284
+ end
285
+ end
286
+
287
+ context "A model with an S3 attachment" do
288
+ setup do
289
+ rebuild_model :styles => { :large => "300x300>",
290
+ :medium => "100x100",
291
+ :thumb => ["32x32#", :gif] },
292
+ :storage => :s3,
293
+ :whiny_thumbnails => true,
294
+ # :s3_options => {:logger => Logger.new(StringIO.new)},
295
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")),
296
+ :default_style => :medium,
297
+ :bucket => ENV['S3_TEST_BUCKET'],
298
+ :path => ":class/:attachment/:id/:style/:basename.:extension"
299
+ @dummy = Dummy.new
300
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
301
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb')
302
+
303
+ assert @dummy.avatar = @file
304
+ assert @dummy.valid?
305
+ assert @dummy.save
306
+
307
+ @files_on_s3 = s3_files_for @dummy.avatar
308
+ end
309
+
310
+ should "write and delete its files" do
311
+ [["434x66", :original],
312
+ ["300x46", :large],
313
+ ["100x15", :medium],
314
+ ["32x32", :thumb]].each do |geo, style|
315
+ cmd = %Q[identify -format "%wx%h" "#{@files_on_s3[style].path}"]
316
+ assert_equal geo, `#{cmd}`.chomp, cmd
317
+ end
318
+
319
+ @d2 = Dummy.find(@dummy.id)
320
+ @d2_files = s3_files_for @d2.avatar
321
+ [["434x66", :original],
322
+ ["300x46", :large],
323
+ ["100x15", :medium],
324
+ ["32x32", :thumb]].each do |geo, style|
325
+ cmd = %Q[identify -format "%wx%h" "#{@d2_files[style].path}"]
326
+ assert_equal geo, `#{cmd}`.chomp, cmd
327
+ end
328
+
329
+ @dummy.avatar = "not a valid file but not nil"
330
+ assert_equal File.basename(@file.path), @dummy.avatar_file_name
331
+ assert @dummy.valid?
332
+ assert @dummy.save
333
+
334
+ saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
335
+
336
+ saved_keys.each do |key|
337
+ assert key.exists?
338
+ end
339
+
340
+ @dummy.avatar = nil
341
+ assert_nil @dummy.avatar_file_name
342
+ assert @dummy.valid?
343
+ assert @dummy.save
344
+
345
+ saved_keys.each do |key|
346
+ assert ! key.exists?
347
+ end
348
+
349
+ @d2 = Dummy.find(@dummy.id)
350
+ assert_nil @d2.avatar_file_name
351
+ end
352
+
353
+ should "work exactly the same when new as when reloaded" do
354
+ @d2 = Dummy.find(@dummy.id)
355
+
356
+ assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
357
+ [:thumb, :medium, :large, :original].each do |style|
358
+ assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s
359
+ end
360
+
361
+ saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
362
+
363
+ @d2.avatar = nil
364
+ assert @d2.save
365
+
366
+ saved_keys.each do |key|
367
+ assert ! key.exists?
368
+ end
369
+ end
370
+
371
+ should "know the difference between good files, bad files, not files, and nil" do
372
+ expected = @dummy.avatar.to_file
373
+ @dummy.avatar = "not a file"
374
+ assert @dummy.valid?
375
+ assert_equal expected.full_name, @dummy.avatar.to_file.full_name
376
+
377
+ @dummy.avatar = @bad_file
378
+ assert ! @dummy.valid?
379
+ @dummy.avatar = nil
380
+ assert @dummy.valid?
381
+
382
+ Dummy.validates_attachment_presence :avatar
383
+ @d2 = Dummy.find(@dummy.id)
384
+ @d2.avatar = @file
385
+ assert @d2.valid?
386
+ @d2.avatar = @bad_file
387
+ assert ! @d2.valid?
388
+ @d2.avatar = nil
389
+ assert ! @d2.valid?
390
+ end
391
+
392
+ should "be able to reload without saving and not have the file disappear" do
393
+ @dummy.avatar = @file
394
+ assert @dummy.save
395
+ @dummy.avatar = nil
396
+ assert_nil @dummy.avatar_file_name
397
+ @dummy.reload
398
+ assert_equal "5k.png", @dummy.avatar_file_name
399
+ end
400
+
401
+ should "have the right content type" do
402
+ headers = s3_headers_for(@dummy.avatar, :original)
403
+ p headers
404
+ assert_equal 'image/png', headers['content-type']
405
+ end
406
+ end
407
+ end
408
+ end
409
+