paperclip 2.1.0 → 2.1.2

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.

File without changes
@@ -28,6 +28,16 @@ class AttachmentTest < Test::Unit::TestCase
28
28
  end
29
29
  end
30
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
+
31
41
  context "on an Attachment" do
32
42
  setup do
33
43
  @dummy = Dummy.new
@@ -72,7 +82,7 @@ class AttachmentTest < Test::Unit::TestCase
72
82
  end
73
83
 
74
84
  should "make sure that they are interpolated correctly" do
75
- assert_equal "1024.omg/1024-bbq/:idwhat/000/001/024.wtf", @dummy.avatar.path
85
+ assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
76
86
  end
77
87
  end
78
88
 
@@ -85,9 +95,9 @@ class AttachmentTest < Test::Unit::TestCase
85
95
  @instance = stub
86
96
  @instance.stubs(:id).returns(41)
87
97
  @instance.stubs(:class).returns(Dummy)
88
- @instance.stubs(:[]).with(:test_file_name).returns("5k.png")
89
- @instance.stubs(:[]).with(:test_content_type).returns("image/png")
90
- @instance.stubs(:[]).with(:test_file_size).returns(12345)
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)
91
101
  @attachment = Paperclip::Attachment.new(:test,
92
102
  @instance)
93
103
  @file = File.new(File.join(File.dirname(__FILE__),
@@ -96,102 +106,115 @@ class AttachmentTest < Test::Unit::TestCase
96
106
  end
97
107
 
98
108
  should "return its default_url when no file assigned" do
99
- assert @attachment.file.nil?
109
+ assert @attachment.to_file.nil?
100
110
  assert_equal "/tests/original/missing.png", @attachment.url
101
111
  assert_equal "/tests/blah/missing.png", @attachment.url(:blah)
102
112
  end
103
-
104
- context "when expecting three styles" do
113
+
114
+ context "with a file assigned in the database" do
105
115
  setup do
106
- styles = {:styles => { :large => ["400x400", :png],
107
- :medium => ["100x100", :gif],
108
- :small => ["32x32#", :jpg]}}
109
- @attachment = Paperclip::Attachment.new(:test,
110
- @instance,
111
- styles)
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)
112
119
  end
113
120
 
114
- context "and assigned a file" do
115
- setup do
116
- @instance.expects(:[]=).with(:test_file_name,
117
- File.basename(@file.path))
118
- @instance.expects(:[]=).with(:test_content_type, "image/png")
119
- @instance.expects(:[]=).with(:test_file_size, @file.size)
120
- @instance.expects(:[]=).with(:test_file_name, nil)
121
- @instance.expects(:[]=).with(:test_content_type, nil)
122
- @instance.expects(:[]=).with(:test_file_size, nil)
123
- @attachment.assign(@file)
124
- end
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
125
 
126
- should "return the real url" do
127
- assert @attachment.file
128
- assert_equal "/tests/41/original/5k.png", @attachment.url
129
- assert_equal "/tests/41/blah/5k.png", @attachment.url(:blah)
130
- end
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
131
129
 
132
- should "be dirty" do
133
- assert @attachment.dirty?
134
- end
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
135
134
 
136
- should "have its image and attachments as tempfiles" do
137
- [nil, :large, :medium, :small].each do |style|
138
- assert File.exists?(@attachment.to_io(style))
139
- end
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)
140
143
  end
141
144
 
142
- context "and saved" do
145
+ context "and assigned a file" do
143
146
  setup do
144
- @attachment.save
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)
145
155
  end
146
156
 
147
- should "commit the files to disk" do
148
- [nil, :large, :medium, :small].each do |style|
149
- io = @attachment.to_io(style)
150
- assert File.exists?(io)
151
- assert ! io.is_a?(::Tempfile)
152
- end
157
+ should "be dirty" do
158
+ assert @attachment.dirty?
153
159
  end
154
160
 
