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,8 +1,4 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
4
-
5
- require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
1
+ require 'test/helper'
6
2
 
7
3
  class GeometryTest < Test::Unit::TestCase
8
4
  context "Paperclip::Geometry" do
@@ -12,15 +8,15 @@ class GeometryTest < Test::Unit::TestCase
12
8
  assert_equal 768, @geo.height
13
9
  end
14
10
 
15
- should "correctly create a square if the height dimension is missing" do
11
+ should "set height to 0 if height dimension is missing" do
16
12
  assert @geo = Paperclip::Geometry.new(1024)
17
13
  assert_equal 1024, @geo.width
18
- assert_equal 1024, @geo.height
14
+ assert_equal 0, @geo.height
19
15
  end
20
16
 
21
- should "correctly create a square if the width dimension is missing" do
17
+ should "set width to 0 if width dimension is missing" do
22
18
  assert @geo = Paperclip::Geometry.new(nil, 768)
23
- assert_equal 768, @geo.width
19
+ assert_equal 0, @geo.width
24
20
  assert_equal 768, @geo.height
25
21
  end
26
22
 
@@ -32,19 +28,20 @@ class GeometryTest < Test::Unit::TestCase
32
28
 
33
29
  should "be generated from a xH-formatted string" do
34
30
  assert @geo = Paperclip::Geometry.parse("x600")
35
- assert_equal 600, @geo.width
31
+ assert_equal 0, @geo.width
36
32
  assert_equal 600, @geo.height
37
33
  end
38
-
34
+
39
35
  should "be generated from a Wx-formatted string" do
40
36
  assert @geo = Paperclip::Geometry.parse("800x")
41
37
  assert_equal 800, @geo.width
42
- assert_equal 800, @geo.height
38
+ assert_equal 0, @geo.height
43
39
  end
44
40
 
45
- should "ensure the modifier is nil if only one dimension present" do
46
- assert @geo = Paperclip::Geometry.parse("123x")
47
- assert_nil @geo.modifier
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
48
45
  end
49
46
 
50
47
  should "ensure the modifier is nil if not present" do
@@ -52,10 +49,19 @@ class GeometryTest < Test::Unit::TestCase
52
49
  assert_nil @geo.modifier
53
50
  end
54
51
 
55
- ['>', '<', '#', '@', '%', '^', '!'].each do |mod|
56
- should "ensure the modifier #{mod} is preserved" do
52
+ ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
53
+ should "ensure the modifier #{mod.inspect} is preserved" do
57
54
  assert @geo = Paperclip::Geometry.parse("123x456#{mod}")
58
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
59
65
  end
60
66
  end
61
67
 
@@ -65,9 +71,29 @@ class GeometryTest < Test::Unit::TestCase
65
71
  assert_equal "123x456>", @src.transformation_to(@dst).to_s
66
72
  end
67
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
+
68
94
  should "be generated from a file" do
69
95
  file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
70
- file = File.new(file)
96
+ file = File.new(file, 'rb')
71
97
  assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
72
98
  assert @geo.height > 0
73
99
  assert @geo.width > 0
data/test/helper.rb CHANGED
@@ -5,6 +5,7 @@ require 'mocha'
5
5
  require 'tempfile'
6
6
 
7
7
  require 'active_record'
8
+ require 'active_support'
8
9
  begin
9
10
  require 'ruby-debug'
10
11
  rescue LoadError
@@ -32,8 +33,12 @@ def rebuild_model options = {}
32
33
  table.column :avatar_file_name, :string
33
34
  table.column :avatar_content_type, :string
34
35
  table.column :avatar_file_size, :integer
36
+ table.column :avatar_updated_at, :datetime
35
37
  end
38
+ rebuild_class options
39
+ end
36
40
 
41
+ def rebuild_class options = {}
37
42
  ActiveRecord::Base.send(:include, Paperclip)
38
43
  Object.send(:remove_const, "Dummy") rescue nil
39
44
  Object.const_set("Dummy", Class.new(ActiveRecord::Base))
@@ -42,3 +47,14 @@ def rebuild_model options = {}
42
47
  has_attached_file :avatar, options
43
48
  end
44
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
@@ -1,10 +1,10 @@
1
- require 'test/helper.rb'
1
+ require 'test/helper'
2
2
 
3
3
  class IntegrationTest < Test::Unit::TestCase
4
4
  context "Many models at once" do
5
5
  setup do
6
6
  rebuild_model
7
- @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
7
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
8
8
  300.times do |i|
9
9
  Dummy.create! :avatar => @file
10
10
  end
@@ -24,13 +24,13 @@ class IntegrationTest < Test::Unit::TestCase
24
24
  @dummy = Dummy.new
25
25
  @file = File.new(File.join(File.dirname(__FILE__),
26
26
  "fixtures",
27
- "5k.png"))
27
+ "5k.png"), 'rb')
28
28
  @dummy.avatar = @file