155
- should "save the files as the right formats and sizes" do
156
- [[:large, 400, 61, "PNG"],
157
- [:medium, 100, 15, "GIF"],
158
- [:small, 32, 32, "JPEG"]].each do |style|
159
- cmd = "identify -format '%w %h %b %m' " +
160
- "#{@attachment.to_io(style.first).path}"
161
- out = `#{cmd}`
162
- width, height, size, format = out.split(" ")
163
- assert_equal style[1].to_s, width.to_s
164
- assert_equal style[2].to_s, height.to_s
165
- assert_equal style[3].to_s, format.to_s
161
+ context "and saved" do
162
+ setup do
163
+ @attachment.save
166
164
  end
167
- end
168
165
 
169
- should "have #file be equal #to_io(:original)" do
170
- assert_equal @attachment.file, @attachment.to_io(:original)
171
- end
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
172
171
 
173
- should "still have its #file attribute not be nil" do
174
- assert ! @attachment.file.nil?
175
- end
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
176
179
 
177
- context "and deleted" do
178
- setup do
179
- @existing_names = @attachment.styles.keys.collect do |style|
180
- @attachment.path(style)
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
181
191
  end
182
- @instance.expects(:[]=).with(:test_file_name, nil)
183
- @instance.expects(:[]=).with(:test_content_type, nil)
184
- @instance.expects(:[]=).with(:test_file_size, nil)
185
- @attachment.assign nil
186
- @attachment.save
187
192
  end
188
193
 
189
- should "delete the files" do
190
- @existing_names.each{|f| assert ! File.exists?(f) }
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
191
213
  end
192
214
  end
193
215
  end
194
216
  end
217
+
195
218
  end
196
219
 
197
220
  context "when trying a nonexistant storage type" do
@@ -82,7 +82,7 @@ class GeometryTest < Test::Unit::TestCase
82
82
 
83
83
  should "not generate from a bad file" do
84
84
  file = "/home/This File Does Not Exist.omg"
85
- assert_raise(Errno::ENOENT){ @geo = Paperclip::Geometry.from_file(file) }
85
+ assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
86
86
  end
87
87
 
88
88
  [['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
@@ -1,11 +1,102 @@
1
1
  require 'test/helper.rb'
2
2
 
3
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"))
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"))
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
+
4
94
  context "A model with a filesystem attachment" do
5
95
  setup do
6
96
  rebuild_model :styles => { :large => "300x300>",
7
97
  :medium => "100x100",
8
98
  :thumb => ["32x32#", :gif] },
99
+ :whiny_thumbnails => true,
9
100
  :default_style => :medium,
10
101
  :url => "/:attachment/:class/:style/:id/:basename.:extension",
11
102
  :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
@@ -19,8 +110,7 @@ class IntegrationTest < Test::Unit::TestCase
19
110
  end
20
111
 
21
112
  should "write and delete its files" do
22
- [["100x15", nil],
23
- ["434x66", :original],
113
+ [["434x66", :original],
24
114
  ["300x46", :large],
25
115
  ["100x15", :medium],
26
116
  ["32x32", :thumb]].each do |geo, style|
@@ -78,16 +168,18 @@ class IntegrationTest < Test::Unit::TestCase
78
168
  end
79
169
 
80
170
  should "know the difference between good files, bad files, not files, and nil" do
81
- expected = @dummy.avatar.file
171
+ expected = @dummy.avatar.to_file
82
172
  @dummy.avatar = "not a file"
83
173
  assert @dummy.valid?
84
- assert_equal expected.path, @dummy.avatar.file.path
174
+ assert_equal expected.path, @dummy.avatar.to_file.path
85
175
 
86
176
  @dummy.avatar = @bad_file
87
177
  assert ! @dummy.valid?
88
178
  @dummy.avatar = nil
89
179
  assert @dummy.valid?
180
+ end
90
181
 
182
+ should "know the difference between good files, bad files, not files, and nil when validating" do
91
183
  Dummy.validates_attachment_presence :avatar
92
184
  @d2 = Dummy.find(@dummy.id)
93
185
  @d2.avatar = @file
@@ -126,6 +218,7 @@ class IntegrationTest < Test::Unit::TestCase
126
218
  :medium => "100x100",
127
219
  :thumb => ["32x32#", :gif] },
128
220
  :storage => :s3,
221
+ :whiny_thumbnails => true,
129
222
  # :s3_options => {:logger => Logger.new(StringIO.new)},
130
223
  :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")),
131
224
  :default_style => :medium,
@@ -204,10 +297,10 @@ class IntegrationTest < Test::Unit::TestCase
204
297
  end
205
298
 
206
299
  should "know the difference between good files, bad files, not files, and nil" do
207
- expected = @dummy.avatar.file
300
+ expected = @dummy.avatar.to_file
208
301
  @dummy.avatar = "not a file"
209
302
  assert @dummy.valid?
210
- assert_equal expected, @dummy.avatar.file
303
+ assert_equal expected.full_name, @dummy.avatar.to_file.full_name
211
304
 
212
305
  @dummy.avatar = @bad_file
213
306
  assert ! @dummy.valid?
@@ -64,12 +64,17 @@ class PaperclipTest < Test::Unit::TestCase
64
64
  assert Dummy.new.respond_to?(:avatar=)
65
65
  end
66
66
 
67
- [[:presence, nil, "5k.png", nil],
68
- [:size, {:in => 1..10240}, "5k.png", "12k.png"]].each do |args|
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|
69
74
  context "with #{args[0]} validations" do
70
75
  setup do
71
76
  Dummy.class_eval do
72
- send(*[:"validates_attachment_#{args[0]}", :avatar, args[1]].compact)
77
+ send(*[:"validates_attachment_#{args[0].to_s[/[a-z_]*/]}", :avatar, args[1]].compact)
73
78
  end
74
79
  @dummy = Dummy.new
75
80
  end
@@ -97,6 +102,21 @@ class PaperclipTest < Test::Unit::TestCase
97
102
  assert_equal 1, @dummy.avatar.errors.length
98
103
  end
99
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
100
120
  end
101
121
  end
102
122
  end
@@ -5,17 +5,13 @@ require 'right_aws'
5
5
 
6
6
  require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
7
7
 
8
- class S3Test < Test::Unit::TestCase
8
+ class StorageTest < Test::Unit::TestCase
9
9
  context "Parsing S3 credentials" do
10
10
  setup do
11
11
  rebuild_model :storage => :s3,
12
12
  :bucket => "testing",
13
13
  :s3_credentials => {:not => :important}
14
14
 
15
- @s3_stub = stub
16
- @bucket_stub = stub
17
- RightAws::S3.expects(:new).returns(@s3_stub)
18
- @s3_stub.expects(:bucket).returns(@bucket_stub)
19
15
  @dummy = Dummy.new
20
16
  @avatar = @dummy.avatar
21
17
 
@@ -50,17 +46,11 @@ class S3Test < Test::Unit::TestCase
50
46
  setup do
51
47
  rebuild_model :storage => :s3,
52
48
  :bucket => "testing",
49
+ :path => ":attachment/:style/:basename.:extension",
53
50
  :s3_credentials => {
54
51
  'access_key_id' => "12345",
55
52
  'secret_access_key' => "54321"
56
53
  }
57
-
58
- @s3_mock = stub
59
- @bucket_mock = stub
60
- RightAws::S3.expects(:new).
61
- with("12345", "54321", {}).
62
- returns(@s3_mock)
63
- @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
64
54
  end
65
55
 
66
56
  should "be extended by the S3 module" do
@@ -78,12 +68,18 @@ class S3Test < Test::Unit::TestCase
78
68
  @dummy.avatar = @file
79
69
  end
80
70
 
81
- should "still return a Tempfile when sent #to_io" do
82
- assert_equal Tempfile, @dummy.avatar.to_io.class
71
+ should "not get a bucket to get a URL" do
72
+ @dummy.avatar.expects(:s3).never
73
+ @dummy.avatar.expects(:s3_bucket).never
74
+ assert_equal "https://s3.amazonaws.com/testing/avatars/original/5k.png", @dummy.avatar.url
83
75
  end
84
76
 
85
77
  context "and saved" do
86
78
  setup do
79
+ @s3_mock = stub
80
+ @bucket_mock = stub
81
+ RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
82
+ @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
87
83
  @key_mock = stub
88
84
  @bucket_mock.expects(:key).returns(@key_mock)
89
85
  @key_mock.expects(:data=)