29
29
  assert @dummy.save
30
30
  end
31
31
 
32
32
  should "create its thumbnails properly" do
33
- assert_match /\b50x50\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
33
+ assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
34
34
  end
35
35
 
36
36
  context "redefining its attachment styles" do
@@ -44,7 +44,7 @@ class IntegrationTest < Test::Unit::TestCase
44
44
  end
45
45
 
46
46
  should "create its thumbnails properly" do
47
- assert_match /\b150x25\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
47
+ assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
48
48
  end
49
49
  end
50
50
  end
@@ -91,6 +91,38 @@ class IntegrationTest < Test::Unit::TestCase
91
91
  end
92
92
  end
93
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
+
94
126
  context "A model with a filesystem attachment" do
95
127
  setup do
96
128
  rebuild_model :styles => { :large => "300x300>",
@@ -101,8 +133,8 @@ class IntegrationTest < Test::Unit::TestCase
101
133
  :url => "/:attachment/:class/:style/:id/:basename.:extension",
102
134
  :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
103
135
  @dummy = Dummy.new
104
- @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
105
- @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
136
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
137
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb')
106
138
 
107
139
  assert @dummy.avatar = @file
108
140
  assert @dummy.valid?
@@ -114,18 +146,18 @@ class IntegrationTest < Test::Unit::TestCase
114
146
  ["300x46", :large],
115
147
  ["100x15", :medium],
116
148
  ["32x32", :thumb]].each do |geo, style|
117
- cmd = %Q[identify -format "%wx%h" #{@dummy.avatar.to_file(style).path}]
149
+ cmd = %Q[identify -format "%wx%h" "#{@dummy.avatar.path(style)}"]
118
150
  assert_equal geo, `#{cmd}`.chomp, cmd
119
151
  end
120
152
 
121
- saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
153
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) }
122
154
 
123
155
  @d2 = Dummy.find(@dummy.id)
124
- assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file.path}`.chomp
125
- assert_equal "434x66", `identify -format "%wx%h" #{@d2.avatar.to_file(:original).path}`.chomp
126
- assert_equal "300x46", `identify -format "%wx%h" #{@d2.avatar.to_file(:large).path}`.chomp
127
- assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file(:medium).path}`.chomp
128
- assert_equal "32x32", `identify -format "%wx%h" #{@d2.avatar.to_file(:thumb).path}`.chomp
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
129
161
 
130
162
  @dummy.avatar = "not a valid file but not nil"
131
163
  assert_equal File.basename(@file.path), @dummy.avatar_file_name
@@ -154,10 +186,10 @@ class IntegrationTest < Test::Unit::TestCase
154
186
 
155
187
  assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
156
188
  [:thumb, :medium, :large, :original].each do |style|
157
- assert_equal @dummy.avatar.to_file(style).path, @d2.avatar.to_file(style).path
189
+ assert_equal @dummy.avatar.path(style), @d2.avatar.path(style)
158
190
  end
159
191
 
160
- saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
192
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) }
161
193
 
162
194
  @d2.avatar = nil
163
195
  assert @d2.save
@@ -171,7 +203,8 @@ class IntegrationTest < Test::Unit::TestCase
171
203
  expected = @dummy.avatar.to_file
172
204
  @dummy.avatar = "not a file"
173
205
  assert @dummy.valid?
174
- assert_equal expected.path, @dummy.avatar.to_file.path
206
+ assert_equal expected.path, @dummy.avatar.path
207
+ expected.close
175
208
 
176
209
  @dummy.avatar = @bad_file
177
210
  assert ! @dummy.valid?
@@ -183,14 +216,14 @@ class IntegrationTest < Test::Unit::TestCase
183
216
  Dummy.validates_attachment_presence :avatar
184
217
  @d2 = Dummy.find(@dummy.id)
185
218
  @d2.avatar = @file
186
- assert @d2.valid?
219
+ assert @d2.valid?, @d2.errors.full_messages.inspect
187
220
  @d2.avatar = @bad_file
188
221
  assert ! @d2.valid?
189
222
  @d2.avatar = nil
190
223
  assert ! @d2.valid?
191
224
  end
192
225
 
193
- should "be able to reload without saving an not have the file disappear" do
226
+ should "be able to reload without saving and not have the file disappear" do
194
227
  @dummy.avatar = @file
195
228
  assert @dummy.save
196
229
  @dummy.avatar = nil
@@ -198,13 +231,44 @@ class IntegrationTest < Test::Unit::TestCase
198
231
  @dummy.reload
199
232
  assert_equal "5k.png", @dummy.avatar_file_name
200
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
+
201
264
  end
202
265
 
203
266
  if ENV['S3_TEST_BUCKET']
204
267
  def s3_files_for attachment
205
268
  [:thumb, :medium, :large, :original].inject({}) do |files, style|
206
- data = `curl '#{attachment.url(style)}' 2>/dev/null`.chomp
269
+ data = `curl "#{attachment.url(style)}" 2>/dev/null`.chomp
207
270
  t = Tempfile.new("paperclip-test")
271
+ t.binmode
208
272
  t.write(data)
209
273
  t.rewind
210
274
  files[style] = t
@@ -212,6 +276,14 @@ class IntegrationTest < Test::Unit::TestCase
212
276
  end
213
277
  end
214
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
+
215
287
  context "A model with an S3 attachment" do
216
288
  setup do
217
289
  rebuild_model :styles => { :large => "300x300>",
@@ -225,8 +297,8 @@ class IntegrationTest < Test::Unit::TestCase
225
297
  :bucket => ENV['S3_TEST_BUCKET'],
226
298
  :path => ":class/:attachment/:id/:style/:basename.:extension"
227
299
  @dummy = Dummy.new
228
- @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
229
- @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
300
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
301
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb')
230
302
 
231
303
  assert @dummy.avatar = @file
232
304
  assert @dummy.valid?
@@ -240,7 +312,7 @@ class IntegrationTest < Test::Unit::TestCase
240
312
  ["300x46", :large],
241
313
  ["100x15", :medium],
242
314
  ["32x32", :thumb]].each do |geo, style|
243
- cmd = %Q[identify -format "%wx%h" #{@files_on_s3[style].path}]
315
+ cmd = %Q[identify -format "%wx%h" "#{@files_on_s3[style].path}"]
244
316
  assert_equal geo, `#{cmd}`.chomp, cmd
245
317
  end
246
318
 
@@ -250,7 +322,7 @@ class IntegrationTest < Test::Unit::TestCase
250
322
  ["300x46", :large],
251
323
  ["100x15", :medium],
252
324
  ["32x32", :thumb]].each do |geo, style|
253
- cmd = %Q[identify -format "%wx%h" #{@d2_files[style].path}]
325
+ cmd = %Q[identify -format "%wx%h" "#{@d2_files[style].path}"]
254
326
  assert_equal geo, `#{cmd}`.chomp, cmd
255
327
  end
256
328
 
@@ -317,7 +389,7 @@ class IntegrationTest < Test::Unit::TestCase
317
389
  assert ! @d2.valid?
318
390
  end
319
391
 
320
- should "be able to reload without saving an not have the file disappear" do
392
+ should "be able to reload without saving and not have the file disappear" do
321
393
  @dummy.avatar = @file
322
394
  assert @dummy.save
323
395
  @dummy.avatar = nil
@@ -325,6 +397,12 @@ class IntegrationTest < Test::Unit::TestCase
325
397
  @dummy.reload
326
398
  assert_equal "5k.png", @dummy.avatar_file_name
327
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
328
406
  end
329
407
  end
330
408
  end
@@ -0,0 +1,68 @@
1
+ require 'test/helper'
2
+
3
+ class IOStreamTest < Test::Unit::TestCase
4
+ context "IOStream" do
5
+ should "be included in IO, File, Tempfile, and StringIO" do
6
+ [IO, File, Tempfile, StringIO].each do |klass|
7
+ assert klass.included_modules.include?(IOStream), "Not in #{klass}"
8
+ end
9
+ end
10
+ end
11
+
12
+ context "A file" do
13
+ setup do
14
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
15
+ end
16
+
17
+ context "that is sent #stream_to" do
18
+
19
+ context "and given a String" do
20
+ setup do
21
+ assert @result = @file.stream_to("/tmp/iostream.string.test")
22
+ end
23
+
24
+ should "return a File" do
25
+ assert @result.is_a?(File)
26
+ end
27
+
28
+ should "contain the same data as the original file" do
29
+ @file.rewind; @result.rewind
30
+ assert_equal @file.read, @result.read
31
+ end
32
+ end
33
+
34
+ context "and given a Tempfile" do
35
+ setup do
36
+ tempfile = Tempfile.new('iostream.test')
37
+ tempfile.binmode
38
+ assert @result = @file.stream_to(tempfile)
39
+ end
40
+
41
+ should "return a Tempfile" do
42
+ assert @result.is_a?(Tempfile)
43
+ end
44
+
45
+ should "contain the same data as the original file" do
46
+ @file.rewind; @result.rewind
47
+ assert_equal @file.read, @result.read
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ context "that is sent #to_tempfile" do
54
+ setup do
55
+ assert @tempfile = @file.to_tempfile
56
+ end
57
+
58
+ should "convert it to a Tempfile" do
59
+ assert @tempfile.is_a?(Tempfile)
60
+ end
61
+
62
+ should "have the Tempfile contain the same data as the file" do
63
+ @file.rewind; @tempfile.rewind
64
+ assert_equal @file.read, @tempfile.read
65
+ end
66
+ end
67
+ end
68
+ end